var isEmail_re = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/; 
function isEmail (s) { 
   return String(s).search (isEmail_re) != -1; 
}

var isZipcode_re = /^\s*[0-9]{5}\s*$/; 
function isZipcode (s) { 
   return String(s).search (isZipcode_re) != -1; 
}

function isInteger(s) {
return (s.toString().search(/^[0-9]+$/) == 0);
}

var isDecimal_re = /^[0-9]+(\.[0-9]{1,2})?$/; 
function isDecimal(s)
{
  return String(s).search (isDecimal_re) != -1; 
}
// Example: obj = findObj("image1");
function findObj(theObj, theDoc)
{
  var p, i, foundObj;
  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
  return foundObj;
}


var colorBorde = "#E70000";

function check_form(f) { // f is the form (passed using the this keyword)
	var formElements = "";
	var bool=1;
	var mensaje = new Array();
	var comparar = new Array();
	for (var n=0; n < f.elements.length; n++) {
	  var elemento = f.elements[n];
      var tipo = elemento.type;
	  var evaluar = elemento.getAttribute('eval');
	  if(tipo == "text" || tipo == "textarea" || tipo == "password"){
		
		if(evaluar != null)
		{
			if(elemento.value == "" && evaluar.match("isEmpty")!=null)
			{
				elemento.style.borderColor = colorBorde;
				elemento.style.borderStyle = "solid";
				mensaje[0]="Debe ingresar todos los campos requeridos";
				//mensaje[0]="You must enter all required fields.<br>";
				bool=0;
			}
			if(evaluar.match("isEmail")=="isEmail" && !isEmail(elemento.value))
			{
				elemento.style.borderColor = colorBorde;
				elemento.style.borderStyle = "solid";
				mensaje[1]="Debe ingresar un correo electrónico válido";
				//mensaje[1]="You must enter a valid email.<br>";
				bool=0;
			}
			if(evaluar.match("isEqual")=="isEqual")
			{
				evalWith = evaluar.substring(evaluar.indexOf("(")+1,evaluar.lastIndexOf(")"))
				obj = findObj(evalWith);
				if(elemento.value != "" || obj.value != "")
				{
					if(elemento.value != obj.value)
					{
						elemento.style.borderColor = colorBorde;
						elemento.style.borderStyle = "solid";
						mensaje[2]="Tiene informacion desigual en los campos marcados";
						//mensaje[2]="You have unequal information in the fields marked.<br>";
						bool=0;
					}else{
						elemento.style.borderColor = "";
						elemento.style.borderStyle = "";
					}
				}
			}
			if(evaluar.match("isZipcode")=="isZipcode" && !isZipcode(elemento.value) && elemento.value != "")
			{
				elemento.style.borderColor = colorBorde;
				elemento.style.borderStyle = "solid";
				mensaje[3]="Debe ingresar un código postal válido.<br>";
				//mensaje[3]="You must enter a valid Zipcode.<br>";
				bool=0;
			}
			if(evaluar.match("isInteger")=="isInteger" && !isInteger(elemento.value))
			{
				elemento.style.borderColor = colorBorde;
				elemento.style.borderStyle = "solid";
				mensaje[4]="Debe ingresar un número entero.<br>";
				//mensaje[4]="You must enter a valid Integer.<br>";
				bool=0;
			}
			if(evaluar.match("isDecimal")=="isDecimal" && !isDecimal(elemento.value))
			{
				elemento.style.borderColor = colorBorde;
				elemento.style.borderStyle = "solid";
				mensaje[5]="Debe ingresar un número decimal(ej: 12.34).<br>Use . como separador decimal<br>";
				//mensaje[5]="You must enter a valid decimal value (ex: 12.34).<br>Use the . as decimal separator<br>";
				bool=0;
			}
		}
      }else if(tipo == "select-one"){
		if(evaluar != null)
		{
			if(elemento.value == "" && evaluar.match("isEmpty")!=null)
			{
				elemento.style.backgroundColor = "#FF6666";
				mensaje[0]="Debe ingresar los campos requeridos.<br>";
				//mensaje[0]="You must enter all required fields.<br>";
				bool=0;
			}
		}
      }
  	}
	if($('mensaje')){
		var msg = "";
		for(i=0;i<mensaje.length;i++)
		{
			if(mensaje[i]){
				msg += mensaje[i];
			}
		}
		$('mensaje').innerHTML=msg;
		$('mensaje').style.display='';
	}
	
	if(bool==0)
	{
		return false;
	}else{
		return true;
	}
}

function cambiarOnFocus(c){
	var str = c.style.borderColor.toString();
	if(str.toUpperCase()==colorBorde.toUpperCase())
	{
		c.style.borderColor = "";
		c.style.borderStyle = "";
		c.style.borderWidth = "";
	}
	c.style.backgroundColor="";
	

}

function checkZipcode(f){
	var bool=1;
	var mensaje = new Array();
	if(whichPressed == 'Calculate'){
		if(f.txtZipcode.value == "" || !isZipcode(f.txtZipcode.value))
		{
			f.txtZipcode.style.borderColor = colorBorde;
			f.txtZipcode.style.borderStyle = "solid";
			mensaje[0]="You must enter a valid Zipcode.<br>";
			bool=0;
		}
	}else if(whichPressed == 'Update'){
		if(f.txtZipcode.value != "" && !isZipcode(f.txtZipcode.value))
		{
			f.txtZipcode.style.borderColor = colorBorde;
			f.txtZipcode.style.borderStyle = "solid";
			mensaje[0]="You must enter a valid Zipcode.<br>";
			bool=0;
		}
	}
	
	if($('mensaje')){
		var msg = "";
		for(i=0;i<mensaje.length;i++)
		{
			if(mensaje[i]){
				msg += mensaje[i];
			}
		}
		$('mensaje').innerHTML=msg;
		$('mensaje').style.display='';
	}
	if(bool==0)
	{
		return false;
	}else{
		return true;
	}
}
function cambioEnvio(c){
	var tax = new Number($('hiddenTax').value);
	var ground = new Number($('hiddenGRD').value);
	var secondDay = new Number($('hidden2DA').value);
	var nextDay = new Number($('hiddenNDA').value);
	var subtotal = new Number($('hiddenSubtotal').value);
	if(c.value=="GRD"){
		var valorTax = new Number((subtotal+ground)*tax/100);
		var valorTotal = new Number((subtotal+ground)*(1+tax/100));
		$('divEnvio').innerHTML = ground + " USD";
		$('divTax').innerHTML = valorTax.toFixed(2) + " USD";
		$('divTotal').innerHTML = valorTotal.toFixed(2) + " USD";
	}else if(c.value=="2DA"){
		var valorTax = new Number((subtotal+secondDay)*tax/100);
		var valorTotal = new Number((subtotal+secondDay)*(1+tax/100));
		$('divEnvio').innerHTML = secondDay + " USD";
		$('divTax').innerHTML = valorTax.toFixed(2) + " USD";
		$('divTotal').innerHTML = valorTotal.toFixed(2) + " USD";
	}else if(c.value=="NDA"){
		var valorTax = new Number((subtotal+nextDay)*tax/100);
		var valorTotal = new Number((subtotal+nextDay)*(1+tax/100));
		$('divEnvio').innerHTML = nextDay + " USD";
		$('divTax').innerHTML = valorTax.toFixed(2) + " USD";
		$('divTotal').innerHTML = valorTotal.toFixed(2) + " USD";
	}
	return true;
}


function changeShipping(c){
	var taxGRD = new Number($('taxGRD').value);
	var tax2DA = new Number($('tax2DA').value);
	var taxNDA = new Number($('taxNDA').value);
	var totalGRD = new Number($('totalGRD').value);
	var total2DA = new Number($('total2DA').value);
	var totalNDA = new Number($('totalNDA').value);
	var subtotal = new Number($('hiddenSubtotal').value);
	if(c.value=="GRD"){
		var valorTax = new Number(taxGRD);
		var valorTotal = new Number(totalGRD);
		$('divTax').innerHTML = valorTax.toFixed(2) + " USD";
		$('divTotal').innerHTML = valorTotal.toFixed(2) + " USD";
	}else if(c.value=="2DA"){
		var valorTax = new Number(tax2DA);
		var valorTotal = new Number(total2DA);
		$('divTax').innerHTML = valorTax.toFixed(2) + " USD";
		$('divTotal').innerHTML = valorTotal.toFixed(2) + " USD";
	}else if(c.value=="NDA"){
		var valorTax = new Number(taxNDA);
		var valorTotal = new Number(totalNDA);
		$('divTax').innerHTML = valorTax.toFixed(2) + " USD";
		$('divTotal').innerHTML = valorTotal.toFixed(2) + " USD";
	}
	return true;
}

function Comprar(URL){
  if (confirm("¿Esta seguro que desea realizar\nla compra de estos articulos?"))
  {
	  	$('hiddenCheckoutZIP').value = $('txtZipcode').value;
	  	$('hiddenCheckoutUPS').value = $('cbxUPS').value;
		document.compraForm.submit();
  }else{
		return false;
  }
	return true;
}

function switchImage(imgName, imgSrc) 
{
  if (document.images)
  {
    if (imgSrc != "none")
    {
      document.images[imgName].src = imgSrc;
    }
  }
}

function gotoURL(url){
	document.location.href = url;
}

function doExpressCheckout()
{
	document.paypalPayment.submit();
	return false;
}

function printpage() {
	window.print();
}

function switchImage(imgName, imgSrc) 
{
  if (document.images)
  {
    if (imgSrc != "none")
    {
      document.images[imgName].src = imgSrc;
    }
  }
}