// JavaScript Document
function validarEmail(valor) {
	var result=false;
	var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;

	if (filter.test(valor)){
		result=true;
	}
	return result;
}

function preCarga(imagenes) { 
	var i; 
	var lista_imagenes = new Array();

	for(i in imagenes){ 
		lista_imagenes[i] = new Image(); 
		lista_imagenes[i].src = imagenes[i];
	}
}

function cambiarImagen(vObj,vImagen){
	vObj.src=vImagen;
}

function cambiarFondo(vObj,vImagen){
	vObj.style.backgroundImage="url('"+vImagen+"')";
}

function CompararFechas(xFecha,xTipo){
	var Numero = new Array();
	if (xTipo==1){
		// Dia
		Numero[0]=3;
		Numero[1]=2;
		// Mes
		Numero[2]=0;
		Numero[3]=2;
	}else{
		// Dia
		Numero[0]=0;
		Numero[1]=2;
		// Mes
		Numero[2]=3;
		Numero[3]=2;
	}
	// Año
	Numero[4]=6;
	Numero[5]=4;
	
	var miFecha1=xFecha1.value;
	if (miFecha1.substr(Numero[0],Numero[1])=="08" || miFecha1.substr(Numero[0],Numero[1])=="09"){
		var miDia1=miFecha1.substr(Numero[0],Numero[1]);
		miDia1=miDia1.replace("0","");
	}else{
		var miDia1=parseInt(miFecha1.substr(Numero[0],Numero[1]));
	}
	if (miFecha1.substr(Numero[2],Numero[3])=="08" || miFecha1.substr(Numero[2],Numero[3])=="09"){
		var miMes1=miFecha1.substr(Numero[2],Numero[3]);
		miMes1=miMes1.replace("0","");
	}else{
		var miMes1=parseInt(miFecha1.substr(Numero[2],Numero[3]));
	}	
	var miAnio1=parseInt(miFecha1.substr(Numero[4],Numero[5]));
	
	var miFecha2=new Date();
	var miDia2=parseInt(miFecha2.getDate());
	var miMes2=parseInt(miFecha2.getMonth())+1;
	var miAnio2=parseInt(miFecha2.getFullYear());
	
	var Result=false;
	
	if (miAnio1>miAnio2){
		Result=true;
	}else if (miAnio1==miAnio2){
		if (miMes1>miMes2){
			Result=true;
		}else if (miMes1==miMes2){
			if (miDia1>miDia2){
				Result=true;
			}
		}
	}
	
	return Result;
}

function Comparar2Fechas(xFecha1,xFecha2,xTipo){
	var Numero = new Array();
	if (xTipo==1){
		// Dia
		Numero[0]=3;
		Numero[1]=2;
		// Mes
		Numero[2]=0;
		Numero[3]=2;
	}else{
		// Dia
		Numero[0]=0;
		Numero[1]=2;
		// Mes
		Numero[2]=3;
		Numero[3]=2;
	}
	// Año
	Numero[4]=6;
	Numero[5]=4;
	
	var miFecha1=xFecha1.value;
	if (miFecha1.substr(Numero[0],Numero[1])=="08" || miFecha1.substr(Numero[0],Numero[1])=="09"){
		var miDia1=miFecha1.substr(Numero[0],Numero[1]);
		miDia1=miDia1.replace("0","");
	}else{
		var miDia1=parseInt(miFecha1.substr(Numero[0],Numero[1]));
	}
	if (miFecha1.substr(Numero[2],Numero[3])=="08" || miFecha1.substr(Numero[2],Numero[3])=="09"){
		var miMes1=miFecha1.substr(Numero[2],Numero[3]);
		miMes1=miMes1.replace("0","");
	}else{
		var miMes1=parseInt(miFecha1.substr(Numero[2],Numero[3]));
	}	
	var miAnio1=parseInt(miFecha1.substr(Numero[4],Numero[5]));
	
	var miFecha2=xFecha2.value;
	if (miFecha2.substr(Numero[0],Numero[1])=="08" || miFecha2.substr(Numero[0],Numero[1])=="09"){
		var miDia2=miFecha2.substr(Numero[0],Numero[1]);
		miDia2=miDia2.replace("0","");
	}else{
		var miDia2=parseInt(miFecha2.substr(Numero[0],Numero[1]));
	}
	if (miFecha2.substr(Numero[2],Numero[3])=="08" || miFecha2.substr(Numero[2],Numero[3])=="09"){
		var miMes2=miFecha2.substr(Numero[2],Numero[3]);
		miMes2=miMes2.replace("0","");
	}else{
		var miMes2=parseInt(miFecha2.substr(Numero[2],Numero[3]));
	}
	var miAnio2=parseInt(miFecha2.substr(Numero[4],Numero[5]));
	
	var Result=false;
	
	if (miAnio1>miAnio2){
		Result=true;
	}else if (miAnio1==miAnio2){
		if (miMes1>miMes2){
			Result=true;
		}else if (miMes1==miMes2){
			if (miDia1>miDia2){
				Result=true;
			}
		}
	}
	
	return Result;
}

function formatoMoneda(num) {
	num = num.toString().replace(/$|,/g,''); 
	simbolo="";
	if(isNaN(num)) 
	num = "0"; 
	sign = (num == (num = Math.abs(num))); 
	num = Math.floor(num*100+0.50000000001); 
	cents = num%100; 
	num = Math.floor(num/100).toString(); 
	if(cents<10) 
	cents = "0" + cents; 
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) 
	num = num.substring(0,num.length-(4*i+3))+','+ 
	num.substring(num.length-(4*i+3)); 
	return (((sign)?'':'-') + simbolo + num + '.' + cents); 
}

function miAjax(){
	var xmlhttp=false;
	var ajaxs = ["Msxml2.XMLHTTP","Msxml2.XMLHTTP.4.0","Msxml2.XMLHTTP.5.0","Msxml2.XMLHTTP.3.0","Microsoft.XMLHTTP"];
	
	for(var i=0 ; !xmlhttp && i<ajaxs.length ; i++){
		try{
			xmlhttp = new ActiveXObject(ajaxs[i]);
		}catch(e){
			xmlhttp = false;
		}
	}

	if(!xmlhttp && typeof XMLHttpRequest!='undefined'){
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function vAncho(){
  var tamanio = 0;
  if (typeof window.innerWidth != 'undefined'){
    	tamanio = window.innerWidth;
  }else if (typeof window.offsetWidth != 'undefined'){
	  tamanio = window.offsetWidth;
  }else if (typeof document.documentElement != 'undefined'
      && typeof document.documentElement.clientWidth !=
      'undefined' && document.documentElement.clientWidth != 0){
 		tamanio = document.documentElement.clientWidth;
  }else{
    	tamanio = document.getElementsByTagName('body')[0].clientWidth;
  }
  return tamanio;
}

function vAlto(){
  var tamanio =0;
  if (typeof window.innerHeight != 'undefined'){
    tamanio = window.innerHeight;
  }else if (typeof window.offsetHeight != 'undefined'){
	  tamanio = window.offsetHeight;
  }else if (typeof document.documentElement != 'undefined'
      && typeof document.documentElement.clientHeight !=
      'undefined' && document.documentElement.clientHeight != 0){
 	tamanio = document.documentElement.clientHeight;
  }else{
    tamanio = document.getElementsByTagName('body')[0].clientHeight;
  }
  return tamanio;
}

function quitarPX(valor){
	var miPX = "px";
	var miValor = valor;
	miValor=miValor.replace(miPX,'');
	return miValor;
}

function xDes(objeto){
	objeto.style.display="none";
}

function xApa(objeto){
	objeto.style.display="block";
}

function calendario(inicio,fin,boton1,boton2){
	var hoy=new Date();
	var anio=hoy.getFullYear();
	var mes=hoy.getMonth();
	var dia=hoy.getDate()+1;
	var ahora=new Date(anio,mes,dia);
	var anterior=ahora;
		 
	$(inicio).datepicker({minDate:ahora, showAnim:"drop", buttonImage: "iconos/btn-close.gif", buttonImageOnly: true, onSelect: function(){
		var befFecha=new Date($(inicio).datepicker("getDate"));
		var befAnio=befFecha.getFullYear();
		var befMes=befFecha.getMonth();
		var befDia=befFecha.getDate()+1;
		anterior=new Date(befAnio,befMes,befDia);
		
		if (fin!=""){
			$(fin).datepicker("setDate","");
			$(fin).datepicker("destroy");
			$(fin).datepicker({minDate:anterior, showAnim:"drop", buttonImage: "iconos/btn-close.gif", buttonImageOnly: true, onSelect: function(){
			
			}});
		}
	}});
	
	if (fin!=""){
	   if ($(inicio).val()!=""){
		   var befFecha=new Date($(inicio).datepicker("getDate"));
		   var befAnio=befFecha.getFullYear();
		   var befMes=befFecha.getMonth();
		   var befDia=befFecha.getDate()+1;
		   anterior=new Date(befAnio,befMes,befDia);
	   }
	   
	   $(fin).datepicker({minDate:anterior, showAnim:"drop", buttonImage: "iconos/btn-close.gif", buttonImageOnly: true, onSelect: function(){
	   
	   }});
	}
	
	if (boton1!=""){
		$(boton1).click(function (){
			$(inicio).datepicker("show");
		});
	}
	
	if (boton2!=""){
		$(boton2).click(function (){
			$(fin).datepicker("show");
		});
	}
}

// **********************************************
// **********************************************

function enviar(){
	var xNombre=document.getElementById("txtName");
	var xEmail=document.getElementById("txtEmail");
	var xPhone=document.getElementById("txtPhone");
	var xForm=document.getElementById("miForm");
	
	if (xNombre.value!="" && xEmail.value!="" && validarEmail(xEmail.value)==true && xPhone.value!=""){
		xForm.submit();
	}else{
		if (xNombre.value==""){
			xNombre.focus();
			alert("Your name is required");
		}else if (xEmail.value==""){
			xEmail.focus();
			alert("Your e-mail is required");
		}else if (validarEmail(xEmail.value)==false){
			xEmail.value="";
			xEmail.focus();
			alert("Your e-mail is invalid");
		}else if (xPhone.value==""){
			xPhone.focus();
			alert("Your phone number is required");
		}
	}
}

function comprar(){
	var xName=document.getElementById("txtName");
	var xAddress=document.getElementById("txtAddress");
    var xCity=document.getElementById("txtCity");
    var xZip=document.getElementById("txtZip");
    var xState=document.getElementById("txtState");
    var xCountry=document.getElementById("txtCountry");
    var xEmail=document.getElementById("txtEmail");
    var xPhone=document.getElementById("txtPhone");
    var xComments=document.getElementById("txtComments");
	/*
	PROCESO AL SOLICITAR DATOS DE TARJETA DE CREDITO
	
    var xCard=document.getElementById("txtCard");
    var xHolder=document.getElementById("txtHolder");
    var xNumber=document.getElementById("txtNumber");
	var xCode=document.getElementById("txtCode");
    var xMonth=document.getElementById("txtExpiryMonth");
	var xYear=document.getElementById("txtExpiryYear");
	
	if (xName.value!="" && xAddress.value!="" && xCity.value!="" && xZip.value!="" && xState.value!="" && xCountry.value!="" && xEmail.value!="" && validarEmail(xEmail.value)==true && xPhone.value!="" && xCard.value!="" && xHolder.value!="" && xNumber.value!="" && xMonth.value!="" && xYear.value!=""){
		var valido = false;
		var siguiente=false;
		
		var fechaAct= new Date();
		var mesAct=parseInt(fechaAct.getMonth())+1;
		var anioAct=parseInt(fechaAct.getFullYear());	
		var mesIns=parseInt(xMonth.value);
		var anioIns=parseInt(xYear.value);
		var numeroCard=xNumber.value;
		var numeroCodigo=xCode.value;
		
		if (anioIns>anioAct){
			siguiente=true;
		}else{
			if (mesIns>mesAct){
				siguiente=true;
			}
		}
		
		if (siguiente==true){
			if (xCard.value=="American Express" && numeroCard.substring(0,1)=="3" && numeroCard.length==15){
				if (numeroCodigo==""){
					valido=true;
				}else if (numeroCodigo.length==4){
					valido=true;
				}else{
					alert("Your credit card verification code is invalid");
				}
			}else if (xCard.value=="Visa" && numeroCard.substring(0,1)=="4" && numeroCard.length==16){
				if (numeroCodigo==""){
					valido=true;
				}else if (numeroCodigo.length==3){
					valido=true;
				}else{
					alert("Your credit card verification code is invalid");
				}
			}else if (xCard.value=="Master Card" && numeroCard.substring(0,1)=="5" && numeroCard.length==16){
				if (numeroCodigo==""){
					valido=true;
				}else if (numeroCodigo.length==3){
					valido=true;
				}else{
					alert("Your credit card verification code is invalid");
				}
			}else{
				alert("Your credit card number is invalid");
			}
		}else{
			alert("Your credit card expiration date is invalid");
		}
		
		if (valido==true){
			document.getElementById('miComprar').submit();
		}
	}else{
		if (xName.value==""){
			xName.focus();
			alert("Your name is required");
		}else if (xAddress.value==""){
			xAddress.focus();
			alert("Your address is required");
		}else if (xCity.value==""){
			xCity.focus();
			alert("Your city is required");
		}else if (xZip.value==""){
			xZip.focus();
			alert("Your zip is required");
		}else if (xState.value==""){
			xState.focus();
			alert("Your state is required");
		}else if (xCountry.value==""){
			xCountry.focus();
			alert("Your country is required");
		}else if (xEmail.value==""){
			xEmail.focus();
			alert("Your e-mail is required");
		}else if (validarEmail(xEmail.value)==false){
			xEmail.focus();
			alert("Your e-mail is invalid");
		}else if (xPhone.value==""){
			xPhone.focus();
			alert("Your phone is required");
		}else if (xCard.value==""){
			xCard.focus();
			alert("Your credit card is required");
		}else if (xHolder.value==""){
			xHolder.focus();
			alert("Your credit card holder is required");
		}else if (xNumber.value==""){
			xNumber.focus();
			alert("Your credit card numbers is required");
		}else if (xMonth.value==""){
			xMonth.focus();
			alert("The expiry month is required");
		}else if (xYear.value==""){
			xYear.focus();
			alert("The expiry year is required");
		}
	}
	*/
	if (xName.value!="" && xAddress.value!="" && xCity.value!="" && xZip.value!="" && xState.value!="" && xCountry.value!="" && xEmail.value!="" && validarEmail(xEmail.value)==true && xPhone.value!=""){
		document.getElementById('miComprar').submit();
	}else{
		if (xName.value==""){
			xName.focus();
			alert("Your name is required");
		}else if (xAddress.value==""){
			xAddress.focus();
			alert("Your address is required");
		}else if (xCity.value==""){
			xCity.focus();
			alert("Your city is required");
		}else if (xZip.value==""){
			xZip.focus();
			alert("Your zip is required");
		}else if (xState.value==""){
			xState.focus();
			alert("Your state is required");
		}else if (xCountry.value==""){
			xCountry.focus();
			alert("Your country is required");
		}else if (xEmail.value==""){
			xEmail.focus();
			alert("Your e-mail is required");
		}else if (validarEmail(xEmail.value)==false){
			xEmail.focus();
			alert("Your e-mail is invalid");
		}else if (xPhone.value==""){
			xPhone.focus();
			alert("Your phone is required");
		}
	}
}
