//Questo script permette la validazione di una form 
//Il tag <form> deve avere la seguente forma:
//<form method="post" action="mailto:nome@nomeazienda.it" name="nomechevuoi" id="nomechevuoi" onSubmit="return convalidaform(this)">  
//Questo script è stato fatto da Anna Blosi (anna@ramanet.it) per cui se non va chiedete con lei.
//Nota interna RAMA: consiglio di eliminare il commento sopra riportato se deve essere pubblicato sul sito di un cliente


// funzione campi non vuoti
function campinonvuoti(entered, alerttext)
{
with (entered)
{
if (value==null || value=="")
{if (alerttext !="") {alert (alerttext); } return false;}
else {return true;}
}
}

function confrontacampi(entered, entered2, alerttext)
{
with (entered)
{
if (value!=entered2.value)
{
	if (alerttext !="") {alert (alerttext); } return false;
}
else {return true;}
}
}

//funzione per controllo lunghezza minima
function convalidalunghezza(entered,alerttext,lung)
{
with(entered)
{
lunghezza=value;
if (lunghezza.length!=lung)
{
if (alerttext !="") alert (alerttext); 
return false;
}
}
}

//funzione controllo checkbox
function convalidacheck()
{
if (document.modulo.attrezz_risto.checked ) return true;
if (document.modulo.arredatore.checked ) return true;
if (document.modulo.arte_bianca.checked ) return true;
if (document.modulo.produttore.checked ) return true;
if (document.modulo.assistenza_tecnica.checked ) return true;
return false;
}

//funzione di convalida radiobutton
function convalidaradio(form)
{
if (form.autorizza[0].checked) return true;
if (form.autorizza[1].checked) return false;
else return false;
}

//funzione per verificare il corretto inserimento della email
function convalidamail(entered,alerttext)
{
with(entered)
{
apos=value.indexOf ("@");
dotpos=value.lastIndexOf (".");
lastposstr=value;
lastpos=lastposstr.length-1 ;
if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2){
	if (alerttext !="") {
		alert(alerttext);
} 
	return false;
}
else {
	return true;
}
}
}

function isDate(dateStr, nome) {

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null && dateStr.lenght==0) {
return true;
}

if (matchArray == null) {
	alert("Data non valida. (campo " + nome + ")")
return false;
}

month = matchArray[3]; // p@rse date into variables
day = matchArray[1];
year = matchArray[5];


if (isNaN(month) || isNaN(day) || isNaN(year)) {
	alert("La data non è valida. (campo " + nome + ")");
return false;
}
if (month < 1 || month > 12) { // check month range
alert("Il mese deve andare da 1 a 12. (campo " + nome + ")");
return false;
}

if (day < 1 || day > 31) {
alert("Il giorno deve andare da 1 a 31. (campo " + nome + ")");
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Questo mese non ha 31 giorni (campo " + nome + ")")
return false;
}

if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
alert("Febbraio " + year + " non ha " + day + " giorni! (campo " + nome + ")");
return false;
}
}
return true; // date is valid
}


//funzione di convalida ke rikiama la funzione campi
function convalidaform(laform)
{
with (laform)
{

if (convalidaradio(laform)==false){
alert('Manca l\'autorizzazione al trattamento dei dati personali');
return false;
}

if (campinonvuoti(azienda, "Il campo Azienda è vuoto!")==false)
{azienda.focus(); return false;}

if (campinonvuoti(piva, "Il campo PIva è vuoto!")==false)
{piva.focus(); return false;}

if (isNaN(piva.value))
{
alert("Inserisci solo valori numerici nella PIVA!"); 
piva.focus();
return false;
}

if (convalidalunghezza(piva,"il campo PIVA deve essere di 11 numeri",11)==false)
{piva.focus(); return false;}

if (campinonvuoti(referente, "Il campo Referente è vuoto!")==false)
{referente.focus(); return false;}

if (campinonvuoti(via, "Il campo Via è vuoto!")==false)
{via.focus(); return false;}

if (campinonvuoti(cap, "Il campo CAP è vuoto!")==false)
{cap.focus(); return false;}

if (isNaN(cap.value))
{
alert("Inserisci solo valori numerici  nel CAP!"); 
cap.focus();
return false;
}

if (campinonvuoti(citta, "Il campo Località è vuoto!")==false)
{citta.focus(); return false;}

if (campinonvuoti(provincia, "Il campo Provincia è vuoto!")==false)
{provincia.focus(); return false;}

if (campinonvuoti(stato, "Il campo Stato è vuoto!")==false)
{stato.focus(); return false;}

if (campinonvuoti(telefono, "Il campo Telefono è vuoto!")==false)
{telefono.focus(); return false;}

if (isNaN(telefono.value))
{
alert("Inserisci solo valori numerici nel Telefono!"); 
telefono.focus();
return false;
}

if (campinonvuoti(fax, "Il campo Fax è vuoto!")==false)
{fax.focus(); return false;}

if (isNaN(fax.value))
{
alert("Inserisci solo valori numerici nel Fax!"); 
fax.focus();
return false;
}

if (campinonvuoti(email, "Il campo Email è vuoto!")==false)
{email.focus(); return false;}

if (convalidamail(email, "Il campo Email non è valido!")==false)
{email.focus(); return false;}

if (campinonvuoti(psw, "Il campo Password è vuoto!")==false)
{psw.focus(); return false;}

if (campinonvuoti(psw2, "Il campo di verifica password è vuoto!")==false)
{psw2.focus(); return false;}

if (confrontacampi(psw, psw2, "La password e la sua verifica sono differenti!")==false)
{psw.focus(); return false;}

return true;
}
}

