// JavaScript Document
// script per la validita sintattica del codice fiscale

function isVowel(ch)
{
	var vowels="AEIOU";
	return !(vowels.indexOf(ch) == -1);
}

function isConsonant(ch)
{
	var consonants="BCDFGHJKLMNPQRSTVWXYZ";
	return !(consonants.indexOf(ch) == -1 );
}

function getVowels(stringa)
{
	var ret="", i, n;
	n = stringa.length
	for(i = 0; i< n; i++)
		if (isVowel(stringa.charAt(i))) ret += stringa.charAt(i);
	return ret;
}

function getConsonants(stringa)
{
	var ret="", i, n;
	n = stringa.length
	for(i = 0; i< n; i++)
		if (isConsonant(stringa.charAt(i))) ret += stringa.charAt(i);
	return ret;
}

function isValidName(name, chk)
{
	var tmp, vowels, consonants, n, i;
	vowels = getVowels(name);
	consonants = getConsonants(name);
	n = consonants.length;	
	
	if  (n > 3) 
		tmp = consonants.charAt(0) + consonants.charAt(2) + consonants.charAt(3);	
	else 
		tmp = consonants.charAt(0) + consonants.charAt(1) + consonants.charAt(2);	
	n = 3 - tmp.length;
	for(i = 1 ; i <= n; i++)
		tmp += vowels.charAt(i-1);
	n = 3 - tmp.length;
	if (n==1) 
		tmp += "XX";
	if (n==1) 
		tmp += "X";
	return (chk == tmp);	
}

function isValidSurname(surname, chk)
{
	var tmp, vowels, consonants, n, i;
	vowels = getVowels(surname);
	consonants = getConsonants(surname);
	
	tmp = consonants.charAt(0) + consonants.charAt(1) + consonants.charAt(2);	
	n = 3 - tmp.length;
	for(i = 1 ; i <= n; i++)
		tmp += vowels.charAt(i-1);
	n = 3 - tmp.length;
	if (n==1) 
		tmp += "XX";
	if (n==1) 
		tmp += "X";
	return (chk == tmp);
}

function isValidDate(date, chk, sex)
{
	var tmp, gg, mm, aa , month_code =  "ABCDEHLMPRST";
	cfReg = /(\d{1,2})\/(\d{1,2})\/(\d{4})/;
	cfReg.test(date);
	gg = RegExp.$1;
	mm = RegExp.$2;
	aa = RegExp.$3;
	if (!sex) 
		gg = parseInt(gg, 10) + 40; // se è di sesso femminile aggiungi 40
	tmp = (aa % 100) + month_code.charAt(mm-1) + gg;
	return (chk == tmp);
}

function isValidControlCode(cf)
{
	var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
	var s = 0;
	for( i = 1; i <= 13; i += 2 )
		s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	for( i = 0; i <= 14; i += 2 )
		s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	if ( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
		return false;
	return true;
}

function isValidFisCode(cfins, surname, name, date, sex)
{
	var cf = cfins.toUpperCase();	
	var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
	if (!cfReg.test(cf))
		return false;
	if (!isValidSurname(surname.toUpperCase(), cf.substr(0,3))) return false;
	if (!isValidName(name.toUpperCase(), cf.substr(3,3))) return false;
	if (!isValidDate(date.toUpperCase(), cf.substr(6,5), sex)) return false;
	//	if (!valida_codice_comune(comune, cf.substr(10,4))) return false;
	if (!isValidControlCode(cf)) return false;
	return true;
}

function isValidFisCode2(cfins)
{
	var cf = cfins.toUpperCase();	
	var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
	if (!cfReg.test(cf))
		return false;
	return true;
}