function MM_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.getElementById(n); return x;
}


function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',fname,args=MM_validateForm.arguments;
  for (i=1; i<(args.length-2); i+=3) { 
	test=args[i+1]; // [R]equerido; [N]umerico; [E]Mail; etc...
	fname=args[i+2]; // Field Name.
	val=MM_findObj(args[i]);
	form=args[0];
	if (val) { 
		nm=val.name; 
		val=val.value;
		
		if (fname.length==0) { fname = nm; }
		
		campo = eval("document." + form + "." + nm);

		if (test.indexOf('R')!=-1) {
			if (val==""){
				alert(fname + " es requerido");
				if (campo.type != 'hidden') campo.focus();
				return false;
			}
		}

		if (test.indexOf('U')!=-1) {
			if (!IsValidUser(val)){
				if (campo.type != 'hidden') campo.focus();
				return false;
			}
		}

		if (test.indexOf('P')!=-1) {
			if (!IsValidPassword(val)){
				if (campo.type != 'hidden') campo.focus();
				return false;
			}
		}

		if (test.indexOf('E')!=-1) { 
			if (!esMail(val)) {
				alert(val + " no es un valor válido para " + fname);
				if (campo.type != 'hidden') campo.focus();
				return false;
			}
		} 
		
		if (test.indexOf('+')!=-1) { 
			if (val!="" && !esUnsigned(val)) {
				alert(val + " no es un valor válido para " + fname);
				if (campo.type != 'hidden') campo.focus();
				return false;
			}
		} 

		if (test.indexOf('D')!=-1) { 
			if (val!="" && !esDate(val)) {
				alert(val + " no es un valor válido para " + fname);
				if (campo.type != 'hidden') campo.focus();
				return false;
			}
		} 

		if (test.indexOf('N')!=-1) {

			if (isNaN("1,0")){ a = ","; b = "."; }
			if (isNaN("1.0")){ a = "."; b = ","; }
			val = val.replace(a,b);

			if (isNaN(val)) {
				alert(fname + " debe ser numérico");
				eval("document." + form + "." + nm + ".focus()");
				return false;
			}
			if (test.indexOf('inRange') != -1) { 
				p=test.indexOf(':');
				min=test.substring(8,p); max=test.substring(p+1);
				if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
				return false;
			} 
		}

		if (test.indexOf('<')!=-1) { 
			q=test.indexOf('<');
			p=test.indexOf('*');
			max=test.substring(q+1,p); 
			
			num = 0;
			for(k=0;k<max.length;k++){
				num = (num * 10) + (max.charAt(k) * 1);
			}
			if (num<val || isNaN(num) || isNaN(num)) {
				alert(fname + " debe ser menor o igual a " + num);
				eval("document." + form + "." + nm + ".focus()");
				return false;
			}
		} 

		if (test.indexOf('>')!=-1) { 
			q=test.indexOf('>');
			p=test.indexOf('*');
			min=test.substring(q+1,p); 
			num = 0;
			neg = false;
			for(k=0;k<min.length;k++){
				if (min.charAt(k)=='-')
					neg = true
				else
					num = (num * 10) + (min.charAt(k) * 1);
			}
//			if (neg==true) num = -1 * num;

			if (val<num || isNaN(num) || isNaN(val)) {
				alert(fname + " debe ser mayor o igual a " + num);
				eval("document." + form + "." + nm + ".focus()");
				return false;
			}
		} 

		if (test.indexOf('C')!=-1) {

			nm=args[i]; 
			if (fname.length==0) { fname = nm; }
			
			var total = 0;
			var max 
			max = eval("document." + form + "." + nm + ".length;");
			for (var idx = 0; idx < max; idx++) {
			if (eval("document." + form + "." + nm + "[" + idx + "].checked") == true) {
				total += 1;
			   }
			}
			if (total == 0){
				alert(fname + " es requerido seleccionar alguna opción.");
				eval("document." + form + "." + nm + "[0].focus()");
				return false;
			}
			return true;
		}



	} 
  }
  return true;
}

function esVacio(val){
if (val.length==0) return true;
return false; 
}

function esNumero(val){
if (isNaN(val)) return false;
return true;
}

function esEmail(val){
p=val.indexOf('@');
if (p<1 || p==(val.length-1)) return false;
return true;
}

function esUnsigned(val){
var re_unsi = /^\d+$/;
	return (re_unsi.test(val))
}

function esMail(val){
var re_mail = /^[\w-\.]+\@[\w\.-]+\.[a-z]{2,4}$/;
	return (re_mail.test(val))
}

function esDate(s_date){
var re_dt = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
	// check format
	if (!re_dt.test(s_date))
		return false;
	// check allowed ranges	
	if (RegExp.$1 > 31 || RegExp.$2 > 12)
		return false;
	// check number of day in month
	var dt_test = new Date(RegExp.$3, Number(RegExp.$2-1), RegExp.$1);
	if (dt_test.getMonth() != Number(RegExp.$2-1))
		return false;
	return true;
}

function IsValidSelec(Text1,Text2){

	if (Text1.value == "0") {
		alert("Debe Seleccionar en donde Buscar");
		return false;
	}
	else if (Text2.value=="") {
		alert("Debe Especificar el Texto a Buscar");
		return false;
	}
	else
	return IsValidText(Text2)
}

function IsValidNumber(astrNumero)
{
var lfstrCheckOK = "0123456789";
var lfstrValueCheck;
var i;
var lfstrChar;

	lfstrValueCheck = astrNumero.value;
	for (i = 0;  i < lfstrValueCheck.length;  i++)
	{
	  lfstrChar = lfstrValueCheck.charAt(i);
	  if (lfstrCheckOK.indexOf(lfstrChar,0) == -1)
	  {
		return false;
	  }
	}
  return true;
}

function IsValidText(astrText)
{
var lfstrCheckOK = "@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz&#402;&#352;&#338;&#381;&#353;&#339;&#382;&#376;ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ0123456789-. !?¿¡_,";
var lfstrValueCheck;
var i;
var lfstrChar;

	lfstrValueCheck = astrText.value;
	for (i = 0;  i < lfstrValueCheck.length;  i++)
	{
	  lfstrChar = lfstrValueCheck.charAt(i);
	  if (lfstrCheckOK.indexOf(lfstrChar,0) == -1)
	  {
		alert("Error en el texto ingresado");
		return false;
	  }
	}
  return true;
}

function IsValidUser(astrText){
var lfstrCheckOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_1234567890.-"
var lfstrValueCheck;

	lfstrValueCheck = astrText ; //.value;
	for (var i = 0;  i < lfstrValueCheck.length;  i++){
	  if (lfstrCheckOK.indexOf(lfstrValueCheck.charAt(i),0) == -1){
		alert("Caracteres inválidos en nombre de usuario.");
		return false;
	  }
	}
  return true;
}


function IsValidPassword(astrText){
var lfstrCheckOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_1234567890.+-/*@#$.-"
var lfstrValueCheck;

	lfstrValueCheck = astrText ; //.value;
	for (var i = 0;  i < lfstrValueCheck.length;  i++){
	  if (lfstrCheckOK.indexOf(lfstrValueCheck.charAt(i),0) == -1){
		alert("Caracteres inválidos en contraseña.");
		return false;
	  }
	}
  return true;
}
