
function ValidLength(str,number) {
	if (str < number)
		return false;
	return true;
}

function ValidChars(CheckStr,chars) {
	for (i = 0;  i < CheckStr.length;  i++) {
    	ch = CheckStr.charAt(i);
    for (j = 0;  j < chars.length;  j++)
		if (ch == chars.charAt(j))
        	break;
		if (j == chars.length)
		return false;
	}
	return true;
}

function ValidString(CheckStr,RepeatNum) {
	if (CheckStr.length >= RepeatNum) {
		for (k = 0; k < CheckStr.length - 2; k++) {
		l = k+1;
			if  (CheckStr.charAt(k) == CheckStr.charAt(l)) {
				m = l+1;
				if (CheckStr.charAt(k) == CheckStr.charAt(m)) {
					return false;
					break;
				}
				else {
					k++;
				};
			}
		}
	}
	return true;
}

function ValidNumber (checkStr) {
	checkNumber = "0123456789";
	for (z = 0; z < checkStr.length; z++) {
		for (a = 0; a < checkNumber.length; a++) {
			if (checkStr.charAt(z) == checkNumber.charAt(a)) {
				return true;
			}
		}
	}
	return false;
}

/*
FUNZIONE validate() - Come dice il nome stesso, controlla i campi di una form prima di spedirli
*/
function validate() {
	if(document.forms[0].Nome.value=='') {
		alert("Inserire Nome")
		document.forms[0].Nome.focus();
		return false;
	}
	if(document.forms[0].Cognome.value=='') {
		alert("Inserire Cognome")
		document.forms[0].Cognome.focus();
		return false;
	}
	/*------------------ INIZIO Validazione Email --------------------*/
var FormEmail = document.forms[0].Email.value
ValidEmailChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_@.";

	if (!ValidLength(FormEmail.length, 5)) {
		alert ("Inserire l'indirizzo E-mail");
		document.forms[0].Email.focus();
		return false;
	}
dot = FormEmail.indexOf(".");
afterdot = FormEmail.charAt(dot +1);

	if (dot == -1 || afterdot == "") {
		alert("Inserire un indirizzo E-mail valido");
		document.forms[0].Email.focus();
		return false;
	}

at = FormEmail.indexOf("@");
afterat = FormEmail.charAt(at +1);

	if (at == -1 || afterat == "") {
		alert("Inserire un indirizzo E-mail valido");
		document.forms[0].Email.focus();
		return false;
	}
	if (!ValidChars(FormEmail, ValidEmailChars)) {
		alert("L'indirizzo E-mail non è valido!");
		document.forms[0].Email.focus();
		return false;
	}
/*------------------ FINE Validazione Email --------------------*/
	if(document.forms[0].Auto.value=='') {
		alert("Inserire l'Auto che si desidera provare")
		document.forms[0].Auto.focus();
		return false;
	}
	if(!(document.forms[0].Trattamento_dati.checked)) {
		alert("E' necessario dare il proprio consenso per il trattamento dei dati personali")
		document.forms[0].Trattamento_dati.focus();
		return false;
	}
}
