var consenso = "Per potersi iscrivere all'evento bisogna acconsentire\nal trattamento dei dati personali";

function controllaEmail(mail) {
	var ok = true;
	var pe = mail.value.lastIndexOf('.');
	var ss = mail.value.indexOf(' ');
	var lch = mail.value.length-1;
	var atr = mail.value.indexOf('@');
	var att = atr+1;

	var sq = mail.value.substring(0,atr); 
	var sw = mail.value.substring(atr,pe); 
	var se = mail.value.substring(pe,lch);

	if((atr<1) || (pe<=att) || (pe==lch) || (pe<1)  || (ss != -1) || (sq.length<2) || (sw.length<3) || (se.length<=1)) {
		ok = false;
	}

	return ok;
}

function controllaCap(cap) {
	var flag = true;
	if(cap.length != 5 || isNaN(cap))
		flag = false;
	return flag;
}

function inserisciVoce() {
	document.getElementById("iscrizione").submit();
}

function controllaCampi() {
	var flag = true;
	var check = document.getElementById("trattamento");
	var nome = document.getElementById("nome");
	var societa = document.getElementById("societa");
	var ruolo = document.getElementById("ruolo");
	var mail = document.getElementById("mail");
	var cap = document.getElementById("cap");
	var telefono = document.getElementById("telefono");
	
	if(nome.value == "") {
		alert("Il campo 'Nome e Cognome' deve essere valorizzato");
		flag = false;
		nome.focus();
	} else if(societa.value == "") {
		alert("Il campo 'Società' deve essere valorizzato");
		flag = false;
		societa.focus();
	} else if(ruolo.value == "") {
		alert("Il campo 'Ruolo' deve essere valorizzato");
		flag = false;
		ruolo.focus();
	} else if(mail.value == "") {
		alert("Il campo 'E-mail' deve essere valorizzato");
		flag = false;
		mail.focus();
	} else if(!controllaEmail(mail)) {
		alert("E-mail non valida");
		flag = false;
		mail.focus();
	} else if(cap.value != "" && !controllaCap(cap.value)) {
		alert("CAP non valido");
		flag = false;
		cap.focus();
	} else if(telefono.value == "") {
		alert("Il campo 'Telefono' deve essere valorizzato");
		flag = false;
		telefono.focus();
	} else if (!check.checked) {
		alert(consenso);
		flag = false;
	}

	return flag;
}
