function controllo_campi_cf(id_cognome, id_nome, id_sesso, id_data_nascita, id_istat)
{
	if (campo_vuoto(id_cognome)) {
		alert("Attenzione ! Per il calcolo del codice fiscale e' necessario inserire il cognome !");
		return false;
	} else if (campo_vuoto(id_nome)) {
		alert("Attenzione ! Per il calcolo del codice fiscale e' necessario inserire il nome !");
		return false;
	} else if (campo_vuoto(id_sesso)) {
		alert("Attenzione ! Per il calcolo del codice fiscale e' necessario inserire il sesso !");
		return false;
	} else if (campo_vuoto(id_data_nascita)) {
		alert("Attenzione ! Per il calcolo del codice fiscale e' necessario inserire la data di nascita !");
		return false;
	} else if (campo_vuoto(i_comune_nascita_id)&& campo_vuoto(i_nazione_nascita_id) ) {
		alert("Attenzione ! Per il calcolo del codice fiscale e' necessario inserire il comune di nascita o la nazione se straniero !");
		return false;
	} else {
		return calcola_codice_fiscale(
			 document.getElementById(id_cognome).value
			,document.getElementById(id_nome).value
			,document.getElementById(id_sesso).value
			,document.getElementById(id_data_nascita).value
			,document.getElementById(id_istat).value
			);
	}
}

function campo_vuoto(campo) {

	if (document.getElementById(campo)==false || document.getElementById(campo)==null ) { 
		return false;
	} else {
		return true;
	}
}


function calcola_codice_fiscale(cognome, nome, sesso, data_nascita, comune_nascita)
{
	var l_voc;
	var l_con;
	var l_ind;
	var l_chr;
	var l_cod_fis = "";
	var l_app = "";
	var is_vocale = new RegExp("[AEIOU]");
	var is_cons   = new RegExp("[BCDFGHJKLMNPQRSTVWXYZ]");


// COGNOME
 
	l_ind = 0;
	
	cognome = cognome.toUpperCase();

	for(l_ind = 0, l_voc = "", l_con = ""; l_ind < cognome.length && l_con.length < 3; l_ind ++) {
		
		l_chr = cognome.charAt(l_ind);
		
		if (is_vocale.test(l_chr)) {
			l_voc += l_chr;
		} else if (is_cons.test(l_chr)) {
			l_con += l_chr;
		}
	}
	if (l_con.length < 3)	{
		l_con += l_voc.substr(0, 3 - l_con.length);
	}
	
	while (l_con.length < 3){
		l_con += 'X';
	}

	l_cod_fis = l_con;

// NOME
	nome=nome.toUpperCase();

	for(l_ind= 0, l_voc = "", l_con = "" ; l_ind <= nome.length; l_ind ++) {
		l_chr = nome.charAt(l_ind);

		if (is_vocale.test(l_chr)) {
			l_voc += l_chr;
		} else if (is_cons.test(l_chr)) {
			l_con += l_chr;
		}
	}

	if (l_con.length >= 4) {
		l_con = l_con.substr(0,1) + l_con.substr(2,2);
	} else if (l_con.length != 3) {
		l_app = l_con + l_voc;
		l_con = l_app.substr(0,3);
	}
	
	while (l_con.length < 3){
		l_con += 'X';
	}

	l_cod_fis += l_con;

// ANNO DI data_nascita(78)

	l_cod_fis += data_nascita.substr(data_nascita.length - 2);

// MESE DI data_nascita(9)
/*	var meseStr = data_nascita.substr(3,2);
	// questo ciokko è fatto perchè il JavaScript ha un baco sul parseInt! By Keller
	if (meseStr == '08'){
		var mese = 7;
	}
	else if (meseStr == '09'){
		var mese = 8;
		}
		else var mese = parseInt(meseStr) - 1;
	var codice_mese = ["A", "B", "C", "D", "E", "H", "L", "M", "P", "R", "S", "T"];
	l_cod_fis += codice_mese[mese];
	*/

	var mese = data_nascita.substr(3,2);
	var lettera_check = new Array("A","B","C","D","E","H","L","M","P","R","S","T");
	l_cod_fis += lettera_check[mese - 1];

// GIORNO DI data_nascita(0A)
	
	/*
	if (sesso=='M')	{
		l_cod_fis += data_nascita.substr(0,2);
	} else {
		var num = parseInt(data_nascita.substr(0,2));
		l_cod_fis += (num + 40);
	}
	*/
	//nuovo con modifiche di Emi-Stefano
	if (sesso == 'M' || sesso == 'M ') {
		l_cod_fis += data_nascita.substr(0,2);
	} else {
		if(data_nascita.substr(0,1)==0){
			var num = parseInt(data_nascita.substr(1,2));
		}else{
			var num = parseInt(data_nascita.substr(0,2));
		}
		num += 40;
		l_cod_fis += num;
	}
	
	
	
	if(comune_nascita!='') {
		//LOCALITA DI comune_nascita(BCDE)
		
		l_cod_fis += comune_nascita;
	
		//ULTIMA LETTERA(F)
		var l_char_index=0;
		var l_tmp_index=0;
		var l_index=1;
	
		do	
		{
			l_chr = l_cod_fis.substr(l_index-1, 1);
			l_char_index = "B1A0KKPPLLC2QQD3RRE4VVOOSSF5TTG6UUH7MMI8NNJ9WWZZYYXX".indexOf(l_chr);
			l_tmp_index +=  ((l_char_index) & 0x7FFE) / 2;
			l_index ++;
	
			if (l_index < 16) {
				l_chr = l_cod_fis.substr(l_index-1, 1);
				l_char_index="A0B1C2D3E4F5G6H7I8J9KKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ".indexOf(l_chr);
				l_tmp_index += ((l_char_index) & 0x7FFE) / 2;
				l_index ++;
			}
		}
		while (l_index <= 15);
	
		l_tmp_index = l_tmp_index % 26;
	
	
		l_cod_fis += "ABCDEFGHIJKLMNOPQRSTUVWXYZ".substr(l_tmp_index, 1);
	}

	
	return l_cod_fis;
	
}

	function calcola_cf()
	{
		
		var istat = "";
		
		
		if(document.getElementById('i_stato_nascita_id').value == 192) { 

			if(!campo_vuoto('t_cognome') || !campo_vuoto('t_nome') || !campo_vuoto('t_sesso') || !campo_vuoto('d_data_nascita') || !campo_vuoto('t_istat_cf')) {
				alert("Errore nel calcolo del Codice Fiscale! \nassicurarsi di aver compilato correttamente i campi:\n- cognome \n- nome \n- data di nascita \n- sesso \n- comune di nascita");	
				return false;
			}
			
			istat = document.getElementById('t_istat_cf').value;
		
		} else {
			if(document.getElementById('t_istat_cf')) {
				document.getElementById('t_istat_cf').value = "";
			}
			
			if(!campo_vuoto('t_cognome') || !campo_vuoto('t_nome') || !campo_vuoto('t_sesso') || !campo_vuoto('d_data_nascita')) {
				alert("Errore nel calcolo del Codice Fiscale! \nassicurarsi di aver compilato correttamente i campi:\n- cognome \n- nome \n- data di nascita \n- sesso ");	
				return false;
			}
		}
		
		

		
		var cf = calcola_codice_fiscale(
			 document.getElementById('t_cognome').value
			,document.getElementById('t_nome').value
			,document.getElementById('t_sesso').value
			,document.getElementById('d_data_nascita').value
			,istat
			);
			
		if(cf) { 
			document.getElementById('t_codice_fiscale').value = cf;
		} else { 
			
			if(document.getElementById('i_stato_nascita_id').value == 192) { 
	
				alert("Errore nel calcolo del Codice Fiscale! \nassicurarsi di aver compilato correttamente i campi:\n- cognome \n- nome \n- data di nascita \n- sesso \n- comune di nascita");	
			
			} else {
				alert("Errore nel calcolo del Codice Fiscale! \nassicurarsi di aver compilato correttamente i campi:\n- cognome \n- nome \n- data di nascita \n- sesso \n- comune di nascita");	
			}
		
			return false;
			
		}
		
		
		
		
		
	}  	