function ControllaMail(valore){
	valore = Trim(valore);
	if(valore.length == 0)
		return false;
	
	regexp=/^[^@\.]+(\.[^@\.]+)*@[^@\.]+(\.[^@\.]+)*\.(\w\w|org|net|biz|com|info)$/g;
	temp=valore.match(regexp);
	if(temp==valore)
		return true;
	else
		return false;
}

function Trim(valore)
{
	re = /^\s+/g;
	valore = valore.replace(re,"");
	re = /\s+$/g;
	valore = valore.replace(re,"");
	valore = String(valore)
	return valore;
}

function checkData(data)
{
	var strData = data	
	var delim1 = strData.indexOf("/")
	var delim2 = strData.lastIndexOf("/")
	if(delim1 != -1 && delim1 == delim2){ // C'è un solo / nella stringa data
		alert("La data immessa non è in un formato accettabile.\n Puoi inserire la data nel seguente formato:\n gg/mm/aaaa")
		return false
	}
	
	if(delim1!=-1){ // Sono presenti i / nella stringa data; estraggo i componenti
		var gg = parseInt(strData.substring(0,delim1),10)
		var mm = parseInt(strData.substring(delim1+1,delim2),10)
		var aaaa = parseInt(strData.substring(delim2+1,strData.length),10)
	}else{ // Non sono presenti i / nella stringa data quindi formato non valido
		alert("La data immessa non è in un formato accettabile.\n Puoi inserire la data nei seguenti formati:\n gg/mm/aaaa, gg-mm-aaaa")
		return false
	}
	
	if(isNaN(gg) || isNaN(mm) || isNaN(aaaa)){ // Uno dei componenti la data non è in formato numerico
		alert("La data immessa non è in un formato accettabile.\n Puoi inserire la data nei seguenti formati:\n gg/mm/aaaa, gg-mm-aaaa")
		return false
	}
	
	if(mm < 1 || mm > 12){ // Il valore del mese non è accettabile
		alert("Il mese deve essere compreso tra 01(gennaio) e 12(dicembre)")
		return false
	}
	
	if(gg < 1 || gg > 31){ // Il valore del giorno non è accettabile
		alert("Il giorno deve essere compreso tra 01 e 31")
		return false
	}
	
	var month = new Array()
	month[0] = "Gennaio"
	month[1] = "Febbraio"
	month[2] = "Marzo"
	month[3] = "Aprile"
	month[4] = "Maggio"
	month[5] = "Giugno"
	month[6] = "Luglio"
	month[7] = "Agosto"
	month[8] = "Settembre"
	month[9] = "Ottobre"
	month[10] = "Novembre"
	month[11] = "Dicembre"
	
	if(mm == 4 || mm == 6 || mm == 9 || mm == 11){ // Il mese ha trenta giorni
		if(gg > 30){
			alert(month[mm-1] + " ha solo 30 giorni")
			return false
		}
	}
	
	if(aaaa%4 > 0 && mm == 2 && gg > 28){ // Anno non bisestile
		alert("Febbraio del " + aaaa + " ha solo 28 giorni")
		return false
	}else if(mm == 2 && gg > 29){ // Troppi giorni in ogni caso
		alert("Febbraio non può avere più di 29 giorni")
		return false
	}
	return true
}

var arrRequested = new Array();

function checkform(form){
	var errore = 0
	var myerr = ""
	var boolReq;
	for(var i=0; i<form.elements.length; i++){
		boolReq = false;
		for(var j = 0; j < arrRequested.length; j++)
		{
			if(form.elements[i].name == arrRequested[j])
			boolReq = true;
		}
		
		if((form.elements[i].type!="submit" && form.elements[i].value=="" && boolReq))
		{
			alert("Attenzione! Non risulta compilato un campo richiesto");
			form.elements[i].focus();
			return false;
		}
		
		if(form.elements[i].type!="submit" && form.elements[i].name=="accetto" && !form.elements[i].checked)
		{
			alert("Non si è data l'autorizzazione al trattamento dei dati personali");
			return false;
		}
		
		if(String(form.elements[i].name).toLowerCase().indexOf("email") >= 0 && String(form.elements[i].value).length > 0)
		{
			if(!ControllaMail(String(form.elements[i].value)))
			{
				alert("Indirizzo e-mail non valido.");
				return false;
			}
		}
		
		if(form.elements[i].name.toLowerCase().indexOf("fld_data") > 0 && String(form.elements[i].value).length > 0)
		{
			if(!checkData(String(form.elements[i].value)))
				return false;
		}
	}
	return true
}
