/*===== verifica la partita iva ====*/
function checkPIVA(val) {
	var myCode = new ChkFiscalCode_it(val, 'PI');
	return(myCode.cfc_check());
}


/*==== verifica il codice fiscale ====*/
function checkCODFIS(val) {
	var myCode = new ChkFiscalCode_it(val, 'CF');
	return(myCode.cfc_check());
}

/*==== verifica un indirizzo email ====*/
function checkEMAIL(val) {
	var email=/^[A-Za-z0-9][\w-.]+[A-Za-z0-9]@[A-Za-z0-9]([\w-.]+[A-Za-z0-9]\.)+([A-Za-z]){2,4}$/i;
	return(email.test(val));
}

/*==== verifica un url =====*/
function checkWWW(val) {
	var www=/^[A-Za-z0-9]([\w-.]+[A-Za-z0-9]\.)+([A-Za-z]){2,4}$/i;
	return(www.test(val));
}

/*==== verifica un numero telefonico (solo numeri, almeno 8)=====*/
function checkTEL(val) {
	var rx = /[^0-9]/;
	var ok = false;
	
	//solo numeri
	ok = !(rx.test(val));
	if(!ok) return false;

	//lunghezza maggiore di 9
	var s = new String(val);
	if(s.length<9) return false;
	
	return true;
}




/*******************************************************/
/* classe per verifica codice fiscale o partita iva */
/****************************************************/
function ChkFiscalCode_it (p_code, p_type){
	this.code = p_code;
	this.type = p_type;
	this.error = '';
	this.number_number = 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);
	
	this.setError = setError;
	this.cfc_check = cfc_check;
	this.cfc_check_PI = cfc_check_PI;
	this.cfc_check_CF = cfc_check_CF;
}

function setError(err) {
	this.error = err;
}
	
function cfc_check() {
	if(this.type == 'PI') return this.cfc_check_PI();
	else if(this.type == 'CF') return this.cfc_check_CF();
}

function cfc_check_PI() {
		
	var code = new String(this.code);
	var odds = 0;
	var evens = 0;
	var ch = 0;
	var chk = 0;

	// ------------------------------------------------------------
	// Verifica la lughezza del codice (11 cifre)
	// ------------------------------------------------------------
	if (code.length != 11) {
		this.setError("Partita iva " + code + " errata: deve essere composta da 11 cifre.");
		return false;
	}
	
	// ------------------------------------------------------------
	// Verifica che il codice sia composto solamente da numeri
	// ------------------------------------------------------------
	var rx = /[0-9]{11}/;
	var ok = false;

	ok = rx.test(code);
	if(!ok) {
		this.setError("partita iva " + code + " errata: deve contenere solo numeri");
		return false;
	} 
	
	// ------------------------------------------------------------
	// Calculate the sum of odd digits
	// ------------------------------------------------------------
	for (i=1; i<11; i=i+2) {
		// Sum the digit as is
		// Example: Digit=5 ... sum to odds
		odds += parseInt(code.charAt(i-1));
	}

	// ------------------------------------------------------------
	// Calculate the sum of even digits
	// ------------------------------------------------------------
	for(i=2; i<11; i=i+2) {
		var doub = 0;
		// Double the digit
		// Example: Digit=8 ... 8*2=16
		doub = parseInt(code.charAt(i-1)) * 2;
		// Sum both digits of the previous double
		// Example: Number=16 ... 1+6=7
		if(doub>=10) {
			doub = 1 + (doub-10); //doub al massimo vale 18 -> 1 + (18-10) = 9
		} 
		// Add the result to the sum
		// Example: Digit=7 ... sum to 
		evens += doub;
	}

	// ------------------------------------------------------------
	// Check the check digit (digit 11)
	// ------------------------------------------------------------
	// Total of odds sum and evens sum
	ch = odds + evens;
	// Substract units of the total from 10, the result is the check digit
	// Example: 23+45=68 ... 10-8=2 ... check digit is 2
	var sch = new String(ch);
	chk = parseInt(sch.charAt(sch.length-1));
	chk = 10 - chk;
	// Check
	if(parseInt(code.charAt(11-1)) != chk) {
		this.setError("La partita iva " + code + " non è corretta");
		return false;
	}
	
	// ------------------------------------------------------------
	// The code is correct
	// ------------------------------------------------------------
	return true;

}



function cfc_check_CF() {

	var code = new String(this.code);
	var rx = /[A-Za-z0-9]{16}/;
	var re = /[^0-9]/;
	var ok = false;
	var odds = 0;
	var evens = 0;
	var sum = 0;
	
	
	code = code.toUpperCase();

	// ------------------------------------------------------------
	// Check the length of the code (16 characters)
	// ------------------------------------------------------------
	if (code.length != 16) {
		this.setError("Codice fiscale " + code + " errato: deve essere composto da 16 caratteri");
		return false;
	}

	// ------------------------------------------------------------
	// Check the content of the code, only numbers and letters are valid
	// ------------------------------------------------------------
	ok = rx.test(code);
	if(!ok) {
		this.setError("Codice Fiscale " + code + " errato: deve contenere solo numeri e lettere");
		return false;
	} 

	// ------------------------------------------------------------
	// Calculate the sum of odd characters
	// ------------------------------------------------------------
	for(i=1; i<16; i=i+2) {
		var c = code.charAt(i-1)
	
		if(! re.test(c) ) { //numeri
			// Sum a number
			// Example: Character=4 ... number_number ... sum 9 (number_number[4]=9)
			odds += this.number_number[parseInt(c)];
		} else { // caratteri
			// Sum a letter
			// Example: Character=A ... letter_number ... sum 1 (A=1)
			toAdd = 0;
			switch(c) {
				case 'A':
					toAdd = 1;
					break;
				case 'B':
					toAdd = 0;
					break;
				case 'C':
					toAdd = 5;
					break;
				case 'D':
					toAdd = 7;
					break;
				case 'E':
					toAdd = 9;
					break;
				case 'F':
					toAdd = 13;
					break;
				case 'G':  
					toAdd = 15;
					break;
				case 'H':
					toAdd = 17;
					break;
				case 'I':
					toAdd = 19;
					break;
				case 'J':
					toAdd = 21;
					break;
				case 'K':
					toAdd = 2;
					break;
				case 'L':
					toAdd = 4;
					break;
				case 'M':
					toAdd = 18;
					break;
				case 'N':
					toAdd = 20;
					break;
				case 'O':
					toAdd = 11;
					break;
				case 'P':
					toAdd = 3;
					break;
				case 'Q':
					toAdd = 6;
					break;
				case 'R':
					toAdd = 8;
					break;
				case 'S':
					toAdd = 12;
					break;
				case 'T':
					toAdd = 14;
					break;
				case 'U':
					toAdd = 16;
					break;
				case 'V':
					toAdd = 10;
					break;
				case 'W':
					toAdd = 22;
					break;
				case 'X':
					toAdd = 25;
					break;
				case 'Y':
					toAdd = 24;
					break;
				case 'Z':
					toAdd = 23;
					break;
			}
			odds += toAdd;
		} // end if(! re.test(c) )
	
	} // end for(i=1; i<16; i=i+2)


	// ------------------------------------------------------------
	// Calculate the sum of even characters
	// ------------------------------------------------------------
	for(i=2; i<16; i=i+2) {
		var c = code.charAt(i-1);

		if(! re.test(c) ) { //numeri
			// Sum a number
			// Example: Character=4 ... sum 4
			evens += parseInt(c);

		} else { //caratteri
			// Sum a letter
			// Example: Character=C ... alphabet ... sum 2 (from 0 to 25)
			evens += (c.charCodeAt(0) - "A".charCodeAt(0));
		}
	} // end for(i=2; i<16; i=i+2)

	// ------------------------------------------------------------
	// Check the check digit (character 16)
	// ------------------------------------------------------------
	// Total of odds sum and evens sum
	sum = odds + evens;
	// Calculate the remainder of the total divided by 26
	// Example: totals 42 and 129
	//   42+129=171 ... 171/26=6,57 ... 6*26=156 ... 171-156=15
	var p = 0;
	p = Math.floor(sum/26);
	p = p * 26;
	p = sum - p;

	// Calculate the check digit (a letter)
	// Example: 3 -> D (0 to 25)
	var alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var chk = alpha.charAt(p);

	// Check
	if (code.charAt(16-1) != chk) {
		this.setError("Il codice fiscale " + code + " non è corretto");
		return false
	}

	// ------------------------------------------------------------
	// The code is correct
	// ------------------------------------------------------------
	return true;
}

/********** fine classe ****************/
