function showConfirmDialog(logged, ship) {
	var confirmButton = logged ? jQuery("a[id$='loggedConfirm']") : jQuery("a[id$='noLoggedConfirm']");
	var spanError = logged ? jQuery("span[id$='loggedError']") :  jQuery("span[id$='noLoggedError']");
	 
	var confirmButton2 = logged ? jQuery("a[id$='loggedConfirm2']") : jQuery("a[id$='noLoggedConfirm']");
	var spanError2 = logged ? jQuery("span[id$='loggedError2']") :  jQuery("span[id$='noLoggedError']");
		
	//reset error message
	spanError.html("");
	spanError2.html("");
	
	//show confirm button
	confirmButton.show();
	confirmButton2.show();
	
	if (!checkCreditCardData(confirmButton, spanError)) {
		//hide confirm button
		 confirmButton.hide();
		 confirmButton2.hide();
	}
	
	var idSped = jQuery("input:[name*='selectedMemCard']:checked").val();


	if(idSped!='-1'){		
		var radioSpedMod = jQuery("input:[name*='radioSped_mod_"+idSped+"']:checked").val();
		
	
		if (logged && ship && radioSpedMod == 'UtilizzoOther') {
			dialog10.show();
			popolaSpedizione(idSped);
			confirmButton.show();
			
		}else if(logged && ship && radioSpedMod == 'UtilizzoDati'){
			
			dialog30.show();
			popolaSpedizione(idSped);
			confirmButton2.show();
			
		}else{
			fillAndCheckRegData(confirmButton, spanError);
			dialog20.show();
		}
		
	}else{
		
		clearUpRiepilogo();
		var radioSped = jQuery("input:[name*='radioSped_new_address']:checked").val();

		if (logged && ship && radioSped == 'UtilizzoOther') {
			dialog10.show();
			checkShippingData(confirmButton, spanError);			
		}else if(logged && ship && radioSped == 'UtilizzoDati'){
			dialog30.show();
			checkShippingData(confirmButton2, spanError2);
		}else{
		
		// Parte della non registrazione
		
			if(radioSped == 'UtilizzoOther'){
			
			checkShippingData(confirmButton, spanError);
			fillAndCheckRegData(confirmButton, spanError);
			dialog20.show();
			
			
			}else if(radioSped == 'UtilizzoDati'){
			
				checkShippingData(confirmButton, spanError);
				fillAndCheckRegData(confirmButton, spanError);
				dialog20.show();
			
					}else{
					
				fillAndCheckRegData(confirmButton, spanError);
				dialog20.show();
					
			
		}
		track('resume');
	}
}

}

function saveShipModified(id){
		checkShippingData2(id);
}

function clearUpRiepilogo(){
	
	jQuery("#riepilogoDatiShipName2").html(" ");
	jQuery("#riepilogoDatiShipIndirizzo2").html(" ");
	jQuery("#riepilogoDatiShipCap2").html(" ");
	jQuery("#riepilogoDatiShipCodFis2").html(" ");
	jQuery("#riepilogoDatiShipTel2").html(" ");
	
	jQuery("#riepilogoDatiFatRegioneSociale").html(" ");
	jQuery("#riepilogoDatiFatName").html(" ");
	jQuery("#riepilogoDatiFatpPartitaIva").html(" ");
	jQuery("#riepilogoDatiFatIndirizzo").html(" ");
	jQuery("#riepilogoDatiFatCap").html(" ");
	jQuery("#riepilogoDatiFatCodFis").html(" ");
	jQuery("#riepilogoDatiFatTel").html(" ");
	
	jQuery("#riepilogoDatiShipName").html(" ");
	jQuery("#riepilogoDatiShipIndirizzo").html(" ");
	jQuery("#riepilogoDatiShipCap").html(" ");
	jQuery("#riepilogoDatiShipCodFis").html(" ");
	jQuery("#riepilogoDatiShipTel").html(" ");


}

function popolaSpedizione(id){

	var typeSelectVal = jQuery("input:[name*='radioSped_mod_"+id+"']:checked").val();
	var nome = jQuery("input:[name*='"+id+":0:nome_send_mod']").val();

	var cognome = jQuery("input:[name*='"+id+":0:cognome_send_mod']").val();
	var indirizzo = jQuery("input:[name*='"+id+":0:indirizzo_send_mod']").val();
	var cap = jQuery("input:[name*='"+id+":0:cap_send_mod']").val();
	var comune = jQuery("input:[name*='"+id+":0:comune_send_mod']").val();
	var provincia = jQuery("input:[name*='"+id+":0:provincia_send_mod']").val();
	var codfis = jQuery("input:[name*='"+id+":0:fiscal_send_mod']").val();
	var tel = jQuery("input:[name*='"+id+":0:tel_send_mod']").val();
	
	var regSociale = jQuery("input:[name*='"+id+":0:reg_sociale_bill_mod']").val();
		var partitaIva = jQuery("input:[name*='"+id+":0:partitaIva_bill_mod']").val();
		var indirizzof = jQuery("input:[name*='"+id+":0:indirizzo_bill_mod']").val();
		var capf = jQuery("input:[name*='"+id+":0:cap_bill_mod']").val();
		var comunef = jQuery("input:[name*='"+id+":0:comune_bill_mod']").val();
		var provinciaf = jQuery("input:[name*='"+id+":0:provincia_bill_mod']").val();

	var radioSpedModPopol = jQuery("input:[name*='radioSped_mod_"+id+"']:checked").val();
	
	/*if(radioSpedModPopol == 'UtilizzoOther'){
	
		 jQuery("#riepilogoDatiShipName").html(nome+" "+cognome);
		 jQuery("#riepilogoDatiShipIndirizzo").html(indirizzo);
		 jQuery("#riepilogoDatiShipCap").html(cap+" "+comune+"("+provincia+")");
		 jQuery("#riepilogoDatiShipCodFis").html(codfis);
		 jQuery("#riepilogoDatiShipTel").html(tel);
		 
		 jQuery("#riepilogoDatiFatRegioneSociale").html(regSociale);
		 jQuery("#riepilogoDatiFatName").html(nome+" "+cognome);
		 jQuery("#riepilogoDatiFatpPartitaIva").html(partitaIva);
		 jQuery("#riepilogoDatiFatIndirizzo").html(indirizzof);
		 jQuery("#riepilogoDatiFatCap").html(capf+" "+comunef+"("+provinciaf+")");
		 jQuery("#riepilogoDatiFatCodFis").html(codfis);
		 jQuery("#riepilogoDatiFatTel").html(tel);
		 
	}else{
	
		 jQuery("#riepilogoDatiShipName2").html(nome+" "+cognome);
		 jQuery("#riepilogoDatiShipIndirizzo2").html(indirizzo);
		 jQuery("#riepilogoDatiShipCap2").html(cap+" "+comune+"("+provincia+")");
		 jQuery("#riepilogoDatiShipCodFis2").html(codfis);
		 jQuery("#riepilogoDatiShipTel2").html(tel);
	
	
	
	}*/

}


function checkShippingData2(id)
{
	var check = true;
	
	
	
	//spanError2.html('');
	confirmButton2.show();
	
	var typeSelectVal = jQuery("input:[name*='radioSped_mod_"+id+"']:checked").val();
	var nome = jQuery("input:[name*='"+id+":nome_send_mod']").val();

	var cognome = jQuery("input:[name*='"+id+":cognome_send_mod']").val();
	var indirizzo = jQuery("input:[name*='"+id+":indirizzo_send_mod']").val();
	var cap = jQuery("input:[name*='"+id+":cap_send_mod']").val();
	var comune = jQuery("input:[name*='"+id+":comune_send_mod']").val();
	var provincia = jQuery("input:[name*='"+id+":provincia_send_mod']").val();
	var codfis = jQuery("input:[name*='"+id+":fiscal_send_mod']").val();
	var tel = jQuery("input:[name*='"+id+":tel_send_mod']").val();



	if(typeSelectVal == 'UtilizzoOther'){
		var regSociale = jQuery("input:[name*='"+id+":reg_sociale_bill_mod']").val();
		var partitaIva = jQuery("input:[name*='"+id+":partitaIva_bill_mod']").val();
		var indirizzof = jQuery("input:[name*='"+id+":indirizzo_bill_mod']").val();
		var capf = jQuery("input:[name*='"+id+":cap_bill_mod']").val();
		var comunef = jQuery("input:[name*='"+id+":comune_bill_mod']").val();
		var provinciaf = jQuery("input:[name*='"+id+":provincia_bill_mod']").val();


		jQuery("#errorecampoFatRegioneSociale"+id).html("");
		jQuery("#errorecampoFatPartitaIva"+id).html("");
		jQuery("#errorecampoFatIndirizzo"+id).html("");
		jQuery("#errorecampoFatCap"+id).html("");
		jQuery("#errorecampoFatComune"+id).html("");
		jQuery("#errorecampoFatComune"+id).html("");
		jQuery("#errorecampoFatProvincia"+id).html("");

	 
		if(isBlank(regSociale)){
		 	//show error message
			jQuery("#errorecampoFatRegioneSociale"+id).html("Ragione Sociale obbligatorio");
			check = false;
		 
		}else if(isBlank(partitaIva)){
			//show error message
			jQuery("#errorecampoFatPartitaIva"+id).html("Partita Iva obbligatorio");
			check = false;

		}else if(isBlank(indirizzof)){
		
			//show error message
		 		 jQuery("#errorecampoFatIndirizzo"+id).html("Indirizzo Fatturazione obbligatorio");
					check = false;

		}else if(isBlank(capf)){
		
			//show error message
		 jQuery("#errorecampoFatCap"+id).html("Cap Fatturazione obbligatorio");
				check = false;

		}else if(isBlank(comunef)){
			check = false;
			//show error message
			jQuery("#errorecampoFatComune"+id).html("Comune Fatturazione obbligatorio");
		}else if(isBlank(provinciaf)){
		
			//show error message
		jQuery("#errorecampoFatProvincia"+id).html("Provincia Fatturazione obbligatorio");
					check = false;
		}else{
					check = true;
		 
		}




	}


	jQuery("#errorecampoFirstName"+id).html(" ");
	jQuery("#errorecampoLastName"+id).html(" ");
	jQuery("#errorecampoIndirizzo"+id).html(" ");
	jQuery("#errorecampoCap"+id).html(" ");
	jQuery("#errorecampoComune"+id).html(" ");
	jQuery("#errorecampoProvincia"+id).html(" ");
	jQuery("#errorecampoTelefono"+id).html(" ");
	jQuery("#errorecampoCodFiscale"+id).html(" ");


	if(isBlank(nome)){

				check = false;
			
			 //show error message
			 jQuery("#errorecampoFirstName"+id).html("FirstName obbligatorio");

				}else if(isBlank(cognome)){

						check = false;
			
			 jQuery("#errorecampoLastName"+id).html("LastName obbligatorio");

			 
						}else if(isBlank(indirizzo) ){
							
							check = false;
						//show error message
					 jQuery("#errorecampoIndirizzo"+id).html("Indirizzo obbligatorio");

						
						}else if(isBlank(cap)){
						
						check = false;

				//show error message
					 jQuery("#errorecampoCap"+id).html("Cap obbligatorio");

						
						}else if(isBlank(comune)){ 
						
						check = false;
						
			//show error message
			 jQuery("#errorecampoComune"+id).html("Comune obbligatorio");
				
						
			
						}else if(isBlank(provincia)){
						
			check = false;
			 jQuery("#errorecampoProvincia"+id).html("Provincia obbligatorio");
			
						
						}else if(isBlank(tel)) {
								check = false;
			//show error message
					jQuery("#errorecampoTelefono"+id).html("Telefono obbligatorio");

								check = false;
						}else if(isBlank(codfis)){
						
		//show error message
			 jQuery("#errorecampoCodFiscale"+id).html("Codice Fiscale obbligatorio");

						
						
	}else{
		check = true;
		// Ulteriore aggiornamento
	}
	
	return check;

}




function checkShippingData(confirmButton, spanError){

	//reinizializzo lo spanErrore e il confirmButton
	spanError.html('');
	confirmButton.show();
	
	
	var typeClient = jQuery("input:[name*='typeClient']:checked").val();
	var typeSelectVal = jQuery("input:[name*='radioSped_new_address']:checked").val();
	var nome = jQuery("input:[name*='nome_send_new']").val();
	var cognome = jQuery("input:[name*='cognome_send_new']").val();
	var indirizzo = jQuery("input:[name*='indirizzo_send_new']").val();
	var cap = jQuery("input:[name*='cap_send_new']").val();
	var comune = jQuery("input:[name*='comune_send_new']").val();
	var provincia = jQuery("select:[name*='provincia_send_new']").val();
	var codfis = jQuery("input:[name*='fiscal_send_new']").val();
	var tel = jQuery("input:[name*='tel_send_new']").val();

	var type = jQuery("input:[name*='typeClient']:checked").val();

	var note = jQuery("textarea:[name*='note_send_new']").val();


	
	if(typeSelectVal == 'UtilizzoOther'){

		
		var regSociale = jQuery("input:[name*='reg_sociale_bill_new']").val();
		
		
		if(typeClient == 'Privato'){
		
		//aggiunto cognome
		var cognomeBill = jQuery("input:[name*='l:cognome_bill']").val();
		
		if(isBlank(cognomeBill)){
			
				//show error message
			 spanError.append("Verifica dati fatturazione<br/>");
			 //hide confirm button
			 confirmButton.hide();

			}
		
		}
		
		var partitaIva = jQuery("input:[name*='partitaIva_bill_new']").val();
		var indirizzof = jQuery("input:[name*='indirizzo_bill_new']").val();
		var capf = jQuery("input:[name*='cap_bill_new']").val();
		var comunef = jQuery("input:[name*='comune_bill_new']").val();
		var provinciaf = jQuery("select:[name*='provincia_bill_new']").val();


	 if(isBlank(regSociale)){

				//show error message
				
				
				
			 spanError.append("Verifica dati fatturazione<br/>");
			 //hide confirm button
			 confirmButton.hide();
			 
			}else if(isBlank(partitaIva)){
			
				//show error message
			 spanError.append("Verifica dati fatturazione<br/>");
			 //hide confirm button
			 confirmButton.hide();

			}else if(isBlank(indirizzof)){
			
				//show error message
			 spanError.append("Verifica dati fatturazione<br/>");
			 //hide confirm button
			 confirmButton.hide();

			}else if(isBlank(capf)){
			
				//show error message
			 spanError.append("Verifica dati fatturazione<br/>");
			 //hide confirm button
			 confirmButton.hide();

			}else if(isBlank(comunef)){
			
				//show error message
			 spanError.append("Verifica dati fatturazione<br/>");
			 //hide confirm button
			 confirmButton.hide();


			}else if(isBlank(provinciaf)){
			
				//show error message
			 spanError.append("Verifica dati fatturazione<br/>");
			 //hide confirm button
			 confirmButton.hide();
			}else if (type=="Privato"){
			
				 if(!ControllaCF(partitaIva)){
						
						//show error message
				 spanError.append("Verifica dati fatturazione - Codice Fiscale Errato<br/>");
				 //hide confirm button
				 confirmButton.hide();
			}
			}else{

				 jQuery("#riepilogoDatiFatRegioneSociale").html(regSociale);
				 //modificato
				 jQuery("#riepilogoDatiFatName").html(regSociale+" "+cognomeBill);
				 jQuery("#riepilogoDatiFatpPartitaIva").html(partitaIva);
				 jQuery("#riepilogoDatiFatIndirizzo").html(indirizzof);
				 jQuery("#riepilogoDatiFatCap").html(capf+" "+comunef+"("+provinciaf+")");
				 jQuery("#riepilogoDatiFatCodFis").html(codfis);
				 jQuery("#riepilogoDatiFatTel").html(tel);
	
			 
				}




	}

	if(isBlank(nome)){
		//show error message
		 spanError.append("Verifica dati spedizione<br/>");
		 //hide confirm button
		 confirmButton.hide();
	}else if(isBlank(cognome)){
		//show error message
		 spanError.append("Verifica dati spedizione<br/>");
		 //hide confirm button
		 confirmButton.hide();
	}else if(isBlank(indirizzo) ){
					
		//show error message
		 spanError.append("Verifica dati spedizione<br/>");
		 //hide confirm button
		 confirmButton.hide();
					
					}else if(isBlank(cap)){

			//show error message
		 spanError.append("Verifica dati spedizione<br/>");
		 //hide confirm button
		 confirmButton.hide();
					
					}else if(isBlank(comune)){ 
					
		//show error message
		 spanError.append("Verifica dati spedizione<br/>");
		 //hide confirm button
		 confirmButton.hide();			
					
		
					}else if(isBlank(provincia)){
					
		//show error message
		 spanError.append("Verifica dati spedizione<br/>");
		 //hide confirm button
		 confirmButton.hide();			
					
		}else if(isBlank(codfis)) {

		//show error message
		 spanError.append("Verifica dati spedizione<br/>");
		 //hide confirm button
		 confirmButton.hide();
		 }else if(isBlank(tel)){
			 //show error message
			 spanError.append("Verifica dati spedizione<br/>");
			 //hide confirm button
			 confirmButton.hide();
		 }else if(!ControllaCF(codfis)){
			//show error message
	 		spanError.append("Verifica dati spedizione - Codice Fiscale Errato<br/>");
			 //hide confirm button
			 confirmButton.hide();
		}else if(!ControllaNote(note)){
			//show error message
	 		spanError.append("Verifica dati spedizione - Campo Note troppo lungo (max. 255)<br/>");
			 //hide confirm button
			 confirmButton.hide();
		}
		 else{


		 jQuery("#riepilogoDatiShipName").html(nome+" "+cognome);
		 jQuery("#riepilogoDatiShipIndirizzo").html(indirizzo);
		 jQuery("#riepilogoDatiShipCap").html(cap+" "+comune+"("+provincia+")");
		 jQuery("#riepilogoDatiShipCodFis").html(codfis);
		 jQuery("#riepilogoDatiShipTel").html(tel);
		 
		 jQuery("#riepilogoDatiShipName2").html(nome+" "+cognome);
		 jQuery("#riepilogoDatiShipIndirizzo2").html(indirizzo);
		 jQuery("#riepilogoDatiShipCap2").html(cap+" "+comune+"("+provincia+")");
		 jQuery("#riepilogoDatiShipCodFis2").html(codfis);
		 jQuery("#riepilogoDatiShipTel2").html(tel);

}

}







function checkCreditCardData(confirmButton, spanError) {
	var ccTypeVal = jQuery("input:[name='myRadio']:checked").val();
	var check = true;
	switch (ccTypeVal) {
	case "CreditCard":
		jQuery(".checkCC").each(function(index) {
			if (isBlank($(this).val())) {
				spanError.append("Dati carta obbligatori<br/>");
				check = false;
				return false;
			}
		});
		jQuery(".cartpay_input").each(function(index) {
			if (isBlank($(this).val())) {
				spanError.append("Dati carta obbligatori<br/>");
				check = false;
				return false;
			}
		});
		break;
	case "Amex":
		jQuery(".checkCCA").each(function(index) {
			if (isBlank($(this).val())) {
				spanError.append("Dati carta obbligatori<br/>");
				check = false;
				return false;
			}
		});
		jQuery(".cartpay_input_amex").each(function(index) {
			if (isBlank($(this).val())) {
				spanError.append("Dati carta obbligatori<br/>");
				check = false;
				return false;
			}
		});
		break;
	case "PayPal":
		break;
	case "Gems":
		break;
	default:
		break;
	}
	return check;
}




function fillAndCheckRegData(confirmButton, spanError){
	 var city = jQuery('#select_city option:selected').text();
	 var email = jQuery("input:[name*='email_reg']").val()
	 var name = jQuery("input:[name*='nome_reg']").val();
	 var surname = jQuery("input:[name*='cognome_reg']").val();
	 var selectSex = jQuery("input:[name$='selectSex']:checked").length == 1;
	 var chkAccept = jQuery("input:[name$='chkAcceptTerms']:checked").length == 1;

	 if (city == 'choose...'  || isBlank(email) || isBlank(name) || isBlank(surname) || !selectSex || !chkAccept) {
		 //show error message
		 spanError.append("Verifica i dati di registrazione<br/>");
		 //hide confirm button
		 confirmButton.hide();
	 } else {
		 //fill data
		 jQuery("#riepilogoDatiRegName").html(name +" "+ surname);
		 jQuery("#riepilogoDatiRegEmail").html(email);
		 jQuery("#riepilogoDatiRegCity").html(city);
	 }
}

function deleteBillingInfo(id)
{
	jQuery("#div_5in_mod_"+id+" input").val("");
	
}

function assegnaTipoDinamico(id)
{			
	var tipoCli = jQuery('input[name*="typeClient'+id+'"]').val();
	if(tipoCli == 'Privato'){
		jQuery('input[name*="valCli'+id+'"]').val(tipoCli);
	}else{
		jQuery('input[name*="valCli'+id+'"]').val(tipoCli);
	}	
}
	
function assegnaTipo()
{
	var tipoCli = jQuery('input[name*="typeClient"]').val();
	if(tipoCli == 'Privato'){
		jQuery('input[name*="valCli"]').val(tipoCli);
	}else{
		jQuery('input[name*="valCli"]').val(tipoCli);	
	}		
}

function valStoredID(cardID){
		jQuery("#storedCardDiv input").val(cardID);

		jQuery('#myDiv_1').hide();
		jQuery('#myDiv_1_amex').hide();
		jQuery('#expireDate').hide();
		jQuery('#p-m-payment').css("opacity", "1");
		jQuery('#p-m-payment_amex').css("opacity", "1");
		jQuery('#p-m-payment_paypal').css("opacity", "1");
		jQuery('#p-m-payment_gems').css("opacity", "1");
		
		jQuery('#cartpay_save').hide();
		jQuery('#cartpay_save_amex').hide();
		
		jQuery('.cpcontainer_alpha100').css("opacity", "0.6");
		jQuery('#mainCardDiv_'+cardID).css("opacity", "1");
		
}

function deleteCardJS(){
	 
		 var cardID = jQuery("#storedCardDiv input").val();
		 jQuery("#delete_card_"+cardID+" button").click();
}


function ControllaCF(cf)
{
    var validi, i, s, set1, set2, setpari, setdisp;
    if( cf == '' )  return false;
    cf = cf.toUpperCase();
    if( cf.length != 16 )
        return false;
    validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    for( i = 0; i < 16; i++ ){
        if( validi.indexOf( cf.charAt(i) ) == -1 )
            return false;
    }
    set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
    s = 0;
    for( i = 1; i <= 13; i += 2 )
        s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
    for( i = 0; i <= 14; i += 2 )
        s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
    if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
        return false;
    return true;
}

function ControllaNote(note){
    if (note.length > 255){
        return false;
    }
    return true;
}

