//////////////////////////////////////////////////

function isEmpty(s)
{
   return ((s == null) || (s.length == 0))
}

//////////////////////////////////////////////////

function isDigit (c)
{   
   return ((c >= "0") && (c <= "9"))
}

//////////////////////////////////////////////////

function isInteger (s)
{    
	var i;

    if (isEmpty(s)) 
       return true;  

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    return true;
}

//////////////////////////////////////////////////

function noemptyvalidation(entered, alertbox)
{
  with (entered)
  {
    if (isEmpty(value))
    {
      if (alertbox!="")
      {
        alert(alertbox);
      }
      return false;
    }
    else
    {
      return true;
    }
  }
}

//////////////////////////////////////////////////

function integervalidation(entered, alertbox)
{
    with (entered)
	
    {
      if (isInteger(value))
      {
         return true;
      }
      else
      {
         if (alertbox!="") 
         {
			alert(alertbox);
         }
         return false;
      }
    }
}         

//////////////////////////////////////////////////

function integervalidationZip(entered, alertbox) {
		with(entered) {
numValue = entered.value.length;
if (numValue == 5) {
         return true;
      }
      else {
         if (alertbox!="") {
            alert(alertbox);
         }
         return false;
      }
    }    
	}

//////////////////////////////////////////////////
function integervalidationCodeFiscale(entered, alertbox) {
		with(entered) {
numValue = entered.value.length;
if (numValue == 16) {
         return true;
      }
      else {
         if (alertbox!="") {
            alert(alertbox);
         }
         return false;
      }
    }    
	}

//////////////////////////////////////////////////

function integervalidationPass(entered, alertbox) {
		with(entered) {
numValue = entered.value.length;
if (numValue == 8 || numValue > 8) {
         return true;
      }
      else {
         if (alertbox!="") {
            alert(alertbox);
         }
         return false;
      }
    }    
	}

//////////////////////////////////////////////////

function emailvalidation(entered)
{
	with(entered)
	{		
		var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
		var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/; // valid
		if (!reg1.test(value) && reg2.test(value))
		{ 				// if syntax is valid
			return true;
		}
		alert("\"" + value + "\" non e' un indirizzo e-mail valido!"); 
		return false;
	}
}


//////////////////////////////////////////////////

function ControllaChecked(entered, alertbox){
		with(entered) {
numValue = entered.value;
if (entered.checked) {
         return true;
      }
      else {
         if (alertbox!="") {
            alert(alertbox);
         }
         return false;
      }
    }    
	}

///////////// DATA CONTROL ////////////////////////////

function controlloEta(entered, alertbox)
{
with (entered)
  {
    var d = document.getElementById("etaUtente").value;
                    
    var et = eta(d, (new Date()).toStringDate() );
    if(isNaN(et)) {
        alert(et);
    }
    else {
        if(et >= 18) {
			return true;
        }
        else
			alert("\"" + d + "\" non e' una data di nascita valida!");
         return false;
    }
}
}
Date.prototype.toStringDate = function ()
{
    return this.getDate() + "/" + (this.getMonth() + 1) + "/" + this.getFullYear()
}

//////////////////////////////////////////////////

function isdate(dateStr, year4) 
{
	if(typeof(year4) == "undefined")
	    var year4 = false;
	
	var ret = new Object();
	ret.isdate = false;
	ret.error = "";
	ret.year = 0;
	ret.month = 0;
	ret.monthname = "";
	ret.dayofweek = "";
	ret.day = 0;
	ret.data = null;
	
    var datePat = null;
    if(year4)
        datePat = /^\s*(\d{1,2})(\/|-|\.)(\d{1,2})(\2)(\d{4})\s*$/;
    else
        datePat = /^\s*(\d{1,2})(\/|-|\.)(\d{1,2})(\2)(\d{2}|\d{4})\s*$/;
    
    
    var matchArray = dateStr.match(datePat); 

    if (matchArray == null) {
        ret.error = "Per favore, introduci la data nei formati gg/mm/aaaa o gg-mm-aaaa o gg.mm.aaaa";
        return ret;
    }

    day = matchArray[1];
    month = matchArray[3]; 
    year = matchArray[5];
    if(year.length == 2)
		if(parseInt(year,10) < 30)
			year = parseInt(year,10) + 2000;
		else
			year = parseInt(year,10) + 1900;

    if (month < 1 || month > 12) {
        ret.error = "Il mese deve essere compreso tra 1 e 12.";
        return ret;
    }

    if (day < 1 || day > 31) {
        ret.error = "Il giorno deve essere compreso tra 1 e 31";
        return ret;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        
        ret.error = "Il mese " + month + " non ha 31 giorni!";
        return ret;
    }

    if (month == 2) {
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
			ret.error = "Febbraio " + year + " non ha " + day + " giorni!";
			return ret;
        }
    }
    isDateError = "";
	ret.isdate = true;
	ret.error = "";
	ret.year = year;
	ret.month = month;
	ret.day = day;
	ret.data = new Date(year, (month - 1), day);
    switch(parseInt(month,10))
    {
        case 1: ret.monthname = "Gennaio"; break;
        case 2: ret.monthname = "Febbraio"; break;
        case 3: ret.monthname = "Marzo"; break;
        case 4: ret.monthname = "Aprile"; break;
        case 5: ret.monthname = "Maggio"; break;
        case 6: ret.monthname = "Giugno"; break;
        case 7: ret.monthname = "Luglio"; break;
        case 8: ret.monthname = "Agosto"; break;
        case 9: ret.monthname = "Settembre"; break;
        case 10: ret.monthname = "Ottobre"; break;
        case 11: ret.monthname = "Novembre"; break;
        case 12: ret.monthname = "Dicembre"; break;
        default: ret.monthname = ""; break;
    }
    switch( ret.data.getDay() )
    {
        case 0: ret.dayofweek = "Domenica"; break;
        case 1: ret.dayofweek = "Lunedì"; break;
        case 2: ret.dayofweek = "Martedì"; break;
        case 3: ret.dayofweek = "Mercoledì"; break;
        case 4: ret.dayofweek = "Giovedì"; break;
        case 5: ret.dayofweek = "Venerdì"; break;
        case 6: ret.dayofweek = "Sabato"; break;
        default: ret.dayofweek = ""; break;
    
    }
    
    return ret;
}


//////////////////////////////////////////////////

function Eta(dataNascita, dataControllo)
{
	var anni;
	anni = dataControllo.getFullYear() - dataNascita.getFullYear();
	anni -= (( dataControllo < new Date(dataControllo.getFullYear(),dataNascita.getMonth() , dataNascita.getDate() )  )?1:0 );
	return anni;
}


//////////////////////////////////////////////////


function eta(stringaDataNascita, stringaDataControllo)
{
		with(etaUtente) {

	var dataNascita, dataControllo, Anni;
	var d;
	
	d = isdate(stringaDataNascita);
	if(d.isdate)
	{
		dataNascita = new Date(d.year,d.month - 1,d.day);
	}
	else
	{
      return false;
	}
	
	d = isdate(stringaDataControllo);
	if(d.isdate)
	{
		dataControllo = new Date(d.year,d.month - 1,d.day);
	}
	else
	{
      return false;
	}
	
	if(dataControllo < dataNascita)
	{
      return false;
	}
	
	return Eta(dataNascita, dataControllo);

}

}

//////////////// END DATA CONTROL ///////////////////////


function convUtenteForm(thisform)
{
   with (thisform)
   {
	  if (noemptyvalidation(nome,"Inserire il nome!")==false) {nome.focus(); return false;};
	  if (noemptyvalidation(cognome,"Inserire il cognome!")==false) {cognome.focus(); return false;};
	  if (noemptyvalidation(sesso,"Selezionare il sesso!")==false) {sesso.focus(); return false;};
	  if (noemptyvalidation(street,"Inserire l'indirizzo!")==false) {street.focus(); return false;};
	  	  if (noemptyvalidation(city,"Inserire la citta'!")==false) {city.focus(); return false;};
	  if (noemptyvalidation(zip,"Inserire il CAP!")==false) {zip.focus(); return false;};
   if (integervalidation(zip,"Valore non corretto, inserire un valore numerico per il CAP!")==false) {zip.focus(); return false;};
   if (integervalidationZip(zip,"Valore non corretto, inserire 5 cifre per il CAP!")==false) {zip.focus(); return false;};
	  if (noemptyvalidation(provincia,"Inserire la provincia!")==false) {provincia.focus(); return false;};
	  if (noemptyvalidation(etaUtente,"Inserire la data di nascita!")==false) {etaUtente.focus(); return false;};
	  if (controlloEta(etaUtente,"Data di nascita non valida!")==false) {etaUtente.focus(); return false;};
	  if (noemptyvalidation(phone,"Inserire il numero di telefono!")==false) {phone.focus(); return false;};
   if (noemptyvalidation(username,"Inserire l'indirizzo e-mail!")==false) {username.focus(); return false;};
   if (emailvalidation(username)==false) {username.focus(); return false;};
   if (noemptyvalidation(password,"Inserire la password scelta!")==false) {password.focus(); return false;};
   if (integervalidationPass(password,"Valore non corretto, inserire min. 8 caratteri per la password!")==false) {password.focus(); return false;};
   if (noemptyvalidation(pagamento,"Selezionare il metodo di pagamento!")==false) {pagamento.focus(); return false;};
if (ControllaChecked(privacy,"Occorre leggere ed accettare la normativa sulla privacy per terminare la registrazione!")==false) {return false;};
	  submit();
   }
}
//////////////////////////////////////////////////

function convUtenteFormA(thisform)
{
   with (thisform)
   {
	  if (noemptyvalidation(nome,"Inserire il nome!")==false) {nome.focus(); return false;};
	  if (noemptyvalidation(cognome,"Inserire il cognome!")==false) {cognome.focus(); return false;};
	  if (noemptyvalidation(sesso,"Selezionare il sesso!")==false) {sesso.focus(); return false;};
	  if (noemptyvalidation(street,"Inserire l'indirizzo!")==false) {street.focus(); return false;};
	  	  if (noemptyvalidation(city,"Inserire la citta'!")==false) {city.focus(); return false;};
	  if (noemptyvalidation(zip,"Inserire il CAP!")==false) {zip.focus(); return false;};
   if (integervalidation(zip,"Valore non corretto, inserire un valore numerico per il CAP!")==false) {zip.focus(); return false;};
   if (integervalidationZip(zip,"Valore non corretto, inserire 5 cifre per il CAP!")==false) {zip.focus(); return false;};
	  if (noemptyvalidation(provincia,"Inserire la provincia!")==false) {provincia.focus(); return false;};
	  if (noemptyvalidation(etaUtente,"Inserire la data di nascita!")==false) {etaUtente.focus(); return false;};
	  if (controlloEta(etaUtente,"Data di nascita non valida!")==false) {etaUtente.focus(); return false;};
	  if (noemptyvalidation(phone,"Inserire il numero di telefono!")==false) {phone.focus(); return false;};
   if (noemptyvalidation(username,"Inserire l'indirizzo e-mail!")==false) {username.focus(); return false;};
   if (emailvalidation(username)==false) {username.focus(); return false;};
   if (noemptyvalidation(pagamento,"Selezionare il metodo di pagamento!")==false) {pagamento.focus(); return false;};
if (ControllaChecked(privacy,"Occorre leggere ed accettare la normativa sulla privacy per terminare l'ordine!")==false) {return false;};
	  submit();
   }
}
//////////////////////////////////////////////////

function convUtenteFormB(thisform)
{
   with (thisform)
   {
	  if (noemptyvalidation(nome,"Inserire il nome!")==false) {nome.focus(); return false;};
	  if (noemptyvalidation(cognome,"Inserire il cognome!")==false) {cognome.focus(); return false;};
	  if (noemptyvalidation(sesso,"Selezionare il sesso!")==false) {sesso.focus(); return false;};
	  if (noemptyvalidation(street,"Inserire l'indirizzo!")==false) {street.focus(); return false;};
	  	  if (noemptyvalidation(city,"Inserire la citta'!")==false) {city.focus(); return false;};
	  if (noemptyvalidation(zip,"Inserire il CAP!")==false) {zip.focus(); return false;};
   if (integervalidation(zip,"Valore non corretto, inserire un valore numerico per il CAP!")==false) {zip.focus(); return false;};
   if (integervalidationZip(zip,"Valore non corretto, inserire 5 cifre per il CAP!")==false) {zip.focus(); return false;};
	  if (noemptyvalidation(provincia,"Inserire la provincia!")==false) {provincia.focus(); return false;};
	  if (noemptyvalidation(etaUtente,"Inserire la data di nascita!")==false) {etaUtente.focus(); return false;};
	  if (controlloEta(etaUtente,"Data di nascita non valida!")==false) {etaUtente.focus(); return false;};
	  if (noemptyvalidation(phone,"Inserire il numero di telefono!")==false) {phone.focus(); return false;};
	  submit();
   }
}
//////////////////////////////////////////////////

function convLogin(thisform)
{
   with (thisform)
   {
   if (noemptyvalidation(username,"Inserire l'indirizzo e-mail!")==false) {username.focus(); return false;};
   if (emailvalidation(username)==false) {username.focus(); return false;};
   if (noemptyvalidation(password,"Inserire la password!")==false) {password.focus(); return false;};
   if (integervalidationPass(password,"Valore non corretto, inserire min. 8 caratteri per la password!")==false) {password.focus(); return false;};
	  submit();
   }
}
//////////////////////////////////////////////////

function convVerifica(thisform)
{
   with (thisform)
   {
   if (noemptyvalidation(cap,"Inserire il CAP!")==false) {cap.focus(); return false;};
   if (integervalidation(cap,"Valore non corretto, inserire un valore numerico per il CAP!")==false) {cap.focus(); return false;};
   if (integervalidationZip(cap,"Valore non corretto, inserire 5 cifre per il CAP!")==false) {cap.focus(); return false;};
	  submit();
   }
}
//////////////////////////////////////////////////

function convSearch(thisform)
{
   with (thisform)
   {
   if (noemptyvalidation(cerca,"Inserire la/le parola/e da ricercare!")==false) {cerca.focus(); return false;};
	  submit();
   }
}
//////////////////////////////////////////////////

function convSearchB(thisform)
{
   with (thisform)
   {
   if (noemptyvalidation(cerca,"Inserire la/le parola/e da ricercare!")==false) {cerca.focus(); return false;};
	  submit();
   }
}
//////////////////////////////////////////////////

