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 IT_EsFecha(Fecha) {
	var p,p2,dia,mes,year,esDate,resto,varFecha;
	
	varFecha=Fecha;
	esDate=false;
	p=varFecha.indexOf("/")
	if (p!=-1) {
		dia=varFecha.substring(0,p);
		resto=varFecha.substring(p+1);
		p2=resto.indexOf("/")
		if (p2!=-1) {
			mes=resto.substring(0,p2);
			year=resto.substring(p2+1);
			if ((dia>0 && dia<32) && (mes>0 && mes<13) && year>-1) {
				esDate=true;
			}
		}
	}
	return esDate;
}

function IT_EsTextoNumero (campo){
	var checkOK = 	"ABCDEFGHIJKLMNÑOPQRSTUVWXYZabcdefghijklmnñopqrstuvwxyz'0123456789";
	 var checkStr = campo;
	 var allValid = true;
	 for (i = 0;  i < checkStr.length;  i++)
	 {
	 	ch = checkStr.charAt(i);
    		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
		        		break;
    			if (j == checkOK.length)
			{
			      	allValid = false;
				break;
			}
  	}
	return allValid;
}

function IT_EsNumero (campo){
	var checkOK = 	"0123456789.,-";
	var checkStr = campo;
	
	return IT_EsCheckOk(checkStr,checkOK);
}

function IT_EsCheckOk(checkStr,checkOK){
	 var allValid = true;
	 for (i = 0;  i < checkStr.length;  i++)
	 {
	 	ch = checkStr.charAt(i);
    		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
		        		break;
    			if (j == checkOK.length)
			{
			      	allValid = false;
				break;
			}
  	}
	return allValid;
}


function IT_validateForm() { //v4.0
  var i,p,q,nm,test,out,num,min,max,errors='',args=IT_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]); out=args[i+1];
    if (val) { nm=val.name;
	 if (out==""){out=nm;}
     if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+out+' it must contain an E-mail address.\n';
	  } else if (test.indexOf('isDate')!=-1) {
        if (!IT_EsFecha(val)) errors+='- '+out+' it must contain a date dd/mm/yy.\n';
      } else if (test.indexOf('isTxNum')!=-1) {
        if (!IT_EsTextoNumero(val)) errors+='- '+out+' it can only contain letters and numbers.\n';
      }else if ((p=test.indexOf('isTxMin'))!=-1) { min=test.substring(p+7);
        if (val.length<min) errors+='- '+out+' it must contain '+min+' characters at least (there are '+val.length+').\n';
      }else if ((p=test.indexOf('isTxMax'))!=-1) { max=test.substring(p+7);
        if (val.length>max) errors+='- '+out+' it must contain '+max+' characters at the most (there are '+val.length+').\n';
      } else if (test!='R') {
        if (isNaN(val)) errors+='- '+out+' it must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (val<min || max<val) errors+='- '+out+' it must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+out+' it is a required field.\n'; }
  } if (errors) alert('Detected errors:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function IT_CamposIguales() {
  var val1,val2,campo1,campo2,args=IT_CamposIguales.arguments;
  if (document.MM_returnValue==true){
  	val1=MM_findObj(args[0]);
  	val2=MM_findObj(args[2]);
 	val1=val1.value;
	val2=val2.value;
  	if (val1!=val2) {
		campo1=args[1];
		campo2=args[3];
		alert('The fields '+campo1+' and '+campo2+' must have the same value');
  	}
 	document.MM_returnValue = (val1==val2);
 }
}
