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="Debe ingresar todos los campos requeridos";
				mensaje[0]="Debe ingresar los campos obligatorios.<br>";
				bool=0;
			}
			if(evaluar.match("isEmail")=="isEmail" && !isEmail(elemento.value))
			{
				elemento.style.borderColor = colorBorde;
				elemento.style.borderStyle = "solid";
				//mensaje="Debe ingresar un correo electrónico válido";
				mensaje[1]="Debe ingresar un correo electrónico válido.<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="Tiene informacion desigual en los campos marcados";
						mensaje[2]="Tiene información desigual en los campos señalados.<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[4]="Ingrese un código postal válido.<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 válido.<br>";
				bool=0;
			}
			if(evaluar.match("isDecimal")=="isDecimal" && !isDecimal(elemento.value))
			{
				elemento.style.borderColor = colorBorde;
				elemento.style.borderStyle = "solid";
				mensaje[4]="Debe ingresar un valor decimal válido (ex: 12.34).<br>Use el . como separador decimal<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 obligatorios.<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 preloadImages()
{
  if(document.images)
  {
    if(!document.imageArray) document.imageArray = new Array();
    var i,j = document.imageArray.length, args = preloadImages.arguments;
    
    for(i=0; i<args.length; i++)
    {
      if (args[i].indexOf("#")!=0)
      {
        document.imageArray[j] = new Image;
        document.imageArray[j++].src = args[i];
      }
    }
  }
}

function switchImage(imgName, imgSrc) 
{
  if (document.images)
  {
    if (imgSrc != "none")
    {
      document.images[imgName].src = imgSrc;
    }
  }
}
function switchImageFlechas(imgName, oper) 
{
  if (document.images)
  {
    if (oper == "+")
    {
	  document.flechitas++;
	  if(document.flechitas>=document.imageArray.length)
	  {
	  	document.flechitas = 0;
	  }
      document.images[imgName].src = document.imageArray[document.flechitas].src;
    }
	else if (oper == "-")
    {
	  document.flechitas--;
	  if(document.flechitas<=-1)
	  {
	  	document.flechitas = document.imageArray.length-1;
	  }
      document.images[imgName].src = document.imageArray[document.flechitas].src;
	}
  }
}
function gotoURL(url){
	document.location.href = url;
}

function openPop(url) { 
	newWindow=window.open(url); 
}
function openPopSized(url,h,w) { 
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	newWindow=window.open(url, 'popup','height='+h+',width='+w+',toolbar=no,scrollbars=no,resizable=no,status=no,top='+TopPosition+',left='+LeftPosition+';');
	newWindow.resizeTo(w,h);
	newWindow.focus(); 
	return false;
}
function openWindow(url) {
   var opt = 'dialogWidth:500px; dialogHeight:500px; center:yes; scroll:yes; status:yes;';
   window.showModalDialog(url, '', opt); 
}