// JavaScript Document
 function controllaPivaCfisc(str){
	

	if (!isNaN(str))
	{
		if (str.length==11)
		{
			return controllaPiva( str );
		}
	}
	return controllaCfisc(str);
}


function controllaPiva( CFisc )
{
	var numero=0;
	var somma=0;
	var resto=0;
	var check=0;
	var Rc = 1;
	var NumericDigits = "0123456789";
	var IsGood = true;

	for (i=0; i < CFisc.length; i++)
	{
	  check = NumericDigits.indexOf( CFisc.charAt(i) );
	  if ( check == -1 )
	  {
		IsGood = false;
		i = CFisc.length + 1;
	  }
	}

    if ( IsGood )
	{
	   for (var i=1; i <=9 ; i+=2)
	   {
		 numero = parseInt( CFisc.charAt(i), "10");
		 numero = numero * 2;
		 resto = (numero % 10);
		 somma = somma +  ((numero-resto)/10) + resto;
	   }

	   for (var i=0; i <=8 ; i+=2)
	   {
		 numero = parseInt( CFisc.charAt(i), "10");
		 somma = somma + numero;
	   }

	   check = 10 - (somma % 10);
	   check = (check % 10)

	   if ( check == parseInt( CFisc.charAt(10), "10" ) )
	     Rc = 0;
	   else
	     Rc = 1;
    }

	if ( Rc==0 ) return true;
	else
		return false;
}


function controllaCfisc(str){
	str=str.toUpperCase();
    var rc=false;
	var index=0;
	var somma=0;
	var resto=0;
	
  	Tab= new Array(1,0,5,7,9,13,15,17,19,21,2,4,18,20,11,3,6,8,12,14,16,10,22,25,24,23);
	if (str.length > 0 && str.length !=16)
	{
		rc = false;
	}
	else
	{
		if (!isEmpty(str))
		{
			for (var intLoop = 1; intLoop <= 13; intLoop=intLoop + 2)
			{
				if (str.charAt(intLoop) >= 'A' && str.charAt(intLoop) <= 'Z')
					index=(str.charCodeAt(intLoop) - 65);
				if (str.charAt(intLoop) >= '0' && str.charAt(intLoop) <= '9')
					index=(str.charCodeAt(intLoop) - 48);
				somma=somma + index;
			}
			for (var intLoop = 0; intLoop <= 14; intLoop+=2)
		 	{
				if (str.charAt(intLoop) >= 'A' && str.charAt(intLoop) <= 'Z')
					index=(str.charCodeAt(intLoop) - 65);
				if (str.charAt(intLoop) >= '0' && str.charAt(intLoop) <= '9')
					index=(str.charCodeAt(intLoop) - 48);
				somma=somma + Tab[index];
		 	}
			resto=(somma % 26);
		   	if ( str.charCodeAt(15) == resto +65 )
			  rc = true;
		}
	}

	return( rc );
	

}