function MM_preloadImages() { //v3.0
  var d=document;if(d.images){if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments;for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){d.MM_p[j]=new Image;d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr;for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;if(!d) d=document;if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document;n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n];for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n);return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments;document.MM_sr=new Array;for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x;if(!x.oSrc) x.oSrc=x.src;x.src=a[i+2];}
}


function nuevoAjax()
{ 
	var xmlhttp=false; 
	try 
	{ 
		// No IE
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
	}
	catch(e)
	{ 
		try
		{ 
			// IE 
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
		} 
		catch(E) {xmlhttp=false;}
	}
	if (!xmlhttp && typeof XMLHttpRequest!="undefined") {xmlhttp=new XMLHttpRequest();} 
	return xmlhttp; 
}

function eliminaEspacios(cadena)
{
	// Funcion para eliminar espacios delante y detras de cada cadena
	while(cadena.charAt(cadena.length-1)==" ") cadena=cadena.substr(0, cadena.length-1);
	while(cadena.charAt(0)==" ") cadena=cadena.substr(1, cadena.length-1);
	return cadena;
}

function muestraMensaje(mensaje,nombre)
{
	divMensaje=document.getElementById(nombre);
	divMensaje.innerHTML=mensaje;
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// FUNCIONES DE LOGIN DE USUARIOS


/*Funcion que me devuelve el numero de caracteres - blancos de una cadena*/
function trim(cadena){
	var nuevacadena="";
	nuevacadena=cadena.replace(/\ /g,"");
	return nuevacadena.length;
}

advanced_search=false;
function habilitar() {
	if(advanced_search!=true){		
		document.getElementById('regalo').c
		advanced_search=true;
	}else{
		document.getElementById('regalo').style.display='none';		
		advanced_search=false;
	}
		
}

//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// FUNCIONES DE TAMAÑO DE LA PAGINA PARA REDIMENCIONAR
function getId(id){
	if(!document.getElementById)id = document.all[id];
	else id = document.getElementById(id);
	return id;
}
function getWinSize(){
	var size = new Object();
	size.w = document.documentElement.clientWidth;
	size.h = document.documentElement.clientHeight;
	return size;
}
function getWinPos(){
	var pos = new Object();
	pos.x = window.screenX;
	pos.y = window.screenY;
	return pos;
}
/*
window.onresize = window.onload = function (){
	var size = getWinSize();
	var pos = getWinPos();
}
*/

function validaCorreo(valor)
{
	var reg=/(^[a-zA-Z0-9\ñ\Ñ\._-]{1,30})@([a-zA-Z0-9\ñ\Ñ\.-]{1,30}$)/;
	if(reg.test(valor)) return true;
	else return false;
}

function ValidateRegister()
{
  if(trim(document.getElementById('nombre').value)=='')
  {
    alert("Debe ingresar su nombre");
    return false;
  }

  if(trim(document.getElementById('apellido').value)=='')
  {
    alert("Debe ingresar su apellido");
    return false;
  }

  if(trim(document.getElementById('tipo_doc').value)=='')
  {
    alert("Debe Seleccionar un tipo de Documento");
    return false;
  }

  if(trim(document.getElementById('numero_doc').value)=='')
  {
    alert("Falta ingresar su numero de documento");
    return false;
  }

  if(trim(document.getElementById('email').value)==0 || validaCorreo(document.getElementById('email').value)!= true )
  {
    alert("no es un email correcto");
    return false;
  }

  if(trim(document.getElementById('email_confir').value)==0 || validaCorreo(document.getElementById('email_confir').value)!= true )
  {
    alert("no es un email correcto");
    return false;
  }

  if(trim(document.getElementById('email').value)!=trim(document.getElementById('email_confir').value))
  {
    alert("Los email no coinciden");return false;
  }

  if(trim(document.getElementById('password').value)=='')
  {
    alert("Falta ingresar su numero de contraseña");
    return false;
  }

  if(trim(document.getElementById('password').value)!=trim(document.getElementById('password_confir').value))
  {
    alert("Las contraseñas no coinciden");
    return false;
  }

  if(trim(document.getElementById('dia').value)=='' || trim(document.getElementById('mes').value)=='' || trim(document.getElementById('ano').value)=='')
  {
    alert("Debe Seleccionar su fecha de nacimiento");
    return false;
  }

  if(trim(document.getElementById('departamento').value)=='')
  {
    alert("Debe seleccionar un Departamento");
    return false;
  }

  if(trim(document.getElementById('ciudad').value)=='')
  {
    alert("Debe ingresar un nombre de ciudad");
    return false;
  }

  if(trim(document.getElementById('direccion').value)=='')
  {
    alert("Debe ingresar una direccion");
    return false;
  }

  var patron = /^(\+?[0-9]{2,3})?[ |-]?(\+?[0-9]{1,2})?[ |-]?([0-9]{6,20})$/;

  if(trim(document.getElementById('telefono').value)=='')
  {
    alert("Debe ingresar un numero de telefono");
    return false;
  }
  else if( null == document.getElementById('telefono').value.match( patron ) )
  {
    alert( "El campo teléfono debe ser numerico" );
    return false;
  }

  document.formRegister.submit()
  return false;
}

function ValidateContactenos()
{
  pedido = document.getElementById( "pedido" );
  if(trim(document.getElementById('nombre2').value)=='')
  {
    alert("Debe ingresar su nombre");
    return false;
  }

  if(trim(document.getElementById('apellido2').value)=='')
  {
    alert("Debe ingresar su apellido");
    return false;
  }

  if(trim(document.getElementById('email2').value)==0 || validaCorreo(document.getElementById('email2').value)!= true )
  {
    alert("no es un email correcto");
    return false;
  }

  if(trim(document.getElementById('pedido').value)!='' && !IsNumeric( pedido.value ))
  {
    alert("Debe ingresar solo números en el campo pedido");
    return false;
  }

  if(trim(document.getElementById('motivo').value)=='')
  {
    alert("Debe ingresar un motivo");
    return false;
  }

  if(trim(document.getElementById('mensaje').value)=='')
  {
    alert("Debe Ingresar su Mensaje");
    return false;
  }
  document.formContacto.submit()
  return false;
}

function ValidateBilling()
{
  if(trim(document.getElementById('nombre_3').value)=='')
  {
    alert("Debe ingresar su nombre");
    return false;
  }

  if(trim(document.getElementById('apellido_3').value)=='')
  {
    alert("Debe ingresar su apellido");
    return false;
  }

  if(trim(document.getElementById('documento_3').value)=='')
  {
    alert("Debe ingresar su numero de documento");
    return false;
  }

  if(trim(document.getElementById('direccion_3').value)=='')
  {
    alert("Debe ingresar la dirección de facturación");
    return false;
  }

  if(trim(document.getElementById('ciudad_3').value)=='')
  {
    alert("Debe ingresar la ciudad de facturación");
    return false;
  }

  var patron = /^(\+?[0-9]{2,3})?[ |-]?(\+?[0-9]{1,2})?[ |-]?([0-9]{6,20})$/;

  if(trim(document.getElementById('telefono_3').value)=='')
  {
    alert("Debe ingresar su teléfono");
    return false;
  }
  else if( null == document.getElementById('telefono_3').value.match( patron ) )
  {
    alert( "El campo teléfono debe ser numerico" );
    return false;
  }

  if(trim(document.getElementById('email_3').value)==0 || validaCorreo(document.getElementById('email_3').value)!= true )
  {
    alert("no es un mail correcto");
    return false;
  }
	
  document.billing.submit();
  return false;
}

function ValidateShipping()
{
  if(trim(document.getElementById('nombre_4').value)=='')
  {
    alert("Debe ingresar su nombre");
    return false;
  }

  if(trim(document.getElementById('apellido_4').value)=='')
  {
    alert("Debe ingresar su apellido");
    return false;
  }

  if(trim(document.getElementById('direccion_4').value)=='')
  {
    alert("Debe ingresar la dirección de énvio");
    return false;
  }

  if(trim(document.getElementById('country2').value)=='')
  {
    alert("Debe seleccionar el pais de énvio");
    return false;
  }

  if( trim(document.getElementById('country2').value)=='Colombia' )
  {
    if(trim(document.getElementById('ciudad_4').value)==''){
      alert("Debe seleccionar la ciudad de énvio");
      return false;
    }
  }
  else
  {
    if(trim(document.getElementById('ciudad_5').value)=='')
    {
      alert("Debe ingresar la ciudad de énvio");
      return false;
    }
    /* */
    if(trim(document.getElementById('zipcode1').value)=='')
    {
      alert("Debe ingresar el codigo postal de énvio");
      return false;
    }
    else if( !IsNumeric( document.getElementById('zipcode1').value ) )
    {
      alert( "El codigo postal debe ser numerico" );
      return false;
    }
    /* */
  }
  var patron = /^(\+?[0-9]{2,3})?[ |-]?(\+?[0-9]{1,2})?[ |-]?([0-9]{6,20})$/;

  if(trim(document.getElementById('telefono_4').value)=='')
  {
    alert("Debe ingresar su teléfono");
    return false;
  }
  else if( null == document.getElementById('telefono_4').value.match( patron ) )
  {
    alert( "El campo teléfono debe ser numerico" );
    return false;
  }

  document.shipping.submit();
  return false;
}

function timer(tiempo,url){	
	
	carga = setTimeout('redirecc("'+url+'")', tiempo);

}

function redirecc(url){
	
	parent.location.href = url;
	
}

function ValidateCodDev()
{
  if(trim(document.getElementById('pedido').value)=='')
  {
    alert("Debe ingresar el numero de pedido");
    return false;
  }

  document.pedidos.submit()
  return false;
}

function ValidateFormSearch()
{
  if(trim(document.getElementById('pedido').value)=='')
  {
    alert("Debe ingresar el numero de pedido");
    return false;
  }
  document.pedidos.submit()
  return false;
}


function mostrarImg(ruta,id_img,id_prod){
	
	//alert(cat)
	document.getElementById('imagenProduct').style.display='inline';
	divResultado = document.getElementById('imagenProduct');
	divResultado.innerHTML = '<p style="padding-top:160px;"><img src="'+ruta+'images/prettyPhoto/dark_rounded/loader.gif"></p>';
	
	datos=ruta+'consultasAjax.php?id_img=newimg&id='+id_img+'&id_prod='+id_prod;
	
	ajax=nuevoAjax();
	ajax.open("GET", datos);
	ajax.onreadystatechange=function() {
		
		if (ajax.readyState==4) {

			var results = ajax.responseText.split("||");			
			divResultado.innerHTML = results[0];
			ZoomImg(results[1]);
			
		}
	}
	ajax.send(null)	
	
}

function productRandom( ruta, tipo, cat, prod, page )
{
  //alert(cat)
  document.getElementById('random').style.display='inline';
  divResultado = document.getElementById('random');

  datos=ruta+'consultasAjax.php?random=random&tipo='+tipo+'&cat='+cat+'&prod='+prod+'&page='+page;

  ajax=nuevoAjax();
  ajax.open("GET", datos);
  ajax.onreadystatechange=function()
  {
    if (ajax.readyState==4)
    {
      divResultado.innerHTML = ajax.responseText;
    }
  }
  ajax.send(null)
}

function ZoomImg(url){
	//alert(paddZoom)
	document.getElementById('hrImgZoom').href=url;
}

function validaEnter(e,enviar){
	
  tecla=(document.all) ? e.keyCode : e.which;
  if(tecla == 13)
	document.getElementById( enviar ).submit();
} 

function OpenPse( url )
{
	window.open( url, "Pago" );
}

function tipoTarjeta( destin )
{
  nombre = document.getElementById( "nombre" );
  tarjeta = document.getElementById( "tarjeta" );
  mes = document.getElementById( "mes" );
  ano = document.getElementById( "ano" );
  codigo = document.getElementById( "codigo" );

  if( document.getElementById( "credit" ).disabled!=true )
  {
    if( nombre != null )
      nombre.value = "";

    if( tarjeta != null )
      tarjeta.value = "";

    if( mes != null )
      mes.value = "";

    if( ano != null )
      ano.value = "";

    if( codigo != null )
      codigo.value = "";

    document.getElementById( "credit" ).submit();
    document.getElementById( "credit" ).disabled=true
  }
}

function ValidaPago()
{
  modo = document.getElementById( "credito" );

  if( modo.checked )
  {
    nombre = document.getElementById( "nombre" );
    tarjeta = document.getElementById( "tarjeta" );
    mes = document.getElementById( "mes" );
    ano = document.getElementById( "ano" );
    codigo = document.getElementById( "codigo" );
    cuotas = document.getElementById( "cuotas" );

    if( nombre.value == "" )
    {
      alert( "Debe ingresar el nombre del tarjeta habiente" );
      return false;
    }

    if( tarjeta.value == "" )
    {
      alert( "Debe ingresar el numero de la tarjeta de credito" );
      return false;
    }
    else if( tarjeta.value.length < 15 )
    {
      alert( "El campo tarjeta debe ser de almenos 15 digitos" );
      return false;
    }
    else if( !IsNumeric( tarjeta.value ) )
    {
      alert( "El campo tarjeta debe ser numerico" );
      return false;
    }

    if( mes.value == "" )
    {
      alert( "Debe seleccionar el mes de vencimiento" );
      return false;
    }

    if( ano.value == "" )
    {
      alert( "Debe seleccionar el año de vencimiento" );
      return false;
    }

    if( codigo.value == "" )
    {
      alert( "Debe ingresar el codigo de verificacion" );
      return false;
    }
    else if( codigo.value.length < 3 )
    {
      alert( "El campo codigo debe ser de almenos 3 digitos" );
      return false;
    }
    else if( !IsNumeric( codigo.value ) )
    {
      alert( "El campo codigo debe ser numerico" );
      return false;
    }

    if( cuotas.value == "" )
    {
      alert( "Debe seleccionar el numero de cuotas" );
      return false;
    }

    if (document.getElementById( "credit" ).disabled!=true)
    {
      document.getElementById( "credit" ).submit();
      document.getElementById( "credit" ).disabled=true
    }
  }
  else
  {
    natural = document.getElementById( "pernat" );
    juridi = document.getElementById( "perjur" );
    bank = document.getElementById( "bank" );

    if( !natural.checked && !juridi.checked )
    {
      alert( "Debe seleccionar un tipo de persona" );
      return false;
    }

    if( bank.value == "" )
    {
      alert( "Debe seleccionar una entidad financiera" );
      return false;
    }

    if (document.getElementById( "credit" ).disabled!=true)
    {
      document.getElementById( "credit" ).submit();
      document.getElementById( "credit" ).disabled=true
    }
  }

  return false;
}

function IsNumeric( sText )
{
  var ValidChars = "0123456789.";
  var IsNumber = true;
  var Char;

  for (i = 0; i < sText.length && IsNumber == true; i++)
  {
    Char = sText.charAt(i);
    if ( ValidChars.indexOf( Char ) == -1 )
    {
      IsNumber = false;
    }
  }
  return IsNumber;
}

function validarNumber(e) {
    tecla = (document.all)?e.keyCode:e.which; //no lo sé muy bien
	if (tecla==8) return true; // no se porque haces una comparación con el valor 8
	patron = /\d/;// de donde tomas este patrón d o el w?
	te = String.fromCharCode(tecla);
	return patron.test(te); 
}

function SelectSaldo(ean,ruta){
	
	document.getElementById("proceSaldo").style.display = "";
	document.getElementById("consSaldo").style.display = "none";
	document.getElementById("comprar").style.display = "none";
	document.getElementById("agotado").style.display = "none";
	
	//alert(ruta)
	ean = ean.split("||");
			
	datos=ruta+'consultasAjax.php?consultaSaldo=Saldo&cod_ean='+ean[0];
	
	ajax=nuevoAjax();
	ajax.open("GET", datos);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			
			//divResultado.innerHTML = ajax.responseText;						
			var results = ajax.responseText.split("||");			
			saldo = results[0];
			sucursal = results[1];
			exist_url = results[2];			

			if(ean!=""){
				if(exist_url=="Existente"){
					if(saldo>1){
					document.getElementById("attr_Size").value = ean[1];
					document.getElementById("Sucursal").value = results[1];
					document.getElementById("comprar").style.display = "";
					document.getElementById("agotado").style.display = "none";
					document.getElementById("proceSaldo").style.display = "none";
					}else{
					document.getElementById("comprar").style.display = "none";
					document.getElementById("proceSaldo").style.display = "none";
					document.getElementById("agotado").style.display = "";
					}
				}else{
					document.getElementById("comprar").style.display = "none";
					document.getElementById("proceSaldo").style.display = "none";
					document.getElementById("agotado").style.display = "none";
					document.getElementById("consSaldo").style.display = "";
					alert('por favor intente nuevamente')
				}
			}else{
				document.getElementById("proceSaldo").style.display = "none";
				document.getElementById("consSaldo").style.display = "";
			}			
			
		}
	}
	ajax.send(null)	
	
}

function resetProductCart(){
	document.getElementById('numeroProductos').innerHTML = '0';
}

function sendSilueta()
{
  document.getElementById('silueta').submit();
}

function sendRemem()
{
  form = document.getElementById( "form_remem" );

  nom_passwd = document.getElementById( "nom_passwd" );
  con_passwd = document.getElementById( "con_passwd" );

  if( nom_passwd.value == "" )
  {
    alert( "Ingrese la nueva contraseña" );
    nom_passwd.focus();
    return false;
  }
  else if( con_passwd.value == "" )
  {
    alert( "Ingrese la confirmacion de contraseña" );
    con_passwd.focus();
    return false;
  }
  else if( nom_passwd.value != con_passwd.value )
  {
    alert( "La contraseña y la confirmacion son diferentes" );
    con_passwd.focus();
    return false;
  }

  if( form.disabled!=true )
  {
    form.submit();
    form.disabled=true
  }
  return false;
}

function OcultarMostrarDivs(divOcultar,divMostrar)
{
  div1 = document.getElementById(divOcultar);
  div2 = document.getElementById(divMostrar);

  if( div1.style.display != 'none' )
  {
    div1.style.display='none';
    div2.style.display='';
  }
  else
  {
    div2.style.display='none';
    div1.style.display='';
  }
}
