//--FUNZIONE PER ELIMINARE GLI SPAZI ECCESSIVI DA UNA STRINGA---------------------//
function compatta_stringa(stringa) {
	return stringa.replace(/\s+/g," ");
}
//-------------------------------------------------------------------------------//

//controllo validita email------------------------------------------------------------//
function check_email(str) {
		var valida = true;
		if (str.indexOf("@")<=-1) {
			valida = false;
		}

		if (str.indexOf("@")!=str.lastIndexOf("@")) {
			valida = false;
		}

		if (str.lastIndexOf(".")<=-1) {
			valida = false;
		}

		utente=str.substr(0,str.indexOf("@"))
		if (utente.length<3) {
			valida = false;
		}
		dominio=str.substr(str.indexOf("@")+1,str.lastIndexOf(".")-str.indexOf("@")-1)
		if (dominio.length<3) {
			valida = false;
		}

		suffisso=str.substr(str.lastIndexOf(".")+1)
		if (suffisso.length<2 || suffisso.length>5) {
			valida = false;
		}

		return valida;
}
//---------------------------------------------------------------------------------------//

function controlla_modulo() {

var ragione = compatta_stringa(document.modulo.ragione.value);
var partita = compatta_stringa(document.modulo.partita.value);
var indirizzo = compatta_stringa(document.modulo.indirizzo.value);
var telefono = compatta_stringa(document.modulo.telefono.value);
var mail = document.modulo.mail.value;
var errore = "";

  if (ragione=="" || ragione == " ") {
  	errore += "> Inserire la propria RAGIONE SOCIALE prima di procedere con l'invio\n";
  }
	 
	if (partita=="" || partita == " " || partita.length<11 || partita.length>11 || isNaN(partita)) {
		errore+="> PARTITA IVA non valida\n";
	} 
	
  if (indirizzo=="" || indirizzo == " ") {
  	errore += "> Inserire l'indirizzo\n";
  }
	
  if (telefono=="" || telefono == " ") {
  	errore += "> Inserire il numero di telefono\n";
  }	

  if (!check_email(mail)) {
  	errore+="> E-mail non valida\n";
  } 
	
	if (document.modulo.risposta[1].checked == true) {
		errore += "> Per procedere con l'invio dei dati è necessario accettare le condizioni riportate nell'INFORMATIVA SULLA PRIVACY\n";
	}

	if (errore != "")
	{
	 	alert(errore);
	}
	else
	{
	 	document.modulo.submit();
	}
}