
var strCodFis="";
var strNome="";
var strCognome="";
var chrControllo='';
var strGiornoSesso="";
var strAnno="";
var strMese="";


function clearField(){
	document.formDatiAnagrafici.txtnome.value="";
	document.formDatiAnagrafici.txtcognome.value="";
	document.formDatiAnagrafici.txtday.selectedIndex=0; 
	document.formDatiAnagrafici.txtmonth.selectedIndex=0; 
	document.formDatiAnagrafici.txtyear.selectedIndex=0;
	document.formDatiAnagrafici.txtfindcomune.value=""; 	
	document.formDatiAnagrafici.comuneName.value=""; 	
	document.formDatiAnagrafici.comuneCode.value=""; 	
	document.formDatiAnagrafici.txtcodicefiscale.value = "????????????????";
	return;
}

function copyToClipboard(){
	clipboardData.setData('Text',document.formDatiAnagrafici.txtcodicefiscale.value);
	return;
}

function setComune(comune){
alert(comune);
}

function findComune(language){
	_nomeComune=document.formDatiAnagrafici.txtfindcomune.value.toUpperCase();
	if (_nomeComune.length<2){
		if (language=="it") {
			alert("Inserire almeno 2 caratteri!\n\nVerranno visualizzati tutti\ni comuni o stati esteri\ncontenenti i caratteri immessi.");
			return;
		}
		if (language=="en") {
			Alert("Insert al least 2 characters!\n\nWill be showed all\nthe cities or outside Italy countries\ncontaining the characters.");
			return;
		}
	}
	
	destinationUrl="find_comune.asp?name="+_nomeComune;
	//alert(destinationUrl);
	//destinationUrl=destinationUrl.replace("/en/","/");
	//destinationUrl=destinationUrl.replace("\\en\\","\\");
	//alert(destinationUrl);
	
   var w = 420;
   var h = 600;
   var l = Math.floor((screen.width-w)/2);
   var t = Math.floor((screen.height-h)/2);
   
   var myWin=window.open(destinationUrl,"","modal=yes,width=" + w + ",height=" + h + ",top=" + t + ",left=" + l);
   if (!myWin) {
	if (language=="it") {
		alert("Attenzione:\n\nPer Visualizzare i Comuni e' necessario abilitare i POPUP");
		return;
	}
	else {
		Alert("Attention:\n\nTo Look the countries you need to turn on the POPUP");
		return;
	}
   }
	
	
	//window.open(destinationUrl,'','width=500,height=550,location=no,status=no');

	return;
}

function getNameCode(nome){
    for (i=0; i<nome.length; i++) 
       {
        switch (nome.charAt(i)) 
              {
               case 'A':
               case 'E':
               case 'I':
               case 'O':
               case 'U': break;
               default:
		    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++) 
                {
                  switch (nome.charAt(i)) 
                        {
                         case 'A':
                         case 'E':
                         case 'I':
                         case 'O':
                         case '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);
         }
         
   return strNome;
}


function getSurnameCode(cognome){
    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'; }
         }
      }
   strCognome = strCognome.substring(0,3);
   
   return strCognome;	
}

function getDaySexCode(giorno,sesso){

  intGiornoSesso = parseInt(giorno) + parseInt((40 * sesso)) ;
  if(intGiornoSesso<10) strGiornoSesso = "0" + intGiornoSesso;
  else strGiornoSesso =  intGiornoSesso;
  
  return strGiornoSesso;
}

function getControlChar(codiceFiscale){
    var somma=0;
    for (i=0; i<15; i++) 
       {
        if (((i+1) % 2) != 0)
          {
           switch (codiceFiscale.charAt(i)) 
                 {
                  case '0':
                  case 'A':{ somma = somma + 1; break;}
                  case '1':
                  case 'B':{ break;}
                  case '2':
                  case 'C':{ somma = somma +  5; break;}
                  case '3':
                  case 'D':{ somma = somma +  7; break;}
                  case '4':
                  case 'E':{ somma = somma +  9; break;}
                  case '5':
                  case 'F':{ somma = somma +  13; break;}
                  case '6':
                  case 'G':{ somma = somma +  15; break;}
                  case '7':
                  case 'H':{ somma = somma +  17; break;}
                  case '8':
                  case 'I':{ somma = somma +  19; break;}
                  case '9':
                  case 'J':{ somma = somma +  21; break;}
                  case 'K':{ somma = somma +  2; break;}
                  case 'L':{ somma = somma +  4; break;}
                  case 'M':{ somma = somma +  18; break;}
                  case 'N':{ somma = somma +  20; break;}
                  case 'O':{ somma = somma +  11; break;}
                  case 'P':{ somma = somma +  3; break;}
                  case 'Q':{ somma = somma +  6; break;}
                  case 'R':{ somma = somma +  8; break;}
                  case 'S':{ somma = somma +  12; break;}
                  case 'T':{ somma = somma +  14; break;}
                  case 'U':{ somma = somma +  16; break;}
                  case 'V':{ somma = somma +  10; break;}
                  case 'W':{ somma = somma +  22; break;}
                  case 'X':{ somma = somma +  25; break;}
                  case 'Y':{ somma = somma +  24; break;}
                  case 'Z':{ somma = somma +  23; break;}
                 }
          } 
        else 
            {
            	if (codiceFiscale.charAt(i)>='0' && codiceFiscale.charAt(i)<='9'){
            		somma=somma+parseInt(codiceFiscale.charAt(i));
        	}
        	if (codiceFiscale.charAt(i)>='A' && codiceFiscale.charAt(i)<='Z'){
        		somma=somma+parseInt(codiceFiscale.charAt(i).charCodeAt(0)-65);
        	}
       
            }
    }
   somma %= 26;
   if (somma>=0) {
   	chrControllo=String.fromCharCode(65+somma);
   } else {
   	chrControllo='?'
   }
         
   return chrControllo;         	
}

// --------------------------------------------------------------------------
function getFiscalCode()
{
 var intGiornoSesso=0;
 var i=0;
 

 strCodFis="";
 strCognome="";
 strNome="";
 strGiornoSesso="";
 chrControllo='';
 
_nome=document.formDatiAnagrafici.txtnome.value.toUpperCase();
_cognome=document.formDatiAnagrafici.txtcognome.value.toUpperCase();
_giorno=document.formDatiAnagrafici.txtday.options[document.formDatiAnagrafici.txtday.selectedIndex].value; 
_mese=document.formDatiAnagrafici.txtmonth.options[document.formDatiAnagrafici.txtmonth.selectedIndex].value;
_anno=document.formDatiAnagrafici.txtyear.options[document.formDatiAnagrafici.txtyear.selectedIndex].value; 
_comune=document.formDatiAnagrafici.comuneCode.value; 
   
   if (document.formDatiAnagrafici.txtsex[0].checked){
   	_sesso=0
   } else {
   	_sesso=1
   }

   
   if (_cognome==""){
   	strCognome='???';
   } else {
   	strCognome=getSurnameCode(_cognome);
   }
   
   if (_nome==""){
   	strNome='???';
   } else {
   	strNome=getNameCode(_nome);
   }   
   
   if (_giorno==Number.NaN || _giorno==""){
   	strGiornoSesso='??';
   } else {
   	strGiornoSesso=getDaySexCode(_giorno,_sesso);
   }
   
   if (_anno==Number.NaN || _anno==""){
   	strAnno='??';
   } else {
   	strAnno=_anno.substring(2,4);
   }
   
   if (_mese==Number.NaN || _mese==""){
   	strMese='?';
   } else {
   	strMese=_mese;
   }

	
   if (_comune==""){
   	strComune='????';
   } else {
   	strComune=_comune;
   }
//   alert("strCognome: "+strCognome+"\n"+"strNome: "+strNome+"\n"+"strAnno: "+strAnno+"\n"+"strMese: "+strMese+"\n"+"strGiornoSesso: "+strGiornoSesso+"\n"+"strComune: "+strComune);
   
   strCodFis = strCognome + strNome + strAnno+ strMese + strGiornoSesso + strComune;
   chrControllo=getControlChar(strCodFis);

document.formDatiAnagrafici.txtcodicefiscale.value = strCodFis + chrControllo;

 
 return; 
}

