//Funzione che controlla il formato dell'email tenendo conto di campi non ammessi e cmapi obbligatori
function isEmail(string) {
	//Variabile contenente caratteri non ammesi
   var iChars = "*|,\:/<>[]{}`\';()&$#%§";
   	//Variabile contenente caratteri obbligatori
   var iCharsOk1 = "@";
   var iCharsOk2 = ".";
   //Variabili di gestione errori
   var OK1=0;
   var OK2=0;
   var NO=0;
	//Se non è una stringa aumento la variabile d'errore
   if (!string) NO++;
    //Se nella stringa sono contenuti dei caratteri non ammessi aumento la var d'errore
   for (var i = 0; i < string.length; i++) {
      if (iChars.indexOf(string.charAt(i)) != -1) {
         NO++;
      }
   }
    //Se nella stringa sono contenuti i caratteri obbligatori aumento la variabile di controllo
   for (var i = 0; i < string.length; i++) {
      if (iCharsOk1.indexOf(string.charAt(i)) > -1) {
         OK1++;
      }
   }
    //Se nella stringa sono contenuti i caratteri obbligatori aumento la variabile di controllo
   for (var i = 0; i < string.length; i++) {
      if (iCharsOk2.indexOf(string.charAt(i)) > -1) {
         OK2++;
      }
   }
    //Se le non ci sono caratteri proibiti e ci sono tutti i caratteri richiesti l'email è valida
   if ((NO==0) && (OK1==1) && (OK2>=1)) {
   		return true;
   }
   else {
   		//Altrimenti visualizzo un messaggio di errore e cancello il campo
   		alert("Invalid email format");
   	   return false;
   }
}

//Funzione per il controllo Con Email del form al momento dell'invio
function controllaConEmail(strEmail) {
	if (isEmail(document.forms[0].elements[strEmail].value)){
		for (var i=0; i<document.forms[0].elements.length; i++) {
			if (document.forms[0].elements[i].name=="Password") {
				return controllaPwd()
			}
			if (document.forms[0].elements[i].name.indexOf("_")==document.forms[0].elements[i].name.length-1){
				if (document.forms[0].elements[i].value==""){
					var valore=document.forms[0].elements[i].name.length-1
				   alert(document.forms[0].elements[i].name.substring(0,valore) + " is a required field")
				   return false;
				}
			}
		}
	}
	else {
		return false
	}
}


//Funzione per il controllo senza Email del form al momento dell'invio
function controlla() {
	for (var i=0; i<document.forms[0].elements.length; i++) {
		if (document.forms[0].elements[i].name=="Password") {
			return controllaPwd()
		}
		if (document.forms[0].elements[i].name.indexOf("_") > -1){
			if (document.forms[0].elements[i].value==""){
				var valore=document.forms[0].elements[i].name.length-1
			   alert(document.forms[0].elements[i].name.substring(0,valore) + " is a required field")
			   return false;
			}
		}
	}
}

//Funzione per il controllo senza Email del form al momento dell'invio
function controllaPwd() {
	if (document.forms[0].Password.value=="" || document.forms[0].Password2.value=="") {
		alert("Insert password in all the required fields");
		return false;
	}
	if (document.forms[0].Password.value!=document.forms[0].Password2.value) {
		alert("Passwords inserted does not match");
		return false;
	}

}


function checkdate(objName) 
{
	var datefield = objName;
	if (chkdate(objName) == false) 
	{
		datefield.select();
		alert("Invalid date.\nThe right format is dd/mm/aaaa");
		datefield.focus();
		return false;
	}
	else 
	{
		return true;
	}
}

//Funzione per il controllo della data inserita
function chkdate(objName) 
{
var strDatestyle = "EU"; //European date style Day Month Year
var monthToText = 0
var runTime = new Date();
var strCurrentYear = runTime.getYear();
var strDate;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var booFound = false;
var datefield = objName;
var strSeparatorArray = new Array("-",",",":","/"," ","|",".","!","\\");
var intElementNr;
var err = 0;

var strMonthArray = new Array("Gen","Feb","Mar","Apr","Mag","Giu","Lug","Ago","Set","Ott","Nov","Dic","Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre");
strDate = datefield.value;
if (strDate.length < 1) 
	{ return true; }
	
for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) 
{
	if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) 
	{
		strDateArray = strDate.split(strSeparatorArray[intElementNr]);
		if ((strDateArray.length < 2) || (strDateArray.length > 3)) 
		{
			err = 1;
			return false;
		}
	else 
	{
		strDay = strDateArray[0];
		strMonth = strDateArray[1];
		if (strDateArray.length == 2) 
		strYear = strCurrentYear
	else
		strYear = strDateArray[2];
	}
	booFound = true;
}

}

if (booFound == false) 
{
	if (strDate.length>5) 
	{
		strDay = strDate.substr(0, 2);
		strMonth = strDate.substr(2, 2);
		strYear = strDate.substr(4);
	}
}

if (strYear == null) 
{  return false;
}

if (strYear.length == 1) 
{ strYear = '0' + strYear
}

if (strYear.length == 2) 
{ if (strYear>70) {
	strYear = '19' + strYear;
  }
  else {
	strYear = '20' + strYear;
  }
}

if (strYear.length > 4)
{ strYear = strYear.substr (0, 4);
}

if (!((strYear >= 1900) && (strYear<2100)))
{ return false;
}

intday = parseInt(strDay, 10);

if (isNaN(intday)) 
{
	err = 2;
	return false;
	}

	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) 
	{
		for (i = 0;i<12;i++) {
		if ((strMonth.toUpperCase() == strMonthArray[i].toUpperCase())|| (strMonth.toUpperCase() == strMonthArray[i+12].toUpperCase())) 
		{
			intMonth = i+1;
			strMonth = strMonthArray[i];
			i = 12;
		}
	}

	if (isNaN(intMonth)) 
	{
		err = 3;
		return false;
	}
}

intYear = parseInt(strYear, 10);

if (isNaN(intYear)) {
err = 4;
return false;
}

if (intMonth>12 || intMonth<1) {
err = 5;
return false;
}

if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
err = 6;
return false;
}

if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
err = 7;
return false;
}

if (intMonth == 2) {
if (intday < 1) {
err = 8;
return false;
}
if (LeapYear(intYear) == true) {
if (intday > 29) {
err = 9;
return false;
}
}
else {
if (intday > 28) {
err = 10;
return false;
}
}
}

if (strDay.length==1){
	strDay=0+strDay
}

mese=strMonth
if (monthToText == 1){
	mese=strMonthArray[intMonth-1]
}

if (mese.length==1){
	mese=0+mese
}

datefield.value = strDay + "/" + mese + "/" + strYear;

return true;
}


function LeapYear(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) { return true; }
}
else {
if ((intYear % 4) == 0) { return true; }
}
return false;
}

//Quando in un box di input non voglio far superare un certo numero di caratteri numchar
function MaxChar(stringa,campo,numchar) {
	if (stringa.length == numchar+1){
		alert("The lenght of the field '"+campo+"' cant exceed "+numchar+" characters")
		document.forms[0].elements[campo].value=stringa.substring(0,numchar)
	}
}


//Blocca-sblocca i bottoni di invio e annulla
function nascondiBottoni(condizione,bottone1,bottone2) {
	document.forms[0].elements[bottone1].disabled=condizione
	document.forms[0].elements[bottone2].disabled=condizione
}
