/**
 * Abilita la visualizzazione di un tag
 * 
 * @param id
 */
function show(id) {
	jQuery("#" + id).show();
}

/**
 * Nasconde uno span
 * 
 * @param divId
 */
function hideSpan(id) {
	jQuery("span[id$='" + id + "']").hide();
}

/**
 * Nasconde i div che hanno come attributo class "loader"
 */
function hideLoader() {
	jQuery(".loader").hide();
}

/**
 * Mostra il lodaer di fianco al campo e nasconde i messaggi precedenti 
 * 
 * @param fieldName
 */
function showLoader(fieldName){
	show(fieldName + 'Loader');
	hideSpan(fieldName + 'ErrorMessage');
}

/**
 * Buycheck 
 */

function showConfirmDialog(logged) {
	var confirmButton = logged ? jQuery("a[id$='loggedConfirm']") : jQuery("a[id$='noLoggedConfirm']");
	var spanError = logged ? jQuery("span[id$='loggedError']") :  jQuery("span[id$='noLoggedError']");

	//reset error message
	spanError.html("");
	//show confirm button
	confirmButton.show();
	
	if (!checkCreditCardData(confirmButton, spanError)) {
		//hide confirm button
		 confirmButton.hide();
	}
	
	if (logged) {
		dialog10.show();
	} else {
		fillAndCheckRegData(confirmButton, spanError);
		dialog20.show();
	}
	track('resume');
}

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;
			}
		});
		break;
	case "Amex":
		jQuery(".checkCCA").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);
	 }
}
