/***************************************************************************************************
 FUNZIONI DI CONTROLLO SU STRINGHE (funzioni generali)
****************************************************************************************************/
//isLengthOK
// Prende in input una stringa e un numero e verifica che il numero sia uguale alla
// lunghezza della stringa.
function isLengthOK(stringa,numero)
{
	return (stringa.length == numero)
}


// isLetter
// Prende in input un carattere e verifica che sia una lettera 
// Ritorna true se è una lettera; false altrimenti
function isLetter (c)
{ 
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

// isDigit
// Prende in input un carattere e verifica che sia una lettera o una cifra
// Ritorna true se è una cifra; false altrimenti
function isDigit (c)
{
	return ((c >= "0") && (c <= "9"))
}

// isLetterOrDigit: 
// Prende in input un carattere e verifica che sia una lettera o una cifra
// Ritorna true se è una lettera o una cifra; false altrimenti
function isLetterOrDigit (c)
{   
	return (isLetter(c) || isDigit(c))
}	


//verifica che la stringa contenga solo lettere (accentate o no) e numeri.
function StringaAlfaNumerica(stringa)
{
	var i;
	for (i = 0; i < stringa.length; i++)
  {   
		var c = stringa.charAt(i);
		if (!isLetterOrDigit(c) && ("òàèéùì".indexOf(c) == -1) && c != " "  && c != "'")
		{	
			return false;
		}	
	}
  return true;
}


// Cancella da s tutti i caratteri che compaiono nella stringa bag.
function stripCharsInBag (s, bag)
{   
	var i;
  var returnString = "";

  // Si ricercano nella stringa s i caratteri della stringa bag
  // Se un carattere non è in bag, lo si appende alla stringa returnString.
  for (i = 0; i < s.length; i++)
  {   
	  // Verifica che il carattere corrente non sia uno spazio bianco.
    var c = s.charAt(i);
		if (bag.indexOf(c) == -1) {returnString += c;}
  }
  return returnString;
}



// Cancella da s tutti i caratteri che non compaiono nella stringa bag.
function stripCharsNotInBag (s, bag)
{   
	var i;
  var returnString = "";

  // Si ricercano nella stringa s i caratteri della stringa bag
  // Se un carattere non è in bag, lo si appende alla stringa returnString.
  for (i = 0; i < s.length; i++)
  {   
    var c = s.charAt(i);
    if (bag.indexOf(c) != -1) returnString += c;
  }

  return returnString;
}



// Rimuove tutti gli spazi bianchi da s
function stripWhitespace (s)
{   
	return stripCharsInBag (s, " ")
}




// validString:
// La funzione prende in input una stringa e verifica che contenga
// soltano lettere o cifre
// In output restituisce true se la stringa è corretta; false altrimenti
function ValidString (stringa)
{   
	var i;
  for (i = 0; i < stringa.length; i++)
  {   
		var c = stringa.charAt(i);
		if (!isLetterOrDigit(c) && ("òàèéùì".indexOf(c) == -1))
		{
			//alert('It is impossible to use the character \"' + c + '\". It is only possible to use letters and numbers from 0 to 9');
			return false;
		}	
	}
  return true;
}

function StringaVuota (NomeCampo, Stringa)
{   
	var i;
	if (Stringa == "")
	{
		alert('Please, fill in the field for ' + NomeCampo + '.');
		return false;
	}
}

function AlmenoUnoSpazio(stringa)
{   
	var i;
  for (i = 0; i < stringa.length; i++)
  {   
		var c = stringa.charAt(i);
		if (c == " ") 
		{
			return true;
		}	
	}
  return false;
}

function AlmenoNCaratteri(stringa,carattere,n)
{   
	var i;
	var num_occorrenze = 0;
	
  for (i = 0; i < stringa.length; i++)
  {   
		var c = stringa.charAt(i);
		if (c == carattere) 
		{
			num_occorrenze++;
		}	
	}
  if(num_occorrenze >= n)
		return true;
	else	
		return false;
}

// LettereSpazioEApice
// La funzione prende in input una stringa e verifica che contenga soltano lettere, spazi oppure apice
// Ritorna:
//	 - true se la stringa è corretta; 
//	 - false altrimenti

function LettereSpazioEApice (NomeCampo, Stringa)
{   
	var i;
	var HowManyLetter = 0

  for (i = 0; i < Stringa.length; i++)
  {   
		var c = Stringa.charAt(i);
	
		if (isLetter(c))	
			HowManyLetter++;	
		else
		{
			if (c != '\'' & c != ' ')
			{
				alert('The field  ' + NomeCampo + ' can only contain letters, spaces and the prime character (\')');		
				return false;			
			}
		}	
	}
	if (HowManyLetter > 0)	
	{
		//c'e' almeno una lettera...
		return true;
	}
	else
	{
		alert('Please, verify the field for ' + NomeCampo + '.');
		return false;
	}
}


// IsNumber:
// La funzione prende in input una stringa e verifica che sia
// di tipo numerico
// In output restituisce true se la stringa è corretta; false altrimenti
function IsNumber (NomeCampo, Stringa)
{   
	var i;
  for (i = 0; i < Stringa.length; i++)
  {   
		var c = Stringa.charAt(i);
		if (!isDigit(c) && c != '+' && c != ' ') 
		{
			alert('The field ' + NomeCampo + ' can only contain numbers');
			return false;
		}	
	}
  return true;
}

// OnlyNumber:
// La funzione prende in input una stringa e verifica che sia
// di tipo numerico
// In output restituisce true se la stringa è corretta; false altrimenti
function OnlyNumber (NomeCampo, Stringa, campo)
{   
	var i;
	if (Stringa == "")
	{
		alert('Please, fill in the field for ' + NomeCampo);
		return false;
	}
	else
	{
		for (i = 0; i < Stringa.length; i++)
		{   
				var c = Stringa.charAt(i);
				if (!isDigit(c) || c == ' ') 
				{
					alert('Please, verify the field for ' + NomeCampo + '.');
					return false;
				}	
			}
	  return true;
	}
}

// NoApice:
// La funzione prende in input una stringa e verifica che non contenga apici
// output restituisce true se la stringa è corretta; false altrimenti
function NoApice(frm, nomecampo, captioncampo, stringa)
{   
	var i;

  for (i = 0; i < stringa.length; i++)
  {   
		var c = stringa.charAt(i);
		if (c == '\'') 
		{
			alert('The field ' + captioncampo + ' cannot contain primes ( \' )');
			return false;
		}	
	}
  return true;
}

function Conferma(msg)
{
	//var risposta = window.confirm("You are deleting a system’s user. Do you confirm this operation?")
	var risposta = window.confirm(msg)
	return risposta
	
}


// -----------------------------------------------------------------------------------
// prende in input il numero di carta di credito (numero), il mese di scadenza (mese)
// e l'anno di scadenza (anno) e ritorna un messaggio di errore (v_msg)
// -----------------------------------------------------------------------------------
function isCreditCard(numero, mese, anno)
{	
	var data_attuale;
	var v_actual_year;
	var v_actual_month;
	var somma;
	var revs, k, prodotto;
	var v_flag_numero, v_flag_data;
	 
	v_flag_numero = false;
	v_flag_data = false;
	
	data_attuale = new Date();
	
	v_actual_year = data_attuale.getFullYear().toString();	//ritorna l'anno "assoluto"
	v_actual_month = data_attuale.getMonth().toString();		//ritorna il mese (partendo da 0)
	
	mese = mese - 1;
	mese = mese.toString();
	
	if((numero != "") && (mese != "") && (anno != ""))
	{
		// controllo sul numero di carta di credito	
		if(numero.length < 14)
			v_flag_numero = true
		else
		{	
			if(!IsNumber('Numero Carta', numero)) 
				v_flag_numero = true;
			else
			{
				if(numero == 0)
					v_flag_numero = true;
				else
				{
					somma = 0;
					revs = "";
					
					for(k=numero.length;k>=1;k--)
					{
						revs = revs + numero.substring(k-1,k)
					}
					
					for(k=0; k<numero.length; k++)
					{
						prodotto = revs.substring(k,k+1)*(2-((k+1)%2));	
						if(prodotto.toString().length == 2)
						{
							prodotto = parseInt(prodotto.toString().substring(0,1)) + parseInt(prodotto.toString().substring(1,2));
						}						
						somma = somma + parseInt(prodotto);
					}
					if ((somma % 10) != 0)
						v_flag_numero = true;
				}
			}
		}	
		
		// controllo sulla data di scadenza
		if (!(IsNumber('Mese', mese) && IsNumber('Anno', anno))) 
		{
			v_flag_data = true
		}	
		else
		{
			if (mese < 0 || mese > 11) 
				v_flag_data = true
			else
			{
				if(anno < v_actual_year) 
					v_flag_data = true
				else
				{
					if(anno == v_actual_year)
					{
						if(eval(mese) < eval(v_actual_month))  
							v_flag_data = true;
					}
				}
			}		
		}
	}
	
	if(v_flag_numero && v_flag_data)
	{
			//alert("Please, verify the fields for credit card number and expiration date");
			//return false;
			return 1;
	}		
	else
	{
		 if(v_flag_numero && !v_flag_data)
		 {
			 //alert("Please, verify the field for credit card number");
			 //return false;
			 return 2;
		 }
		 else
		 {
		   if((!v_flag_numero) && v_flag_data)
		   {
					//alert("Please, verify the field for credit card expiration date");
					//return false;
					return 3;
			 }	
			 return 0;
			 //return true;
		 }
	}	 		
}		
		


/****************************************************************************************************
 FUNZIONI PER IL CONTROLLO E LA MANIPOLAZIONE DELLE DATE
 ****************************************************************************************************/

/*	Check_Data
		Questa funzione verifica la correttezza di una data.
		Input: giorno, mese, anno
		Output: true se la data è corretta, false altrimenti.		*/

function Check_Data(data)
{

	var anno = data.substring(0,4)			//Restituisce l'anno della data del blocco
	var mese = data.substring(4,6)			//Restituisce il mese della data del blocco
	var giorno = data.substring(6,9)		//Restituisce il giorno della data del blocco

	var giorni_del_mese = new Array(12);
	
	giorni_del_mese[0]= 31;
	giorni_del_mese[1] = 28;
	giorni_del_mese[2] = 31;
	giorni_del_mese[3] = 30;
	giorni_del_mese[4] = 31;	
	giorni_del_mese[5] = 30;	
	giorni_del_mese[6] = 31;	
	giorni_del_mese[7] = 31;	
	giorni_del_mese[8] = 30;	
	giorni_del_mese[9] = 31;
	giorni_del_mese[10] = 30;
	giorni_del_mese[11] = 31;

	if (anno % 4 == 0)
		giorni_del_mese[1] = 29;
		
	if (Number(giorno) != 0) 
	{
		if (Number(giorno) <= giorni_del_mese[Number(mese)-1])
			return true;
		else
			return false;
	}	

	return false
}



// apre finestra popup con file
var clPopPop = null // handle for the pop window
function Pop(szPop,szPWidth,szPHeight) {
        //riceve file, larghezza, altezza
        var szFile
        var szWidth = szPWidth
        var szHeight = szPHeight

        // compone stringa parametri finestra
        var szWinFeatures = "scrollbars=yes"
                  szWinFeatures += ",width="
                  szWinFeatures += szWidth
                  szWinFeatures += ",height="
                  szWinFeatures += szHeight

        var szErrMsg  = "\nIl Vostro Browser non e' stato in grado di aprire un'altra finestra "
                szErrMsg += "\nper visualizzare il documento richiesto.  Vi preghiamo di chiudere"
                szErrMsg += "\ntutte le finestre inutilizzate del browser , e riprovare." 

        if (szPop)  
                szFile = szPop   // open with a given file
        else
                szFile = "/def_pop.htm"  // by default, open the Default Pop file

        // Netscape Navigator allows only 100 windows at a time
        // use the existing one to avoid exceeding the limit
        if (clPopPop) {
                if (!clPopPop.closed)    // if Pop window not closed 
                        clPopPop.location.href = szFile
                else 
                        clPopPop = window.open(szFile, 'Pop', szWinFeatures)
        } else 
                clPopPop = window.open(szFile, 'Pop', szWinFeatures)
        
        // check if new window failed to open
        if (clPopPop) 
                clPopPop.focus() 
        else 
                alert(szErrMsg) 
}

//Verifica che la proprietà value dell'oggetto objEmail contenga un indirizzo Email corretto
function ControlloEmail(ObjEmail)
{ 
	if (stripWhitespace(ObjEmail.value).length > 0) 
	{
//		if(AlmenoNCaratteri(ObjEmail.value,"@",2))
//		{
//			alert("Please, verify the field for email");
//			ObjEmail.focus();
//			return false;
//		}
//		else
//		{
			//var i=new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
			var i=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
			if(!i.test(ObjEmail.value)) 
			{ 
				alert("Please, verify the field for email");
				ObjEmail.focus();
				return false;
			}
			return true;
		//}
	}
	return true;
} 

function ControllaEmail(ObjEmail)
{ 
	var emailLen
	var emailLenNoWhiteSpace
	
	if (stripWhitespace(ObjEmail.value).length > 0) 
	{
		ObjEmail.value = stripWhitespace(ObjEmail.value);
		
		var i=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		
		if(!i.test(ObjEmail.value)) 
		{ 
			alert("Please, verify the field for email");
			ObjEmail.focus();
			return false;
		}
		
		return true;
		
		//if(AlmenoNCaratteri(ObjEmail.value,"@",2))
//		{
//			alert("Please, verify the field for email");
//			ObjEmail.focus();
//			return false;
//		}
//		else
//		{
//			var i=new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
//			if(!i.test(ObjEmail.value)) 
//			{ 
//				alert("Please, verify the field for email");
//				ObjEmail.focus();
//				return false;
//			}
//			//Si verifica che nell'email non ci siano degli spazi
//			emailLen = ObjEmail.value.length;
//			emailLenNoWhiteSpace = stripWhitespace(ObjEmail.value).length;
//			if(emailLen != emailLenNoWhiteSpace)
//			{
//				alert("Please, verify the field for email");
//				ObjEmail.focus();
//				return false;
//			}
//			return true;
//		}
	}
	return true;
}



//Questa funzione è utilizzata per controllare la correttezza (a grandi linee)
//del cognome, del nome, dell'indirizzo, della località.
function ControllaStringaGenerica(ObjStringa, NomeCampo)
{
	var i, lungMin, funz_da_appl;
	var HowManyLetter = 0

	switch (NomeCampo)
	{
		case 'Cognome':
		case 'Cognome dati consegna':
			lungMin = 2;
			break;
		case 'Nome':
		case 'Nome dati consegna':
			lungMin = 3;
			break;
		case 'Indirizzo':
		case 'Indirizzo dati consegna':
			lungMin = 4;
			break;
		case 'Località':
		case 'Località dati consegna':
			lungMin = 2;
			break;
		default:
			lungMin = 0;
			break;
	}
	

	if(stripWhitespace(ObjStringa.value) == "")
	{
		alert("Please, fill in the field for " + NomeCampo + ".");
		ObjStringa.value = ""	//Si eliminano eventuali spazi...
		ObjStringa.focus();
		return false;
	}		
	else
	{
		if(stripWhitespace(ObjStringa.value).length < lungMin)
		{
			alert('Please, verify the field for  ' + NomeCampo + '.');
			ObjStringa.focus();
			return false;
		}
		else
		{
			for (i = 0; i < ObjStringa.value.length; i++)
			{   
				var c = ObjStringa.value.charAt(i);
	
				if (StringaAlfaNumerica(c))	
					HowManyLetter++;	
			}
			if (HowManyLetter > 0)	
			{
				//c'e' almeno una lettera...
				return true;
			}
			else
			{
				alert('Please, verify the field for ' + NomeCampo + '.');
				ObjStringa.focus();
				return false;
			}	
		}
	}
}	
	



function ControllaCAP_RAGGRUP(ObjCAP, strNomeCampo, strObbl)
{
	var i;
	if(stripWhitespace(ObjCAP.value) != "") 
	{	
		for (i = 0; i < ObjCAP.value.length; i++)
		{   
			var c = ObjCAP.value.charAt(i);	
			//if (!isDigit(c))	
			if (!StringaAlfaNumerica(c))	
			{
				alert('Please, verify the field for ' + strNomeCampo + '.')
				ObjCAP.focus();
				return false;
			}	
		}	
		return true;
	}
	else
	{
		if(strObbl=='1')
		{
			alert('Please, verify the field for ' + strNomeCampo + '.')
			return false;
		}
		else
			return true;
	}
}




function CheckPrefETel(NomeCampo, Stringa)
{
	var i;
	if(Stringa.length > 1)
	{
		for (i = 0; i < Stringa.length; i++)
		{   
			var c = Stringa.charAt(i);
			
			if(!(c == "+" || c == "/" || c == "-" || c == " ")) {
				if (!isDigit(c))	
				{
					alert('Please, verify the field for ' + NomeCampo + '.')
					return false;
				}	
			}
		}	
		return true;
	}
	else
	{
		alert('Please, verify the field for  ' + NomeCampo + '.');
		return false;
	}
}


//Questa funzione è utilizzata per controllare la correttezza (a grandi linee)
//del cognome, del nome, dell'indirizzo, della località.
function ControllaRagioneSociale(ObjStringa, NomeCampo)
{
	var i, lungMin, funz_da_appl;
	var HowManyLetter = 0
	
	lungMin = 2;
	
	if(stripWhitespace(ObjStringa.value) == "")
	{
		alert("Please, fill in the field for " + NomeCampo);
		ObjStringa.value = ""	//Si eliminano eventuali spazi...
		ObjStringa.focus();
		return false;
	}		
	else
	{
		if(stripWhitespace(ObjStringa.value).length < lungMin)
		{
			alert('Please, verify the field for ' + NomeCampo + '.');
			ObjStringa.focus();
			return false;
		}
		else
		{
			for (i = 0; i < ObjStringa.value.length; i++)
			{   
				var c = ObjStringa.value.charAt(i);
				
				if (ValidString(c))	
					HowManyLetter++;	
			}
			if (HowManyLetter > 1)	
			{
				//c'e' almeno una lettera...
				return true;
			}
			else
			{
				alert('Please, verify the field for ' + NomeCampo + '.');
				ObjStringa.focus();
				return false;
			}	
		}
	}
}	
	

//Attenzione! il codice fiscale deve essere maiuscolo!
function ControllaCodiceFiscale(objCodfisc)
{
  var s;
  var stringa = objCodfisc.value;
  var CodAziendale = "si";
  
  //Si verifica che il codice fiscale non sia un codice aziendale (lungo 11 crt
  //e completamente numerico)
  for (i = 0; i < stringa.length; i++)
  {   
		var c = stringa.charAt(i);
		if (!isDigit(c)) 
		{
			CodAziendale = "no";
		}	
	}
	
	if(CodAziendale == "no")
	{
		for(s=0,i=0;i<=15;i++)
		{
		  s = parseInt(s) + parseInt(CambiaDispari(objCodfisc.value.substr(i,1)));		
		  i = i + 1;
		}
  
		for(i=1;i<=14;i++)
		{
		  s = parseInt(s) + parseInt(CambiaPari(objCodfisc.value.substr(i,1)));		
		  i = i + 1;
		}  
 
		 s = s % 26;

		if(CheckDigitCodFisc(s) == objCodfisc.value.substr(15,1))
		{
		  return true;
		}    
		else
		{
			alert('Please, verify the field for fiscal code');
			objCodfisc.focus();  
		  return false;
		}    
	}
	else
		return true;	
}

function CheckDigitCodFisc(val)
{
  switch(val)
  {
    case 0: 
        return("A")
    case 1:
        return("B")
    case 2:
        return("C")
    case 3:
        return("D")
    case 4:
        return("E")
    case 5:
        return("F")
    case 6:
        return("G")
    case 7:
        return("H")
    case 8:
        return("I")
    case 9:
        return("J")
    case 10:
        return("K")
    case 11:
        return("L")
    case 12:
        return("M")
    case 13:
        return("N")
    case 14:
        return("O")
    case 15:
        return("P")
    case 16:
        return("Q")
    case 17:
        return("R")
    case 18:
        return("S")
    case 19:
        return("T")
    case 20:
        return("U")
    case 21:
        return("V")
    case 22:
        return("W")
    case 23:
        return("X")
    case 24:
        return("Y")
    case 25:
        return("Z")
  }
}


function ControllaPiva(objIva)
{
  var p1, p2, p3, p4, p5, s, decine, unita;
    
  if(objIva.value.length == 11)
  {
		s = 0;
	
		p1 = objIva.value.substr(1,1);
		p1 = p1 * 2;

		p2 = objIva.value.substr(3, 1);
		p2 = p2 * 2;

		p3 = objIva.value.substr(5, 1);
		p3 = p3 * 2;
		
		p4 = objIva.value.substr(7, 1);
		p4 = p4 * 2;

		p5 = objIva.value.substr(9, 1);
		p5 = p5 * 2;

		if(parseInt(p1) < 9)
		{
			s = parseInt(s) + parseInt(p1);
		}	
		else
		{
			decine = parseInt(p1/10);
			unita = p1 % 10;
			s = parseInt(s) + parseInt(decine) + parseInt(unita);
		}	

		if(parseInt(p2) < 9)
		{
			s = parseInt(s) + parseInt(p2);
		}	
		else
		{
			decine = parseInt(p2/10);
			unita = parseInt(p2 % 10);
			s = parseInt(s) + parseInt(decine) + parseInt(unita);
		}	
		
		if(parseInt(p3) < 9)
		{
			s = parseInt(s) + parseInt(p3);
		}	
		else
		{
			decine = parseInt(p3/10);
			unita = p3 % 10;
			s = parseInt(s) + parseInt(decine) + parseInt(unita);
		}	

		if(parseInt(p4) < 9)
		{
			s = parseInt(s) + parseInt(p4);
		}	
		else
		{
			decine = parseInt(p4/10);
			unita = p4 % 10;
			s = parseInt(s) + parseInt(decine) + parseInt(unita);
		}	
		
		if(parseInt(p5) < 9)
		{
			s = parseInt(s) + parseInt(p5);
		}	
		else
		{
			decine = parseInt(p5/10);
			unita = p5 % 10;
			s = parseInt(s) + parseInt(decine) + parseInt(unita);
		}							

		s = s + parseInt(objIva.value.substr(0, 1));
		s = s + parseInt(objIva.value.substr(2, 1));
		s = s + parseInt(objIva.value.substr(4, 1));		
		s = s + parseInt(objIva.value.substr(6, 1));		
		s = s + parseInt(objIva.value.substr(8, 1));				

    
		if((parseInt(s%10) == 0) && (parseInt(objIva.value%10) == 0))
		{
			return(true);
		}	
		else
		{
			if(parseInt(10 - parseInt(s%10)) == parseInt(objIva.value%10))
				return(true);
			else
			{
				alert('Please, verify the field for VAT number');
				objIva.focus();  			
				return(false);
			}	
		}
	}
	else
	{
		if(objIva.value.length > 0)
		{
			alert('Please, verify the field for VAT number');
			objIva.focus();  			
			return(false);
		}	
	}		
}

function CambiaPari(val)
{
	switch (val)
	{
		case 'A':
		  return(0)
		case '0':
		  return(0)
		case 'B':
		  return(1)
		case '1':
		  return(1)
		case 'C':
		  return(2)
		case '2':
		  return(2)
		case 'D':
		  return(3)
		case '3':
		  return(3)
		case 'E':
		  return(4)
		case '4':
		  return(4)
		case 'F':
		  return(5)
		case '5':
		  return(5)
		case 'G':
		  return(6)
		case '6':
		  return(6)
		case 'H':
		  return(7)
		case 'I':
		  return(8)
		case 'J':
		  return(9)
		case '7':
		  return(7)
		case '8':
		  return(8)
		case '9':
		  return(9)
		case 'K':
		  return(10)
		case 'L':
		  return(11)
		case 'M':
		  return(12)
		case 'N':
		  return(13)
		case 'O':
		  return(14)
		case 'P':
		  return(15)
		case 'Q':
		  return(16)
		case 'R':
		  return(17)
		case 'S':
		  return(18)
		case 'T':
		  return(19)
		case 'U':
		  return(20)
		case 'V':
		  return(21)
		case 'W':
		  return(22)
		case 'X':
		  return(23)
		case 'Y':
		  return(24)
		case 'Z':
		  return(25)
	}
}

function CambiaDispari(val)
{
	switch (val)
	{
		case 'A':
		  return(1)
		case 'B':
		  return(0)
		case 'C':
		  return(5)
		case 'D':
		  return(7)
		case 'E':
		  return(9)
		case 'F':
		  return(13)
		case 'G':
		  return(15)
		case 'H':
		  return(17)
		case 'I':
		  return(19)
		case 'J':
		  return(21)
		case '0':
		  return(1)
		case '1':
		  return(0)
		case '2':
		  return(5)
		case '3':
		  return(7)
		case '4':
		  return(9)
		case '5':
		  return(13)
		case '6':
		  return(15)
		case '7':
		  return(17)
		case '8':
		  return(19)
		case '9':
		  return(21)
		case 'K':
		  return(2)
		case 'L':
		  return(4)
		case 'M':
		  return(18)
		case 'N':
		  return(20)
		case 'O':
		  return(11)
		case 'P':
		  return(3)
		case 'Q':
		  return(6)
		case 'R':
		  return(8)
		case 'S':
		  return(12)
		case 'T':
		  return(14)
		case 'U':
		  return(16)
		case 'V':
		  return(10)
		case 'W':
		  return(22)
		case 'X':
		  return(25)
		case 'Y':
		  return(24)
		case 'Z':
		  return(23)
	}
}


function ControllaPwd(Pwd, NomeCampo)
{
	if(stripWhitespace(Pwd.value) == "")
	{
		alert("Please, fill in the field for " + NomeCampo);
		Pwd.value = ""
		Pwd.focus();
		return false;
	}
	else
	{
		if((stripWhitespace(Pwd.value).length < 5) || 
			 (stripWhitespace(Pwd.value).length > 10))
		{
			alert('Please, verify the field for ' + NomeCampo + '.');
			Pwd.focus();
			return false;
		}
		return true;
	}	
}	

function CheckFasceOrarie(objDalleH, objDalleM, objAlleH, objAlleM)
{
	if (objDalleH.options[objDalleH.selectedIndex].text != "HH" || objDalleM.options[objDalleM.selectedIndex].text != "MM" ||
			objAlleH.options[objAlleH.selectedIndex].text != "HH" || objAlleM.options[objAlleM.selectedIndex].text != "MM")
	{
		ora_partenza_disp = objDalleH.options[objDalleH.selectedIndex].text + objDalleM.options[objDalleM.selectedIndex].text;
		ora_arrivo_disp = objAlleH.options[objAlleH.selectedIndex].text + objAlleM.options[objAlleM.selectedIndex].text;						

		v_msg = ""

		if ((objDalleH.options[objDalleH.selectedIndex].text == "HH" && objDalleM.options[objDalleM.selectedIndex].text != "MM") ||
				(objDalleH.options[objDalleH.selectedIndex].text != "HH" && objDalleM.options[objDalleM.selectedIndex].text == "MM") ||
				(objAlleH.options[objAlleH.selectedIndex].text == "HH" && objAlleM.options[objAlleM.selectedIndex].text != "MM") ||
				(objAlleH.options[objAlleH.selectedIndex].text != "HH" && objAlleM.options[objAlleM.selectedIndex].text == "MM"))
			{
				alert("Please, verify the field for time band and telephone availability.");
				objDalleH.focus();
				return false;
			}
		else
		{
			if ((ora_partenza_disp != "HHMM") && (ora_arrivo_disp != "HHMM"))
			{
				if (ora_partenza_disp > ora_arrivo_disp)
				{
					alert("Please, verify the field for time band and telephone availability");
					objDalleH.focus();
					return false;   							
				}
//				else
//				{
					//if(DiffTraOre(objDalleH.options[objDalleH.selectedIndex].text,objDalleM.options[objDalleM.selectedIndex].text,objAlleH.options[objAlleH.selectedIndex].text,objAlleM.options[objAlleM.selectedIndex].text))
					//	return true;	
					//else	
					//{
					//	alert("Please, verify the field for time band and telephone availability");					
					//	objDalleH.focus();
					//	return false;	
					//}	
//				}	
			}	
			else	
			{
				if ((ora_partenza_disp == "HHMM") && (ora_arrivo_disp != "HHMM"))
				{
					alert("Please, verify the field for time band and telephone availability");
					objDalleH.focus();
					return false;   							
				}	
				else
				{
					alert("Please, verify the field for time band and telephone availability");
					objAlleH.focus();
					return false;   							
				}	
			}			
		}
	}	
	return true
}	

function SpostaFascia(objDalleH1, objDalleM1, objAlleH1, objAlleM1, objDalleH2, objDalleM2, objAlleH2, objAlleM2)
{
	
	Fascia1_Dalle = objDalleH1.options[objDalleH1.selectedIndex].text + ":" + objDalleM1.options[objDalleM1.selectedIndex].text;
	Fascia1_Alle =  objAlleH1.options[objAlleH1.selectedIndex].text + ":" + objAlleM1.options[objAlleM1.selectedIndex].text;						
	Fascia2_Dalle = objDalleH2.options[objDalleH2.selectedIndex].text + ":" + objDalleM2.options[objDalleM2.selectedIndex].text;
	Fascia2_Alle =  objAlleH2.options[objAlleH2.selectedIndex].text + ":" + objAlleM2.options[objAlleM2.selectedIndex].text;						
	
	if((Fascia1_Dalle == "HH:MM") && (Fascia1_Alle == "HH:MM") && (Fascia2_Dalle != "HH:MM") && (Fascia2_Alle != "HH:MM"))
	{
		objDalleH1.selectedIndex = objDalleH2.selectedIndex
		objDalleM1.selectedIndex = objDalleM2.selectedIndex
		objAlleH1.selectedIndex = objAlleH2.selectedIndex
		objAlleM1.selectedIndex = objAlleM2.selectedIndex
		
		objDalleH2.selectedIndex = 0
		objDalleM2.selectedIndex = 0
		objAlleH2.selectedIndex = 0
		objAlleM2.selectedIndex = 0
	}	
	return true;		
}

function SpostaTelefono(pref1, num1, pref2, num2)
{
	if((pref1.value == "") && (num1.value == "") && (pref2.value != "") && (num2.value != ""))
	{
		pref1.value = pref2.value
		num1.value = num2.value
		pref2.value = ""
		num2.value = ""
		
	}
	return true;
}


//Verifica che tra gli orari passati come parametri, vi siano almeno diffRich minuti
function DiffTraOre(hh,mm,hh1,mm1)
{
	var diff;
	
	var diffRich = 30;	
	
	if(hh == hh1)
		diff = Math.abs(mm-mm1)
	else
	{
		if(Math.abs(hh-hh1)<=1)
			diff = (parseInt(60) - parseInt(mm)) + parseInt(mm1)	
		else
			return true;
	}

	if(diff >= diffRich)
		return true;
	else
		return false;		
}


function TrimVJS(strtmp)
{
	var j=0;
	for (i = 0; i < strtmp.length; i++)
	{   
		var c = strtmp.charAt(i);
		if (c == ' ') 
		{
			j++;
		}
	}
	if(strtmp.length == j)
		return "";
	else
		return strtmp;
	
}

function ControlloPagamentoConsegna(frmName) {
	flag = 0;
	if((frmName.f_TIPO_PAGAMENTO.selectedIndex == 0) && (frmName.f_TIPO_CONSEGNA.selectedIndex == 0)) {
		alert("Please, select the shipping method and the method of payment");
		flag = 1;
	}
	
	if((frmName.f_TIPO_PAGAMENTO.selectedIndex == 0) && (flag == 0)) {
		alert("Please, select the shipping method and/or the method of payment");
		flag = 1;
	}
	if((frmName.f_TIPO_CONSEGNA.selectedIndex == 0) && (flag == 0)) {
		alert("Please, select the shipping method and/or the method of payment");
		flag = 1;
	}
	if(flag == 1)
		return false;
	else	
	{
		if((frmName.f_TIPO_PAGAMENTO.options[frmName.f_TIPO_PAGAMENTO.selectedIndex].value == "C  ") && (frmName.f_TIPO_CONSEGNA.options[frmName.f_TIPO_CONSEGNA.selectedIndex].value == "SO" || frmName.f_TIPO_CONSEGNA.options[frmName.f_TIPO_CONSEGNA.selectedIndex].value == "SR")) 
			return window.confirm("Attention, payment at delivery has been selected for a present. This option is possible exclusively when the present is ordered together with a personal purchase; at the delivery time of the personal purchase all expenses will be paid off.");
		else
			return true;
	}	
}

function ControlloPagamentoConsegnaOriginale(frmName) {
	if((frmName.f_TIPO_PAGAMENTO.selectedIndex == 0) && (frmName.f_TIPO_CONSEGNA.selectedIndex == 0)) {
		alert("Please, select the shipping method and the method of payment");
		return false;
	}
	
	if(frmName.f_TIPO_PAGAMENTO.selectedIndex == 0) {
		alert("Please, select the shipping method and the method of payment");
		return false;
	}
	if(frmName.f_TIPO_CONSEGNA.selectedIndex == 0) {
		alert("Please, select the shipping method and the method of payment");
		return false;
	}
	
	return true;
}



// gestisce tendina per cambio di pagina
function changePage(theform) 
{
	// riceve opzione selezionata
	num = theform.f_ordina_altro.selectedIndex;

	//riceve il value dell'opzione selezionata
	pagina = theform.f_ordina_altro[num].value;

	//se qualcosa e' selezionato go to
	if (pagina!="nothing") 
	{  
		top.location.href=pagina;
	}
}

function ModificaQtaPage(theform) 
{
	theform.f_modifica_qta.value = "modificata quantita";
	theform.submit();
}


