 function EMF_findObj(n, d) { //v4.01
  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=EMF_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
 }

 function EMF_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=EMF_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=EMF_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+args[i+1]+' ha de contenir una adreça d\'e-mail.\n';
      } else if ( test == 'URL'	) {
	url_validate = validate_url(val);
	if (!url_validate) {
	 errors+= '- '+args[i+1]+' url no es valid\n';
	}
      } else if ( test == 'DNI' ) {
	nif_validate = validate_nif(val);
	if (!nif_validate) {
	 errors+= '- '+args[i+1]+' no es un valid DNI\n';
	}
      } else if ( test == 'NUM' ) {
        if (isNaN(val)) errors+='- '+args[i+1]+' ha de contenir un número.\n';
      } else if ( test == 'DATA' ) {
	data_validate = validate_data(val);
	if (!data_validate) {
	 errors+= '- '+args[i+1]+' no es una data valida\n';
	}
      } else if (test!='R') {
        if (isNaN(val)) errors+='- '+args[i+1]+' ha de contenir un número.\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+='- '+args[i+1]+' ha de contenir un número entre '+min+' i '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+args[i+1]+' es obligatori.\n'; }
  } if (errors) alert('S\'han trobat els següents errors:\n'+errors);
  document.EMF_returnValue = (errors == '');
 }


 function validate_nif( cif ) {
                // Based on php function of David Vidal Serra.
                //Returns: 1 = NIF ok, 2 = CIF ok, 3 = NIE ok, -1 = NIF bad, -2 = CIF bad, -3 = NIE bad, 0 = ??? bad
                num = new Array();
                cif = cif.toUpperCase();
                for (i = 0; i < 9; i ++) {
                    num[i] = cif.substr(i, 1);
                }
                //invalid format ?
                if (!cif.match('((^[A-Z]{1}[0-9]{7}[A-Z0-9]{1}$|^[T]{1}[A-Z0-9]{8}$)|^[0-9]{8}[A-Z]{1}$)')) {
                    return 0;
                }
                //NIF estandard
                if (cif.match('(^[0-9]{8}[A-Z]{1}$)')){
                    if (num[8] == 'TRWAGMYFPDXBNJZSQVHLCKE'.substr(cif.substr(0, 8) % 23, 1)){
                        return 1;
                    } else {
                        return -1;
                    }
                }
                //CIF ...
                suma = num[2] + num[4] + num[6];
                for (i = 1; i < 8; i += 2) {
                    suma += toString((2 * num[i])).substr(0,1) + toString((2 * num[i])).substr(1,1);
                }
                n = 10 - suma.substr( suma.length - 1, 1);
                // NIF especial ?
                if (cif.match('^[KLM]{1}')) {
                    if (num[8] == String.fromCharCode(64 + n)){
                        return 1;
                    } else {
                        return -1;
                    }
                }
                //CIFs
                if (cif.match('^[ABCDEFGHJNPQRSUVW]{1}')) {
                    if (num[8] == String.fromCharCode(64 + n) || num[8] == n.substr(n.length - 1, 1)) {
                        return 2;
                    } else {
                        return -2;
                    }
                }
                //NIEs
                //T
                if (cif.match('^[T]{1}')) {
                    if (num[8] == cif.match('^[T]{1}[A-Z0-9]{8}$')) {
                        return 3;
                    } else {
                        return -3;
                    }
                }
                //XYZ
                if (cif.match('^[XYZ]{1}')) {
                    tmpstr = cif.replace('X', '0');
                    tmpstr = tmpstr.replace('Y', '1');
                    tmpstr = tmpstr.replace('Z', '2');
                    if (num[8] == 'TRWAGMYFPDXBNJZSQVHLCKE'.substr( tmpstr.substr(0, 8) % 23, 1)) {
                        return 3;
                    } else {
                        return -3;
                    }
                }
                //error 0
                return 0;

 }


 function validate_decimal(valor) {
  num_decimals = 2;
 

 }

 function validate_url(url) {
	var regexp = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
 	return regexp.test(url);
 }

 function validate_data(txtDate){  
   var objDate;  // date object initialized from the txtDate string  
   var mSeconds; // milliseconds from txtDate  
   
   // date length should be 10 characters - no more, no less  
   if (txtDate.length != 10) return false;  
   
   // extract day, month and year from the txtDate string  
   // expected format is mm/dd/yyyy  
   // subtraction will cast variables to integer implicitly  
   var day   = txtDate.substring(8,10)  - 0;  
   var month = txtDate.substring(5,7)  - 1; // because months in JS start with 0  
   var year  = txtDate.substring(0,4) - 0;  

   var data = month + "/" + day + "/" + year;

      // third and sixth character should be /  
   if (txtDate.substring(7,8) != '-') return false;  
   if (txtDate.substring(4,5) != '-') return false;  
   
   // test year range  
   if (year < 999 || year > 3000) return false;  
   
   // convert txtDate to the milliseconds  
   mSeconds = (new Date(year, month, day)).getTime();  
   
   // set the date object from milliseconds  
   objDate = new Date();  
   objDate.setTime(mSeconds);  
   
   // if there exists difference then date isn't valid  
   if (objDate.getFullYear() != year)  return false;  
   if (objDate.getMonth()    != month) return false;  
   if (objDate.getDate()     != day)   return false;  
   
   // otherwise return true  
   return true;  
 }  
