/* ************** funzioni di validazione Forms **************************

include : formValidation.js

FUNZIONI DI USO COMUNE
* function isNotExpired(mese, anno, hideAlert)
* function forwardTo(strurl, querystring)
* function isLeapYear( yyy )
* function getDaysPerMonth( yyy )
* function isYMDValid( ddd, mmm, yyy )
* function decodeHtml(htmlString)
* function partitaIVA(sz_Codice)
* function fiscale(codice,CPI)
* function LuhnCheck(str)

BASKET
* function submitBasket(actionName,giftForm,parName)

GADGET
* function submitGadget(theForm)

DATI FATT & SPEDIZIONE
* function submitFattData(theForm)
* function submitSpedData(theForm)
* function submitShippingData(theForm)

ACQUISTO PRODOTTI
* function submitPaymentMode(theForm)

PROFILO DI ACQUISTO
* function submitProfiloAcquisto(theForm, forwardName)

OPZIONE REGALO
* function submitOpzioneRegalo(theForm)
* function modifyTestoRegalo( textField, charsLeft) --- per decrementare il testo del messaggio

ABILITAZIONE TR+
* function submitTRAbilitazione(theForm,number)
* function submitTRPayment(theForm)
* function submitSetPromoTR(theForm)

PRODOTTO TELEFONO
* function submitPhoneProduct(theForm)

PRODOTTO RICARICA
* function submitRechargeProduct(theForm,productID)

DATI DI ATTIVAZIONE SIM
type user: 0->individuo;1->libero prof.; 2->azienda
typeProduct: S->SIM; R->Ricaricabile
* function submitSIMOptionData(theForm,typeUser,typeProduct)

DATI PAGAMENTO RICARICA
* function submitPaymentRecharge(theForm)

DILLO A UN AMICO MAIL
* function controllaFormMail(theForm)
*********************************************************************** */

//---------------- Funzioni di uso comune --------------------------------

var data = new Date();
var mm, aaaa;
mm = data.getMonth() + 1;
aaaa = data.getYear();
cvvCheckEnabled=false;

function isNotExpired(mese, anno, hideAlert){
	
	//Nel caso che la jsp chiamante del controllo ? la enableTRNew
	//aggiunge all'anno corrente 1900 a causa di un diverso passaggio di parametri rispetto agli acquisti

	var currData = new Date();	
	var currY = currData.getYear();
	var currMonth = currData.getMonth() + 1;
	var paramAnno = anno;
	
	//normalizzazione anno (parametro e anno corrente)
	if(paramAnno<1000){
		paramAnno = paramAnno+1900;
	}
	if(currY<1000){
		currY = currY+1900;
	}

	errors = false;
 	if (paramAnno < currY){
	 	errors = true;
  	}
  	else {
	  	if (paramAnno == currY){
	  		if (mese < currMonth){
	 			errors = true;
  			}
	  		//errors = true;
  		}
	}
  	
  	
  	
  	if(errors){
	  	if(hideAlert){
			return "err_data";
  		}else{
	  		alert("Il mese e l\'anno di scadenza della Carta di Credito non possono essere anteriori alla data di oggi");
      		return false;
  		}
  	}else{
		if(hideAlert){
			return "";
  		}else{
  			return true;
		}
  	}
}

function forwardTo(strurl, querystring){

  var forward = strurl + querystring;
  //alert('forwardTO['+forward+']');

  window.location = forward;
}

function isLeapYear( yyy )
{
	yy=parseInt((yyy==""?0:yyy));
	var isLeap = false;
	if ( 0 == yy % 4 )
	{
		if ( 0 == yy % 100 )
		{
			if ( 0 == yy % 400 )
				isLeap = true;
		}
		else isLeap = true;
	}
	return isLeap;
}

function getDaysPerMonth( yyy )
{
	yy=parseInt((yyy==""?0:yyy));
	var ml = new Array(12)
	ml[0] = ml[2] = ml[4] = ml[6] = ml[7] = ml[9] = ml[11] = 31;	// Jan, Mar, May, Jul, Aug, Oct, Dec
	ml[3] = ml[5] = ml[8] = ml[10] = 30;				// Apr, Jun, Sep, Nov
	if ( isLeapYear( yy ) )	 					// Feb
		ml[1] = 29;
	else
	    	ml[1] = 28;
	return ml;
}

function isYMDValid( ddd, mmm, yyy )
{
	yy=parseInt((yyy==""?0:yyy));
	mm=parseInt((mmm==""?0:mmm));
	dd=parseInt((ddd==""?0:ddd));
	if (mm < 1 || mm > 12) return false;
	if (yy == 0) return false;

	ml = getDaysPerMonth( yy );
	if ( dd < 1 || dd > ml[mm-1] ) return false;

	return true;
}

/*
 * Trasforma le codifiche html delle lettere accentate in caratteri accentati unicode
 */
function decodeHtml(htmlString)
{
  htmlString = htmlString.replace("&amp;ugrave;","&#249;");
  htmlString = htmlString.replace("&amp;egrave;","&#232;");
  htmlString = htmlString.replace("&amp;agrave;","&#224;");
  htmlString = htmlString.replace("&amp;eacute;","&#233;");
  htmlString = htmlString.replace("&amp;igrave;","&#236;");
  htmlString = htmlString.replace("&amp;ograve;","&#242;");
  
  var str = htmlString;
  
  var re=/&#\d+;/g;
  arr = str.match(re);
  if (arr)
  {
    for (i=0;i<arr.length;i++)
    {
      var currCode = arr[i];
      var currnum = currCode.substr(2,currCode.length-3); 
      str = str.replace(currCode, String.fromCharCode(currnum));
    }
  }
  
  return str;
}

function partitaIVA(sz_Codice)
{
	if( sz_Codice == '' )
		{
		  return false;
		}
	if( sz_Codice.length != 11 )
		{
		return false;
		}

	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( sz_Codice.charAt(i) ) == -1 )
			{
			return false;
			}
	}
	s = 0;
	for( i = 0; i <= 9; i += 2 )
		s += sz_Codice.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ){
		c = 2*( sz_Codice.charCodeAt(i) - '0'.charCodeAt(0) );
		if( c > 9 )  c = c - 9;
		s += c;
	}
	if( ( 10 - s%10 )%10 != sz_Codice.charCodeAt(10) - '0'.charCodeAt(0) )
		{
		return false;
		}

	return true;
}

function fiscale(codice,CPI)
{
	if (CPI=="yes")
	{
		//---- Controllo Partita Iva ----
		if (codice.length <16 && codice.length !=11 )
			return false;

		if (codice.length ==11 && caratteri(codice,"1234567890")==false)
			return false;
	}
	if (CPI=="no" && codice.length <16)
		return false;
	//---- Controllo Codice Fiscale
	var SolProbNet477;
	var codfis=/[a-zA-Z]{6}\d\d[a-zA-Z]\d\d[a-zA-Z]\d\w\w[a-zA-Z]/g;
	if (codice.length==16)
	{
		//---- Controllo Formale ----
		if (!(codfis.test(codice)))
			return false;

		//---- Controllo Ultima Lettera  ----
		var nAppoNum=0;
		var nTempNum=0;
		var i=0;
		var myStringa= "B1A0KKPPLLC2QQD3RRE4VVOOSSF5TTG6UUH7MMI8NNJ9WWZZYYXX";
		var myStringa1="A0B1C2D3E4F5G6H7I8J9KKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ";
		var myStringa2="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		var szLastChar=codice.substr(15,1);
		var szTempCodfisc=codice.substr(0,15);
		var strRitorno;
		szLastChar=szLastChar.toUpperCase();
		szTempCodfisc=szTempCodfisc.toUpperCase();
		while(i<15)
		{
			//---- Calcolo Dispari ----
			nAppoNum=myStringa.indexOf(szTempCodfisc.substr(i,1));
			modApp=nAppoNum % 2;
			if ( modApp==0)
				nTempNum = nTempNum  + (nAppoNum)/2;
			else
				nTempNum = nTempNum  + (nAppoNum-1)/2;
			i=i+1;

			//---- Calcolo Pari ----
			if (i<15)
			{
				nAppoNum=myStringa1.indexOf(szTempCodfisc.substr(i,1));
				modApp=nAppoNum % 2;
				if ( modApp==0)
					nTempNum = nTempNum  + (nAppoNum)/2;
				else
					nTempNum = nTempNum  + (nAppoNum-1)/2;
				i=i+1;
			}
		}
		nTempNum=(nTempNum % 26);
		strRitorno=(myStringa2.substr(nTempNum,1) == szLastChar)
		if (!(strRitorno))
		{
			SolProbNet477=codfis.test(codice);
			return false;
		}
	}
	SolProbNet477=codfis.test(codice);
	return true;
}

// Algoritmo di Luhn per carta di credito
function LuhnCheck(str)
{
  var result = true;

  var sum = 0;
  var mul = 1;
  var strLen = str.length;

  for (i = 0; i < strLen; i++)
  {
    var digit = str.substring(strLen-i-1,strLen-i);
    var tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
  if ((sum % 10) != 0)
    result = false;

  return result;
}

// ----- funzioni utility

function selectComboValue(field, value)
{
	var boxLength = field.length;
	
	for (var i=0;i<boxLength;i++)
		{
			if (i==0)
				{
					field.options[i].selected = true;
				}
			if (field.options[i].value==value || (field.options[i].value=="" && value=="null"))
				{
					field.options[i].selected = true;
					return true;
				}
		} 	
	return false;	
}

function submitForm(theForm,actionName)
{
	theForm.action = actionName;
	theForm.submit();
}

function checkRadioField(theField,theValue)
{
  var rechargeLength = theField.length;
  if (isNaN(rechargeLength))
   {
          if(theField.value==theValue)
               theField.checked = true;
   }
  else
      for (var cont=0;cont<rechargeLength ;cont++)
        {
          if (theField[cont].value==theValue)
                theField[cont].checked = true;
        }
}

function isAnyRadioChecked(theField)
{
  var isChecked = false;
  var rechargeLength = theField.length;
  if (isNaN(rechargeLength))
   {
   	return theField.checked;
   }
  else
      for (var cont=0;cont<rechargeLength ;cont++)
        {
          if (theField[cont].checked)
              isChecked = true;
        }
  return isChecked;      
}

function isValidRadioChecked(theField, theValue)
{
  var isSelected = false;
  var rechargeLength = theField.length;
  if (isNaN(rechargeLength))
   {
   	return (theField.value == theValue);
   }
  else
      for (var cont=0;cont<rechargeLength ;cont++)
        {
          if (theField[cont].value == theValue)
              isSelected = true;
        }
  
  return isSelected;
        
}

// ****************************************************************************************

// Patch form di selezione carta regalo
function selectGift()
{
 // Do nothing
}

// Annulla un campo
var isFirstCDC = true;
function clearCDCField(theForm)
{
	
	var cardn=theForm.elements["ccForm.creditCardInfo.cardNo"].value.indexOf("********");
 if (isFirstCDC && cardn!=-1)
 	{
	 alert("Attenzione: il campo Carta di Credito e' stato modificato e andra' re-inserito completamente.");
	 isFirstCDC = false;

 	 theForm.elements["ccForm.creditCardInfo.cardType"].selectedIndex = 0;
    theForm.elements["ccForm.creditCardInfo.cardNo"].value = "";
    theForm.elements["ccForm.creditCardInfo.expireMonth"].selectedIndex = 0;
    theForm.elements["ccForm.creditCardInfo.expireYear"].selectedIndex = 0;
    theForm.elements["ccForm.creditCardInfo.cardOwner"].value = "";
    theForm.elements["ccForm.creditCardInfo.cardCVV2"].value = "";
    //theForm.elements["ccForm.creditCardInfo.fiscalCode"].value = "";
	 
 	}
	
}

//clear PIN and CDC fields for the enable-modify tr jsp when edit favourite number
function clearPINCDCField(theForm)
{
		theForm.elements["trForm.trModel.omnipayInfo.omnipayIVRpin"].value = "";
		theForm.elements["trForm.confirmOmnipayIVRpin"].value = "";
 		theForm.elements["ccForm.creditCardInfo.cardType"].selectedIndex = 0;
    	theForm.elements["ccForm.creditCardInfo.cardNo"].value = "";
    	theForm.elements["ccForm.creditCardInfo.expireMonth"].selectedIndex = 0;
    	theForm.elements["ccForm.creditCardInfo.expireYear"].selectedIndex = 0;
    	theForm.elements["ccForm.creditCardInfo.cardOwner"].value = "";
    	theForm.elements["ccForm.creditCardInfo.cardCVV2"].value = "";
    	//theForm.elements["ccForm.creditCardInfo.fiscalCode"].value = "";
}

// Verifica del numero di controllo
function verifyCVV(cardType,cvv){

var cards = "visa|3,mastercard|3,amex|4,diners|3,aura|3";

var c = cards.split(",");
var num=-1;
for(i=0;i<c.length;i++){
var n = c[i].split("|");
		if(cardType==n[0]){
				num=n[1];
		}
}

if(cvv.length!=num&&num!=-1&&cvvCheckEnabled){
return false;
}else{
return true;
}

}

// Verifica del numero della carta di credito
function verifyCard(numCard,typeCard,hideAlert)
{
/*
 1) Il numero di CDC deve essere un numerico di almeno 13 cifre.
 2) La prima cifra di un numero di carta Visa deve essere 4.
 3) La prima cifra di un numero di carta Mastercard deve essere 5, la seconda cifra deve essere > 0 e < 6.
 4) La prima cifra di un numero di carta Diners deve essere 3 la seconda (0,6,8,9)
 5) La prima cifra di un numero di carta Aura deve essere 6.
 7) Il numero di carta Amex deve essere di 15 cifre e cominciare per 37.
*/

  if (isFirstCDC)
  	{
	  var axtPattern = "*******************";
	  if (numCard.length>=13 && numCard.indexOf("*")>=0)
	  	{
	  	  var axtNumCard = (""+numCard).substring(0,(""+numCard).length-4);
		  // var axtNumCard = (""+numCard).substring(0,(""+numCard).lastIndexOf("*")-1);
		  if (axtPattern.indexOf(axtNumCard)==0)  // Non sono stati cambiati
			{
				if(hideAlert){
					return "";
				}else{
					return true;
				}
			  
		  	}
	  	}
  	}
	
	
  if (numCard==""){
    if(hideAlert){
		return "err_cdc_not_set";
	}else{
		alert("Verifica le seguenti informazioni: Numero della Carta di Credito");
    	return false;
	}
  }
  if ((""+numCard).length<13 || isNaN(numCard) ||!LuhnCheck(numCard)){
	if(hideAlert){
		return "err_cdc";
	}else{
		alert("Il numero della Carta di Credito inserito non e' corretto.");
    	return false;
	}
  }
  if (typeCard=="visa" && (""+numCard).substring(0,1)!="4"){
	if(hideAlert){
		return "err_cdc_type";
	}else{
		alert("Il numero della Carta di Credito inserito non corrisponde al tipo di Carta selezionato.");
    	return false;
	}  
  }
  if (typeCard=="mastercard" && (""+numCard).substring(0,1)!="5"){
    if(hideAlert){
		return "err_cdc_type";
	}else{
		alert("Il numero della Carta di Credito inserito non corrisponde al tipo di Carta selezionato.");
    	return false;
	}
  }
  if (typeCard=="diners" && ((""+numCard).substring(0,1)!="3" || "0689".indexOf((""+numCard).substring(1,2))==-1)){
    if(hideAlert){
		return "err_cdc_type";
	}else{
		alert("Il numero della Carta di Credito inserito non corrisponde al tipo di Carta selezionato.");
    return false;
	}
  }
  if ((typeCard=="americanexpress" || typeCard=="amex") && ((""+numCard).length!=15 || (""+numCard).substring(0,2)!="37")){
    if(hideAlert){
		return "err_cdc_type";
	}else{
		alert("Il numero della Carta di Credito inserito non corrisponde al tipo di Carta selezionato.");
    	return false;
	}
  }
  if (typeCard=="aura" && ((""+numCard).substring(0,1)!="6" || (""+numCard).length!=19)){
    if(hideAlert){
		return "err_cdc_type";
	}else{
		alert("Il numero della Carta di Credito inserito non corrisponde al tipo di Carta selezionato.");
    	return false;
	}
  }

  if(hideAlert){
		return "";
	}else{
    	return true;
	}

}

//verifica che il nome e cognome contengano solo caratteri alfabetici
function checkOwner(code){
	for (var i=0;i<code.length;i++){
         if ((code.charCodeAt(i)<65 || code.charCodeAt(i)>90) && 
         	(code.charCodeAt(i)<97 || code.charCodeAt(i)>122) && 
         	(code.charCodeAt(i)!=32) && (code.charCodeAt(i) != 39) && (code.charCodeAt(i) != 46))
         	{   
	            return false;
         	}
        }
	
	return true;
}

// consente di inserire solo caratteri alfabatici, apostrofati e un insieme di caratteri accentati
function checkRRField(field){
	for (var i=0;i<field.length;i++){
         if ((field.charCodeAt(i)<48 || field.charCodeAt(i)>57) &&
            (field.charCodeAt(i)<65 || field.charCodeAt(i)>90) && 
         	(field.charCodeAt(i)<97 || field.charCodeAt(i)>122) && 
         	(field.charCodeAt(i)!=32) && (field.charCodeAt(i) != 39) && (field.charCodeAt(i) != 46) &&
         	(field.charCodeAt(i)!=192) && (field.charCodeAt(i) != 193) && (field.charCodeAt(i) != 200) &&
         	(field.charCodeAt(i)!=201) && (field.charCodeAt(i) != 204) && (field.charCodeAt(i) != 205) &&
         	(field.charCodeAt(i)!=210) && (field.charCodeAt(i) != 211) && (field.charCodeAt(i) != 217) &&
         	(field.charCodeAt(i)!=218) && (field.charCodeAt(i) != 224) && (field.charCodeAt(i) != 225) &&
         	(field.charCodeAt(i)!=232) && (field.charCodeAt(i) != 233) && (field.charCodeAt(i) != 236) &&
         	(field.charCodeAt(i)!=237) && (field.charCodeAt(i) != 242) && (field.charCodeAt(i) != 243) &&
         	(field.charCodeAt(i)!=249) && (field.charCodeAt(i) != 250) && (field.charCodeAt(i) != 38))
         	{   
	            return false;
         	}
        }
	
	return true;
}

// consente di inserire solo caratteri alfabatici, apostrofati e un insieme di caratteri accentati
// rispetto alla checkRRField2 consente anche il trattino (-)
function checkRRField2(field){
	for (var i=0;i<field.length;i++){
         if ((field.charCodeAt(i)<48 || field.charCodeAt(i)>57) &&
            (field.charCodeAt(i)<65 || field.charCodeAt(i)>90) && 
         	(field.charCodeAt(i)<97 || field.charCodeAt(i)>122) && (field.charCodeAt(i)!=32)
         	&& (field.charCodeAt(i) != 39) && (field.charCodeAt(i) != 45) && (field.charCodeAt(i) != 46) &&
         	(field.charCodeAt(i)!=192) && (field.charCodeAt(i) != 193) && (field.charCodeAt(i) != 200) &&
         	(field.charCodeAt(i)!=201) && (field.charCodeAt(i) != 204) && (field.charCodeAt(i) != 205) &&
         	(field.charCodeAt(i)!=210) && (field.charCodeAt(i) != 211) && (field.charCodeAt(i) != 217) &&
         	(field.charCodeAt(i)!=218) && (field.charCodeAt(i) != 224) && (field.charCodeAt(i) != 225) &&
         	(field.charCodeAt(i)!=232) && (field.charCodeAt(i) != 233) && (field.charCodeAt(i) != 236) &&
         	(field.charCodeAt(i)!=237) && (field.charCodeAt(i) != 242) && (field.charCodeAt(i) != 243) &&
         	(field.charCodeAt(i)!=249) && (field.charCodeAt(i) != 250) && (field.charCodeAt(i) != 38))
         	{   
	            return false;
         	}
        }
	
	return true;
}

//verifica che l'indirizzo contenga solo caratteri alfanumerici
function checkRRAddress(field){
	for (var i=0;i<field.length;i++){
         if ( (field.charCodeAt(i)<48 || field.charCodeAt(i)>57) &&
         	(field.charCodeAt(i)<65 || field.charCodeAt(i)>90) && 
         	(field.charCodeAt(i)<97 || field.charCodeAt(i)>122) && 
         	(field.charCodeAt(i)!=32) && (field.charCodeAt(i) != 39) && (field.charCodeAt(i) != 46) &&
         	(field.charCodeAt(i) != 92)&& (field.charCodeAt(i) != 47) &&
         	(field.charCodeAt(i)!=192) && (field.charCodeAt(i) != 193) && (field.charCodeAt(i) != 200) &&
         	(field.charCodeAt(i)!=201) && (field.charCodeAt(i) != 204) && (field.charCodeAt(i) != 205) &&
         	(field.charCodeAt(i)!=210) && (field.charCodeAt(i) != 211) && (field.charCodeAt(i) != 217) &&
         	(field.charCodeAt(i)!=218) && (field.charCodeAt(i) != 224) && (field.charCodeAt(i) != 225) &&
         	(field.charCodeAt(i)!=232) && (field.charCodeAt(i) != 233) && (field.charCodeAt(i) != 236) &&
         	(field.charCodeAt(i)!=237) && (field.charCodeAt(i) != 242) && (field.charCodeAt(i) != 243) &&
         	(field.charCodeAt(i)!=249) && (field.charCodeAt(i) != 250) && (field.charCodeAt(i) != 38))
         	{
	            return false;
         	}
        }
	
	return true;
}

//------ Validazione Form Basket ---------------------------------

function submitBasket(actionName,giftForm,parName)
{
	var okRegalo = giftForm.elements["shop.gift_option"].checked;

	var strurl = window.location.href;
	strurl = strurl.substring(0,strurl.lastIndexOf("/")+1) + actionName +"?tabName=SHOP"+ (okRegalo ? "&"+parName+"=y" : "");
	top.location = strurl;
}

function submitQuantity(theForm)
{
	/*if(allElements > 1){
		var q = document.forms["updateProductQuantity"][i].elements["shop.product_quantity"].value;
		if(isNaN(q)){
			alert("Inserire solo valori numerici");
			return false;
		}
	}
	else{
		var q = document.updateProductQuantity.elements["shop.product_quantity"].value;
			if(isNaN(q)){
				alert("Inserire solo valori numerici");
				return false;
			}
	}*/
	var q = theForm.elements["shop.product_quantity"].value;
	
	//controllo inserimento dati corretti: 0 < q < 100
	
	if(isNaN(q)){
		alert("Inserire solo valori numerici");
		return false;
	}else if(q < 0 || q >= 100){
		alert("Quantita' non ammessa");
		return false;
	}else{
		return true;
	}
}

//------ Validazione disabilitazione a TR+ ---------------------------------
function checkDisableTR(url)
{
  	  if (!confirm("Confermi di voler essere disabilitato a Telericarica Piu' ?"))
  	  		{
	  	  	 return false;
	  	  	} 
  	  else
  	  		{
  	  			var strurl = window.location.href;
				strurl = strurl.substring(0,strurl.lastIndexOf("/")) +"/"+ url;  	
				top.location = strurl;
  	  		}
}

//------ Validazione Form Gadget ---------------------------------
function selectGadget(theFormField,currentRecord)
{
  var gadgetLength = theFormField.length;
  if (isNaN(gadgetLength))
   {
		theFormField.checked=true;
   }
  else
  	{
		theFormField[currentRecord].checked=true;  	
  	}
  
  return true;  
}

function submitGadget(theForm)
{
  var gadgetChecked = false;
  var gadgetLength = theForm.elements["shop.product_Id"].length;
  if (isNaN(gadgetLength))
   {
          if(theForm.elements["shop.product_Id"].checked)
               gadgetChecked = true;
   }
  else
      for (var cont=0;cont<gadgetLength;cont++)
        {
          if (theForm.elements["shop.product_Id"][cont].checked)
                gadgetChecked = true;
        }
  if (!gadgetChecked)
        {
          if (!confirm("Ricordati di scegliere il tuo omaggio oppure clicca sul tasto \"Continua\"."))
                return false;
        }

  return true;
}

//----- Validazione form dati di fatturazione

function submitFattData(theForm)
{
  var nomeCognome = theForm.elements["shipment.invoicingName"].value;
  var indirizzo = theForm.elements["shipment.invoicingAddress"].value;  
  var citta = theForm.elements["shipment.invoicingCity"].options[theForm.elements["shipment.invoicingCity"].selectedIndex].value;
  var cap = theForm.elements["shipment.invoicingZipCode"].options[theForm.elements["shipment.invoicingZipCode"].selectedIndex].value;
  var provincia = theForm.elements["shipment.invoicingProvince"].options[theForm.elements["shipment.invoicingProvince"].selectedIndex].value;
  var ragSociale = theForm.elements["shipment.billingInformation"].value;
  var formaGiuridica = theForm.elements["shipment.legalPosition"].value;
  var codFiscale = theForm.elements["shipment.fiscalCode"].value;
  var piva = theForm.elements["shipment.piva"].value;
  
  var err_arr=new Array();
  
  if (nomeCognome=="")
  	{
	  	err_arr["shipment.invoicingName"]="err_nome";
  	}
  if (indirizzo=="")
  	{
	  	err_arr["shipment.invoicingAddress"]="err_ind";
  	}
  if (provincia=="")
  	{
	  	err_arr["shipment.invoicingProvince"]="err_prov";
  	}
  if (citta=="")
  	{
	  	err_arr["shipment.invoicingCity"]="err_comune";
  	}
  if (cap=="")
  	{
	  	err_arr["shipment.invoicingZipCode"]="err_cp";
  	}
  if (isNaN(cap) || (""+cap).length<5)
  	{
	  	err_arr["shipment.invoicingZipCode"]="err_uncorrect_cp";
  	}
  /*if (ragSociale=="")
  	{
  		alert("Verifica le seguenti informazioni: ragione sociale.");
  		return false;
  	}	*/
  /*if (formaGiuridica=="")
  	{
  		alert("Verifica le seguenti informazioni: forma giuridica.");
  		return false;
  	}	*/
  if (codFiscale=="" && piva=="")
	{
		err_arr["shipment.fiscalCodePiva"]="err_cf";
	}

  selectedCF = "";
  selectedPIVA = "";
  isCF = false;
  isPIVA = false;
  if (codFiscale!="" && fiscale(codFiscale,"no"))
  		{
  		  selectedCF = codFiscale;
  		  selectedPIVA = "";
  		  isCF = true;
  		  isPIVA = false;
  		}
  else if (piva!="" && fiscale(piva,"no"))
  		{
  		  selectedCF = piva;
  		  selectedPIVA = "";
  		  isCF = true;
  		  isPIVA = false;
  		}
  // else if (piva!="" && partitaIVA(piva,"no"))
  else if (piva!="" && partitaIVA(piva))
  		{
  		  selectedCF = "";
  		  selectedPIVA = piva;
  		  isCF = false;
  		  isPIVA = true;
  		}
  // else if (codFiscale!="" && partitaIVA(codFiscale,"no"))
  else if (codFiscale!="" && partitaIVA(codFiscale))
  		{
  		  selectedCF = "";
  		  selectedPIVA = codFiscale;
  		  isCF = false;
  		  isPIVA = true;
  		}
  		  			
  if (!isCF && !isPIVA)
  	{
	  	err_arr["shipment.fiscalCodePiva"]="err_uncorrect_cf";
  	}
  else
   {
	  if (isPIVA)
	  		{
			  theForm.elements["shipment.fiscalCode"].value = selectedPIVA;
			  theForm.elements["shipment.piva"].value = selectedPIVA;	  		
	  		}
	  else if (isCF)
	  		{
			  theForm.elements["shipment.fiscalCode"].value = selectedCF;
			  theForm.elements["shipment.piva"].value = selectedCF;	  		
	  		}	  		
   }

  return err_arr;
}

// Validazione form dati di spedizione
function submitSpedData(theForm)
{
  var nomeCognome = theForm.elements["shipment.shippingName"].value;
  var indirizzo = theForm.elements["shipment.shippingAddress"].value;
  var citta = theForm.elements["shipment.shippingCity"].options[theForm.elements["shipment.shippingCity"].selectedIndex].value;
  var cap = theForm.elements["shipment.shippingZipCode"].options[theForm.elements["shipment.shippingZipCode"].selectedIndex].value;
  var provincia = theForm.elements["shipment.shippingProvince"].options[theForm.elements["shipment.shippingProvince"].selectedIndex].value;

  var err_arr=new Array();
  
  if (nomeCognome=="")
  	{
	  	err_arr["shipment.shippingName"]="err_nome";
  	}
  if (indirizzo=="")
  	{
	  	err_arr["shipment.shippingAddress"]="err_ind";
  	}
  if (provincia=="")
  	{
	  	err_arr["shipment.shippingProvince"]="err_prov";

  	}
  if (citta=="")
  	{
	  	err_arr["shipment.shippingCity"]="err_comune";
  	}
  if (cap=="")
  	{
	  	err_arr["shipment.shippingZipCode"]="err_cp";
  	}
  if (isNaN(cap) || (""+cap).length<5)
  	{
	  	err_arr["shipment.shippingZipCode"]="err_uncorrect_cp";
  	}

  return err_arr;
}

//----- Profilo di acquisto -------------------

function checkLightCustomerProfileForm(theForm)
{
  var cognome = theForm.elements["profile.lastName"].value.toUpperCase();
  var nome = theForm.elements["profile.firstName"].value.toUpperCase();
  var codFiscale = theForm.elements["profile.fiscalCode"].value.toUpperCase();

  if (nome=="" || nome.search(/\S/)==-1)
  	{
  		alert("Verifica le seguenti informazioni: Nome");
  		return  false;
  	}
  if (cognome=="" || cognome.search(/\S/)==-1)
  	{
  		alert("Verifica le seguenti informazioni: Cognome");
  		return  false;
  	}
  if (codFiscale=="")
  	{
  		alert("Verifica le seguenti informazioni: Codice Fiscale o Partita IVA");
  		return  false;
  	}
  if((!fiscale(codFiscale,"no") && !partitaIVA(codFiscale)))
  	{
  		alert("Il codice fiscale o la partita IVA inseriti non sono corretti.");
  		return  false;
  	}

	if (fiscale(codFiscale,"no"))
		{
			var strCF = "";
			var cnt = 0;
			for (i=0;i<cognome.length;i++) {
				ch = cognome.substring( i, i+1 );
				if ("QWRTYPSDFGHJKLZXVBNM".indexOf(ch)>=0) {
					  if (cnt<3) strCF += ch;				  	
					  cnt++;	
					}
			}
			cnt = 0;
			for (i=0;i<nome.length;i++) {
				ch = nome.substring( i, i+1 );
				if ("QWRTYPSDFGHJKLZXVBNM".indexOf(ch)>=0) {
					  if (cnt<3) strCF += ch;				  	
					  cnt++;	
					}
			}
			
		  if(codFiscale.substring(0,6)!=strCF)
		  	{
		  		alert("Il codice fiscale inserito non e' corretto.");
		  		return  false;
		  	}
		}

  theForm.elements["profile.fiscalCode"].value = codFiscale;
  theForm.elements["profile.lastName"].value = cognome;
  theForm.elements["profile.firstName"].value = nome;
  
  return true;
}


function checkCustomerProfileForm(theForm)
{
	//EP modifica flusso ricarica 19092006
	var err_arr = new Array();
	
  var cognome = theForm.elements["profile.lastName"].value;
  var nome = theForm.elements["profile.firstName"].value;
  var luogoNascita = theForm.elements["profile.birthCity"].options[theForm.elements["profile.birthCity"].selectedIndex].value;
  var provinciaNascita = theForm.elements["profile.birthProvince"].options[theForm.elements["profile.birthProvince"].selectedIndex].value;
  try
  	{
	  var giornoNascita = theForm.elements["birthDay"][theForm.elements["birthDay"].selectedIndex].value;
	  var meseNascita = theForm.elements["birthMonth"][theForm.elements["birthMonth"].selectedIndex].value;
	  var annoNascita = theForm.elements["birthYear"][theForm.elements["birthYear"].selectedIndex].value;
  	}
  catch (e)
  	{
	  var giornoNascita = "RO";
	  var meseNascita = "RO";
	  var annoNascita = "RO";
  	}

  var codFiscale = theForm.elements["profile.fiscalCode"].value;
  var indirizzo = theForm.elements["profile.address"].value;
  var comune = theForm.elements["profile.city"].options[theForm.elements["profile.city"].selectedIndex].value;
  var provincia = theForm.elements["profile.province"].options[theForm.elements["profile.province"].selectedIndex].value;
  var cap = theForm.elements["profile.zipCode"].options[theForm.elements["profile.zipCode"].selectedIndex].value;
  var telefono = theForm.elements["profile.phone"].value;
  try
  	{
	  var male = theForm.elements["profile.gender"][0].checked;
	  var female = theForm.elements["profile.gender"][1].checked;
	  var gender = false;
  	}
  catch (e)
  	{
  	  var male = false;
  	  var female = false;
  	  var gender = theForm.elements["profile.gender"].value == "" ? false:true;
  	}
  if (nome=="" || nome.search(/\S/)==-1)
  	{	
	  	err_arr["profile.firstName"]="err_name";
  		//alert("Verifica le seguenti informazioni: Nome");
  	}
  if (cognome=="" || cognome.search(/\S/)==-1)
  	{
	  	err_arr["profile.firstName"]="err_lastName";
  		//alert("Verifica le seguenti informazioni: Cognome");
  	}
  if (provinciaNascita=="" || provinciaNascita.search(/\S/)==-1)
  	{
  		err_arr["profile.birthProvince"]="err_prov_nasc";
  	 }
  if (!((""+provinciaNascita).length==2))
  	{
	  	err_arr["profile.birthProvince"]="err_uncorrect_prov_nasc";
  		//alert("La provincia di nascita inserita non e' corretta.");
  	}
  if (luogoNascita=="" || luogoNascita.search(/\S/)==-1)
  	{
	  	err_arr["profile.birthCity"]="err_luogo_nasc";
  	}
  if (giornoNascita!="RO")
  	{
	  if (!isYMDValid(giornoNascita,meseNascita,annoNascita))
	  	{
		  	err_arr["birthDate"]="err_data_nasc";
	  	}
  	}
  if (!male && !female && !gender)
  	{
	  	err_arr["profile.gender"]="err_gender";
  		//alert("Verifica le seguenti informazioni: Sesso");
  	}
  if (codFiscale=="")
  	{
	  	err_arr["profile.fiscalCode"]="err_cf";
  		//alert("Verifica le seguenti informazioni: Codice Fiscale o Partita IVA");
  	}
  if((!fiscale(codFiscale,"no") && !partitaIVA(codFiscale)))
  	{
	  	err_arr["profile.fiscalCode"]="err_uncorrect_cf";
  		//alert("Il codice fiscale o la partita IVA inseriti non sono corretti.");
  	}
  if (indirizzo=="" || indirizzo.search(/\S/)==-1)
  	{
	  	err_arr["profile.address"]="err_ind";
  	}
  if (provincia==""  || provincia.search(/\S/)==-1)
  	{
  		err_arr["profile.province"]="err_prov";
  		// inserire la funzione di controllo CF
  	}
  if (!((""+provincia).length==2))
  	{
	  	err_arr["profile.province"]="err_prov";
  		//alert("La provincia inserita non e' corretta.");
  	}
  if (comune=="" || comune.search(/\S/)==-1)
  	{
	  	err_arr["profile.city"]="err_comune";
  		// inserire la funzione di controllo CF
  	}
  if (cap=="" || cap.search(/\S/)==-1)
  	{
	  	err_arr["profile.zipCode"]="err_cp";
  	}

  if (isNaN(cap) || (""+cap).length<5)
  	{
	  	err_arr["profile.zipCode"]="err_uncorrect_zipCode";
  		//alert("Il codice postale inserito non e' corretto.");
  	}
  if (telefono=="" || telefono.search(/\S/)==-1)
  	{
	  	err_arr["profile.phone"]="err_tel";
  		//alert("Verifica le seguenti informazioni: Recapito Telefonico");
  	}
  if ((""+telefono).length<8 || isNaN(telefono))
  	{
	  	err_arr["profile.phone"]="err_uncorrect_tel";
  		//alert("Il numero di telefono inserito non e' corretto.");
  	}
  
  	return err_arr;
}

function submitProfiloAcquisto(theForm, forwardName)
{
	var isTRPage = true;
	try
		{
		 theForm.elements["shop.forward_name"].value=forwardName;
		 isTRPage = false;
		}
	catch (e)
		{
		 // No OP
		}
		
		//EP19092006 revisione flusso ricariche
	return(checkCustomerProfileForm(theForm));
	//if (checkCustomerProfileForm(theForm))
	//{
		// *** il submit viene fatto nella <form> 02072003AM ***
		//theForm.submit();
		//return true;
	//}
	//else
	//{
		// *** obsoleto l'utilizzo ? 02072003AM ***
		//if (isTRPage)
		//	return false;
	//}
}

//---- Validazione dati form opzione regalo

function submitOpzioneRegalo(theForm)
{
  var gadgetChecked = false;

  var gadgetLength = theForm.productId.length;
  if (isNaN(gadgetLength))
 	{
 	  if (theForm.productId.checked)
 	  	gadgetChecked = true;
 	}
  else
 	{
  	 for (var cont=0;cont<gadgetLength;cont++)
  	 	{
  	 	  if (theForm.productId[cont].checked)
  	 	  	gadgetChecked = true;
  	 	}
 	}

  if (!gadgetChecked)
  	{
  	  alert("Devi scegliere una carta regalo.")
  	  return false;
  	}

  if (theForm.message.value=="" || theForm.message.value.search(/\S/) == -1)
    {
      alert("Devi scrivere il messaggio che accompagnera' il tuo regalo prima di procedere.")
  	  return false;
    }

  return true;
}

// Da implementare sulla onPropertyChange
function modifyTestoRegalo( textField, charsLeft)
{
	var StrLen = textField.value.length;
	if (StrLen > 250 )
		{
		  alert("Il messaggio non puo' superare i 250 caratteri.");
		  textField.value = textField.value.substring(0,250);
		  num = 0;
		}
	else
		{
		num = 250 - StrLen;
		}
	charsLeft.value = num;
}

// ----------------- Acquisto prodotti ----------------------
function submitPaymentMode(theForm)
{
  var paymentMode = -1;

  for (i=0; i<theForm.elements["paymentMode"].length; i++){
    if (theForm.elements["paymentMode"][i].checked){
      paymentMode = theForm.elements["paymentMode"][i].value;
      break;
    }
  }

  if(paymentMode == -1){
  	paymentMode = theForm.elements["paymentMode"].value;
  	theForm.elements["paymentMode"].checked=true;
  }


  if (paymentMode == 1 ){
    var typeCarta = theForm.elements["cartaDiCredito.tipoCarta"].options[theForm.elements["cartaDiCredito.tipoCarta"].selectedIndex].value;
    var numCarta = theForm.elements["cartaDiCredito.numeroCarta"].value;
    var meseScad = theForm.elements["cartaDiCredito.meseScadenza"].options[theForm.elements["cartaDiCredito.meseScadenza"].selectedIndex].value;
    var annoScad =  theForm.elements["cartaDiCredito.annoScadenza"].options[theForm.elements["cartaDiCredito.annoScadenza"].selectedIndex].value;
    var owner = theForm.elements["cartaDiCredito.propCarta"].value;
    var numSec =  theForm.elements["cartaDiCredito.sicNumero"].value;
    var codFiscale = theForm.elements["cartaDiCredito.codiceFiscale"].value;

    if (!verifyCard(numCarta,typeCarta) || numCarta.search(/\S/) == -1){
      //alert("Il numero della Carta di Credito inserito non e' corretto.");
  		return false;
    }
    else if (meseScad==0){
  		alert("Verifica le seguenti informazioni: Mese scadenza");
  		return false;
  	}
    else if (annoScad==0){
  		alert("Verifica le seguenti informazioni: Anno scadenza");
  		return false;
  	}
    else if(!isNotExpired(meseScad, annoScad)){
      alert("Il mese e l'anno di scadenza della Carta di Credito non possono essere anteriori alla data di oggi.");
      return false;
    }
    
    else if (owner=="" || owner.search(/\S/)==-1){
  		alert("Verifica le seguenti informazioni: Nome e Cognome");
  		return false;
  	}
    else if(!checkOwner(owner))
    	{
    		alert("Il Nome e Cognome devono contenere solo caratteri alfabetici.");
  		return false;
    	}
    else if(numSec=="")
    	{
    		alert("Verifica le seguenti informazioni: Numero di sicurezza.");
    		return false;
    	}
    else if (numSec.search(/\S/)==-1 || numSec.match(/\D/)){
  		alert("Il Numero di Sicurezza deve contenere solo caratteri numerici.");
  		return false;
  	}

    else if((typeCarta=="amex") && (numSec.length!=4)){
        alert("Verifica le seguenti informazioni: Numero di sicurezza.");
    	return false;
    }
    else if(numSec.length!=3 && numSec.length!=4)
    	{
    		alert("Verifica le seguenti informazioni: Numero di sicurezza.");
    		return false;
    	}

    else if (codFiscale==""){
  		alert("Verifica le seguenti informazioni: Codice Fiscale");
  		return false;
  	}
    else if (codFiscale.search(/\S/)==-1 || !fiscale(codFiscale,"no")){
  		alert("Il codice fiscale inserito non e' corretto.");
  		return false;
  	}
  	else if(!verifyCVV(typeCarta,numSec)){
  		alert("Verifica le seguenti informazioni: Numero di sicurezza.");
  		return false;
  	}
  }
  else if (paymentMode == 2){
    var omniPayNo = theForm.elements["omniPay.numero"].value;
	 var omniPayPrefix = theForm.elements["omniPay.prefisso"].value;
	 if (omniPayPrefix=="" && omniPayNo!="")
	 	{
      alert("Verifica le seguenti informazioni: Prefisso Numero Omnipay");
  		return false;
	 	}
    if (omniPayNo==""){
      alert("Verifica le seguenti informazioni: Numero Omnipay");
  		return false;
    }
    else if (omniPayNo.search(/\S/) == -1 || omniPayNo.match(/\D/) || (omniPayNo.length!=6 && omniPayNo.length!=7)){
      alert("Il numero di telefono inserito non e' un numero Omnipay OnPhone.");
  		return false;
    }
  }
  else if (paymentMode == 3){
    return true;
  }
  else if (paymentMode == 4){
    return true;
  }
  else if (paymentMode == 5){
    return true;
  }
  else{
    alert("Selezionare il metodo di pagamento");
    return false;
  }

  return true;
}

// ----------------- Pagamento TR+ --------------------------

function submitTRPayment(theForm)
{
  
}

function submitSetPromoTR(theForm)
	{
		if (theForm.elements["isPrizeActive"].value=="Y")
			{
				var pref = theForm.dialingCodePrize[theForm.dialingCodePrize.selectedIndex].value;
				var number = theForm.phoneNumberPrize.value;
				
				if (pref=="")
					{
					 alert("Verifica le seguenti informazioni: Prefisso numero Premiato");
					 return false;
					}						
				if ((number.length!=6 && number.length!=7) || isNaN(number))
					{
					 alert("Attenzione: Il numero di telefono Premiato inserito non e' corretto.");			
					 return false;
					}		
				
			}
		if (theForm.elements["isSponsorActive"].value=="Y")
			{
				var pref = theForm.dialingCodeSponsor[theForm.dialingCodeSponsor.selectedIndex].value;
				var number = theForm.phoneNumberSponsor.value;

				if (pref=="" && number=="")
					{
			          if (!confirm("Ricordati di scegliere il numero Sponsor oppure clicca sul tasto \"Continua\"."))
			                return false;
					}
				if (pref!="" || number!="")
					{
						if (pref=="")
							{
							 alert("Verifica le seguenti informazioni: Prefisso numero Sponsor");
							 return false;
							}						
						if ((number.length!=6 && number.length!=7) || isNaN(number))
							{
							 alert("Attenzione: Il numero di telefono Sponsor inserito non e' corretto.");			
							 return false;
							}		
					}
			}
			
		return true;
	}

//----------- Selezione Dati TR+ ------------------------------------

// Aggiunge e rimuove i numeri di telefono.
// Utilizzato in /jsp/profilo/enableTR2.jsp

function selectMe()
{
	var boxLength = document.enableTRForm.msisdnNumbers.length;
	var selectedItem = document.enableTRForm.available.selectedIndex;
	if (selectedItem!=-1)
		{
		var selectedText = document.enableTRForm.available.options[selectedItem].text;
		var selectedValue = document.enableTRForm.available.options[selectedItem].value;
		var i;
		var isNew = true;
		if (boxLength != 0)
			{
			for (i = 0; i < boxLength; i++)
				{
				thisitem = document.enableTRForm.msisdnNumbers.options[i].text;
				if (thisitem == selectedText)
					{
					isNew = false;
					break;
	      				}
	   			}
			}
		if (isNew)
			{
			newoption = new Option(selectedText, selectedValue, false, false);
			document.enableTRForm.msisdnNumbers.options[boxLength] = newoption;
			}
		document.enableTRForm.available.selectedIndex=-1;
		}
}

function removeMe()
{
	var boxLength = document.enableTRForm.msisdnNumbers.length;
	arrSelected = new Array();
	var count = 0;
	for (i = 0; i < boxLength; i++)
		{
		if (document.enableTRForm.msisdnNumbers.options[i].selected)
			{
			arrSelected[count] = document.enableTRForm.msisdnNumbers.options[i].value;
			}
		count++;
		}
	var x;
	for (i = 0; i < boxLength; i++)
		{
		for (x = 0; x < arrSelected.length; x++)
			{
			if (document.enableTRForm.msisdnNumbers.options[i].value == arrSelected[x])
				{
				document.enableTRForm.msisdnNumbers.options[i] = null;
   				}
			}
		boxLength = document.enableTRForm.msisdnNumbers.length;
   		}
}

function saveMe()
{
	var boxLength = document.enableTRForm.msisdnNumbers.length;
	if (boxLength != 0)
	{
		for (i = 0; i < boxLength; i++)
		{
			document.enableTRForm.msisdnNumbers.options[i].selected = true;
		}
	}
}

function submitTRAbilitazione(theForm)
{
	var err_arr = new Array();
	
	// I Controllo: almeno un numero selezionato
	var boxLength = theForm.elements["trForm.trNumbers"].length;
	
	var selTrNum = false;
	// - un solo MSISDN, deve essere checked
	if (boxLength==null && theForm.elements["trForm.trNumbers"].checked)
	{
		selTrNum = true;
	}
  else // - piu' di un MSISDN, almeno uno deve essere checked
  {  
  	for(i = 0; i < boxLength; i++){
  		if(theForm.elements["trForm.trNumbers"][i].checked){
  			selTrNum = true;
  		}
  	}
  }
	if (!selTrNum)
	{
		err_arr["trNumbers"] = "err_num_not_set";
	}
	var codPin = theForm.elements["trForm.trModel.omnipayInfo.omnipayIVRpin"].value;
	var codPinVer = document.getElementById("confirmIVR").value;
	
	if (codPin=="")
		{
		 err_arr["omnipayIVRpin"] = "err_pin_not_set";
		}
	// ***** AM 02092003 --- aggiunta (""+codPin).length<5 || codPin=="     "	
	if ((""+codPin).length<5 || codPin=="     " || isNaN(codPin))
		{
		err_arr["omnipayIVRpin"] = "err_pin";
		}
	
	if (codPinVer=="")
		{
			err_arr["confirmOmnipayIVRpin"] = "err_pin2_not_set";
		}
	if (codPinVer!=codPin)
		{
			err_arr["confirmOmnipayIVRpin"] = "err_pin2";
		}

	numPreferito  = theForm.elements["trForm.telNumber"].value;
	numPreferito1 = theForm.elements["trForm.telNumber1"].value;
	numPreferito2 = theForm.elements["trForm.telNumber2"].value;
	
	/*
	 * Controllo per l'inserimento di favourite uguali
	 */
	if((numPreferito1!=''&&numPreferito1!='null')||(numPreferito2!=''&&numPreferito2!='null'))
	{
	  if(numPreferito==numPreferito1||numPreferito1==numPreferito2||numPreferito2==numPreferito)
	  {
	    err_arr["favNumber"] = "err_equal_pref";
	  }
  }
	
	
	if (number!="null" || number1!="null" || number2!="null")
		{
		
		//controlla se i numeri non sono stati modificati skippando il nuovo inserimento
		if ((numPreferito!=number && number!='' && number!='null')||
		    ((number1!='null' && numPreferito1!=number1) || (number1=='null' && numPreferito1!=""))||
		    ((number2!='null' && numPreferito2!=number2) || (number2=='null' && numPreferito2!="")))
			{
				number = numPreferito;
				number1 = numPreferito1;
				number2 = numPreferito2;
				theForm.elements["trForm.trModel.omnipayInfo.omnipayIVRpin"].value = "";
				document.getElementById("confirmIVR").value ="";
				theForm.elements["trForm.isPhoneChanged"].value = "Y";
				clearPINCDCField(theForm);
				err_arr["mod_preferred"] = "not_an_error";
			}
		}
	phoneNumber = theForm.elements["trForm.telNumber"].value;
	if (phoneNumber=="")
		{
			err_arr["telNumber"] = "err_pref_not_set";
		}	
		
	if ((numPreferito.length!=9 && numPreferito.length!=10) || isNaN(numPreferito))
		{
			err_arr["telNumber"] = "err_not_vf_num";
		}		

	numPreferito = theForm.elements["trForm.telNumber1"].value;
	if (numPreferito!="")
		{
			if ((numPreferito.length!=9 && numPreferito.length!=10) || isNaN(numPreferito))
				{	
					err_arr["telNumber1"] = "err_not_vf_num1";	
				}
		}

	numPreferito = theForm.elements["trForm.telNumber2"].value;
	if (numPreferito!="")
		{
			if ((numPreferito.length!=3 && (numPreferito.length!=9 && numPreferito.length!=10)) || isNaN(numPreferito))
				{
					err_arr["telNumber2"] = "err_not_vf_num2";
				}
		}	

    var codFiscale = theForm.elements["ccForm.creditCardInfo.fiscalCode"].value;
    var numCarta = theForm.elements["ccForm.creditCardInfo.cardNo"].value;
    var numSec =  theForm.elements["ccForm.creditCardInfo.cardCVV2"].value;
    var typeCarta = theForm.elements["ccForm.creditCardInfo.cardType"].options[theForm.elements["ccForm.creditCardInfo.cardType"].selectedIndex].value;
    var meseScad = theForm.elements["ccForm.creditCardInfo.expireMonth"].options[theForm.elements["ccForm.creditCardInfo.expireMonth"].selectedIndex].value;
    var annoScad =  theForm.elements["ccForm.creditCardInfo.expireYear"].options[theForm.elements["ccForm.creditCardInfo.expireYear"].selectedIndex].value;
    var owner = theForm.elements["ccForm.creditCardInfo.cardOwner"].value;

    var err = verifyCard(numCarta,typeCarta,true);
    if (err!="" || numCarta.search(/\S/) == -1)
    	{
	    	err_arr["numCarta"] = err;
    	}
    if (meseScad==0)
    	{
	    	err_arr["meseScad"] = "err_mese_not_set";
    	}
    if (annoScad==0)
    	{
	    	err_arr["annoScad"] = "err_anno_not_set";
    	}
    	err = isNotExpired(meseScad, annoScad,true);
   	if(err!="")
      	{
	      	err_arr["meseScad"] = err;
      	}
    if (owner=="" || owner.search(/\S/)==-1){
	    err_arr["owner"] = "err_cdc_nome_not_set";
        }
    if(!checkOwner(owner))
    	{
	    	err_arr["owner"] = "err_nome";
    	}
    if(numSec=="")
    	{	
	    	err_arr["numSec"] = "err_cdc_cvv2_not_set";
    	}
    if (numSec.search(/\S/) == -1 || numSec.match(/\D/) )
    	{
	    	err_arr["numSec"] = "err_cdc_cvv2_err";
    	}
    if(numSec.length!=3 && numSec.length!=4)
    	{
	    	err_arr["numSec"] = "err_cdc_cvv2";
    	}
        
    if((typeCarta == "amex") && (numSec.length!=4))
    	{
	    	err_arr["numSec"] = "err_cdc_cvv2";
    	}

    if (codFiscale=="")
    	{
	    	err_arr["codFiscale"] = "err_cf_not_set";
    	}
    if (codFiscale.search(/\S/) == -1 || !fiscale(codFiscale,"no"))
    	{
	    	err_arr["codFiscale"] = "err_cdc_cf";
    	}
    if(!verifyCVV(typeCarta,numSec))
    	{
	    	err_arr["numSec"] = "err_cdc_cvv2_not_ver";
        }
  
	return err_arr;
}

//--------- Validazione prodotto telefono -----------------
function submitPhoneProduct(theForm) {

 	var typeProduct = 0;   // 0: solo telefono; 1: telefono + ricarica; 2: telefono + SIM ricaricabile

  try {
  	tipoPiano = theForm.elements["simData.idPianoTelefonico"].options[theForm.elements["simData.idPianoTelefonico"].selectedIndex].value;
  	typeProduct = 2;
  }
  catch (e) {
  	try {
  		// Da cambiare ***
  		numTelToRecharge = theForm.elements["rechargePrefix"].options[theForm.elements["rechargePrefix"].selectedIndex].value;
  		typeProduct = 1;
  	}
  	catch (e) {
  		typeProduct = 0;
  	}
  }
	
  if (typeProduct == 0) {
  	//return true;
  }
  else if (typeProduct == 1) // telefono + ricarica

  {
		var pref1 = theForm.elements["rechargePrefix"].options[theForm.elements["rechargePrefix"].selectedIndex].value;
		var phone1 = ""+pref1+theForm.elements["rechargeNumber"].value;
		var pref2 = theForm.elements["confirmRechargePrefix"].options[theForm.elements["confirmRechargePrefix"].selectedIndex].value;
		var phone2 = ""+pref2+theForm.elements["confirmRechargeNumber"].value;

		if (pref1=="" || pref2=="")
			{
		   alert("Prefisso numero da ricaricare non valido.");
			return false;
			}
		if ((phone1.length!=9 && phone1.length!=10) || isNaN(phone1)) 
		{
		   alert("Numero da ricaricare non valido.");
			return false;
		}
		if (phone1 != phone2) {
		  alert("I due numeri di telefono non coincidono.");
			return false;
		}
  }

//if (typeProduct == 2)
	else if (typeProduct == 2) // telefono + SIM ricaricabile

	{
	
	 // read id layer from hidden form field
	 var idlayer = -1;
	 try
	 {
	  idlayer = parseInt(document.addProductToCartForm.elements["idLayer"+tipoPiano].value);
	  if (isNaN(idlayer))
	  {
  	  idlayer=-1;
	  }
	 }
	 catch(er)
	 {
	  idlayer=-1;
	 }
	 
	 /*
	 if (tipoPiano=="RINFINITY" || tipoPiano=="ALTINFINITY" || tipoPiano=="BISINFINITY")
	 		idlayer = 0;
	 if (tipoPiano=="RHAPPY" || tipoPiano=="ALTHAPPY" || tipoPiano=="BISHAPPY")
	 		idlayer = 1;
	 else	if (tipoPiano=="RINFTOPRIC" || tipoPiano=="ALTINFTOPRIC" || tipoPiano=="BISINFTOPRIC")
	 		idlayer = 2;	
	 else	if (tipoPiano=="RFLATSIMPLY" || tipoPiano=="ALTFLATSIMPLY" || tipoPiano=="BISFLATSIMPLY")
	 		idlayer = 3;	
	 else	if (tipoPiano=="RAUTOCHIAMAS" || tipoPiano=="ALTAUTOCHIAMA" || tipoPiano=="BISCHIAMASUPER")
	 		idlayer = 4;	
	 else	if (tipoPiano=="REASY" || tipoPiano=="ALTEASY" || tipoPiano=="BISEASY")
	 		idlayer = 5;	
	 else	if (tipoPiano=="RNATION" || tipoPiano=="ALTERNATION" || tipoPiano=="BISNATION")
	 		idlayer = 6;	
	 */

	 if (tipoPiano!="0")
	 	{
			pref = theForm.elements["simData.elOpzPianoTel1.datoopzione1.prefisso"][idlayer].value;
			numero = theForm.elements["simData.elOpzPianoTel1.datoopzione1.numero"][idlayer].value;
			if (pref=="" && numero!="")
				{
		        alert("Prefisso del numero telefonico numero non valido.");
		        return false;
				}
						
			if (theForm.elements["simData.elOpzPianoTel1.datoopzione1.numero"][idlayer].value.search(/\S/)!=-1 &&
			    ((theForm.elements["simData.elOpzPianoTel1.datoopzione1.numero"][idlayer].value.length!=6 &&
			      theForm.elements["simData.elOpzPianoTel1.datoopzione1.numero"][idlayer].value.length!=7) ||
    			 theForm.elements["simData.elOpzPianoTel1.datoopzione1.numero"][idlayer].value.match(/\D/)))
    			 {	
					alert("Attenzione! Il numero telefonico deve essere di sei o sette cifre.");
					return false;
				}
	 	}
	 else
	 	{
			alert("Selezionare il piano.");
			return false;
	 	}	 			   			
	 		
    }
    
    // NEW 2006 05 08 Controllo box regalo
    // =====================================================
    try
    {
	    if (document.getElementById('box_regalo').style.display != "none")
	    {
	    	// Check selezionata una carta regalo
	    	// ----------------------------------
	    	var giftChecked = false;
				var giftLength = theForm.elements["giftId"].length;
				if (isNaN(giftLength))
				{
				     if(theForm.elements["giftId"].checked)
				               giftChecked = true;
				}
				else
				{
			    for (var cont=0;cont<giftLength;cont++)
		      {
		          if (theForm.elements["giftId"][cont].checked)
		                giftChecked = true;
		      }
		    }
	    	if (!giftChecked)
	    	{
	    		alert("Devi scegliere una carta regalo.");
		    	return false;
	    	}
	    	
	    	// Check messaggio di testo inserito
	    	// ---------------------------------
	    	if (theForm.elements["giftMsg"].value=="" || theForm.elements["giftMsg"].value.search(/\S/) == -1)
	      {
	        alert("Devi scrivere il messaggio che accompagnera' il tuo regalo prima di procedere.")
	  	    return false;
	      }
	    }
	  }
	  catch (er)
	  {
	  	// do nothing
	  }
    // =====================================================
    // END NEW 2006 05 08
	// inizio controlli Now+
	/* START SG-06395 */
	var divNowPiu=document.getElementById("divNowPiu");
	
	if (divNowPiu) {
		if (theForm.elements["opzioneNowPiu"].value=="1") {
			if (theForm.elements["offertaNowPiu"].value=="") {
		        alert("Attenzione: selezionare l'offerta su cui attivare Now+");
		        
		  	    return false;			
			}
		}
		
		if (theForm.elements["opzioneNowPiu"].value=="2") {
			if (theForm.elements["msisdnNowPiu"].value=="") {
		        alert("Attenzione: selezionare l'MSISDN su cui attivare Now+");
		        
		  	    return false;			
			}
		}		
	}
	// fine controlli Now+
	
	/* END SG-06395 */
  return true;
}

//--------- Validazione prodotto recharge -------------------
function submitRechargeProduct(theForm,productID)
{

    if (theForm.elements["simData.elOpzPianoTel1.datoopzione1.numero"] != null) 
    	{
		  var pref1 = theForm.elements["simData.elOpzPianoTel1.datoopzione1.prefisso"].options[theForm.elements["simData.elOpzPianoTel1.datoopzione1.prefisso"].selectedIndex].value;
		  var phone1 = theForm.elements["simData.elOpzPianoTel1.datoopzione1.numero"].value;
		}

	 if (pref1=="" && phone1!="") 
			{
			   alert("Attenzione: inserire il prefisso telefonico.");
				return false;
			}
	 if (phone1 != null && phone1.search(/\S/) != -1) 
			{
			if ((phone1.length!=6 && phone1.length!=7) || isNaN(phone1)) 
				{
			  		alert("Attenzione! Il numero di cellulare deve essere di sei o sette cifre.");
					return false;
				}
			}

  return true;
}

//---- Validazione dati di attivazione SIM
// type user: 0->individuo;1->libero prof.; 2->azienda
// typeProduct: S->SIM; R->Ricaricabile
function submitSIMOptionData(theForm,typeProduct)
{
 typeUser = -1;
 if (theForm.elements["check"][0].checked)	// Privato
 	typeUser = 0;
 if (theForm.elements["check"][1].checked)	// Libero professionista
 	typeUser = 1;
 if (theForm.elements["check"][2].checked)	// Azienda
 	typeUser = 2;

 var PRODSIM = "S";
 var PRODRIC = "R";

 if (typeUser==-1)
 	{
 		alert("Selezionare il tipo di individuo.");
 		return false;
 	}

 var nome = theForm.elements["personalData.nome"];
 var cognome = theForm.elements["personalData.cognome"];
 var indirizzo = theForm.elements["personalData.indirizzo"];
 var comune = theForm.elements["personalData.comune"].options[theForm.elements["personalData.comune"].selectedIndex].value;
 var provincia = theForm.elements["personalData.provincia"].options[theForm.elements["personalData.provincia"].selectedIndex].value;
 var cap = theForm.elements["personalData.cap"].options[theForm.elements["personalData.cap"].selectedIndex].value;
 var nazionalita = theForm.elements["identityDocument.nazionalita"].options[theForm.elements["identityDocument.nazionalita"].selectedIndex].value;      

 if (typeUser==0)	// Privato
 	{
     try
     	{
        var giornoNascita = theForm.elements["personalData.birthDay"][theForm.elements["personalData.birthDay"].selectedIndex].value;
    	  var meseNascita = theForm.elements["personalData.birthMonth"][theForm.elements["personalData.birthMonth"].selectedIndex].value;
    	  var annoNascita = theForm.elements["personalData.birthYear"][theForm.elements["personalData.birthYear"].selectedIndex].value;
     	}
     catch (e)
     	{
    	  var giornoNascita = "RO";
    	  var meseNascita = "RO";
    	  var annoNascita = "RO";
     	}

	  if (nome.value=="" || nome.value.search(/\S/)==-1)
	    {
	     alert("Verifica le seguenti informazioni: Nome");
	     nome.focus();
        return false;
      }
    else if (cognome.value=="" || cognome.value.search(/\S/)==-1)
      {
	     alert("Verifica le seguenti informazioni: Cognome");
	     cognome.focus();
        return false;
      }
    else if (!isYMDValid(giornoNascita,meseNascita,annoNascita))
    	{
    		alert("La data di nascita inserita non e' corretta.");
    		return  false;
    	}
    else if (!theForm.elements["personalData.sesso"][0].checked && !theForm.elements["personalData.sesso"][1].checked)
      {
    		alert("Verifica le seguenti informazioni: Sesso");
    		return  false;
      }
    else if (indirizzo.value=="" || indirizzo.value.search(/\S/)==-1)
      {
	      alert("Verifica le seguenti informazioni: Indirizzo");
	      indirizzo.focus();
         return false;
      }
    else if (provincia=="" || provincia.search(/\S/)==-1)
      {
	      alert("Verifica le seguenti informazioni: Provincia");
	      // inserire la funzione di controllo CF
	      theForm.elements["personalData.provincia"].focus();
         return false;
      }
    else if (comune=="" || comune.search(/\S/)==-1 || comune.length>40)
      {
      	if ( comune.length>40 )
   		   alert("Il comune impostato non e' corretto");
	      else
		      alert("Verifica le seguenti informazioni: Comune");
	      // inserire la funzione di controllo CF
	      theForm.elements["personalData.comune"].focus();
        return false;
      }
    else if (cap.match(/\D/) || cap.length<5)
      {
    		alert("Il codice postale inserito non e' corretto.");
         theForm.elements["personalData.cap"].focus();
    		return  false;
    	}
    /** Perfezionare il controllo identityDocument.tipo:
     *  0 -> Carta d'identita'
     *  1 -> Patente
     *  2 -> Passaporto
     */
    else if (	theForm.elements["identityDocument.numero"].value=="" ||
    		theForm.elements["identityDocument.numero"].value.search(/\S/)==-1)
      {
        alert("Verifica le seguenti informazioni: Numero documento");
        theForm.elements["identityDocument.numero"].focus();
        return false;
      }
    else if (	nazionalita=="" || nazionalita.search(/\S/)==-1)
      {
        alert("Verifica le seguenti informazioni: Nazione documento");
        theForm.elements["identityDocument.nazionalita"].focus();
        return false;
      }

 		if (typeProduct==PRODRIC){
      if (!checkPianoTel(theForm,PRODRIC)){
        return false;
      }
    }
		if (typeProduct==PRODSIM){
      if (theForm.elements["personalData.recapitotel"].value.search(/\S/)==-1 ||
          theForm.elements["personalData.recapitotel"].value.match(/\D/)){

        alert("Recapito telefonico non valido");
        theForm.elements["personalData.recapitotel"].focus();
        return false;
      }
      if (!checkPianoTel(theForm,PRODSIM)){
        return false;
      }
    }
 	}

 if (typeUser==1)	// Libero professionista
 	{
	  // ------------ recupero le variabili per il check -----------

     try
     	{
        var giornoNascita = theForm.elements["rappresentanteLegale.birthDay"][theForm.elements["rappresentanteLegale.birthDay"].selectedIndex].value;
    	  var meseNascita = theForm.elements["rappresentanteLegale.birthMonth"][theForm.elements["rappresentanteLegale.birthMonth"].selectedIndex].value;
    	  var annoNascita = theForm.elements["rappresentanteLegale.birthYear"][theForm.elements["rappresentanteLegale.birthYear"].selectedIndex].value;
     	}
     catch (e)
     	{
    	  var giornoNascita = "RO";
    	  var meseNascita = "RO";
    	  var annoNascita = "RO";
     	}

 	  var reg_province = theForm.elements["iscrizioneRegistro.provincia"].options[theForm.elements["iscrizioneRegistro.provincia"].selectedIndex].value;      
	  
	  // ------------
	  
	  if (nome.value=="" || nome.value.search(/\S/)==-1)
	    {
	      alert("Verifica le seguenti informazioni: Nome");
	      nome.focus();
        return false;
      }
    else if (cognome.value=="" || cognome.value.search(/\S/)==-1)
      {
	      alert("Verifica le seguenti informazioni: Cognome");
	      cognome.focus();
        return false;
      }
    else if (indirizzo.value=="" || indirizzo.value.search(/\S/)==-1)
      {
	      alert("Verifica le seguenti informazioni: Indirizzo");
	      indirizzo.focus();
        return false;
      }
    else if (provincia=="" || provincia.search(/\S/)==-1)
      {
	      alert("Verifica le seguenti informazioni: Provincia");
	      // inserire la funzione di controllo CF
	      theForm.elements["personalData.provincia"].focus();
        return false;
      }
    else if (comune=="" || comune.search(/\S/)==-1 || comune.length>40)
      {
      	if ( comune.length>40 )
   		   alert("Il comune impostato non e' corretto");
	      else
		      alert("Verifica le seguenti informazioni: Comune");
	      // inserire la funzione di controllo CF
	      theForm.elements["personalData.comune"].focus();
        return false;
      }
    else if (cap.match(/\D/) || cap.length<5)
      {
    		alert("Il codice postale inserito non e' corretto.");
        theForm.elements["personalData.cap"].focus();
    		return false;
    	}
    else if (theForm.elements["personalData.partitaiva"].value.search(/\S/)==-1 ||
            !partitaIVA(theForm.elements["personalData.partitaiva"].value))
      {
        alert("La partita IVA inserita non e' corretta.");
        theForm.elements["personalData.partitaiva"].focus();
    		return false;
      }
    else if (	theForm.elements["iscrizioneRegistro.numero"].value=="" || 
    		theForm.elements["iscrizioneRegistro.numero"].value.search(/\S/)==-1)
      {
        alert("Verifica le seguenti informazioni: Numero registro imprese.");
        theForm.elements["iscrizioneRegistro.numero"].focus();
        return false;
      }
    else if (theForm.elements["iscrizioneRegistro.anno"].value.search(/\S/)==-1 &&
             (theForm.elements["iscrizioneRegistro.anno"].value.match(/\D/) || theForm.elements["iscrizioneRegistro.anno"].value.length!=4))
      {
        alert("Verifica le seguenti informazioni: Anno iscrizione registro imprese.");
        theForm.elements["iscrizioneRegistro.anno"].focus();
        return false;
      }
    else if (	reg_province=="" || reg_province.search(/\S/)==-1)
      {
        alert("Verifica le seguenti informazioni: Provincia");
        theForm.elements["iscrizioneRegistro.provincia"].focus();
        return false;
      }
    /** Perfezionare il controllo identityDocument.tipo:
     *  0 -> Carta d'identita'
     *  1 -> Patente
     *  2 -> Passaporto
     */
    else if (	theForm.elements["identityDocument.numero"].value=="" || 
    		theForm.elements["identityDocument.numero"].value.search(/\S/)==-1)
      {
        alert("Verifica le seguenti informazioni: Numero documento");
        theForm.elements["identityDocument.numero"].focus();
        return false;
      }
    else if (	nazionalita=="" || nazionalita.search(/\S/)==-1)
      {
        alert("Verifica le seguenti informazioni: Nazione documento");
        theForm.elements["identityDocument.nazionalita"].focus();
        return false;
      }

 		if (typeProduct==PRODRIC)
 			{
      		if (!checkPianoTel(theForm,PRODRIC))
      			{
        			return false;
      			}
    		}
		if (typeProduct==PRODSIM){
      if (	theForm.elements["rappresentanteLegale.nome"].value=="" || 
      		theForm.elements["rappresentanteLegale.nome"].value.search(/\S/)==-1)
        {
          alert("Verifica le seguenti informazioni: Nome del rappresentante legale");
          theForm.elements["rappresentanteLegale.nome"].focus();
          return false;
        }
      else if (!theForm.elements["rappresentanteLegale.sesso"][0].checked && !theForm.elements["rappresentanteLegale.sesso"][1].checked)
        {
      		alert("Verifica le seguenti informazioni: Sesso del rappresentante legale");
      		return false;
        }
      else if (!isYMDValid(giornoNascita,meseNascita,annoNascita))
      	{
      		alert("La data di nascita inserita non e' corretta.");
      		return  false;
      	}
      else if (	theForm.elements["rappresentanteLegale.posizioneaziendale"].value=="" ||
      		theForm.elements["rappresentanteLegale.posizioneaziendale"].value.search(/\S/)==-1)
        {
          alert("Verifica le seguenti informazioni: Posizione aziendale");
          theForm.elements["rappresentanteLegale.posizioneaziendale"].focus();
          return false;
        }
      if (!checkPianoTel(theForm,PRODSIM)){
        return false;
      }
    }
 	}

 if (typeUser==2)	// Azienda
 	{

	  // ------------ recupero le variabili per il check -----------
 	
     try
     	{
        var giornoNascita = theForm.elements["rappresentanteLegale.birthDay"][theForm.elements["rappresentanteLegale.birthDay"].selectedIndex].value;
    	  var meseNascita = theForm.elements["rappresentanteLegale.birthMonth"][theForm.elements["rappresentanteLegale.birthMonth"].selectedIndex].value;
    	  var annoNascita = theForm.elements["rappresentanteLegale.birthYear"][theForm.elements["rappresentanteLegale.birthYear"].selectedIndex].value;
     	}
     catch (e)
     	{
    	  var giornoNascita = "RO";
    	  var meseNascita = "RO";
    	  var annoNascita = "RO";
     	}

 	 var reg_province = theForm.elements["iscrizioneRegistro.provincia"].options[theForm.elements["iscrizioneRegistro.provincia"].selectedIndex].value;      

	 // -------------
	 
    if (theForm.elements["personalData.denominazione"].value=="" ||
    	theForm.elements["personalData.denominazione"].value.search(/\S/)==-1 || 
    	theForm.elements["personalData.denominazione"].length>14)
      {
        alert("Verifica le seguenti informazioni: Denominazione");
        theForm.elements["personalData.denominazione"].focus();
        return false;
      }
    else if (	theForm.elements["personalData.formagiuridica"].value=="" || 
    		theForm.elements["personalData.formagiuridica"].value.search(/\S/)==-1)
      {
        alert("Verifica le seguenti informazioni: Forma giuridica");
        theForm.elements["personalData.formagiuridica"].focus();
        return false;
      }
    else if (indirizzo.value=="" || indirizzo.value.search(/\S/)==-1)
      {
	      alert("Verifica le seguenti informazioni: Indirizzo");
	      indirizzo.focus();
        return false;
      }
    else if (provincia=="" || provincia.search(/\S/)==-1)
      {
	      alert("Verifica le seguenti informazioni: Provincia");
	      // inserire la funzione di controllo CF
	      theForm.elements["personalData.provincia"].focus();
        return false;
      }
    else if (comune=="" || comune.search(/\S/)==-1 || comune.length>40)
      {
      	if ( comune.length>40 )
   		   alert("Il comune impostato non e' corretto");
	      else
		      alert("Verifica le seguenti informazioni: Comune");
	     theForm.elements["personalData.comune"].focus();
        return false;
      }
    else if (cap.match(/\D/) || cap.length<5)
      {
    		alert("Il codice postale inserito non e' corretto.");
         theForm.elements["personalData.cap"].focus();
    		return false;
    	}
    else if (theForm.elements["personalData.partitaiva"].value.search(/\S/)==-1 ||
            !partitaIVA(theForm.elements["personalData.partitaiva"].value))
      {
        alert("La partita IVA inserita non e' corretta.");
        theForm.elements["personalData.partitaiva"].focus();
    	  return false;
      }
    else if (	theForm.elements["iscrizioneRegistro.numero"].value=="" || 
    		theForm.elements["iscrizioneRegistro.numero"].value.search(/\S/)==-1)
      {
        alert("Verifica le seguenti informazioni: Numero registro imprese.");
        theForm.elements["iscrizioneRegistro.numero"].focus();
        return false;
      }
    else if (theForm.elements["iscrizioneRegistro.anno"].value.search(/\S/)==-1 &&
             (theForm.elements["iscrizioneRegistro.anno"].value.match(/\D/) || theForm.elements["iscrizioneRegistro.anno"].value.length!=4))
      {
        alert("Verifica le seguenti informazioni: Anno iscrizione registro imprese");
        theForm.elements["iscrizioneRegistro.anno"].focus();
        return false;
      }
    else if (	reg_province=="" || reg_province.search(/\S/)==-1)
      {
        alert("Verifica le seguenti informazioni: Provincia");
        theForm.elements["iscrizioneRegistro.provincia"].focus();
        return false;
      }
    /** Perfezionare il controllo identityDocument.tipo:
     *  0 -> Carta d'identita'
     *  1 -> Patente
     *  2 -> Passaporto
     */
    else if (	theForm.elements["identityDocument.numero"].value=="" ||
    		theForm.elements["identityDocument.numero"].value.search(/\S/)==-1)
      {
        alert("Verifica le seguenti informazioni: Numero documento");
        theForm.elements["identityDocument.numero"].focus();
        return false;
      }
    else if (	nazionalita=="" || nazionalita.search(/\S/)==-1)
      {
        alert("Verifica le seguenti informazioni: Nazione documento");
        theForm.elements["identityDocument.nazionalita"].focus();
        return false;
      }

    if (typeProduct==PRODRIC){
      if (theForm.elements["rappresentanteLegale.nome"].value=="" ||
      	  theForm.elements["rappresentanteLegale.nome"].value.search(/\S/)==-1)
        {
          alert("Verifica le seguenti informazioni: Nome del rappresentante legale");
          theForm.elements["rappresentanteLegale.nome"].focus();
          return false;
        }
      else if (theForm.elements["rappresentanteLegale.cognome"].value=="" ||
      	  theForm.elements["rappresentanteLegale.cognome"].value.search(/\S/)==-1)
        {
          alert("Verifica le seguenti informazioni: Nome del rappresentante legale");
          theForm.elements["rappresentanteLegale.cognome"].focus();
          return false;
        }
      else if (theForm.elements["rappresentanteLegale.posizioneaziendale"].value=="" ||
      	  theForm.elements["rappresentanteLegale.posizioneaziendale"].value.search(/\S/)==-1)
        {
          alert("Verifica le seguenti informazioni: Nome del rappresentante legale");
          theForm.elements["rappresentanteLegale.posizioneaziendale"].focus();
          return false;
        }
    
    
      if (!checkPianoTel(theForm,PRODRIC))
      	{
        		return false;
      	}
    	}
		if (typeProduct==PRODSIM)
		{
		
      if (theForm.elements["rappresentanteLegale.nome"].value=="" ||
      	  theForm.elements["rappresentanteLegale.nome"].value.search(/\S/)==-1)
        {
          alert("Verifica le seguenti informazioni: Nome del rappresentante legale");
          theForm.elements["rappresentanteLegale.nome"].focus();
          return false;
        }
      else if (!theForm.elements["rappresentanteLegale.sesso"][0].checked && !theForm.elements["rappresentanteLegale.sesso"][1].checked)
        {
      		alert("Verifica le seguenti informazioni: Sesso del rappresentante legale");
      		return false;
        }
      else if (!isYMDValid(giornoNascita,meseNascita,annoNascita))
      	{
      		alert("La data di nascita inserita non e' corretta.");
      		return  false;
      	}
      else if (theForm.elements["rappresentanteLegale.comune"].value=="" || 
      		theForm.elements["rappresentanteLegale.comune"].value.search(/\S/)==-1)
        {
          alert("Verifica le seguenti informazioni: Comune del rappresentante legale");
          theForm.elements["rappresentanteLegale.comune"].focus();
          return false;
        }
      else if (theForm.elements["rappresentanteLegale.posizioneaziendale"].value=="" ||
      		theForm.elements["rappresentanteLegale.posizioneaziendale"].value.search(/\S/)==-1)
        {
          alert("Verifica le seguenti informazioni: Posizione aziendale");
          theForm.elements["rappresentanteLegale.posizioneaziendale"].focus();
          return false;
        }
      if (!checkPianoTel(theForm,PRODSIM)){
        return false;
      }
    }
 	}

 return true;
}

//---- Validazione dati riepilogo pagamento ricarica
function submitPaymentRecharge(dataForm)
{
  var tipoPayment = -1;

  for (i=0; i<dataForm.elements["tipopag"].length; i++){
    if (dataForm.elements["tipopag"][i].checked){
      tipoPayment = dataForm.elements["tipopag"][i].value;
      break;
    }
  }

  if(tipoPayment == -1){
  	tipoPayment = dataForm.elements["tipopag"].value;
  }
  
  if (tipoPayment==-1)
  	{
  		alert("Selezionare la metodologia di pagamento.");
  		return false;
  	}

  if (tipoPayment==1)	// Cdc
  	{
	  var typeCarta = dataForm.elements["creditcardname"].options[dataForm.elements["creditcardname"].selectedIndex].value;
	  var numCarta = dataForm.elements["cardno"].value;
	  var annoScad =  dataForm.elements["creditCardExpYear"].options[dataForm.elements["creditCardExpYear"].selectedIndex].value;
	  var meseScad = dataForm.elements["creditCardExpMesi"].options[dataForm.elements["creditCardExpMesi"].selectedIndex].value;
	  var owner = dataForm.elements["nominativo"].value;
	  var numSec =  dataForm.elements["numsic"].value;
	  var codFiscale = dataForm.elements["codfiscale"].value;

	  if (!verifyCard(numCarta,typeCarta) || numCarta=="")
	  	{
	  		//alert("Il numero della Carta di Credito inserito non e' corretto.");
	  		return false;
	  	}

	  if (meseScad==0)
	  	{
	  		alert("Verifica le seguenti informazioni: Mese scadenza");
	  		return false;
	  	}

	  if (annoScad==0)
	  	{
	  		alert("Verifica le seguenti informazioni: Anno scadenza");
	  		return false;
	  	}

    if(!isNotExpired(meseScad, annoScad))
      {
        return false;
      }

	  if (owner=="" || owner.search(/\S/)==-1)
	  	{
  			alert("Verifica le seguenti informazioni: Nome e Cognome");
  			return false;
  		}
    	else if(!checkOwner(owner))
    		{
    			alert("Il Nome e Cognome devono contenere solo caratteri alfabetici.");
  				return false;
    		}

	  if (numSec=="")
	  	{
	  		alert("Verifica le seguenti informazioni: Numero di sicurezza.");
	  		return false;
	  	}
	  else if (numSec.search(/\S/) == -1 || numSec.match(/\D/) )
	    	{
	    		alert("Il Numero di Sicurezza deve contenere solo caratteri numerici.");
	    		return false;
	    	}
	  else if((""+numSec).length!=3 && (""+numSec).length!=4)
	  	{
	  		alert("Verifica le seguenti informazioni: Numero di sicurezza.");
	  		return false;
	  	}
	  else if(!verifyCVV(typeCarta,numSec))
	  	{
  			alert("Verifica le seguenti informazioni: Numero di sicurezza.");
  			return false;
  		}

	  if (codFiscale=="" )
	  	{
	  		alert("Verifica le seguenti informazioni: Codice Fiscale");
  			return  false;
	  	}
	  
	 // **** AM 02092003 -- aggiunta codFiscale.search(/\S/) == -1
	  if(codFiscale.search(/\S/) == -1 || !fiscale(codFiscale,"no"))
	  	{
	  		alert("Il codice fiscale inserito non e' corretto.");
	  		return  false;
	  	}
  	}
  else if (tipoPayment==3) // Omnipay onphone
  	{
		var omnipayPhonePref = dataForm.elements["pre_omnipay"].options[dataForm.elements["pre_omnipay"].selectedIndex].value;
		var omnipayPhoneNumber = ""+omnipayPhonePref+dataForm.elements["num_omnipay"].value;
  		    
		if (omnipayPhonePref=="")
			{
      	alert("Verifica le seguenti informazioni: Prefisso Numero Omnipay");
  			return false;
			}	
      if (omnipayPhoneNumber=="")
      	{
      	alert("Verifica le seguenti informazioni: Numero Omnipay");
  			return false;
    		}
  	   else if ((omnipayPhoneNumber.length!=9 && omnipayPhoneNumber.length!=10) || isNaN(omnipayPhoneNumber))
  			{
  				alert("Il numero di telefono inserito non e' un numero Omnipay OnPhone.");
  				return false;
  			}
  	}
  else if (tipoPayment==2 || tipoPayment==5)	// 2:bonifico ; 5:ewallet
  	{
  		return true;
  	}

 return true;
}

//Validazione form Dillo a un amico Mail
function controllaFormMail(formObj)
	{
		var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
		if ((formObj.fname.value == "") || (formObj.fname.value == "undefined"))
		{
			alert("Inserisci il nome del tuo amico");
      formObj.fname.focus();
			return false;
		}
		if (!email_reg_exp.test(formObj.fmail.value) || (formObj.fmail.value == "") || (formObj.fmail.value == "undefined"))
		{
			alert("Inserire un indirizzo email corretto");
      formObj.fmail.focus();
			return false;
		}
		if (formObj.fautorizzo.checked == false)
		{
			alert("Devi accettare le Regole della Netiquette");
      formObj.fautorizzo.focus();
			return false;
		}

		return true;
	}

// Validazione del numero Omnione in dati spedizione fatturazione
function submitShippingData(theForm)
	{
		var omnioneChecked = theForm.elements["omnioneRegistration"][0].checked;
		var omnioneNotChecked = theForm.elements["omnioneRegistration"][1].checked;

		if (omnioneChecked)
			{
				var omnionePhonePref = theForm.elements["prefix"].options[theForm.elements["prefix"].selectedIndex].value;
				var omnionePhoneNumber = ""+omnionePhonePref+theForm.elements["number"].value;
		  		if ((omnionePhoneNumber.length!=9 && omnionePhoneNumber.length!=10) || isNaN(omnionePhoneNumber))
		  			{
		  				alert("Numero Vodafone One non valido.");
		  				return false;
		  			}
			}

  		return true;
	}


//--------- Controlli sui piani telefonici ------------------
function checkPianoTel(theForm,typeProduct){

  var PRODRIC = "R";
  var PRODSIM = "S";

	if (typeProduct==PRODRIC)
	{

    var tipoPiano = theForm.elements["simData.idPianoTelefonico"][0][theForm.elements["simData.idPianoTelefonico"][0].selectedIndex].value;

	 var idlayer = -1;
	 
	 
	 /*
	 if (tipoPiano=="RINFINITY" || tipoPiano=="ALTINFINITY" || tipoPiano=="BISINFINITY")
	 		idlayer = 0;
	 if (tipoPiano=="RHAPPY" || tipoPiano=="ALTHAPPY" || tipoPiano=="BISHAPPY")
	 		idlayer = 1;
	 else	if (tipoPiano=="RINFTOPRIC" || tipoPiano=="ALTINFTOPRIC" || tipoPiano=="BISINFTOPRIC")
	 		idlayer = 2;	
	 else	if (tipoPiano=="RFLATSIMPLY" || tipoPiano=="ALTFLATSIMPLY" || tipoPiano=="BISFLATSIMPLY")
	 		idlayer = 3;	
	 else	if (tipoPiano=="RAUTOCHIAMAS" || tipoPiano=="ALTAUTOCHIAMA" || tipoPiano=="BISCHIAMASUPER")
	 		idlayer = 4;		 		
	 else	if (tipoPiano=="REASY" || tipoPiano=="ALTEASY" || tipoPiano=="BISEASY")
	 		idlayer = 5;	
	 else	if (tipoPiano=="RNATION" || tipoPiano=="ALTERNATION" || tipoPiano=="BISNATION")
	 		idlayer = 6;	
	 */
	 
	 // read id layer from hidden form field
	 var idlayer = -1;
	 try
	 {
	  idlayer = parseInt(theForm["idLayer"+tipoPiano].value);
	  if (isNaN(idlayer))
	  {
  	  idlayer=-1;
	  }
	 }
	 catch(er)
	 {
	  idlayer=-1;
	 }
	 
	 if (tipoPiano!="0")
	 	{
				pref = theForm.elements["simData.elOpzPianoTel1.datoopzione1.prefisso"][idlayer].value;
				numero = theForm.elements["simData.elOpzPianoTel1.datoopzione1.numero"][idlayer].value;
				if (pref=="" && numero!="")
					{
			        alert("Prefisso del numero telefonico numero non valido.");
			        return false;
					}
							
				if (theForm.elements["simData.elOpzPianoTel1.datoopzione1.numero"][idlayer].value.search(/\S/)!=-1 &&
				    ((theForm.elements["simData.elOpzPianoTel1.datoopzione1.numero"][idlayer].value.length!=6 &&
				      theForm.elements["simData.elOpzPianoTel1.datoopzione1.numero"][idlayer].value.length!=7) ||
	    			 theForm.elements["simData.elOpzPianoTel1.datoopzione1.numero"][idlayer].value.match(/\D/)))
	    			 {	
						alert("Attenzione! Il numero telefonico deve essere di sei o sette cifre.");
						return false;
					}
	 	}
	 else
	 	{
			alert("Selezionare il piano.");
			return false;
	 	}	 			   			  			
 	}

  return true;
}

function submitRR(theForm)
{

  var nome = theForm.elements["shipment.invoicingName"].value;
  var codFiscale = theForm.elements["shipment.fiscalCode"].value;
  var indirizzo = theForm.elements["shipment.invoicingAddress"].value;
  var comune = theForm.elements["shipment.invoicingCity"].options[theForm.elements["shipment.invoicingCity"].selectedIndex].value;
  var provincia = theForm.elements["shipment.invoicingProvince"].options[theForm.elements["shipment.invoicingProvince"].selectedIndex].value;
  var cap = theForm.elements["shipment.invoicingZipCode"].options[theForm.elements["shipment.invoicingZipCode"].selectedIndex].value;
  var ragioneSociale = theForm.elements["shipment.billingInformation"].value;
  var formaGiuridica = theForm.elements["shipment.legalPosition"].value;

  var err_arr = new Array();
  
  if ( (nome=="" || nome.search(/\S/)==-1 )
  	&&  ( ragioneSociale=="" || ragioneSociale.search(/\S/)==-1 ) )
	{

		err_arr["shipment.invoicingName"]="err_nome";
		err_arr["shipment.billingInformation"]="err_ragSociale";
	}
	else if(!checkRRField(nome))
  {
  	err_arr["shipment.invoicingName"]="err_nome_alfa";
  }
  if (codFiscale.search(/\S/)==-1)
	{
		err_arr["shipment.fiscalCode"]="err_cf";
	}
  if((!fiscale(codFiscale,"no") && !partitaIVA(codFiscale)))
	{
		err_arr["shipment.fiscalCode"]="err_uncorrect_cf";
	}
  if (indirizzo=="")
	{
		err_arr["shipment.invoicingAddress"]="err_ind";
	}
	else if(!checkRRAddress(indirizzo))
  {
  	err_arr["shipment.invoicingAddress"]="err_ind_alfa";
  }
  if (provincia=="")
	{
		err_arr["shipment.invoicingProvince"]="err_prov";
	}
	else if (provincia.length!=2 || !isNaN(provincia))
	{
		err_arr["shipment.invoicingProvince"]="err_uncorrect_prov";
	}
  if (comune=="")
	{
		err_arr["shipment.invoicingCity"]="err_comune";
	}
	else if(!checkRRField2(comune))
  {
  	err_arr["shipment.invoicingCity"]="err_comune_alfa";
  }
  if (cap=="")
	{
		err_arr["shipment.invoicingZipCode"]="err_cp";
	}
  if (isNaN(cap) || (""+cap).length!=5)
	{
		err_arr["shipment.invoicingZipCode"]="err_uncorrect_cp";
	}

	return err_arr;
}