//--------------------------- VALIDAZIONE FORM ---------------------------------------
function init_form()
{
	errore = "";
	flag = 0;
}
function controlla_numeri(tipo, lingua)
{
	mess = new Array(2);
	mess[0] = "ATTENZIONE inserire solo valori NUMERICI";
	mess[1] = "WARNING! NUMBERS only to be inserted here";
	mess[2] = "ACHTUNG hier nur Zahlen einfügen"
	var nr1=0;
	switch (tipo)
	{
		case "budget":
			nr1 = document.prenotazioni.budget.value;
			break;
			
		case "cap":
			nr1 = document.prenotazioni.cap.value;
			break;
	}
  flg=0;
  str="";
  spc=""
  arw="";
  for (var i=0;i<nr1.length;i++)
  {
  		cmp="0123456789"
   		tst=nr1.substring(i,i+1)
	   if (cmp.indexOf(tst)<0)
   		{
   			flg++;
		   str+=" "+tst;
		   spc+=tst;
		   arw+="^";
	   }
	   else
   		{	
   			arw+="_";
   		}
 }
 if (flg!=0)
  {
  	 if (spc.indexOf(" ")>-1) 
  	 	{
		    str+=" and a space";
	   }
	alert (mess[lingua]);
	
	switch (tipo)
	{
		case "budget":
			document.prenotazioni.budget.value = "";
			document.prenotazioni.budget.focus();
			break;
			
		case "cap":
			document.prenotazioni.cap.value = "";
			document.prenotazioni.cap.focus();
			break;
	}
}
}

function verifica_form(lingua)
{
	verifica_name(lingua);
	verifica_mail(lingua);
	gestione_errori(lingua);
	
	if (flag != 0)
		{	
			errore = "";
			flag = 0;
			return false;
		}
	else 
		{
			return true;
		}
}
function verifica_name(lingua)
{
	mess = new Array(2);
	mess[0] = "\n NOME";
	mess[1] = "\n NAME";
	mess[2] = "\n VORNAME";
	valore_campo = document.prenotazioni.name.value
	if (valore_campo == "")
		{
		errore = errore + mess[lingua];
		flag = 1;
		}
}

function emailControl(StrMail)
{
   // Controllo il formato dell'indirizzo e-mail. rstituisce true o false
   if (StrMail.length>6)
      {
      var pos;
      var dotpos;
      pos = StrMail.indexOf("@");
      if ( (pos >= (StrMail.length-3) ) || (pos < 2) )
         {
         return (false);
         }
      pos=pos+1;
      dotpos = StrMail.indexOf(".", pos);
      if (dotpos > (StrMail.length-3) )
         {
         return (false);
         }
      Strmail=StrMail.substr(pos);
      if ( (StrMail.length < 5) || (dotpos <= 0) )
         {
         return (false);
         }
      }
   else
      {
      return (false);
      }
   return true;
}
function verEmail(stringa)
{
// verifica LESSICALE dell'indirizzo inserito
   var iChars = "*|,\"<:>[]{}`\';()&$#%";
   for (var i = 0; i < stringa.length; i++)
   {
      if (iChars.indexOf(stringa.charAt(i)) != -1)
         return false;
   }
   return true;
}
function verifica_mail(lingua)
{
	mess = new Array(2);
	mess[0] = "\n E-MAIL";
	mess[1] = "\n E-MAIL address";
	mess[2] = "\n E-MAIL";
	res = true;
	valore_campo = document.prenotazioni.email.value;
	if (valore_campo != "")
	{
		// verifica lessicale dell'email inserita
		res = verEmail(valore_campo);
		if (res == true)
		{
			// verifica se la sintassi č corretta <name>@<server>
			res = emailControl(valore_campo);
			if (res == false)
			{
				errore = errore + mess[lingua];
				flag = 1;
			}
		}
		else 
		{
			errore = errore + mess[lingua];
			flag = 1;
		}
	}
	else
	{
		errore = errore + mess[lingua];
		flag = 1;
	}
}
function gestione_errori(lingua)
{
	mess = new Array(2);
	mess[0] = "ATTENZIONE verificare il contenuto dei campi: \n";
	mess[1] = "WARNING! Check inserted data in the following fields: \n";
	mess[2] = "ACHTUNG kontrollieren Sie den Inhalt der Felder \n";
	if (errore != "")
		{
			alert (mess[lingua] + errore);
		}
}

