 // check form

// funzione per il controllo e-mail 
function checkEmail(emailStr) {
/*	if (emailStr.length == 0) {
   		return true;
	}*/
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray == null) {
	   return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	if (user.match(userPat) == null) {
 	  return false;
	}
	var IPArray = domain.match(ipDomainPat);
	if (IPArray != null) {
 	  for (var i = 1; i <= 4; i++) {
 	     if (IPArray[i] > 255) {
  	       return false;
   	     }
  	  }
 	  return true;
	}
	var domainArray=domain.match(domainPat);
	if (domainArray == null) {
 	  return false;
	}
	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;
	if ((domArr[domArr.length-1].length < 2) ||
	   (domArr[domArr.length-1].length > 3)) {
 	  return false;
	}
	if (len < 2) {
 	  return false;
	}
	return true;
}

// funzione controllo date
function isValidDate(day, month, year) {
	
	if (month < 1 || month > 12) {
              return false;
        }
        if (day < 1 || day > 31) {
              return false;
        }
        if ((month == 4 || month == 6 || month == 9 || month == 11) &&
           (day == 31)) {
                return false;
        }
        if (month == 2) {
            var leap = (year % 4 == 0 &&
                  (year % 100 != 0 || year % 400 == 0));
            if (day>29 || (day == 29 && !leap)) {
                 return false;
            }
        }
        return true;
}

// funzioni controllo codice fiscale
function Trim(str){
   reTrim=/\s+$|^\s+/g;
   return str.replace(reTrim,"");
}

function checkCodFisc(str){
   var ereg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
   Codice=Trim(str.toUpperCase());
   return ereg.test(Codice);
}

// funzione controllo user name
function checkUserID(){
	var ereg = /[^a-zA-Z0-9_\.@àáèéìòùÀÁÈÉÌÍÒÓÙÚ]/m;
	return ereg.test(document.theForm.UserName.value);	
}

function writeUserID(s){
	var ereg = /[^a-zA-Z0-9_\.@àáèéìòùÀÁÈÉÌÍÒÓÙÚ]/g;
	return s.replace(ereg,'');	
}

function TrimSpace(str){
   reTrim=/\s+/g;
   return str.replace(reTrim,"");
}

function writeUserName(){
	var checkDipPA = document.theForm.checkDipPA.checked;
	var email = Trim(document.theForm.email.value.toLowerCase());
	
	if (checkDipPA){
            if (email != ""){    
	      var pEmail = email.split("@");
   	      if (pEmail.length > 1){
   		 document.theForm.email.value = pEmail[0];	
   	      }
   	    
   	      if (document.theForm.ammApp.options[document.theForm.ammApp.selectedIndex].value == -1){
   	         alert(msg21);
  	         document.theForm.ammApp.focus();
  	         return false;
   	      }
   	    
   	      email = pEmail[0]+"@"+document.theForm.ammApp.options[document.theForm.ammApp.selectedIndex].value;
   	      document.theForm.UserName.value = document.theForm.UserName.title = writeUserID(email);
   	      document.theForm.UserName.disabled = true;
   	    }else{
   	      alert(msg23);
  	      document.theForm.email.focus();
  	      return false;
   	    }
	}	
}

function onblurEmail(){
	var checkDipPA = document.theForm.checkDipPA.checked;
	var email = Trim(document.theForm.email.value.toLowerCase());
	
	if (checkDipPA){
            if (email != ""){    
	      var pEmail = email.split("@");
   	      if (pEmail.length > 1){
   		 document.theForm.email.value = pEmail[0];	
   	      }
   	      
   	      if (document.theForm.ammApp.options[document.theForm.ammApp.selectedIndex].value == -1){
   	      	email = pEmail[0];
   	      }else{
   	        email = pEmail[0]+"@"+document.theForm.ammApp.options[document.theForm.ammApp.selectedIndex].value;
   	      }
       	      document.theForm.UserName.value = document.theForm.UserName.title = writeUserID(email);
   	      document.theForm.UserName.disabled = true;
   	    }
	}	
}


function onfocusEmail(){
	var checkDipPA = document.theForm.checkDipPA.checked;
	if (checkDipPA){   
   	    if (document.theForm.ElencoAmmPA.value == "-1"){
      	      alert(msg22);
 	      document.theForm.ElencoAmmPA.focus();
  	      return false;
            }
            
            if (document.theForm.ammApp.options[document.theForm.ammApp.selectedIndex].value == -1){
   	      alert(msg21);
  	      document.theForm.ammApp.focus();
  	      return false;
   	    }
   	      
        }	
}

function checkUserName(){
	var checkDipPA = document.theForm.checkDipPA.checked;
	if (checkDipPA){   
   	    if (document.theForm.ElencoAmmPA.value == "-1"){
      	      alert(msg22);
 	      document.theForm.ElencoAmmPA.focus();
  	      return false;
            }
            
            if (document.theForm.ammApp.options[document.theForm.ammApp.selectedIndex].value == -1){
   	      alert(msg21);
  	      document.theForm.ammApp.focus();
  	      return false;
   	    }
   	    
   	    if(document.theForm.email.value == ""){
   	      alert(msg23);
  	      document.theForm.email.focus();
  	      return false;
   	    }
   	    
            writeUserName();
        }	
}
 

// controllo di tutti campi presenti sul form
function checkData(theForm){
   var ereg = /[a-zA-Z0-9]/m;
   
   if (!ereg.test(document.theForm.cognome.value)){
   	alert(msg6);
  	document.theForm.cognome.focus();
  	return false;
   }
   
   if (!ereg.test(document.theForm.nome.value)){
   	alert(msg7);
  	document.theForm.nome.focus();
  	return false;
   }
   ereg = /[0|1|2|3]{1}[0-9]{1}\/[0|1]{1}[0-9]{1}\/1[0-9]{3}/m;	
   if (!ereg.test(document.theForm.data.value)){
   	alert(msg9);
  	document.theForm.data.focus();
  	return false;
   }
   
   var dataNasc = document.theForm.data.value;
   var d = dataNasc.split("/");
   var gg = d[0];
   var mm = d[1];
   var aaaa = d[2];
   if (!isValidDate(gg,mm,aaaa)){
   	alert(msg10);
  	document.theForm.data.focus();
  	return false;	
   }
   
   if (!document.theForm.luogo.value.length > 0){
   	alert(msg11);
  	document.theForm.luogo.focus();
  	return false;	
   }
   
   if (!checkCodFisc(document.theForm.fcode.value)){
   	alert(msg12);
  	document.theForm.fcode.focus();
  	return false;
   }
   
   if (!toCheckCodFis(document.theForm.fcode.value)){
   	alert(msg12);
  	document.theForm.fcode.focus();
  	return false;
   }
   
   if (document.theForm.prov.selectedIndex == 0){
   	alert(msg13);
  	document.theForm.prov.focus();
  	return false;
   }
   
  /* controllo lincence a 4 caratteri  
   ereg = /[a-zA-Z0-9]{4}/m;
   if (!ereg.test(theForm['Licence'].value)){
   	alert("licence non corretto");
  	theForm['Licence'].focus();
  	return false;
   }
 */
   if(document.theForm.Licence.value.length < 1 || document.theForm.Licence.value.length > 4){
   	alert(msg14);
  	document.theForm.Licence.focus();
  	return false;
   }
 
   var checkDipPA = document.theForm.checkDipPA.checked;
   
   if (checkDipPA){
      
      if (document.theForm.ElencoAmmPA.value == "-1"){
      	alert(msg22);
 	document.theForm.ElencoAmmPA.focus();
  	return false;
      }
   	
      if(TrimSpace(document.theForm.den_ufficio.value).length < 1){
   	alert(msg16);
  	document.theForm.den_ufficio.focus();
  	return false;
      }
      
      if(TrimSpace(document.theForm.tel_ufficio.value).length < 1){
   	alert(msg17);
  	document.theForm.tel_ufficio.focus();
  	return false;
      }	
      
      if(TrimSpace(document.theForm.ind_ufficio.value).length < 1){
   	alert(msg18);
  	document.theForm.ind_ufficio.focus();
  	return false;
      }
      
      if(document.theForm.accettoCheckbox.checked != true){
   	alert(msg19);
        document.theForm.accettoCheckbox.focus();
        return false;
      }
   	
   }
   
   var email = document.theForm.email.value;
   if (checkDipPA){
   	email = email + "@" + document.theForm.ammApp.options[document.theForm.ammApp.selectedIndex].value;
   }
   
   if (!checkEmail(email)){
   	alert(msg8);
  	document.theForm.email.focus();
  	return false;
   }

   if(document.theForm.UserName.value.length<4){
   	alert(msg1);
  	document.theForm.UserName.focus();
  	return false;
   }

   var checkUserN = checkUserID();
   if (checkUserN){
 	alert(msg2);
  	document.theForm.UserName.focus();
  	return false;
   }
  
   if(document.theForm.Password.value.length<8||document.theForm.Password.value.length>14){
   	alert(msg3);
  	document.theForm.Password.focus();
  	document.theForm.Password.value = "";
  	document.theForm.Confirm.value = "";
  	return false;
   }

   if(document.theForm.Confirm.value.length<8||document.theForm.Password.value.length>14){
   	alert(msg4);
        document.theForm.Confirm.focus();
        return false;
   }
  
   if(document.theForm.accetto[0].checked!=true){
   	alert(msg5);
        document.theForm.accetto[0].focus();
        return false;
   }
   
   var strLicence = (document.theForm.Licence.value).toUpperCase();
   
   for (var i = 1; i < arrayAmmPA.length;i++){
   	   var valueSel = arrayAmmPA[i].split("|");
	   if (strLicence == valueSel[1]){
	   	if(document.theForm.checkDipPA.checked != true){
	   	  alert(msg20);
	          document.theForm.Licence.focus();
	          return false;
	        }
	   }
   }
   return true;
}

function setFocus1(){
   document.theForm.Password.focus();
   return true;
}

function setFocus2(){
   document.theForm.Confirm.focus();
   return true;
}