// Java Script functions 
// Calcolo del codice fiscale
// (c) Copyright 1998-1999 Nicola Asuni
// start          : 03/11/1998
// ultima modifica: 09/04/1999


// Variables:
// --------------------------------------------------------------------------
 
var strCodFis="";
var strCodFis2="";
var strcognomesott="";
var strcod_fiscale="";
var strnome="";
var strgiornosex="";
var chrcontrollo='';



// Controls/Definitions:
// --------------------------------------------------------------------------
DefaultValues();

// --------------------------------------------------------------------------

// Functions:

// --------------------------------------------------------------------------
// Setta i prodotti ed i valori di default
// --------------------------------------------------------------------------
function DefaultValues()
{
 strCodFis="";
 strcognomesott="";
 strnome="";
 strgiornosex="";
 chrcontrollo='';
 strcognome="";

return;
}

function CheckCognome(field)
{
 if(field.length < 1)
   {
    //return(0);
	//alert("Manca il Cognome")
   }
return(1);
}
// --------------------------------------------------------------------------
function CheckNome(field)
{ 
 if(field.length < 1)
   {
   //alert("Manca il Nome")
	//return(0);
   }
return(1);
}

function CalcolaCF(nome, cognome)
{
if(CheckCognome(cognome)&&CheckNome(nome)){
   	strc_cognome="";
 	strnome="";

 	strcognome=""
    for (i=0; i<cognome.length; i++) 
        {
         switch (cognome.charAt(i)) 
                {
                  case 'A':
                  case 'E':
                  case 'I':
                  case 'O':
                  case 'U': break;            
                  default : 
                  if((cognome.charAt(i)<='Z')&& (cognome.charAt(i)>'A')){
                   strcognome = strcognome + cognome.charAt(i);
				   }
                }
        }

    if (strcognome.length < 3) 
      {
       for (i=0; i < cognome.length; i++) 
          {
           switch (cognome.charAt(i)) 
                 {
                  case 'A':
                  case 'E':
                  case 'I':
                  case 'O':
                  case 'U': strcognome = strcognome + cognome.charAt(i);
                 }
          }
       if (strcognome.length < 3) 
         {
          for (i=strcognome.length; i<=3; i++) 
             { strcognome = strcognome + 'X'; }
         }
      }
   strc_cognome= strcognome
   strc_cognome = strc_cognome.substring(0,3);
//   alert ("ora strc_cognome vale:"+strc_cognome);
 //------------------------------------------------------------ 



  // processa il nome
  //----------------------------------------------------------------

  //alert (nome)
    for (i=0; i<nome.length; i++) 
       {

		//alert("sono dentro al for i vale:"+i);
        si=0;
		if (nome.charAt(i)=='A') {si=1;}
		if (nome.charAt(i)=='E') {si=1;}
		if (nome.charAt(i)=='I') {si=1;}
		if (nome.charAt(i)=='O') {si=1;}
		if (nome.charAt(i)=='U') {si=1;}
        
		if (si==0) {
			 if((nome.charAt(i)<='Z')&& (nome.charAt(i)>'A'))
                  strnome = strnome + nome.charAt(i);
        }
       }
    if (strnome.length > 3) 
      {
        strnome = strnome.substring(0,1) + strnome.substring(2,4);
      } 
    else {
          if (strnome.length < 3) 
            {
             for (i=0; i<nome.length; i++) 
                {
                  
				  si=0;
				  if (nome.charAt(i)=='A') {si=1;}
				  if (nome.charAt(i)=='E') {si=1;}
				  if (nome.charAt(i)=='I') {si=1;}
				  if (nome.charAt(i)=='O') {si=1;}
				  if ((si==1) || (nome.charAt(i)=='U')) {strnome = strnome + nome.charAt(i)}

                }
             if (strnome.length < 3) 
               {
                for (i=strnome.length; i<=3; i++) 
                   {strnome = strnome + 'X';}
               }
            }
          strnome = strnome.substring(0,3);
          
//          alert ("ora strnome vale:"+strnome);
         }
 //--------------------------------------- Fine processa nome

 strCodFis2 = strc_cognome + strnome;
 //alert("strnome "+strnome)
 //if (document.form1.c_cf_piva=="") document.form1.c_cf_piva=strCodFis2
}	
}
function ControllaCF(nome, cognome, cf)
{
 var u_nome, u_cognome;
 u_nome = nome.toUpperCase();
 u_cognome = cognome.toUpperCase();
 var gs=0;
 var i=0;
 var somma=0; 
 flag_err=false;
 CalcolaCF(u_nome, u_cognome);
 strCodFis=cf.toUpperCase();
if(
    CheckCognome(u_cognome) &&
    CheckNome(u_nome) 
   )
 {
 // calcola la cifra di controllo
 //--------------------------------------------
    for (i=0; i<15; i++) 
       {

		   //alert ("i vale:"+i+", somma="+somma);
		   //alert (strCodFis.charAt(i));

        if (((i+1) % 2) != 0) //caratteri dispari
          {

	   if (strCodFis.charAt(i)=='0') { somma += 1;}
           if (strCodFis.charAt(i)=='A') { somma += 1;}
           if (strCodFis.charAt(i)=='1') { somma += 0;}
           if (strCodFis.charAt(i)=='B') { somma += 0;}
           if (strCodFis.charAt(i)=='2') { somma += 5;}
           if (strCodFis.charAt(i)=='C') { somma += 5;}
           if (strCodFis.charAt(i)=='3') { somma += 7;}
           if (strCodFis.charAt(i)=='D') { somma += 7;}
           if (strCodFis.charAt(i)=='4') { somma += 9;}
           if (strCodFis.charAt(i)=='E') { somma += 9;}
           if (strCodFis.charAt(i)=='5') { somma += 13;}
           if (strCodFis.charAt(i)=='F') { somma += 13;}
           if (strCodFis.charAt(i)=='6') { somma += 15;}
           if (strCodFis.charAt(i)=='G') { somma += 15;}
           if (strCodFis.charAt(i)=='7') { somma += 17;}
           if (strCodFis.charAt(i)=='H') { somma += 17;}
           if (strCodFis.charAt(i)=='8') { somma += 19;}
           if (strCodFis.charAt(i)=='I') { somma += 19;}
           if (strCodFis.charAt(i)=='9') { somma += 21;}
           if (strCodFis.charAt(i)=='J') { somma += 21;}
           if (strCodFis.charAt(i)=='K') { somma += 2;}
           if (strCodFis.charAt(i)=='L') { somma += 4;}
           if (strCodFis.charAt(i)=='M') { somma += 18;}
           if (strCodFis.charAt(i)=='N') { somma += 20;}
           if (strCodFis.charAt(i)=='O') { somma += 11;}
           if (strCodFis.charAt(i)=='P') { somma += 3;}
           if (strCodFis.charAt(i)=='Q') { somma += 6;}
           if (strCodFis.charAt(i)=='R') { somma += 8;}
           if (strCodFis.charAt(i)=='S') { somma += 12;}
           if (strCodFis.charAt(i)=='T') { somma += 14;}
           if (strCodFis.charAt(i)=='U') { somma += 16;}
           if (strCodFis.charAt(i)=='V') { somma += 10;}
           if (strCodFis.charAt(i)=='W') { somma += 22;}
           if (strCodFis.charAt(i)=='X') { somma += 25;}
           if (strCodFis.charAt(i)=='Y') { somma += 24;}
           if (strCodFis.charAt(i)=='Z') { somma += 23;}


//--------------------------------------------------------------------


          } 
        else //caratteri pari
            {
            
				   if (strCodFis.charAt(i)=='0') { somma += 0;}
				   if (strCodFis.charAt(i)=='A') { somma += 0;}
				   if (strCodFis.charAt(i)=='1') { somma += 1;}
				   if (strCodFis.charAt(i)=='B') { somma += 1;}
				   if (strCodFis.charAt(i)=='2') { somma += 2;}
				   if (strCodFis.charAt(i)=='C') { somma += 2;}
				   if (strCodFis.charAt(i)=='3') { somma += 3;}
				   if (strCodFis.charAt(i)=='D') { somma += 3;}
				   if (strCodFis.charAt(i)=='4') { somma += 4;}
				   if (strCodFis.charAt(i)=='E') { somma += 4;}
				   if (strCodFis.charAt(i)=='5') { somma += 5;}
				   if (strCodFis.charAt(i)=='F') { somma += 5;}
				   if (strCodFis.charAt(i)=='6') { somma += 6;}
				   if (strCodFis.charAt(i)=='G') { somma += 6;}
				   if (strCodFis.charAt(i)=='7') { somma += 7;}
				   if (strCodFis.charAt(i)=='H') { somma += 7;}
				   if (strCodFis.charAt(i)=='8') { somma += 8;}
				   if (strCodFis.charAt(i)=='I') { somma += 8;}
				   if (strCodFis.charAt(i)=='9') { somma += 9;}
				   if (strCodFis.charAt(i)=='J') { somma += 9;}
				   if (strCodFis.charAt(i)=='K') { somma += 10;}
				   if (strCodFis.charAt(i)=='L') { somma += 11;}
				   if (strCodFis.charAt(i)=='M') { somma += 12;}
				   if (strCodFis.charAt(i)=='N') { somma += 13;}
				   if (strCodFis.charAt(i)=='O') { somma += 14;}
				   if (strCodFis.charAt(i)=='P') { somma += 15;}
				   if (strCodFis.charAt(i)=='Q') { somma += 16;}
				   if (strCodFis.charAt(i)=='R') { somma += 17;}
				   if (strCodFis.charAt(i)=='S') { somma += 18;}
				   if (strCodFis.charAt(i)=='T') { somma += 19;}
				   if (strCodFis.charAt(i)=='U') { somma += 20;}
				   if (strCodFis.charAt(i)=='V') { somma += 21;}
				   if (strCodFis.charAt(i)=='W') { somma += 22;}
				   if (strCodFis.charAt(i)=='X') { somma += 23;}
				   if (strCodFis.charAt(i)=='Y') { somma += 24;}
				   if (strCodFis.charAt(i)=='Z') { somma += 25;}
            }
    }
	
   somma %= 26;

   if (somma==0) { chrcontrollo='A'; }
   if (somma==1) { chrcontrollo='B'; }
   if (somma==2) { chrcontrollo='C'; }
   if (somma==3) { chrcontrollo='D'; }
   if (somma==4) { chrcontrollo='E'; }
   if (somma==5) { chrcontrollo='F'; }
   if (somma==6) { chrcontrollo='G'; }
   if (somma==7) { chrcontrollo='H'; }
   if (somma==8) { chrcontrollo='I'; }
   if (somma==9) { chrcontrollo='J'; }
   if (somma==10) { chrcontrollo='K'; }
   if (somma==11) { chrcontrollo='L'; }
   if (somma==12) { chrcontrollo='M'; }
   if (somma==13) { chrcontrollo='N'; }
   if (somma==14) { chrcontrollo='O'; }
   if (somma==15) { chrcontrollo='P'; }
   if (somma==16) { chrcontrollo='Q'; }
   if (somma==17) { chrcontrollo='R'; }
   if (somma==18) { chrcontrollo='S'; }
   if (somma==19) { chrcontrollo='T'; }
   if (somma==20) { chrcontrollo='U'; }
   if (somma==21) { chrcontrollo='V'; }
   if (somma==22) { chrcontrollo='W'; }
   if (somma==23) { chrcontrollo='X'; }
   if (somma==24) { chrcontrollo='Y'; }
   if (somma==25) { chrcontrollo='Z'; }
   

//alert ('Controllo=' + chrcontrollo);


}
if (strCodFis.length>0)
{
	if (strCodFis.length!=16) {
		if (strCodFis.length==11)
		{
			// Controllo per probabile Partita IVA
			if (ControllaPI(piva)) {
				flag_err=false;
			} else {
				str_err="La partita Iva inserita non è corretta"; 
				flag_err=true;
				//alert("La partita Iva inserita non è corretta")
			}
		}
		else 
		{
			flag_err=true;
			str_err="Il Codice Fiscale o Partita IVA\ninserito/a è errato/a"; 
			//alert("Il Codice Fiscale o Partita IVA\ninserito/a è errato")
		}
	}
	else 
	{
	    //alert('Entro qui')
	    //alert('strCodFis2 '+strCodFis2)
	    //alert('strCodFis.substr(0,6) '+strCodFis.substr(0,6))
		//str_err="Il Codice Fiscale inserito è errato"; ----
		if (strCodFis.substr(0,6)!=strCodFis2)
		{
			//alert (strCodFis.substr(0,6));
			//alert (strCodFis2);
			
			//alert ("IL codice fiscale non corrisponde al nome e cognome inseriti!");
			str_err = "IL codice fiscale non corrisponde al nome e cognome inseriti!";
			flag_err=true;
		}
		
		if (strCodFis.charAt(15)!=chrcontrollo)
		{
			//alert(strCodFis);
			//alert(chrcontrollo);
			//alert ("ATTENZIONE. L\'ultimo carattere del codice fiscale inserito è errato.");
			str_err = "ATTENZIONE. L\'ultimo carattere del codice fiscale inserito è errato.";
			flag_err=true;
			
		}
	}
}
 if (flag_err) {
 	//alert (str_err); 
	return false;
 }
 //alert("tutto corretto");
 return true; 
}

// Funzione per controllare il selfcheck della Partita IVA
function ControllaPI(piva)
{
	//alert("pino!");
	for(n=0;n < piva.length; n++)
	{
		if (isNaN(piva.charAt(n))) {
			//alert("CFC!");
			return false;
		}
	}
	return CtrlPartitaIva(piva);
	//alert("gino!");
}

function CtrlPartitaIva(In_Piva)
{
  var FSI;
  var FSS;
  var Char11;
  var tmp;
  tmp="";
  Char11=0;
  flag = false;
  if ((In_Piva.length != 11) || (isNaN(In_Piva))) {
    //alert("Partita Iva/Codice Fiscale Errato - incompleto o non numerico"); ---
    return false;
  }
  for (FSI = 1;FSI<=10;FSI++)
  {
    FSS = parseInt(In_Piva.charAt(FSI-1));
    if ((FSI % 2) == 0 )
    {
      FSS = FSS * 2;
      tmp=""+FSS;
      //alert (tmp.length);
      if (tmp.length == 2) {
        FSS = parseInt(FSS / 10) + (FSS % 10);
      }
    }
    Char11 += FSS;
  }
  Char11=eval(Char11);
  Char11 = 10 - (Char11 % 10);
  //alert("Char11="+Char11);
  //alert("In_Piva="+In_Piva.charAt(10));
  if ((Char11 % 10) != In_Piva.charAt(10))
  {
    //alert( "Partita Iva/Codice Fiscale Errato - ultimo numero corretto " + Char11); ---
    flag=false;
  }
  else
    flag = true;
  
  return flag;
}

// --------------------------------------------------------------------------
//  END OF SCRIPT
// --------------------------------------------------------------------------

