/***************************************
TRIM
****************************************/
function trim(stringa){
    while (stringa.substring(0,1) == ' '){
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' '){
        stringa = stringa.substring(0,stringa.length-1);
    }
    return stringa;
}



/**************************************
	Controllo del Codice Fiscale
	Linguaggio: JavaScript
***************************************/

function ControllaCF(cf)
{
	var validi, i, s, set1, set2, setpari, setdisp;
	if( cf == '' )  return '';
	cf = cf.toUpperCase();
	if( cf.length != 16 )
		return false;
	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for( i = 0; i < 16; i++ ){
		if( validi.indexOf( cf.charAt(i) ) == -1 )
			return false;
	}
	set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
	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;
}


/*****************************************
	Controllo della Partita I.V.A.
	Linguaggio: JavaScript
******************************************/

function ControllaPIVA(pi)
{
	if( pi == '' )  return '';
	if( pi.length != 11 )
		return false;
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( pi.charAt(i) ) == -1 )
			return false;
	}
	s = 0;
	for( i = 0; i <= 9; i += 2 )
		s += pi.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ){
		c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
		if( c > 9 )  c = c - 9;
		s += c;
	}
	if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
		return false;

	return true;
}

function checkFormRegister(form)
{
	if (trim(form.username.value)=='')
	{
		alert('ATTENZIONE!!! Il campo username è obbligatorio.');
		form.username.focus();
		return;
	}	
	if (trim(form.password.value)=='')
	{
		alert('ATTENZIONE!!! Il campo password è obbligatorio.');
		form.password.focus();
		return;
	}	
	if (trim(form.password1.value)=='')
	{
		alert('ATTENZIONE!!! Inserire nuovamente la password.');
		form.password1.focus();
		return;
	}
	//check password
	if(trim(form.password1.value)!='' && trim(form.password.value)!='')
	{
		if(form.password1.value!=form.password.value)
			{
					alert('ATTENZIONE!!! Le password inserite non corrispondono.');
					form.password1.focus();
					return;
			} 
	}
	
	if (trim(form.nome.value)=='')
		{
			alert('ATTENZIONE!!! Il campo nome è obbligatorio.');
			form.nome.focus();
			return;
		}
	if (trim(form.cognome.value)=='')
		{
			alert('ATTENZIONE!!! Il campo cognome è obbligatorio.');
			form.cognome.focus();
			return;
		}
	
	if (trim(form.data_nascita_giorno.value)=='' || trim(form.data_nascita_mese.value)=='' || trim(form.data_nascita_anno.value)=='')
		{
			alert('ATTENZIONE!!! Il campo data di nascita è obbligatorio.');
			form.data_nascita_giorno.focus();
			return;
		}
	else
		{	if (isNaN(form.data_nascita_giorno.value) || isNaN(form.data_nascita_mese.value) || isNaN(form.data_nascita_anno.value))
				{
					alert('ATTENZIONE!!! Inserire una data di nascita valida.');
					form.data_nascita_giorno.focus();
					return;
				}
			if(form.data_nascita_giorno.value.length!=2 || parseInt(form.data_nascita_giorno.value)>31) 
				{	alert(form.data_nascita_giorno.value);
					alert('ATTENZIONE!!! Inserire un giorno di nascita valido (Formato GG).');
					form.data_nascita_giorno.focus();
					return;
				}
			if(form.data_nascita_mese.value.length!=2 || parseInt(form.data_nascita_mese.value)>12) 
				{
					alert('ATTENZIONE!!! Inserire un mese di nascita valido (Formato MM).');
					form.data_nascita_giorno.focus();
					return;
				}
			if(form.data_nascita_anno.value.length!=4) 
				{
					alert('ATTENZIONE!!! Inserire un anno di nascita valido (Formato AAAA).');
					form.data_nascita_giorno.focus();
					return;
				}
		}
	if (trim(form.citta.value)=='')
		{
			alert('ATTENZIONE!!! Il campo città è obbligatorio.');
			form.citta.focus();
			return;
		}

	if (trim(form.via.value)=='')
		{
			alert('ATTENZIONE!!! Il campo via è obbligatorio.');
			form.via.focus();
			return;
		}	
	if (trim(form.numero.value)=='')
		{
			alert('ATTENZIONE!!! Il campo numero è obbligatorio.');
			form.numero.focus();
			return;
		}	
	if (trim(form.cap.value)=='')
		{
			alert('ATTENZIONE!!! Il campo cap è obbligatorio.');
			form.cap.focus();
			return;
		}
	var cap = form.cap.value;
		if(cap!='' && isNaN(cap)) 
			{
				alert('ATTENZIONE!!! Inserire un numero di cap valido.');
				form.fax.focus();
				return;
			}
	if (trim(form.email.value)=='')
		{
			alert('ATTENZIONE!!! Il campo email è obbligatorio.');
			form.email.focus();
			return;
		}
	if (trim(form.email1.value)=='')
		{
			alert('ATTENZIONE!!! Inserire nuovamente la mail.');
			form.email1.focus();
			return;
		}
	if (trim(form.email1.value)!='' && trim(form.email.value)!='')
		{
			if(form.email1.value!=form.email.value)
			{
				alert('ATTENZIONE!!! Gli indirizzi email inseriti non corrispondono.');
				form.email1.focus();
				return;
			}
		}
	if (form.email.value.indexOf('@')==-1)
		{
			alert('ATTENZIONE!!! Inserire un indirizzo e-mail valido.');
			form.email.focus();
			return;
		}

	var telefono = document.getElementById('telefono');
			caratteri_ammessi = Array("\\","/","-","."," ","#","+");
			telefono_v = telefono.value;
		if(telefono_v!='')
		{	
			
			for(i=0;i<caratteri_ammessi.length;i++)
			{
				curr = telefono_v;
				telefono_v = curr.replace(caratteri_ammessi[i],'');
			}	
			
			if(isNaN(telefono_v))
			{
				alert('ATTENZIONE!!! Inserire un numero telefonico valido.');
				form.telefono.focus();
				return;
			}
		}
		else {alert('ATTENZIONE!!! Il campo telefono è obbligatorio.'); return;}

		/*	var fax = document.getElementById('fax');
			caratteri_ammessi = Array("\\","/","-","."," ","#","+");
			fax_v = fax.value;
		if(fax_v!='')
		{	
			for(i=0;i<caratteri_ammessi.length;i++)
			{
				curr = fax_v;
				fax_v = curr.replace(caratteri_ammessi[i],'');
			}	
			
			if(isNaN(fax_v))
			{
				alert('ATTENZIONE!!! Inserire un numero di fax valido.');
				form.fax.focus();
				return;
			}	
		}	
		else alert('ATTENZIONE!!! Il campo fax è obbligatorio.');
*/
	if (trim(form.codice_fiscale.value)=='')
		{
			alert('ATTENZIONE!!! Il campo codice fiscale è obbligatorio.');
			form.codice_fiscale.focus();
			return;
		}
	var cod_fisc = form.codice_fiscale.value;
	if(!ControllaCF(cod_fisc)) 
		{
			alert('ATTENZIONE!!! Inserire un codice fiscale valido.');
			form.codice_fiscale.focus();
			return;
		}

	if(trim(form.partita_iva.value)!='')
		{
			if(!ControllaPIVA(form.partita_iva.value))
			{
			alert('ATTENZIONE!!! Inserire una partita iva valida.');
			form.partita_iva.focus();
			return;
			}
		}
			
	form.submit();
	return;

}


