// --------------------------------------------------------------------------
// Variables:
// --------------------------------------------------------------------------
var strCodFis="";
var strcognome="";
var strnome="";
var strgiornosex="";
var chrcontrollo='';


// --------------------------------------------------------------------------
// Controls/Definitions:
// --------------------------------------------------------------------------
DefaultValues();


// --------------------------------------------------------------------------
// Functions:
// --------------------------------------------------------------------------
function DefaultValues()
{
	strCodFis="";
	strcognome="";
	strnome="";
	strgiornosex="";
	chrcontrollo='';

	Cognome = "";
	Nome = "";
	Sesso = 0;
	Comune = "";
	CodiceFiscale = "";
	AnnoCento = 19;
	AnnoDieci = "0";
	AnnoZero = "0";
	Mese = "A";
	Giorno = 1;
 
	return;
}

// --------------------------------------------------------------------------
function CalcolaCodiceFiscale(txtCognome, txtNome, txtGG, txtMM, txtAAAA, txtSesso, txtComune)
{
  var gs=0;
  var i=0;
  var somma=0;

  strCodFis="";
  strcognome="";
  strnome="";
  strgiornosex="";
	chrcontrollo='';
 
	Giorno=parseInt(txtGG, 10);
	AnnoCento=parseInt(txtAAAA.slice(0, 2), 10);
	AnnoDieci=parseInt(txtAAAA.slice(2, 3), 10);
	AnnoZero=parseInt(txtAAAA.slice(3, 4), 10);

	switch (txtMM) 
	{
      	case "01": Mese = "A"; break;
      	case "02": Mese = "B"; break; 
      	case "03": Mese = "C"; break;
      	case "04": Mese = "D"; break;
      	case "05": Mese = "E"; break;
      	case "06": Mese = "H"; break;
      	case "07": Mese = "L"; break;
      	case "08": Mese = "M"; break;
      	case "09": Mese = "P"; break;
      	case "10": Mese = "R"; break;
      	case "11": Mese = "S"; break;
      	case "12": Mese = "T"; break;
	}
	
	if (txtSesso == "M") 
		Sesso=0;
	else
		Sesso=1;
	Comune=txtComune;
/*
	alert("txtCognome: " + txtCognome);
	alert("txtNome: " + txtNome);
	alert("Giorno: " + Giorno);
	alert("Mese: " + Mese);
	alert("AnnoCento: " + AnnoCento);
	alert("AnnoDieci: " + AnnoDieci);
	alert("AnnoZero: " + AnnoZero);
	alert("Sesso: " + Sesso);
	alert("Comune: " + Comune);
*/	
  // Processa il cognome
  //----------------------------------------------------------------
		for (i=0; i<txtCognome.length; i++) 
		{
    	switch (txtCognome.charAt(i)) 
			{
      	case 'A':
        case 'E':
        case 'I':
        case 'O':
        case 'U': break;            
        default : 
        	if((txtCognome.charAt(i)<='Z')&& (txtCognome.charAt(i)>'A'))
						strcognome = strcognome + txtCognome.charAt(i);
			}
		}

    if (strcognome.length < 3) 
		{
    	for (i=0; i<txtCognome.length; i++) 
      {
      	switch (txtCognome.charAt(i)) 
        {
        	case 'A':
          case 'E':
          case 'I':
          case 'O':
          case 'U': strcognome = strcognome + txtCognome.charAt(i);
				}
			}
      
			if (strcognome.length < 3) 
      {
      	for (i=strcognome.length; i<=3; i++) 
        	{ strcognome = strcognome + 'X'; }
			}
		}
    strcognome = strcognome.substring(0,3);
		
 //------------------------------------------------------------ 

  // processa il nome
  //----------------------------------------------------------------
		for (i=0; i<txtNome.length; i++) 
    {
    	switch (txtNome.charAt(i)) 
			{
      	case 'A':
        case 'E':
        case 'I':
        case 'O':
        case 'U': break;
        default:
 				if((txtNome.charAt(i)<='Z')&& (txtNome.charAt(i)>'A'))
					strnome = strnome + txtNome.charAt(i);
			}
		}
    
		if (strnome.length > 3) 
    {
    	strnome = strnome.substring(0,1) + strnome.substring(2,4);
    } 
    else 
		{
			if (strnome.length < 3) 
      {
      	for (i=0; i<txtNome.length; i++) 
        {
        	switch (txtNome.charAt(i)) 
          {
          	case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U': strnome = strnome + txtNome.charAt(i);
					}
				}
          
				if (strnome.length < 3) 
        {
        	for (i=strnome.length; i<=3; i++) 
          	{strnome = strnome + 'X';}
				}
			}
        
			strnome = strnome.substring(0,3);
		}
 //--------------------------------------- Fine processa nome

 // processa giorno e sesso
 //--------------------------------------------
		gs = Giorno + (40 * Sesso);
  	if (gs<10) 
			strgiornosex = "0" + gs;
  	else 
			strgiornosex =  gs;
 //--------------------------------------------

 		strCodFis = strcognome + strnome + AnnoDieci + AnnoZero + Mese + strgiornosex + Comune;
 
 // calcola la cifra di controllo
 //--------------------------------------------
    for (i=0; i<15; i++) 
    {
    	if (((i+1) % 2) != 0) //caratteri dispari
      {
      	switch (strCodFis.charAt(i)) 
        {
        	case '0':
          case 'A':{ somma += 1; break;}
          case '1':
          case 'B':{ somma += 0; break;}
          case '2':
          case 'C':{ somma += 5; break;}
          case '3':
          case 'D':{ somma += 7; break;}
          case '4':
          case 'E':{ somma += 9; break;}
          case '5':
          case 'F':{ somma += 13; break;}
          case '6':
          case 'G':{ somma += 15; break;}
          case '7':
          case 'H':{ somma += 17; break;}
          case '8':
          case 'I':{ somma += 19; break;}
          case '9':
          case 'J':{ somma += 21; break;}
          case 'K':{ somma += 2; break;}
          case 'L':{ somma += 4; break;}
          case 'M':{ somma += 18; break;}
          case 'N':{ somma += 20; break;}
          case 'O':{ somma += 11; break;}
          case 'P':{ somma += 3; break;}
          case 'Q':{ somma += 6; break;}
          case 'R':{ somma += 8; break;}
          case 'S':{ somma += 12; break;}
          case 'T':{ somma += 14; break;}
          case 'U':{ somma += 16; break;}
          case 'V':{ somma += 10; break;}
          case 'W':{ somma += 22; break;}
          case 'X':{ somma += 25; break;}
          case 'Y':{ somma += 24; break;}
          case 'Z':{ somma += 23; break;}
				}
			} 
      else //caratteri pari
      {
      	switch (strCodFis.charAt(i)) 
        {
        	case '0':
          case 'A':{ somma += 0; break;}
          case '1':
          case 'B':{ somma += 1; break;}
          case '2':
          case 'C':{ somma += 2; break;}
          case '3':
          case 'D':{ somma += 3; break;}
          case '4':
          case 'E':{ somma += 4; break;}
          case '5':
          case 'F':{ somma += 5; break;}
          case '6':
          case 'G':{ somma += 6; break;}
          case '7':
          case 'H':{ somma += 7; break;}
          case '8':
          case 'I':{ somma += 8; break;}
          case '9':
          case 'J':{ somma += 9; break;}
          case 'K':{ somma += 10; break;}
          case 'L':{ somma += 11; break;}
          case 'M':{ somma += 12; break;}
          case 'N':{ somma += 13; break;}
          case 'O':{ somma += 14; break;}
          case 'P':{ somma += 15; break;}
          case 'Q':{ somma += 16; break;}
          case 'R':{ somma += 17; break;}
          case 'S':{ somma += 18; break;}
          case 'T':{ somma += 19; break;}
          case 'U':{ somma += 20; break;}
          case 'V':{ somma += 21; break;}
          case 'W':{ somma += 22; break;}
          case 'X':{ somma += 23; break;}
          case 'Y':{ somma += 24; break;}
          case 'Z':{ somma += 25; break;}
				}
			}
    }
		
		somma %= 26;
		switch (somma) 
		{
    	case 0: {chrcontrollo='A'; break;}
      case 1: {chrcontrollo='B'; break;}
      case 2: {chrcontrollo='C'; break;}
      case 3: {chrcontrollo='D'; break;}
      case 4: {chrcontrollo='E'; break;}
      case 5: {chrcontrollo='F'; break;}
      case 6: {chrcontrollo='G'; break;}
      case 7: {chrcontrollo='H'; break;}
      case 8: {chrcontrollo='I'; break;}
      case 9: {chrcontrollo='J'; break;}
      case 10: {chrcontrollo='K'; break;}
      case 11: {chrcontrollo='L'; break;}
      case 12: {chrcontrollo='M'; break;}
      case 13: {chrcontrollo='N'; break;}
      case 14: {chrcontrollo='O'; break;}
      case 15: {chrcontrollo='P'; break;}
      case 16: {chrcontrollo='Q'; break;}
      case 17: {chrcontrollo='R'; break;}
      case 18: {chrcontrollo='S'; break;}
      case 19: {chrcontrollo='T'; break;}
      case 20: {chrcontrollo='U'; break;}
      case 21: {chrcontrollo='V'; break;}
      case 22: {chrcontrollo='W'; break;}
      case 23: {chrcontrollo='X'; break;}
      case 24: {chrcontrollo='Y'; break;}
      case 25: {chrcontrollo='Z'; break;}
		}
 //--------------------------------------------

	return strCodFis + chrcontrollo;
}

function CalcolaMasterDaOmocodico(codicefiscale)
{
	var pos2bechecked = new Array(0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0);
	var i, c

	codicefiscale = codicefiscale.toUpperCase();
	for (i = 0; i < codicefiscale.length; i++)
  {
		/* Se è da controllare  */
		if (pos2bechecked[i] == 1) {
			c = '';
			switch (codicefiscale.charAt(i)) 
      {
      	case 'L': c = '0'; break;
      	case 'M': c = '1'; break;
      	case 'N': c = '2'; break;
      	case 'P': c = '3'; break;
      	case 'Q': c = '4'; break;
      	case 'R': c = '5'; break;
      	case 'S': c = '6'; break;
      	case 'T': c = '7'; break;
      	case 'U': c = '8'; break;
      	case 'V': c = '9'; break;
			}
			if (c != '') codicefiscale = codicefiscale.slice(0, i) + c + codicefiscale.slice(i + 1, codicefiscale.length)
		}
	}

	return codicefiscale;
}

function testChecksum(txtCF)
{
	var counter;
  var i;
  var base;

  counter = 0;
  txtCF = txtCF.toUpperCase();
  for (i = 0; i < 15; i++) {
  	counter += getPositionNumber(txtCF.substring(i, i + 1), 1 - (i % 2));
	}
  base = "A";
	
	return (txtCF.substring(15, 16) == String.fromCharCode((counter % 26) + base.charCodeAt(0)));
}

function partitaIVA(sz_Codice){

   var n_Val,n_Som1=0,n_Som2=0,lcv;

   if (sz_Codice.length!=11 || isNaN(parseFloat(sz_Codice)) || parseFloat(sz_Codice)<parseFloat(0))
      return false;

   for (lcv=0;lcv<9;lcv+=2){
      n_Val=parseInt(sz_Codice.charAt(lcv));
      n_Som1+=n_Val;
      n_Val=parseInt(sz_Codice.charAt(lcv+1));
      n_Som1+=Math.floor(n_Val/5) + (n_Val<<1) % 10;
   }

   n_Som2 = 10 - (n_Som1 % 10);
   n_Val=parseInt(sz_Codice.charAt(10));

	 if (n_Som2==10) n_Som2 = 0;
   if (n_Som2==n_Val)
      return true;

   return false;
}


// --------------------------------------------------------------------------
//  END OF SCRIPT
// --------------------------------------------------------------------------

