function checkFormContatti()
{
	ok = true;
	document.getElementById('invia').style.disabled="disabled";
	
	if(document.getElementById('privacy').checked)
	{
		var msg = "I seguenti campi sono errati:";
		if(trim(document.getElementById('nome').value) == "")
		{
			ok = false;
			msg += "\n * Nome";
		}
		if(trim(document.getElementById('cognome').value) == "")
		{
			ok = false;
			msg += "\n * Cognome";
		}
		if(trim(document.getElementById('email').value) == "")
		{
			ok = false;
			msg += "\n * Email";
		}else
		{
			if(!checkEmail(document.getElementById('email').value))
			{
				ok = false;
				msg += "\n * Email (errata)";
			}
				
		}
		if(trim(document.getElementById('ente').value) == "")
		{
			ok = false;
			msg += "\n * Azienda / Ente";
		}
		if(trim(document.getElementById('partitaiva').value) == "")
		{
			ok = false;
			msg += "\n * Partita IVA";
		} else {
			if(!ControllaPIVA(document.getElementById('partitaiva').value))
			{
				ok = false;
				msg += "\n * Partita IVA";
			}	
		}
		if(trim(document.getElementById('messaggio').value) == "")
		{
			ok = false;
			msg += "\n * Messaggio";
		}
		
		if(ok)
		{
			document.getElementById('formContatti').submit();
		}else
		{
			document.getElementById('invia').style.disabled="";
			alert(msg);
		}
	}else
	{
		alert("E' necessario accettare le condizioni sulla privacy");
	}		
}

function checkForm()
{
	ok = true;
	document.getElementById('invia').style.disabled="disabled";
	
	
	
	if(document.getElementById('privacy').checked)
	{
			var msg = "I seguenti campi sono errati:";
			
			ok = false;
			var checkboxmsg = "\n * Nessun comparto selezionato";
			var checkboxs = document.getElementsByName('comparto');
			for(var i = 0, inp; inp = checkboxs[i]; i++) {
				if (inp.checked == true) {
					ok = true;
					checkboxmsg = "";
				}
			}
			msg += checkboxmsg;
			
			if(trim(document.getElementById('nome').value) == "")
			{
				ok = false;
				msg += "\n * Nome";
			}
			if(trim(document.getElementById('cognome').value) == "")
			{
				ok = false;
				msg += "\n * Cognome";
			}
			if(trim(document.getElementById('email').value) == "")
			{
				ok = false;
				msg += "\n * Email";
			}else
			{
				if(!checkEmail(document.getElementById('email').value))
				{
					ok = false;
					msg += "\n * Email (errata)";
				}
					
			}
			if(trim(document.getElementById('ente').value) == "")
			{
				ok = false;
				msg += "\n * Azienda / Ente";
			}
			if(trim(document.getElementById('partitaiva').value) == "")
			{
				ok = false;
				msg += "\n * Partita IVA";
			} else {
				if(!ControllaPIVA(document.getElementById('partitaiva').value))
				{
					ok = false;
					msg += "\n * Partita IVA";
				}	
			}
			if(trim(document.getElementById('sitoweb').value) == "")
			{
				ok = false;
				msg += "\n * Sito web";
			}
		if(ok)
		{
			document.getElementById('formReferenti').submit();
		}else
		{
			document.getElementById('invia').style.disabled="";
			alert(msg);
		}
	}else
	{
		alert("E' necessario accettare le condizioni sulla privacy");
	}		
}

function ControllaPIVA(pi)
{
    if( pi == '' )  return false;
    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 checkEmail(mail)
{	
	var espressione = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;	
	if (!espressione.test(mail))	
	{	    
		return false;
	}else
	{
		return true;
	}
}
		
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;
}
