
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;
}

/* Estensioni di file possibili: .doc, .rtf, .pdf, .xls, .zip, .rar */
function TrovaEstensione() {
	var nomefile = document.Preventivo.Allegato.value;
	estensione = nomefile.substr(nomefile.lastIndexOf(".")+1);
	return estensione;
}

/*
FUNZIONE validate() - Come dice il nome stesso, controlla i campi di una form prima di spedirli
*/
function validate() {
	if(document.Preventivo.Nome.value=='') {
		alert("Inserire il Nome");
		document.Preventivo.Nome.focus();
		return false;
	}

	if(document.Preventivo.Cognome.value=='') {
		alert("Inserire il Cognome");
		document.Preventivo.Cognome.focus();
		return false;
	}

/*------------------ INIZIO Validazione Indirizzo -------------------*/
var FormAddr1 = document.Preventivo.Indirizzo.value ;
var ValidAddyChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzùàòèéì'0123456789 *°#.-/,";

	if (!ValidLength(FormAddr1.length, 3)) {
		alert ("Inserire l'Indirizzo.");
		document.Preventivo.Indirizzo.focus();
		return false;
	}
	if (!ValidNumber(FormAddr1)) {
		alert ("ATTENZIONE! Indirizzo non valido.\nEsempio: via Verdi, 15");
		document.Preventivo.Indirizzo.focus();
		return false;
	}
	if (!ValidChars(FormAddr1,ValidAddyChars)) {
		alert ("ATTENZIONE! Indirizzo non valido.\nEsempio: via Verdi, 15");
		document.Preventivo.Indirizzo.focus();
		return false;
	}
	if(document.Preventivo.Citta.value=='') {
		alert("Inserire la Città");
		document.Preventivo.Citta.focus();
		return false;
	}
/*------------------ FINE Validazione Indirizzo -------------------*/

/*------------------ INIZIO Validazione Cap --------------------*/
var FormZip = document.Preventivo.Cap.value;
var ZipNums = "0123456789";
  
	if (FormZip.length == 0) {
	    alert("Inserire il CAP");
	    document.Preventivo.Cap.focus();
	    return false;
	}
	if (!ValidChars(FormZip,ZipNums)) {
	    alert("ATTENZIONE! CAP non valido.\nEsempio: 46100");
		document.Preventivo.Cap.focus();
		return false;
	}
	if (FormZip.length == 5)
		null;
	else {
		alert("ATTENZIONE! CAP non valido.\nEsempio: 46100");
		document.Preventivo.Cap.focus();
		return false;
	}
/*------------------ FINE Validazione Cap --------------------*/

	if(document.Preventivo.Provincia.value=='') {
		alert("Selezionare la Provincia");
		document.Preventivo.Provincia.focus();
		return false;
	}

/*------------------ INIZIO Validazione Telefono/Fax --------------------*/
var FormTel = document.Preventivo.Telefono.value;
var telNums = "0123456789-/+. ";

	if (FormTel.length == 0) {
	    alert("Inserire il Telefono");
	    document.Preventivo.Telefono.focus();
	    return false;
	}
	if (!ValidChars(FormTel,telNums)) {
		alert("Numero di Telefono non valido");
		document.Preventivo.Telefono.focus();
		return false;
	}

/*------------------ FINE Validazione Telefono/Fax --------------------*/

/*------------------ INIZIO Validazione Email --------------------*/
var FormEmail = document.Preventivo.Email.value;
ValidEmailChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_@.";

	if (!ValidLength(FormEmail.length, 5)) {
		alert ("Inserire l'indirizzo E-mail");
		document.Preventivo.Email.focus();
		return false;
	}
dot = FormEmail.indexOf(".");
afterdot = FormEmail.charAt(dot +1);

	if (dot == -1 || afterdot == "") {
		alert("Inserire un indirizzo E-mail valido");
		document.Preventivo.Email.focus();
		return false;
	}

at = FormEmail.indexOf("@");
afterat = FormEmail.charAt(at +1);

	if (at == -1 || afterat == "") {
		alert("Inserire un indirizzo E-mail valido");
		document.Preventivo.Email.focus();
		return false;
	}
	if (!ValidChars(FormEmail, ValidEmailChars)) {
		alert("L'indirizzo E-mail non è valido!");
		document.Preventivo.Email.focus();
		return false;
	}
/*------------------ FINE Validazione Email --------------------*/

	if(document.Preventivo.Lavoro.value=='') {
		alert("Selezionare la Professione");
		document.Preventivo.Lavoro.focus();
		return false;
	}
	if(document.Preventivo.Giorno.value=='') {
		alert("Inserire il giorno richiesto per il contatto\nEsempio: lunedì 15 gennaio 2008");
		document.Preventivo.Giorno.focus();
		return false;
	}
	if(document.Preventivo.Ora.value=='') {
		alert("Inserire un orario di contatto, anche indicativo");
		document.Preventivo.Ora.focus();
		return false;
	}

/*------------------ INIZIO Validazione Visita --------------------*/
	if(document.Preventivo.Visita[0].checked==false && document.Preventivo.Visita[1].checked==false) {
		alert("Desideri la visita di un tecnico Fapes?");
		document.Preventivo.Visita[0].focus();
		return false;
	}
/*------------------ FINE Validazione Visita --------------------*/

/*------------------ INIZIO Validazione Allegato --------------------*/
	var estensioni = new Array("doc", "rtf", "pdf", "xls", "zip", "rar", "txt");
	if (document.Preventivo.Allegato.value != '')
	{
		estensione=TrovaEstensione();
		est_ok=0;
		k=0;
		while (k<estensioni.length) {
			if(estensioni[k]==estensione){
				est_ok++;
			}
			k++;
		}

		if (est_ok == 0)
		{
			alert("Attenzione: formato file non valido!");
			document.Preventivo.Allegato.focus();
			return false;
		}
	}

/*------------------ Fine Validazione Allegato --------------------*/

	if(!(document.Preventivo.Privacy.checked)) {
		alert("E' necessario dare il proprio consenso per il trattamento dei dati personali");
		document.Preventivo.Privacy.focus();
		return false;
	}
}

