
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 e Cognome")
		document.forms[0].Nome.focus();
		return false;
	}

/*------------------ INIZIO Validazione Indirizzo -------------------*/
var FormAddr1 = document.forms[0].Indirizzo.value ;
var ValidAddyChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzùàòèéì'0123456789 *°#.-/,";

	if (!ValidLength(FormAddr1.length, 3)) {
		alert ("Inserire l'Indirizzo.");
		document.forms[0].Indirizzo.focus();
		return false;
	}
	if (!ValidNumber(FormAddr1)) {
		alert ("ATTENZIONE! Indirizzo non valido.\nEsempio: via Verdi, 15");
		document.forms[0].Indirizzo.focus();
		return false;
	}
	if (!ValidChars(FormAddr1,ValidAddyChars)) {
		alert ("ATTENZIONE! Indirizzo non valido.\nEsempio: via Verdi, 15");
		document.forms[0].Indirizzo.focus();
		return false;
	}
/*------------------ FINE Validazione Indirizzo -------------------*/

/*------------------ INIZIO Validazione Cap --------------------*/
var FormZip = document.forms[0].Cap.value
var ZipNums = "0123456789";
  
	if (FormZip.length == 0) {
	    alert("Inserire il CAP");
	    document.forms[0].Cap.focus();
	    return false;
	}
	if (!ValidChars(FormZip,ZipNums)) {
	    alert("ATTENZIONE! CAP non valido.\nEsempio: 46100");
		document.forms[0].Cap.focus();
		return false;
	}
	if (FormZip.length == 5)
		null;
	else {
		alert("ATTENZIONE! CAP non valido.\nEsempio: 46100");
		document.forms[0].Cap.focus();
		return false;
	}
/*------------------ FINE Validazione Cap --------------------*/

	if(document.forms[0].Citta.value=='') {
		alert("Inserire la Città")
		document.forms[0].Citta.focus();
		return false;
	}
	if(document.forms[0].Prov.value=='') {
		alert("Inserire la Provincia")
		document.forms[0].Prov.focus();
		return false;
	}

/*------------------ INIZIO Validazione Telefono/Fax --------------------*/
var FormTel = document.forms[0].Telefono.value
var telNums = "0123456789-/+. ";

	if (!ValidChars(FormTel,telNums)) {
		alert("Numero di Telefono non valido");
		document.forms[0].Telefono.focus();
		return false;
	}

var FormFax = document.forms[0].Fax.value
var faxNums = "0123456789-/+. ";

	if (!ValidChars(FormFax,faxNums)) {
		alert("Numero di Fax non valido");
		document.forms[0].Fax.focus();
		return false;
	}
/*------------------ FINE Validazione Telefono/Fax --------------------*/

/*------------------ 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].Messaggio.value=='') {
		alert("Digitare il Messaggio")
		document.forms[0].Messaggio.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;
	}
}
