function InviaPassword() {
	if (EmailOK(document.formPassword.email.value))
		document.formPassword.submit();
	else
		alert("Email errata o mancante");
}

function ContaQuanti(pos,str,car,q) {
	// funzione ricorsiva che conta quante sottostringhe
	// sono contenute all'interno di una stringa
	var CQ = str.indexOf(car,pos);
	if (CQ == -1) {
		return(q);
	} else {
		return(ContaQuanti(CQ+1,str,car,q+1));
	}
}

function IsEmpty(field) {
	var OK = true;
	if (field.length > 0) {
		var i = 0;
		for (i=0; i<field.length; i++) {
			if (field.charAt(i) != " ") {
				OK = false;
				break;
			}
		}
	}
	return(OK);
}

function EmailOK(email) {
	var OK = true;
	if (IsEmpty(email)) {
		OK = false;
	} else {
		if (ContaQuanti(0,email,"@",0) == 1) {
			var pAt = email.indexOf("@");
			var pP = email.indexOf(".");
			if ((pP > 0) && (pP < (email.length - 2))) {
				var pPprec = -1;
				pP = 0;
				do
				{
					pP = email.indexOf(".",pP);
					if (pP == (-1)) {
						break;
					} else {
						if ((pP == pAt+1) || (pP == pAt-1) || (pPprec == (pP-1) || (pP > (email.length - 3)))) {
							OK = false;
							break;
						} else {
							pPprec = pP;
							pP += 1;
						}
					}
				}
				while (pP < email.length);
			} else {
				OK = false;
			}
		} else {
			OK = false;
		}
		if (email.indexOf("'") > -1)
			OK = false;
	}
	return(OK);
}

function checkRegister(codice) {
	//codice: se = 1 -> va inserito il codice, se = 0 no
	var strErr;

	strErr = "";
	if (IsEmpty(document.formRegister.newNome.value))
		strErr = strErr + "\nIl campo Nome non è compilato";
	if (IsEmpty(document.formRegister.newCognome.value))
		strErr = strErr + "\nIl campo Cognome non è compilato";
	if (IsEmpty(document.formRegister.newProvincia.value))
		strErr = strErr + "\nIl campo Provincia non è compilato";
	else {
		if (IsEmpty(document.formRegister.newCitta.value))
			strErr = strErr + "\nIl campo Città non è compilato";
	}
	if (!EmailOK(document.formRegister.newEmail.value))
		strErr = strErr + "\nIl campo E-mail non sembra essere correttamente compilato";
	if ((document.formRegister.newPassword.value.length < 8) || (document.formRegister.newPassword2.value.length < 8))
		strErr = strErr + "\nIl campo Password deve contenere almeno 8 caratteri";
	if (document.formRegister.newPassword.value != document.formRegister.newPassword2.value)
		strErr = strErr + "\nIl campo Password non è stato digitato bene";
	if ((document.formRegister.newPassword.value.indexOf("'") > -1) || (document.formRegister.newPassword2.value.indexOf("'") > -1))
		strErr = strErr + "\nIl campo Password non può contenere apici";
	if (document.formRegister.newDomandaSegreta.value == "0")
		strErr = strErr + "\nIl campo Domanda segreta non è compilato";
	if (IsEmpty(document.formRegister.newRispostaSegreta.value))
		strErr = strErr + "\nIl campo Risposta segreta non è compilato";

	if (codice == 1)
		strErr = strErr + checkCodice(document.formRegister);

	if (strErr == "") {
		if (document.formRegister.Privacy.checked)
			//document.formRegister.submit();
			return(true);
		else {
			alert("Per proseguire, occorre acconsentire altrattamento dati.");
			return(false);
		}
	} else {
		alert("ATTENZIONE!!\n" + strErr);
		return(false);
	}
}

function checkLogin(codice) {
	//codice: se = 1 -> va inserito il codice, se = 0 no
	var strErr;

	strErr = "";
	if (!EmailOK(document.formLogin.email.value))
		strErr = strErr + "\nIl campo E-mail non sembra essere correttamente compilato";
	if (document.formLogin.password.value.length < 7)
		strErr = strErr + "\nIl campo Password deve contenere almeno 7 caratteri";
	if (document.formLogin.password.value.indexOf("'") > -1)
		strErr = strErr + "\nIl campo Password non può contenere apici";
	if (codice == 1)
		strErr = strErr + checkCodice(document.formLogin);

	if (strErr == "")
		//document.formLogin.submit();
		return(true);
	else
		alert("ATTENZIONE!!\n" + strErr);
		return(false);
}

function checkCodice(oForm) {
	var strErr;

	strErr = "";
	if (oForm.codice.value.length < 10)
		strErr = strErr + "\nIl campo Codice deve contenere almeno 10 caratteri";
	if (oForm.codice.value.indexOf("'") > -1)
		strErr = strErr + "\nIl campo Codice non può contenere apici";
/*
	if (strErr == "")
		//document.formLogin.submit();
		return(true);
	else
		alert("ATTENZIONE!!\n" + strErr);
		return(false);
*/
	return(strErr);
}
