function domandaValidator(theForm)
{
  if (theForm.domanda.value == "0")
  {
    alert("E' necessario scegliere una Domanda Segreta.");
    theForm.domanda.focus();
    return (false);
  }    
	
  if (theForm.risposta.value.length < 4)
  {
    alert("La lunghezza minima della Risposta Segreta è di 4 caratteri.");
    theForm.risposta.focus();
    return (false);
  }
	
	return (true);
} 
 

function passwordValidator(theForm)
{
  if (theForm.password.value.length < 6)
  {
    alert("Il campo \"password\" ha lunghezza minima di 6 caratteri.");
    theForm.password.focus();
    return (false);
  }    
	
	if (theForm.password.value != theForm.passwordConfirm.value)
	{
	    alert("La password digitata non corrisponde a quella confermata.");
	    theForm.passwordConfirm.focus();
	    return (false);
	 }    
	cifraturaPassword(theForm);
	
	return (true);
}

function cifraturaPassword(	theForm ) 
{
	   bs="\\ ";
       cs=' !"#$%&';
       cs+="`()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRS"+"TUVWXYZ^[_]`abcdefghijklmnopqrstuvwxyz{|}~\t\r\n"+bs;
       cm=0;
       cm1=0;
       
       sixteen="0000 0001 0010 0011 0100 0101 0110 0111 "
              +"1000 1001 1010 1011 1100 1101 1110 1111 ";
       sixletter=";    .    *    ,    x    (    i    :    "
                +"_    -    ^    <    >    `    ~    =    ";
        ls="";
        ls1="";
        kwd="giovannibattista";
        kwdpd=kwd;		   

		if (theForm.name=="inizio")
		{							   
			msg=theForm.passwordOrig.value;
		}
		else {
        	msg=theForm.password.value;
		}
        sl=msg.length;
        while (kwd.length<sl){
         kwd+=kwdpd;
         }
        
        for (var i=0;i<msg.length;i++){
         chk=kwd.charAt(i);
         cmk=cs.indexOf(chk)+32;
         chm=msg.charAt(i);
         cmm=cs.indexOf(chm)+32;
         for (var j=7;j>-1;j--){
          c=Math.pow(2,j);
          if (cmk>=c){
           cmk=cmk-c;
           ls+="1";
           }
          else{
           ls+="0";
           }
          if (cmm>=c){
           cmm=cmm-c;
           ls1+="1";
           }
          else{
           ls1+="0";
           }
          }
         }
       
        ls2="";
        ls4="";
        for (var i=0;i<ls1.length;i++){
         ch=ls.charAt(i);
         ch1=ls1.charAt(i);
         if (ch=="0"){
          if (ch1=="0"){
           ch1="1";
           }
          else{
           ch1="0";
           } 
          }
         ls2+=ch1;
         }
        for (var i=0;i<ls2.length;i=i+4){
         ls3=ls2.substring(i,i+4);
         y=sixteen.indexOf(ls3);
         ls4+=sixletter.charAt(y);
         }
        theForm.password.value=ls4; 	  
		return(true);
}




function form_Validator2(theForm)
{	   
//Uses the first branch only for insert cases, otherwise the control passess directly to the second part of the script
if (theForm.name=="insert")
{		
  if (!theForm.text1[0].checked) 
  {
    alert("Devi sottoscrivere la Privacy");
    theForm.text1[0].focus();
    return (false);
  }
  
  if (!theForm.text2[0].checked) 
  {
    alert("Devi sottoscrivere la Privacy");
        theForm.text2[0].focus();
    return (false);
  }
  
  if (!theForm.text3[0].checked) 
  {
    alert("Devi sottoscrivere la Privacy");
        theForm.text3[0].focus();
    return (false);    
  }

  if (theForm.cognome.value.length < 2)
  {
    alert("Il campo \"Cognome\" è obbligatorio.");
    theForm.cognome.focus();
    return (false);
  }	

  if (theForm.cognome.value.indexOf("\'")>-1 || theForm.cognome.value.indexOf("\"")>-1)
  {  
    alert("Spiacente ma i caratteri \' e \" non sono validi. Sostituirli con il carattere \"`\".");
    theForm.cognome.focus();
    return (false);
  }
  
  if (theForm.nome.value.length < 2)
  {
    alert("Il campo \"Nome\" è obbligatorio..");
    theForm.nome.focus();
    return (false);
  }
  if (theForm.nome.value.indexOf("\'")>-1 || theForm.nome.value.indexOf("\"")>-1)
  {  
    alert("Spiacente ma i caratteri \' e \" non sono validi. Sostituirli con il carattere \"`\".");
    theForm.nome.focus();
    return (false);
  }
 
  if (theForm.domanda.value == "0")
  {
    alert("E' necessario scegliere una Domanda Segreta.");
    theForm.cognome.focus();
    return (false);
  }
  
  if (theForm.risposta.value.length < 4)
  {
    alert("La lunghezza minima della Risposta Segreta è di 4 caratteri.");
    theForm.cognome.focus();
    return (false);
  }
  
  if (theForm.datanascita.value.length < 4)
  {
    alert("Il campo \"data di nascita\" è obbligatorio nel formato GG/MM/AAAA");
    theForm.datanascita.focus();
    return (false);
  }	 
  data_nascita=theForm.datanascita.value;
  if ( !dataValidator(data_nascita) )
  {
  	alert ("Il formato del campo data non è corretto (GG/MM/AAAA)!");
    theForm.datanascita.focus();
    return (false);	
  }					 
          	var data_odierna = new Date();

			giorno = data_odierna.getDate();
			mese = data_odierna.getMonth()+1;
			anno = data_odierna.getYear();
			
			if (anno < 1000)
			{ anno = anno + 1900;
			}
			
			if ((anno-data_nascita.substring(6))<16)
			{
				alert("Siamo spiacenti ma l'età minima consentita per la registrazione è 16 anni"  );
				return false;
			}
			if ((anno-data_nascita.substring(6))==16 && mese<=data_nascita.substring(3,5))
			{
				alert("Siamo spiacenti ma l'età minima consentita per la registrazione è 16 anni" );
				return false;
			}

  if (theForm.email.value.length < 4)
  {
    alert("Il campo \"email\" è obbligatorio..");
    theForm.email.focus();
    return (false);
  }
  if (theForm.email.value.indexOf("\'")>-1 || theForm.email.value.indexOf("\"")>-1)
  {  
    alert("Spiacente ma i caratteri \' e \" non sono ammessi nella email.");
    theForm.email.focus();
    return (false);
  }

if (theForm.email.value.indexOf("@")<3 || theForm.email.value.indexOf(".")==-1)
		{
         alert("Attenzione: formato email errato");
		 theForm.email.focus();
         return false;
        }


  if (theForm.password.value.length < 6)
  {
    alert("Il campo \"password\" è obbligatorio e la lunghezza minima sono 6 caratteri.");
    theForm.password.focus();
    return (false);
  } 
  if (theForm.name=="insert")
  {
   if (theForm.password.value != theForm.passwordConfirm.value)
	{
	    alert("La password digitata non corrisponde a quella confermata.");
	    theForm.passwordConfirm.focus();
	    return (false);
	}    
  }  				  
  cifraturaPassword(theForm)

	if (theForm.txtsett.selectedIndex<0) 
	{
// Per ottenere il testo:	regione = box.options[box.selectedIndex].text;
	alert ("Il campo \"Settore di Interesse\" è essenziale per la ricerca del lavoro ed è obbligatorio.."); 
	theForm.txtsett.focus();
	return false;
	}
	
	  if (theForm.sesso.selectedIndex == 0)
  {
    alert("Il campo \"Sesso\" è obbligatorio..");
    theForm.sesso.focus();
    return (false);
  }
	
	if (theForm.canaleinformativo.selectedIndex<1) 
	{
// Per ottenere il testo:	regione = box.options[box.selectedIndex].text;
	alert ("Il campo \"Canale Informativo\" è obbligatorio.."); 
	theForm.canaleinformativo.focus();
	return false;
	}
}
//--------------- Controlli sia insert che update!
if (theForm.name=="insert" || theForm.name=="anagrafica")
{

  if (theForm.citta.selectedIndex == 0)
  {
    alert("Il campo \"Citta\" è obbligatorio..");
    theForm.citta.focus();
    return (false);
  }
  if (theForm.citta.value.indexOf("\'")>-1 || theForm.citta.value.indexOf("\"")>-1)
  {  
    alert("Spiacente ma i caratteri \' e \" non sono validi. Sostituirli con il carattere \"`\".");
    theForm.citta.focus();
    return (false);
  }
  if (theForm.cap.selectedIndex == 0)
  {
    alert("Il campo \"Citta\" è obbligatorio..");
    theForm.citta.focus();
    return (false);
  }
  
  if (theForm.indirizzo.value.length < 2)
  {
    alert("Il campo \"indirizzo\" è obbligatorio..");
    theForm.indirizzo.focus();
    return (false);
  }

  if (theForm.indirizzo.value.indexOf("\'")>-1 || theForm.indirizzo.value.indexOf("\"")>-1)
  {  
    alert("Spiacente ma i caratteri \' e \" non sono validi. Sostituirli con il carattere \"`\".");
    theForm.indirizzo.focus();
    return (false);
  }
  
  
	box = theForm.txtregione;
	if (box.selectedIndex<1) 
	{
// Per ottenere il testo:	regione = box.options[box.selectedIndex].text;
	alert ("Il campo \"Regione\" è obbligatorio.."); 
	box.focus();
	return false;
	}

  if (theForm.cap.value.length < 5)
  {
    alert("Il campo \"cap\" è obbligatorio e di 5 caratteri..");
    theForm.cap.focus();
    return (false);
  }
  else
  {
  	         	cmp="0123456789";	
				cap=theForm.cap.value
				for (var i=0;i<cap.length;i++)
			{

	         	tst=cap.substring(i,i+1);
	         	if (cmp.indexOf(tst)<0){ 
				  alert ("Il campo cap deve contenere solamente caratteri numerici");
				  theForm.cap.focus();
		          return false;
        	 	} 
				
        	}
	}

//---------------------------Inserire un controllo sulla retribuzione attuale
  if (theForm.telefonocasa.value.length>0)
  {										
    nr1=theForm.telefonocasa.value
	flg=0;

       		for (var i=0;i<nr1.length;i++)
			{
	         	cmp="0123456789";
	         	tst=nr1.substring(i,i+1);
	         	if (cmp.indexOf(tst)<0){ 
				  alert ("Il campo Telefono Casa deve essere numerico:" + nr1);
				  theForm.telefonocasa.focus();
		          return false;
        	 	} 
				
        	}
	}

 	if (theForm.cellulare.value.length>0)
	{										
    nr1=theForm.cellulare.value
	flg=0;
			
       		for (var i=0;i<nr1.length;i++)
			{
	         	cmp="0123456789";
	         	tst=nr1.substring(i,i+1);
	         	if (cmp.indexOf(tst)<0){ 
				  alert ("Il campo cellulare deve essere numerico:" + nr1);
				  theForm.cellulare.focus();
		          return false;
        	 	} 
				
        	}
	}
	if (theForm.telefonocasa.value.length ==0 && theForm.cellulare.value.length==0)
	{
				  alert ("Almeno uno tra cellulare e Telefono è obbligatorio");
				  theForm.telefonocasa.focus();
		          return false;	
	}

}


if (theForm.name=="lavoro")
{

  if (theForm.attualelavoro.value.indexOf("\'")>-1 || theForm.attualelavoro.value.indexOf("\"")>-1)
  {  
    alert("Spiacente ma i caratteri \' e \" non sono validi");
    theForm.attualelavoro.focus();
    return (false);
  }
  if (theForm.esperienzalavorativa1.value.indexOf("\'")>-1 || theForm.esperienzalavorativa1.value.indexOf("\"")>-1)
  {  
    alert("Spiacente ma i caratteri \' e \" non sono validi");
    theForm.esperienzalavorativa1.focus();
    return (false);
  }

  if (theForm.esperienzalavorativa2.value.indexOf("\'")>-1 || theForm.esperienzalavorativa2.value.indexOf("\"")>-1)
  {  
    alert("Spiacente ma i caratteri \' e \" non sono validi");
    theForm.esperienzalavorativa2.focus();
    return (false);
  }

  if (theForm.hobby.value.indexOf("\'")>-1 || theForm.hobby.value.indexOf("\"")>-1)
  {  
    alert("Spiacente ma i caratteri \' e \" non sono validi");
    theForm.hobby.focus();
    return (false);
  } 
} 
//--------------------------
  return (true);
}

function dataValidator(data){ 

			if (data.length<10)
			{ return (false);}
			cmp="0123456789";
       		for (var i=0;i<2;i++)
			{	         	
	         	tst=data.substring(i,i+1);
	         	
	         	if (cmp.indexOf(tst)<0){ 		  	  
		          return false;
        	 	} 		
			 }
			
			for (var i=3;i<5;i++)
			{	         	
	         	tst=data.substring(i,i+1);
	         	if (cmp.indexOf(tst)<0){ 
		          return false;
        	 	} 		
			 }
			for (var i=6;i<10;i++)
			{	         	
	         	tst=data.substring(i,i+1);
	         	if (cmp.indexOf(tst)<0){ 
		          return false;
        	 	} 		
			 }
			 tst=data.substring(0,3);
			if (tst=="00/")
			{ 
		          
			return false;
        	 	}
			tst=data.substring(3,6);
			if (tst=="00/")
			{ 
		          return false;
        	 	}
			tst=data.substring(5);			
			if (tst=="/0000")
			{ 
		          return false;
        	 	}

	return (true);
}  



