/*validate email*/
function isValidEmail(email) 
{
  var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/   
  return re.test(email);
}


function validate(frm)
{

	frm.sendButton.disabled = true;

	// Nome
	if(frm.Nome.value == "")
	{
		alert("Inserisci un valore valido per il campo Nome");
		frm.Nome.focus();
		frm.sendButton.disabled = false;
		return false;
	}
	// Cognome
	if(frm.Cognome.value == "")
	{
		alert("Inserisci un valore valido per il campo Cognome");
		frm.Cognome.focus();
		frm.sendButton.disabled = false;
		return false;
	}
	//Telefono
	if((frm.Telefono.value.length < 6) || (isNaN(frm.Telefono.value)))
	{
		alert("Inserisci un valore valido per il campo Numero di Telefono");
		frm.Telefono.focus();
		frm.sendButton.disabled = false;
		return false;
	}
	/*Fax
	if((frm.Fax.value == "") || (isNaN(frm.Fax.value)))
	{
		alert("Inserisci un valore valido per il campo Numero di Fax");
		frm.Fax.focus();
		return false;
	}*/
	/* Indirizzo
	if(frm.Indirizzo.value == "")
	{
		alert("Inserisci un valore valido per il campo Indirizzo");
		frm.Indirizzo.focus();
		return false;
	}*/
	// Citta'
	if(frm.Citta.value == "")
	{
		alert("Inserisci un valore valido per il campo Citta'");
		frm.Citta.focus();
		frm.sendButton.disabled = false;
		return false;
	}
	// Provincia
	if(frm.Provincia.value == "")
	{
		alert("Inserisci un valore valido per il campo Provincia");
		frm.Provincia.focus();
		frm.sendButton.disabled = false;
		return false;
	}
	/*Cap
	if((frm.Cap.value == "") || (isNaN(frm.Cap.value)))
	{
		alert("Inserisci un valore valido per il campo C.a.p");
		frm.Cap.focus();
		return false;
	}*/
	// Email
	if(isValidEmail(frm.Email.value) == "")
	{
		alert("Inserisci un valore valido per il campo E-mail");
		frm.Email.focus();
		frm.sendButton.disabled = false;
		return false;
	}
	//Tipo
	if(frm.Tipo[frm.Tipo.selectedIndex].value == "")
	{
		alert("Inserisci un valore valido per il campo Tipo");
		frm.Tipo.focus();
		frm.sendButton.disabled = false;
		return false;
	}
	//Flotta
	if(frm.DimensioneFlotta[frm.DimensioneFlotta.selectedIndex].value == "")
	{
		alert("Inserisci un valore valido per il campo Dimensione Flotta");
		frm.DimensioneFlotta.focus();
		frm.sendButton.disabled = false;
		return false;
	}
	//Campi per Azienda
	if(frm.Tipo[frm.Tipo.selectedIndex].value != "Privato")
	{
		// Ragione Sociale
		if(frm.RagioneSociale.value == "")
		{
			alert("Inserisci un valore valido per il campo Ragione Sociale");
			frm.RagioneSociale.focus();
			frm.sendButton.disabled = false;
			return false;
		}
		/*Partita Iva/Codice Fiscale
		if(frm.PartitaIva_CodiceFiscale.value == "")
		{
			alert("Inserisci un valore valido per il campo Partita Iva/Codice Fiscale");
			frm.PartitaIva_CodiceFiscale.focus();
			return false;
		}*/
	}
	//Privacy
	if(frm.privacy[0].checked == false)
	{
		alert("Devi consentire il trattamento dei tuoi dati personali");
		frm.sendButton.disabled = false;
		return false;
	}
	
	//Codice fiscale
	if(frm.CodiceFiscale.value != "")
	{
		if (!ControllaCF(frm.CodiceFiscale.value))
		{
			alert("Codice fiscale non valido");
			frm.Tipo.focus();
			frm.sendButton.disabled = false;
			return false;
		}
	}

	//partita Iva
	if(frm.PartitaIva.value != "")
	{
		if (!ControllaPIVA(frm.PartitaIva.value))
		{
			alert("Partita Iva non valida");
			frm.Tipo.focus();
			frm.sendButton.disabled = false;
			return false;
		}
	}
	
	//messaggio
	if(frm.Messaggio.value != "")
	{
		if (frm.Messaggio.value.length >= 2000)
		{
			alert("Messaggio troppo lungo");
			frm.Messaggio.focus();
			frm.sendButton.disabled = false;
			return false;
		}
	}

	return true;
}

/**************************************
    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;
}

