// Dropdown Menu
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

// Slider Configuration
$(document).ready(function(){
		if(document.getElementById('photos')!=null)
		$('#photos').galleryView({
			panel_width: 558,
			panel_height: 135,
			frame_width: 59,
			frame_height: 40,
			filmstrip_size: 10,
			background_color: 'transparent',
			pause_on_hover: true
		});
});

// tabs Configuration
$(document).ready(function() {

	//Default Action
	$(".product-tab-content").hide(); //Hide all content
	$(".product-longdesc-heading li:first").addClass("active").show(); //Activate first tab
	$(".product-tab-content:first").show(); //Show first tab content
	
	//On Click Event
	$(".product-longdesc-heading li").click(function() {
		$(".product-longdesc-heading li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".product-tab-content").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active content
		return false;
	});
	$("#tab1").click(function() {
		$(document).height(947);
		return false;
	});

});

/*
window.onload = function(){
	document.getElementById('container').setAttribute('style', '');	
};
*/

/* coupon code js */
function discountCoupon(order){
	$('#discount_coupon_link').css('display', 'none');
	if (order == 'yes'){
		$('#discount_coupon_link_input').css('display', 'block');
		//$('#discount_coupon_link_input_go').css('display', 'block');
	}
	if (order == 'no'){
		var order = '&coupon=';
		$.post("check_discount_coupon.php", order, function(theResponse){});
	}
}

function cancelCoupon(){
	//$('#discount_coupon_link_input').css('display', 'none');
	$('#discount_coupon_link_input_go').css('display', 'none');
	//$('#discount_coupon_link').css('display', 'block');
	$('#couponcode').val('');
	var order = '&coupon=';
	$.post("check_discount_coupon.php", order, function(theResponse){});
}

function showUseCancelCoupon(val){
	val = val.replace(/\s/g, "");
	if (val == ''){
		$('#discount_coupon_link_input_go').css('display', 'none');
	} else {
		$('#discount_coupon_link_input_go').css('display', 'block');
	}
}

function RoundTo2dp(X, howMuch) {
	//howMuch = howMuch*10;
	//str = Math.round(X * howMuch) / howMuch;
	brStr = X.toString();
	num = brStr.indexOf(".");
	if (num > 0){		
		fstr = brStr.substring(0, (num));
		scStr = brStr.substring((num+1));
		if (scStr.length > 2){
			retNum = fstr+"."+scStr.substring(0, 2);
		} else if (scStr.length == 2){
			retNum = fstr+"."+scStr;
		} else {
			retNum = fstr+"."+scStr+"0";
		}
	} else {
		retNum = brStr+".00";
	}
	retNumm = retNum;
    return retNumm;
	//return Math.round(X * howMuch) / howMuch;
}

function calculateTotalPoints(){
	var order = '';
	$.post("check_points.php", order, function(theResponse){
		total = $('#subtotalprice').text().substr(1);
		realVal = theResponse;
		newtotal = (parseFloat(total)-realVal);
		$('#subtotalprice').html('&euro;'+RoundTo2dp(newtotal,2));
		$('#points_calculation').css('display', 'block');
		$('#pointsvalue').html('&euro;'+RoundTo2dp(realVal,2));
		$('#pointsLinkText').html('&nbsp; &nbsp;0 (&euro; 0.00)');
		Ftotal = $('#disTot').text().substr(1);
		Fnewtotal = (parseFloat(Ftotal)-realVal);
		$('#disTot').html('<strong>&euro;'+RoundTo2dp(Fnewtotal, 2)+'</strong>');
		if (document.getElementById('payable_amount')){			
			F2total = $('#payable_amount').val();
			F2newtotal = (parseFloat(F2total)-realVal);
			$('#payable_amount').val(RoundTo2dp(F2newtotal, 2));
		}

		if (document.getElementById('creditPoints')){			
			$('#creditPoints').val(RoundTo2dp(realVal, 2));
		}
	});
}

function useCoupon(){
	$('#discount_coupon_error').css('display', 'none');
	$('#discount_coupon_error').html('');
	var order = '&coupon='+$('#couponcode').val();
	$.post("check_discount_coupon.php", order, function(theResponse){
		if (theResponse != '0.00'){
			if (theResponse == '00.00'){
				$('#discount_coupon_error').css('display', 'block');
				$('#discount_coupon_error').html('<img src="images/info-icon.gif" align="absbottom" />Il buono sconto che state usando è stato già utilizzato.');
			} else {
				$('#discountvalue').html('&pound;'+theResponse);		
				$('#discount_coupon_calculation').css('display', 'block');
				$('#discount_coupon_link_input_go').css('display', 'none');
				$('#discount_coupon_link_input').css('display', 'none');
				
				//$('#discount_coupon_reuse').css('display', 'block');
				total = $('#subtotalprice').text().substr(1);

				realVal = theResponse;
				newtotal = (parseFloat(total)-realVal);
				$('#subtotalprice').html('&euro;'+RoundTo2dp(newtotal,2));
			}
		} else {
			$('#discount_coupon_error').css('display', 'block');
			$('#discount_coupon_error').html('<img src="images/info-icon.gif" align="absbottom" />Il buono sconto inserito non esiste.');
		}
	});
}

function blankCoupon(){
	$('#discount_coupon_error').css('display', 'none');
}

function useCouponOnLoad(ccode){	
	var order = '&coupon='+ccode;
	$.post("check_discount_coupon.php", order, function(theResponse){
		if (theResponse != '0.00'){
			if (theResponse == '00.00'){
				$('#discount_coupon_error').css('display', 'block');
				$('#discount_coupon_error').html('<img src="images/info-icon.gif" align="absbottom" />Il buono sconto che state usando è stato già utilizzato.');
			} else {
				$('#discountvalue').html('&euro;'+theResponse);		
				$('#discount_coupon_calculation').css('display', 'block');
				$('#discount_coupon_link_input_go').css('display', 'none');
				$('#discount_coupon_link_input').css('display', 'none');
				$('#discount_coupon_link').css('display', 'none');
				
				total = $('#subtotalprice').text().substr(1);

				realVal = theResponse;
				newtotal = (parseFloat(total)-realVal);
				$('#subtotalprice').html('&euro;'+RoundTo2dp(newtotal,2));
				//$('#discount_coupon_reuse').css('display', 'block');
			}
		} else {
			$('#discount_coupon_error').css('display', 'block');
			$('#discount_coupon_error').html('<img src="images/info-icon.gif" align="absbottom" />Il buono sconto inserito non esiste.');
		}
	});
}

function useCouponOnLoadDeliveryoptions(ccode){	
	var order = '&coupon='+ccode;
	$.post("check_discount_coupon.php", order, function(theResponse){
		$('#discountvalue').html('&pound;'+theResponse);		
		$('#discountCouponBlock').css('display', 'block');
		
		total = $('#subtotalprice').text().substr(1);

		realVal = theResponse;
		newtotal = (parseFloat(total)-realVal);
		$('#subtotalprice').html('&euro;'+RoundTo2dp(newtotal,2));

		endtotal = $('#disTot').text().substr(1);

		newtotal = (parseFloat(endtotal)-realVal);
		$('#disTot').html('<strong>&euro;'+RoundTo2dp(newtotal,2)+'</strong>');

		$('#discount').val(RoundTo2dp(theResponse,2));

	});
}

function useCouponOnLoadPayment(ccode){	
	var order = '&coupon='+ccode;
	$.post("check_discount_coupon.php", order, function(theResponse){
		$('#discount').val(theResponse.toString());
		theResponse = parseFloat(theResponse);
		$('#payable_amount').val($('#payable_amount').val()-theResponse);
		$('#shopping_amount').val($('#shopping_amount').val()-theResponse);		
	});
}

function addDelevery(oldtotal, snet){
	
	// Overriding "oldtotal" variable
	if (document.getElementById('pointsvalue')){
		Ftotal = $('#pointsvalue').text().substr(1);
		oldtotal = parseFloat(oldtotal-Ftotal);
	}

	charge = snet.replace(snet.substring(snet.indexOf('~')),'');
	
	// added this line for counting discount only
	//if ($('#discountvalue')) {
	if (document.getElementById('discountvalue')){
		oldtotal = parseFloat(oldtotal)-parseFloat($('#discountvalue').text().substr(1));
	}

	oldtotal = parseFloat(oldtotal);

	charge = parseFloat(charge);
	fm = document.orderFrm;	
	document.getElementById('disTot').innerHTML = '<strong>&euro;' + RoundTo2dp(parseFloat(charge+oldtotal),2) + '</strong>&nbsp;';
	fm.payable_amount.value = parseFloat(charge+oldtotal);
}

$(document).ready(function(){
	//$('#discount_coupon_link_input').css('display', 'none');
	$('#discount_coupon_link').css('display', 'none');
	$('#discount_coupon_calculation').css('display', 'none');
	$('#discount_coupon_link_input_go').css('display', 'none');
	$('#discount_coupon_reuse').css('display', 'none');
	$('#discount_coupon_error').css('display', 'none');
});

function useNewCoupon(){
	$('#discount_coupon_link_input').css('display', 'none');
	$('#discount_coupon_calculation').css('display', 'none');
	$('#discount_coupon_link_input_go').css('display', 'none');
	$('#discount_coupon_reuse').css('display', 'none');
	$('#discount_coupon_link').css('display', 'block');
	$('#couponcode').val('');
	var order = '&coupon=';
	$.post("check_discount_coupon.php", order, function(theResponse){});
}
/* coupon code js ends */

function openCategories(ulId){
	if (document.getElementById(ulId+"-subs").style.display == 'none'){
		$("#"+ulId+"-subs").show(800);
		$("#"+ulId).addClass('cat-title-open');
	} else {
		document.getElementById(ulId).setAttribute('class', 'cat-title');
		$("#"+ulId+"-subs").hide(500);
	}
}

/*
this function 'showhide' takes 2 params to show and divs
*/

function removeItem(quantityObj){
	document.getElementById(quantityObj).value = 0;	
	//document.getElementById('action').value = 'Update Cart Contents';	
	document.getElementById('cartForm').submit();
}

function showhide(hideDiv, showDiv){
	//$("#" + hideDiv).fadeOut(300);
	//$("#" + showDiv).fadeIn(800);
	$("#" + hideDiv).hide('fast');
	$("#" + showDiv).show('fast');
}

function showBig(parent_id){
	toc = "big-"+parent_id;

	activeid = false;

	var allDivs = document.getElementsByTagName('div');
	for (i=0; i < allDivs.length; i++){
		if (allDivs[i].id.search("big-") >= 0){
			if (allDivs[i].id == toc){
				if (document.getElementById(allDivs[i].id).style.opacity == 1){
					activeid = true;
				}
			}
			if (activeid == false || (allDivs[i].id != toc && activeid == true)){
				$("#" + allDivs[i].id).animate({ display : "none", opacity : 0}, { queue:false, duration:200 });
			}			
		}
	}

	$("#" + toc).show(300);

	if (activeid == false){
		$("#" + toc).animate({ display : "block", opacity : 1}, { queue:false, duration:500 });
	}	
}

function showBigOld(parent_id){
	toc = "big-"+parent_id;

	activeid = false;

	var allDivs = document.getElementsByTagName('div');
	for (i=0; i < allDivs.length; i++){
		if (allDivs[i].id.search("big-") >= 0){
			if (allDivs[i].id == toc){
				if (document.getElementById(allDivs[i].id).style.display == 'block'){
					activeid = true;
				}
			}
			if (activeid == false || (allDivs[i].id != toc && activeid == true)){
				$("#" + allDivs[i].id).hide(300);
			}
		}
	}
	if (activeid == false){
		$("#" + toc).show(300);
	}	
}

function logMeIn(){
	fm = document.loginF;

	if (fm.useremail.value.replace(/\s/g, "") == ''){
		alert("Inserire email utente.");
		fm.useremail.focus();
		return false;
	}

	if(! ValidateEmail(fm.useremail, ''))
		return false;

	if (fm.userpass.value.replace(/\s/g, "") == ''){
		alert("Inserire la password.");
		fm.userpass.focus();
		return false;
	}

}

function validateregisterf(){
	fm = document.registerf;
	var regexLetter = /^[a-zA-Z\']+$/;
	var regexLetterAccent = /^[a-zA-Z\'ÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúý]+$/;

	if (fm.customer_fname.value.replace(/\s/g, "") == ''){
		alert("Inserire il nome.");
		fm.customer_fname.focus();
		return false;
	}

	if(!fm.customer_fname.value.replace(/\s/g, "").match(regexLetterAccent)){
		alert("Solo lettere dell'alfabeto sono permessi per il nome.");
		fm.customer_fname.focus();
		return false;
	}

	if (fm.customer_lname.value.replace(/\s/g, "") == ''){
		alert("Inserire il cognome.");
		fm.customer_lname.focus();
		return false;
	}

	if(!fm.customer_lname.value.replace(/\s/g, "").match(regexLetterAccent)){
		alert("Solo lettere dell'alfabeto sono permessi per il cognome.");
		fm.customer_lname.focus();
		return false;
	}

	if (fm.customer_email.value.replace(/\s/g, "") == ''){
		alert("Inserire un indirizzo email.");
		fm.customer_email.focus();
		return false;
	}	

	if(! ValidateEmail(fm.customer_email, ''))
		return false;

	if (fm.customer_email.value.replace(/\s/g, "") != fm.conf_customer_email.value.replace(/\s/g, "")){
		alert("Indirizzo email e Conferma indirizzo email non coincidono.");
		fm.conf_customer_email.focus();
		return false;
	}

	if (fm.customer_pass.value.replace(/\s/g, "") == ''){
		alert("Inserire la password.");
		fm.customer_pass.focus();
		return false;
	}

	if (fm.customer_pass.value.replace(/\s/g, "") != fm.conf_customer_pass.value.replace(/\s/g, "")){
		alert("Password e Conferma Password non coincidono.");
		fm.conf_customer_pass.focus();
		return false;
	}

	if (fm.customer_phone.value.replace(/\s/g, "") == '') {
		alert("Inserire un numero di telefono.");
		fm.customer_phone.focus();
		return false;
	}

	if (fm.customer_phone.value != '') {
		if (!ValidatePhone(fm.customer_phone.value) || fm.customer_phone.value.length < 6 || fm.customer_phone.value.length > 20){
			alert("Inserire un valido numero di telefono.");
			fm.customer_phone.focus();
			return false;
		}
	}

	if (fm.media.value.replace(/\s/g, "") == '') {
		alert("Selezionare un mass media.");
		fm.media.focus();
		return false;
	}

	if (fm.media.value != '') {
		if ((fm.media.value == 'SE' || fm.media.value == 'MZ') && fm.medianame.value == ''){
			if (fm.media.value == 'SE'){
				alert("Selezionare un motore di ricerca.");
			} else {
				alert("Selezionare una rivista.");
			}
			fm.medianame.focus();
			return false;
		}
	}

	if (fm.customer_bill_addr.value.replace(/\s/g, "") == ''){
		alert("Inserire indirizzo di fatturazione nella Riga 1.");
		fm.customer_bill_addr.focus();
		return false;
	}

	if (fm.customer_bill_town.value.replace(/\s/g, "") == ''){
		alert("Inserire la provincia nell'indirizzo di fatturazione.");
		fm.customer_bill_town.focus();
		return false;
	}

	if (fm.customer_bill_postcode.value.replace(/\s/g, "") == ''){
		alert("Inserire il Codice Avviamento Postale nell'indirizzo di fatturazione.");
		fm.customer_bill_postcode.focus();
		return false;
	}

	/*if (fm.customer_bill_city.value.replace(/\s/g, "") == ''){
		alert("Please enter billing city.");
		fm.customer_bill_city.focus();
		return false;
	}*/

	if (fm.customer_bill_country.value.replace(/\s/g, "") == ''){
		alert("Selezionare lo Stato nell'indirizzo di fatturazione.");
		fm.customer_bill_country.focus();
		return false;
	}

	if (fm.customer_bill_country.value.replace(/\s/g, "") == 'US' && fm.customer_bill_state.value.replace(/\s/g, "") == ''){
		alert("Please select billing state.");
		fm.customer_bill_state.focus();
		return false;
	}
	if (fm.captcha.value.replace(/\s/g, "") == ''){
		alert("Inserire il Spam Filter.");
		fm.captcha.focus();
		return false;
	}

	flag = false;
	for (i=0; i<fm.same_ship.length; i++) {
		if (fm.same_ship[i].checked==true){
			if (fm.same_ship[i].value == '0'){
				if (fm.customer_ship_addr.value.replace(/\s/g, "") == ''){
					alert("Inserire indirizzo di fatturazione nella Riga 1.");
					fm.customer_ship_addr.focus();
					return false;
				}
				
				if (fm.customer_ship_town.value.replace(/\s/g, "") == ''){
					alert("Inserire la provincia nell'indirizzo di spedizione.");
					fm.customer_ship_town.focus();
					return false;
				}				

				if (fm.customer_ship_postcode.value.replace(/\s/g, "") == ''){
					alert("Inserire il Codice Avviamento Postale nell'indirizzo di spedizione.");
					fm.customer_ship_postcode.focus();
					return false;
				}

				/*if (fm.customer_ship_city.value.replace(/\s/g, "") == ''){
					alert("Please enter shipping city.");
					fm.customer_ship_city.focus();
					return false;
				}*/

				if (fm.customer_ship_country.value.replace(/\s/g, "") == ''){
					alert("Inserire lo Stato nell'indirizzo di spedizione.");
					fm.customer_ship_country.focus();
					return false;
				}
			} else {
				fm.customer_ship_country.value = fm.customer_bill_country.value;
				//fm.customer_ship_city.value = fm.customer_bill_city.value.replace(/\s/g, "");
				fm.customer_ship_postcode.value = fm.customer_bill_postcode.value;
				fm.customer_ship_addr.value = fm.customer_bill_addr.value;
				fm.customer_ship_addr2.value = fm.customer_bill_addr2.value;
				fm.customer_ship_town.value = fm.customer_bill_town.value;
				fm.customer_ship_state.value = fm.customer_bill_state.value;
			}
            flag = true;
         }	
	}
	
	if (!flag){
		alert("Selezionare l'indirizzo di spedizione.");
		return false;	
	}

	if (fm.customer_ship_country.value.replace(/\s/g, "") == 'US' && fm.customer_ship_state.value.replace(/\s/g, "") == ''){
		alert("Please select shipping state.");
		fm.customer_ship_state.focus();
		return false;
	}

	if (fm.customer_terms.checked == false){
		alert("Accettare Termini e Condizioni.");
		fm.customer_terms.focus();
		return false;
	}

	return true;
}

function setShip(){
	fm = document.registerf;

	for (i=0; i<fm.same_ship.length; i++) {
		if (fm.same_ship[i].checked==true){
			if (fm.same_ship[i].value == '0'){
				fm.customer_ship_state.selectedIndex = 0;
				fm.customer_ship_country.selectedIndex = 0;
				//fm.customer_ship_city.value = '';
				fm.customer_ship_postcode.value = '';
				fm.customer_ship_addr.value = '';
				fm.customer_ship_addr2.value = '';
				fm.customer_ship_town.value = '';
				if (fm.customer_ship_addr.value.replace(/\s/g, "") == ''){
					alert("Inserire la via nell'indirizzo di spedizione.");
					fm.customer_ship_addr.focus();
					return false;
				}
				
				if (fm.customer_ship_town.value.replace(/\s/g, "") == ''){
					alert("Inserire la provincia nell'indirizzo di spedizione.");
					fm.customer_ship_town.focus();
					return false;
				}				

				if (fm.customer_ship_postcode.value.replace(/\s/g, "") == ''){
					alert("Inserire il Codice Avviamento Postale nell'indirizzo di spedizione.");
					fm.customer_ship_postcode.focus();
					return false;
				}

				/*if (fm.customer_ship_city.value.replace(/\s/g, "") == ''){
					alert("Please enter shipping city.");
					fm.customer_ship_city.focus();
					return false;
				}*/

				if (fm.customer_ship_country.value.replace(/\s/g, "") == ''){
					alert("Inserire lo Stato nell'indirizzo di spedizione.");
					fm.customer_ship_country.focus();
					return false;
				}
			} else {
				var ss = fm.customer_ship_country.options;
				for (j = 0; j < ss.length; j++){
					if (ss[j].value == fm.customer_bill_country.value){
						fm.customer_ship_country.selectedIndex = j;
						break;
					}
				}

				//fm.customer_ship_city.value = fm.customer_bill_city.value.replace(/\s/g, "");
				fm.customer_ship_postcode.value = fm.customer_bill_postcode.value;
				fm.customer_ship_addr.value = fm.customer_bill_addr.value;
				fm.customer_ship_addr2.value = fm.customer_bill_addr2.value;
				fm.customer_ship_town.value = fm.customer_bill_town.value;
				fm.customer_ship_state.value = fm.customer_bill_state.value;
			}
            flag = true;
         }	
	}
}

function ValidateEmail(email, formName){
		var atCharPresent = false;
		var dotPresent = false;
		
		for ( var Idx = 0; Idx < email.value.length; Idx++ ){
			if ( email.value.charAt ( Idx ) == '@' )
				atCharPresent = true;
			if ( email.value.charAt ( Idx ) == '.' )
				dotPresent = true;
		}
		if ( !atCharPresent || !dotPresent ){
			alert("Inserire un indirizzo email valido.");
			email.focus();
			return false;
			}
	return true;
}

function validatefpf(){
	fm = document.fpf;
	if (fm.customer_username.value.replace(/\s/g, "") == ''){
		alert("Inserire email utente .");
		fm.customer_username.focus();
		return false;
	}
}

function validateedipf(){
	fm = document.registerf;
	var regexLetter = /^[a-zA-Z\']+$/;
	var regexLetterAccent = /^[a-zA-Z\'ÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúý]+$/;

	if (fm.customer_fname.value.replace(/\s/g, "") == ''){
		alert("Inserire il nome.");
		fm.customer_fname.focus();
		return false;
	}

	if(!fm.customer_fname.value.replace(/\s/g, "").match(regexLetterAccent)){
		alert("Solo lettere dell'alfabeto sono permessi per il nome.");
		fm.customer_fname.focus();
		return false;
	}

	if (fm.customer_lname.value.replace(/\s/g, "") == ''){
		alert("Inserire il cognome.");
		fm.customer_lname.focus();
		return false;
	}

	if(!fm.customer_lname.value.replace(/\s/g, "").match(regexLetterAccent)){
		alert("Solo lettere dell'alfabeto sono permessi per il cognome.");
		fm.customer_lname.focus();
		return false;
	}

	if (fm.customer_email.value.replace(/\s/g, "") == ''){
		alert("Inserire un indirizzo email.");
		fm.customer_email.focus();
		return false;
	}	

	if(! ValidateEmail(fm.customer_email, ''))
		return false;

	/*
	if (fm.customer_username.value.replace(/\s/g, "") == ''){
		alert("Please enter username.");
		fm.customer_username.focus();
		return false;
	}
	*/

	if (fm.customer_phone.value.replace(/\s/g, "") == '') {
		alert("Inserire un numero di telefono.");
		fm.customer_phone.focus();
		return false;
	}

	if (fm.customer_phone.value != '') {
		if (!ValidatePhone(fm.customer_phone.value) || fm.customer_phone.value.length < 6 || fm.customer_phone.value.length > 20){
			alert("Inserire un valido numero di telefono.");
			fm.customer_phone.focus();
			return false;
		}
	}

	if (fm.customer_bill_addr.value.replace(/\s/g, "") == ''){
		alert("Inserire la via nell'indirizzo di fatturazione.");
		fm.customer_bill_addr.focus();
		return false;
	}

	if (fm.customer_bill_postcode.value.replace(/\s/g, "") == ''){
		alert("Inserire il Codice Avviamento Postale nell'indirizzo di fatturazione.");
		fm.customer_bill_postcode.focus();
		return false;
	}

	/*if (fm.customer_bill_city.value.replace(/\s/g, "") == ''){
		alert("Please enter billing city.");
		fm.customer_bill_city.focus();
		return false;
	}*/

	if (fm.customer_bill_country.value.replace(/\s/g, "") == ''){
		alert("Selezionare lo Stato nell'indirizzo di fatturazione.");
		fm.customer_bill_country.focus();
		return false;
	}

	if (fm.customer_bill_country.value.replace(/\s/g, "") == 'US' && fm.customer_bill_state.value.replace(/\s/g, "") == ''){
		alert("Please select billing state.");
		fm.customer_bill_state.focus();
		return false;
	}

	return true;
}

function validateShipAddr(){
	fm = document.addShip;
	var regexLetter = /^[a-zA-Z\']+$/;
	var regexLetterAccent = /^[a-zA-Z\'ÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúý]+$/;

	if (fm.customer_ship_addr.value.replace(/\s/g, "") == ''){
		alert("Inserire la via nell'indirizzo di spedizione.");
		fm.customer_ship_addr.focus();
		return false;
	}

	if(fm.customer_ship_town.value.replace(/\s/g, "")=="") {
		alert("Inserire la provincia nell'indirizzo di spedizione.");
		fm.customer_ship_town.focus();
		return false;
	}

	if (fm.customer_ship_postcode.value.replace(/\s/g, "") == ''){
		alert("Inserire il Codice Avviamento Postale nell'indirizzo di spedizione.");
		fm.customer_ship_postcode.focus();
		return false;
	}

	/*if(fm.customer_ship_city.value.replace(/\s/g, "") == ''){
		alert("Please enter city.");
		fm.customer_ship_city.focus();
		return false;
	}*/

	if (fm.customer_ship_country.value.replace(/\s/g, "") == ''){
		alert("Inserire lo Stato nell'indirizzo di spedizione.");
		fm.customer_ship_country.focus();
		return false;
	}

	if (fm.customer_ship_country.value.replace(/\s/g, "") == 'US' && fm.customer_ship_state.value.replace(/\s/g, "") == ''){
		alert("Please select shipping state.");
		fm.customer_ship_state.focus();
		return false;
	}

	return true;
}



var comment_changed2 = 0;
var comment_changedl = 0;
var comment_changedg = 0;

function removeText3(email) {
  if(comment_changed2 == 0) {
    email.value='';
    comment_changed2 = 1;
  }
}

function addText3(email) {
  if(email.value == '') {
    email.value = 'Inserire una parola chiave:';
    comment_changed2 = 0;
  }
}

function removeText2(email) {
  if(comment_changedl == 0) {
    email.value='';
    comment_changedl = 1;
  }
}

function addText2(email) {
  if(email.value == '') {
    email.value = 'E-Mail';
    comment_changedl = 0;
  }
}

function removeTextg(password) {
  if(comment_changedg == 0) {
    password.value='';
    comment_changedg = 1;
  }
}

function addTextg(password) {
  if(password.value == '') {
    password.value = '******';
    comment_changedg = 0;
  }
}


//
 function RoundTo2dp(X, howMuch) {
       //howMuch = howMuch*10;
       //str = Math.round(X * howMuch) / howMuch;
       brStr = X.toString();
       num = brStr.indexOf(".");
       if (num > 0){                
               fstr = brStr.substring(0, (num));
               scStr = brStr.substring((num+1));
               if (scStr.length > 2){
                       retNum = fstr+"."+scStr.substring(0, 2);
               } else if (scStr.length == 2){
                       retNum = fstr+"."+scStr;
               } else {
                       retNum = fstr+"."+scStr+"0";
               }
       } else {
               retNum = brStr+".00";
       }
       retNumm = retNum;
   return retNumm;
       //return Math.round(X * howMuch) / howMuch;
}

//
function validatecontact(){
	fm = document.contactF;
	if (fm.name.value.replace(/\s/g, "") == ''){
		alert("Inserire il nome.");
		fm.name.focus();
		return false;
	}

	if (fm.email.value.replace(/\s/g, "") == ''){
		alert("Inserire un indirizzo email.");
		fm.email.focus();
		return false;
	}	

	if(! ValidateEmail(fm.email, ''))
		return false;

	if (fm.subject.value.replace(/\s/g, "") == ''){
		alert("Inserire l'oggetto della vostra richiesta.");
		fm.subject.focus();
		return false;
	}

	if (fm.enquiry.value.replace(/\s/g, "") == ''){
		alert("Digitare la vostra richiesta.");
		fm.enquiry.focus();
		return false;
	}

	if (fm.captcha.value.replace(/\s/g, "") == ''){
		alert("Inserire il la somma dei due numeri per inviare la vostra richiesta.");
		fm.captcha.focus();
		return false;
	}
}

//
function validatetestimonial(){
	fm = document.testimonialF;
	if (fm.name.value.replace(/\s/g, "") == ''){
		alert("Inserire il nome.");
		fm.name.focus();
		return false;
	}

	if (fm.email.value.replace(/\s/g, "") == ''){
		alert("Inserire un indirizzo email.");
		fm.email.focus();
		return false;
	}	

	if(! ValidateEmail(fm.email, ''))
		return false;

	if (fm.areacity.value.replace(/\s/g, "") == ''){
		alert("Inserire il Cittá.");
		fm.areacity.focus();
		return false;
	}

	if (fm.enquiry.value.replace(/\s/g, "") == ''){
		alert("Digitare i vostri commenti.");
		fm.enquiry.focus();
		return false;
	}

	if (fm.captcha.value.replace(/\s/g, "") == ''){
		alert("Inserire il la somma dei due numeri per inviare la vostra richiesta.");
		fm.captcha.focus();
		return false;
	}
}

function switchvaluesArray(partOne, partTwo){
	valarr = partOne.split("~");
	namarr = partTwo.split("~");
	for (i = 0; i < valarr.length; i++){
		document.getElementById(namarr[i]).value = valarr[i];
	}
}

function switchvalues(partOne, partTwo){
	valarr = partOne;
	namarr = partTwo;
	document.getElementById(namarr).value = valarr;
}

function validItem(selectname, formname, idd){
	divid = selectname + idd;
	formname = (formname + idd).toString();
	if(document.getElementById(divid).value == ''){
		alert('Selezionare la quantità dal menù a discesa.');
		return false;
	} else {
		return true;	
	}
}

function call_Cart(frm)
{
		
	document.frm.submit();
}


function del_Ship_Addr(id){
	if(confirm("Siete sicuri di volere cancellare questo indirizzo di spedizione?"))
	window.location.href="index.php?action=addship&task=del&aid="+id;
}

function MM_swapImgRestore() { //v3.0
 var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
 var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {  d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
 if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
 for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
 if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
 var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
  if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function paybypal(){
	document.orderFrm.action.value='paypal';
	document.orderFrm.submit();
}

function ValidatePhone(sText){
   var ValidChars = "0123456789 -()+";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
   }
   return IsNumber; 
}

function sefunction(){
	var mlist =document.getElementById("media");
	var td  = document.getElementById("sediv")
	if(mlist.value == "SE"){
		td.innerHTML = "<select name='medianame' id='medianame' ><option value='' >Quale Motore di Ricerca?</option><option value='Altavista'>Altavista</option><option value='AOL'>AOL</option><option value='Ask Jeeves'>Ask Jeeves</option><option value='BLUEYONDER'>BLUEYONDER</option> <option value='DogPile'>DogPile</option><option value='Google'>Google</option><option value='MSN'>MSN</option><option value='Yahoo'>Yahoo</option><option value='Other Search Engine'>Other</option></select>";
	} else if(mlist.value == "MZ"){
		td.innerHTML = "<select name='medianame' id='medianame' ><option value='' >Quale Rivista?</option><option value='3sixty'>3sixty</option><option value='Bent'>Bent</option><option value='Boyz'>Boyz</option><option value='H & E Naturist'>H & E Naturist</option><option value='Hot Magazine'>Hot Magazine</option><option value='Mature Times'>Mature Times</option><option value='QX International'>QX International</option><option value='Street Fighters'>Street Fighters</option><option value='Other Magazine'>Other</option></select>";
	} else {
		td.innerHTML = "";
	}
}

/*
function ValidatePhone(strPhone) {
	var regPhone = /^\(?(\d{4})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
	if (regPhone.test(strPhone)) {
			return true;
	} else {
			return false;
	}
}
*/
/* IE hack for dropdown list */
function ExpandWidth(width1, id)
{
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
	{
		var myObj = document.getElementById(id);
		if (myObj.tag == null)
		{
			myObj.style.width = 'auto';
			
			if(myObj.clientWidth<=width1)
			{
				myObj.tag = width1.toString()+'px';
				myObj.style.width = myObj.tag.toString();
				
			}
			else
				myObj.tag = 'auto';
		}
		else
		{
			myObj.style.width = myObj.tag.toString();
		}
	}
}
/* IE hack for dropdown list */
function ShrinkWidth(width1, id)
{
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
	{
		var myObj = document.getElementById(id);
		myObj.style.width = width1.toString()+'px';
	}
}

function validateregisterfG(){
	fm = document.registerf;
	var regexLetter = /^[a-zA-Z\']+$/;
	var regexLetterAccent = /^[a-zA-Z\'ÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúý]+$/;

	if (fm.customer_fname.value.replace(/\s/g, "") == ''){
		alert("Inserire il nome.");
		fm.customer_fname.focus();
		return false;
	}

	if(!fm.customer_fname.value.replace(/\s/g, "").match(regexLetterAccent)){
		alert("Solo lettere dell'alfabeto sono permessi per il nome.");
		fm.customer_fname.focus();
		return false;
	}

	if (fm.customer_lname.value.replace(/\s/g, "") == ''){
		alert("Inserire il cognome.");
		fm.customer_lname.focus();
		return false;
	}

	if(!fm.customer_lname.value.replace(/\s/g, "").match(regexLetterAccent)){
		alert("Solo lettere dell'alfabeto sono permessi per il cognome.");
		fm.customer_lname.focus();
		return false;
	}

	if (fm.customer_email.value.replace(/\s/g, "") == ''){
		alert("Inserire un indirizzo email.");
		fm.customer_email.focus();
		return false;
	}	

	if(! ValidateEmail(fm.customer_email, ''))
		return false;

	if (fm.customer_email.value.replace(/\s/g, "") != fm.conf_customer_email.value.replace(/\s/g, "")){
		alert("Indirizzo email e Conferma indirizzo email non coincidono.");
		fm.conf_customer_email.focus();
		return false;
	}

	if (fm.customer_phone.value.replace(/\s/g, "") == '') {
		alert("Please enter phone number.");
		fm.customer_phone.focus();
		return false;
	}

	if (fm.customer_phone.value != '') {
		if (!ValidatePhone(fm.customer_phone.value) || fm.customer_phone.value.length < 6 || fm.customer_phone.value.length > 20){
			alert("Inserire un valido numero di telefono.");
			fm.customer_phone.focus();
			return false;
		}
	}

	if (fm.media.value.replace(/\s/g, "") == '') {
		alert("Selezionare un mass media.");
		fm.media.focus();
		return false;
	}

	if (fm.media.value != '') {
		if ((fm.media.value == 'SE' || fm.media.value == 'MZ') && fm.medianame.value == ''){
			if (fm.media.value == 'SE'){
				alert(". Selezionare un motore di ricerca.");
			} else {
				alert("Selezionare una rivista.");
			}
			fm.medianame.focus();
			return false;
		}
	}

	if (fm.customer_bill_addr.value.replace(/\s/g, "") == ''){
		alert("Inserire la via nell'indirizzo di fatturazione.");
		fm.customer_bill_addr.focus();
		return false;
	}

	if (fm.customer_bill_town.value.replace(/\s/g, "") == ''){
		alert("Inserire la provincia nell'indirizzo di fatturazione.");
		fm.customer_bill_town.focus();
		return false;
	}

	if (fm.customer_bill_postcode.value.replace(/\s/g, "") == ''){
		alert("Inserire il Codice Avviamento Postale nell'indirizzo di fatturazione.");
		fm.customer_bill_postcode.focus();
		return false;
	}

	/*if (fm.customer_bill_city.value.replace(/\s/g, "") == ''){
		alert("Please enter billing city.");
		fm.customer_bill_city.focus();
		return false;
	}*/

	if (fm.customer_bill_country.value.replace(/\s/g, "") == ''){
		alert("Selezionare lo Stato nell'indirizzo di fatturazione.");
		fm.customer_bill_country.focus();
		return false;
	}

	if (fm.customer_bill_country.value.replace(/\s/g, "") == 'US' && fm.customer_bill_state.value.replace(/\s/g, "") == ''){
		alert("Please select billing state.");
		fm.customer_bill_state.focus();
		return false;
	}

	flag = false;
	for (i=0; i<fm.same_ship.length; i++) {
		if (fm.same_ship[i].checked==true){
			if (fm.same_ship[i].value == '0'){
				if (fm.customer_ship_addr.value.replace(/\s/g, "") == ''){
					alert("Inserire la via nell'indirizzo di spedizione.");
					fm.customer_ship_addr.focus();
					return false;
				}
				
				if (fm.customer_ship_town.value.replace(/\s/g, "") == ''){
					alert("Inserire la provincia nell'indirizzo di spedizione.");
					fm.customer_ship_town.focus();
					return false;
				}				

				if (fm.customer_ship_postcode.value.replace(/\s/g, "") == ''){
					alert("Inserire il Codice Avviamento Postale nell'indirizzo di spedizione.");
					fm.customer_ship_postcode.focus();
					return false;
				}

				/*if (fm.customer_ship_city.value.replace(/\s/g, "") == ''){
					alert("Please enter shipping city.");
					fm.customer_ship_city.focus();
					return false;
				}*/

				if (fm.customer_ship_country.value.replace(/\s/g, "") == ''){
					alert("Inserire lo Stato nell'indirizzo di spedizione.");
					fm.customer_ship_country.focus();
					return false;
				}
			} else {
				fm.customer_ship_country.value = fm.customer_bill_country.value;
				//fm.customer_ship_city.value = fm.customer_bill_city.value.replace(/\s/g, "");
				fm.customer_ship_postcode.value = fm.customer_bill_postcode.value;
				fm.customer_ship_addr.value = fm.customer_bill_addr.value;
				fm.customer_ship_addr2.value = fm.customer_bill_addr2.value;
				fm.customer_ship_town.value = fm.customer_bill_town.value;
				fm.customer_ship_state.value = fm.customer_bill_state.value;
			}
            flag = true;
         }	
	}
	
	if (!flag){
		alert("Selezionare l'indirizzo di spedizione.");
		return false;	
	}

	if (fm.customer_ship_country.value.replace(/\s/g, "") == 'US' && fm.customer_ship_state.value.replace(/\s/g, "") == ''){
		alert("Please select shipping state.");
		fm.customer_ship_state.focus();
		return false;
	}
	
	var order = 'step=1';

	order += '&customer_fname='+getLastInputValueByName('customer_fname');
	order += '&customer_lname='+getLastInputValueByName('customer_lname');
	order += '&customer_email='+getLastInputValueByName('customer_email');
	order += '&customer_phone='+getLastInputValueByName('customer_phone');
	order += '&media='+getSelectedComboValueById('media');
	order += '&medianame='+getSelectedComboValueById('medianame');
	order += '&customer_bill_addr='+getLastInputValueByName('customer_bill_addr');
	order += '&customer_bill_addr2='+getLastInputValueByName('customer_bill_addr2');
	order += '&customer_bill_town='+getLastInputValueByName('customer_bill_town');
	order += '&customer_bill_postcode='+getLastInputValueByName('customer_bill_postcode');
	order += '&customer_bill_country='+getSelectedComboValueById('customer_bill_country');
	order += '&customer_bill_state='+getSelectedComboValueById('customer_bill_state');
	order += '&same_ship='+getSelectedRadioValueByName('same_ship');
	order += '&customer_ship_addr='+getLastInputValueByName('customer_ship_addr');
	order += '&customer_ship_addr2='+getLastInputValueByName('customer_ship_addr2');
	order += '&customer_ship_town='+getLastInputValueByName('customer_ship_town');
	order += '&customer_ship_postcode='+getLastInputValueByName('customer_ship_postcode');
	order += '&customer_ship_country='+getSelectedComboValueById('customer_ship_country');
	order += '&customer_ship_state='+getSelectedComboValueById('customer_ship_state');
	order += '&chooseoption=guest';

	// Extra parameters if jump from higher steps to lower step
	order += '&guest_id='+getLastInputValueByName('guest_id');
	order += '&addr_id='+getLastInputValueByName('addr_id');
	// Extra parameters if jump from higher steps to lower step
	
	$.post("onepageCheckout_ajax.php", order, function(theResponse){
		$('#onePageCheckout').html(theResponse);
	});
	return false;
	//return true;
}

// Checkout register function
function postregisterValues(){
	var order = 'step=1';

	order += '&customer_fname='+getLastInputValueByName('customer_fname');
	order += '&customer_lname='+getLastInputValueByName('customer_lname');
	order += '&customer_email='+getLastInputValueByName('customer_email');
	order += '&customer_pass='+getLastInputValueByName('customer_pass');
	order += '&customer_phone='+getLastInputValueByName('customer_phone');
	order += '&media='+getSelectedComboValueById('media');
	order += '&medianame='+getSelectedComboValueById('medianame');
	order += '&customer_bill_addr='+getLastInputValueByName('customer_bill_addr');
	order += '&customer_bill_addr2='+getLastInputValueByName('customer_bill_addr2');
	order += '&customer_bill_town='+getLastInputValueByName('customer_bill_town');
	order += '&customer_bill_postcode='+getLastInputValueByName('customer_bill_postcode');
	order += '&customer_bill_country='+getSelectedComboValueById('customer_bill_country');
	order += '&customer_bill_state='+getSelectedComboValueById('customer_bill_state');
	order += '&same_ship='+getSelectedRadioValueByName('same_ship');
	order += '&customer_ship_addr='+getLastInputValueByName('customer_ship_addr');
	order += '&customer_ship_addr2='+getLastInputValueByName('customer_ship_addr2');
	order += '&customer_ship_town='+getLastInputValueByName('customer_ship_town');
	order += '&customer_ship_postcode='+getLastInputValueByName('customer_ship_postcode');
	order += '&customer_ship_country='+getSelectedComboValueById('customer_ship_country');
	order += '&customer_ship_state='+getSelectedComboValueById('customer_ship_state');
	order += '&customer_newsletter_type_email='+getSelectedRadioValueByName('customer_newsletter_type_email');
	order += '&customer_newsletter_type_post='+getSelectedRadioValueByName('customer_newsletter_type_post');
	order += '&customer_terms='+getLastInputValueByName('customer_terms');
	order += '&captcha='+getLastInputValueByName('captcha');
	order += '&chooseoption=register';
	
	$.post("onepageCheckout_ajax.php", order, function(theResponse){
		$('#onePageCheckout').html(theResponse);
	});
}

function deleteItem( stage, addr_id, guest_id, removeD ){
	var order = 'saction=removeItem&step='+stage+'&removeD='+removeD;

	if (addr_id != '' && typeof (addr_id) == 'string' ) {
		order += '&aid='+addr_id;
	}

	if (guest_id != '' && typeof (guest_id) == 'string' ) {
		order += '&gid='+guest_id;
		order += '&chooseoption=guest';
	}
	$.get("onepageCheckout_ajax.php", order, function(theResponse){
		//alert('response sent.');
		$('#onePageCheckout').html(theResponse);
	});
}

//Single page checkout:
function onepageChekout( stage, addr_id, guest_id ){
	var order = 'step='+stage;

	if ( stage == 8 ) {
		if ( getSelectedRadioValueByName('accptTOC') != '' ){
			order += '&shopping_details='+getLastInputValueByName('shopping_details');
			order += '&shopping_amount='+getLastInputValueByName('shopping_amount');
			order += '&couponcode='+getLastInputValueByName('couponcode');
			order += '&discount='+getLastInputValueByName('discount');
			order += '&tax_amount='+getLastInputValueByName('tax_amount');
			order += '&shipping_amount='+getLastInputValueByName('shipping_amount');
			order += '&payable_amount='+getLastInputValueByName('payable_amount');
			order += '&aid='+getLastInputValueByName('aid');
			order += '&customer_id='+getLastInputValueByName('customer_id');
			order += '&guest_id='+getLastInputValueByName('guest_id');
			order += '&deliveryOs='+getLastInputValueByName('deliveryOs');
			order += '&creditPoints='+getLastInputValueByName('creditPoints');
			order += '&orderId='+getLastInputValueByName('orderId');
			order += '&orderRefId='+getLastInputValueByName('orderRefId');
			order += '&cod='+getLastInputValueByName('cod');
			order += '&offerIDS='+getLastInputValueByName('offerIDS');
			
			$.post("onepageCheckout_ajax.php", order, function(theResponse){
				$('#onePageCheckout').html(theResponse);
			});
		} else {
			alert('Accettare Termini e Condizioni.');
		}
	}
	
	if ( stage == 7 ) {
		if ( getSelectedRadioValueByName('accptTOC') != '' ){
			order += '&shopping_details='+getLastInputValueByName('shopping_details');
			order += '&shopping_amount='+getLastInputValueByName('shopping_amount');
			order += '&couponcode='+getLastInputValueByName('couponcode');
			order += '&discount='+getLastInputValueByName('discount');
			order += '&tax_amount='+getLastInputValueByName('tax_amount');
			order += '&shipping_amount='+getLastInputValueByName('shipping_amount');
			order += '&payable_amount='+getLastInputValueByName('payable_amount');
			order += '&aid='+getLastInputValueByName('aid');
			order += '&customer_id='+getLastInputValueByName('customer_id');
			order += '&guest_id='+getLastInputValueByName('guest_id');
			order += '&deliveryOs='+getLastInputValueByName('deliveryOs');
			order += '&creditPoints='+getLastInputValueByName('creditPoints');
			order += '&orderId='+getLastInputValueByName('orderId');
			order += '&orderRefId='+getLastInputValueByName('orderRefId');
			order += '&offerIDS='+getLastInputValueByName('offerIDS');
			
			$.post("onepageCheckout_ajax.php", order, function(theResponse){
				$('#onePageCheckout').html(theResponse);
			});
		} else {
			alert('Accettare Termini e Condizioni.');
		}
	}
	
	if ( stage == 6 ) {
		if (getSelectedRadioValueByName('deliveryOs') != '') {
			order += '&couponcode='+getLastInputValueByName('couponcode');
			order += '&action='+getLastInputValueByName('action');
			order += '&validcatalogs='+getLastInputValueByName('validcatalogs');
			order += '&discount='+getLastInputValueByName('discount');
			order += '&shopping_details='+getLastInputValueByName('shopping_details');
			order += '&shopping_amount='+getLastInputValueByName('shopping_amount');
			order += '&tax_amount='+getLastInputValueByName('tax_amount');
			order += '&shipping_amount='+getLastInputValueByName('shipping_amount');
			order += '&payable_amount='+getLastInputValueByName('payable_amount');
			order += '&aid='+getLastInputValueByName('aid');
			order += '&customer_id='+getLastInputValueByName('customer_id');
			order += '&guest_id='+getLastInputValueByName('guest_id');
			order += '&deliveryOs='+getSelectedRadioValueByName('deliveryOs');
			order += '&offerIDS='+getLastInputValueByName('offerIDS');

			$.post("onepageCheckout_ajax.php", order, function(theResponse){
				$('#onePageCheckout').html(theResponse);
			});
		} else {
			alert('Selezione un metodo di spedizione');
		}
	}

	if ( stage == 5 ) {
		if (getSelectedRadioValueByName('deliveryOs') != '' ) {
			order += '&couponcode='+getLastInputValueByName('couponcode');
			order += '&action='+getLastInputValueByName('action');
			order += '&validcatalogs='+getLastInputValueByName('validcatalogs');
			order += '&discount='+getLastInputValueByName('discount');
			order += '&shopping_details='+getLastInputValueByName('shopping_details');
			order += '&shopping_amount='+getLastInputValueByName('shopping_amount');
			order += '&tax_amount='+getLastInputValueByName('tax_amount');
			order += '&shipping_amount='+getLastInputValueByName('shipping_amount');
			order += '&payable_amount='+getLastInputValueByName('payable_amount');
			order += '&aid='+getLastInputValueByName('aid');
			order += '&customer_id='+getLastInputValueByName('customer_id');
			order += '&guest_id='+getLastInputValueByName('guest_id');
			order += '&deliveryOs='+getSelectedRadioValueByName('deliveryOs');
			order += '&offerIDS='+getLastInputValueByName('offerIDS');

			$.post("onepageCheckout_ajax.php", order, function(theResponse){
				$('#onePageCheckout').html(theResponse);
			});
		} else {
			alert('Selezione un metodo di spedizione');
		}
	}
	
	if ( stage == 4 ) {
		order += '&PageState='+getLastInputValueByName('PageState');
		order += '&navigate='+getLastInputValueByName('navigate');
		order += '&shopping_details='+getLastInputValueByName('shopping_details');
		order += '&shopping_amount='+getLastInputValueByName('shopping_amount');
		order += '&couponcode='+getLastInputValueByName('couponcode');
		order += '&discount='+getLastInputValueByName('discount');
		order += '&tax_amount='+getLastInputValueByName('tax_amount');
		order += '&shipping_amount='+getLastInputValueByName('shipping_amount');
		order += '&payable_amount='+getLastInputValueByName('payable_amount');
		order += '&aid='+getLastInputValueByName('aid');
		order += '&customer_id='+getLastInputValueByName('customer_id');
		order += '&guest_id='+getLastInputValueByName('guest_id');
		order += '&deliveryOs='+getLastInputValueByName('deliveryOs');
		order += '&CardType='+getSelectedComboValueById('CardType');
		order += '&CardHolder='+getLastInputValueByName('CardHolder');
		order += '&CardNumber='+getLastInputValueByName('CardNumber');
		order += '&sMonth='+getSelectedComboValueById('sMonth');
		order += '&sYear='+getSelectedComboValueById('sYear');
		order += '&StartDate='+getLastInputValueByName('StartDate');
		order += '&eMonth='+getSelectedComboValueById('eMonth');
		order += '&eYear='+getSelectedComboValueById('eYear');
		order += '&ExpiryDate='+getLastInputValueByName('ExpiryDate');
		order += '&IssueNumber='+getLastInputValueByName('IssueNumber');
		order += '&CV2='+getLastInputValueByName('CV2');
		order += '&creditPoints='+getLastInputValueByName('creditPoints');
		order += '&offerIDS='+getLastInputValueByName('offerIDS');
		
		$.post("onepageCheckout_ajax.php", order, function(theResponse){
			$('#onePageCheckout').html(theResponse);
		});
	}
	
	if ( stage == 3 ) {
		if (getSelectedRadioValueByName('deliveryOs') != '') {
			order += '&couponcode='+getLastInputValueByName('couponcode');
			order += '&action='+getLastInputValueByName('action');
			order += '&validcatalogs='+getLastInputValueByName('validcatalogs');
			order += '&discount='+getLastInputValueByName('discount');
			order += '&shopping_details='+getLastInputValueByName('shopping_details');
			order += '&shopping_amount='+getLastInputValueByName('shopping_amount');
			order += '&tax_amount='+getLastInputValueByName('tax_amount');
			order += '&shipping_amount='+getLastInputValueByName('shipping_amount');
			order += '&payable_amount='+getLastInputValueByName('payable_amount');
			order += '&aid='+getLastInputValueByName('aid');
			order += '&customer_id='+getLastInputValueByName('customer_id');
			order += '&guest_id='+getLastInputValueByName('guest_id');
			order += '&deliveryOs='+getSelectedRadioValueByName('deliveryOs');
			order += '&offerIDS='+getLastInputValueByName('offerIDS');
			
			$.post("onepageCheckout_ajax.php", order, function(theResponse){
				$('#onePageCheckout').html(theResponse);
			});
		} else {
			alert('Selezione un metodo di spedizione');
		}
	} else if( stage == 1 || stage == 2 ) {
		if (addr_id != '' && typeof (addr_id) == 'string' ) {
			order += '&aid='+addr_id;
		}

		if (guest_id != '' && typeof (guest_id) == 'string' ) {
			order += '&gid='+guest_id;
			order += '&chooseoption=guest';
		}
		
		// Parameter added when reverting back from higher step to lower one
		shippingType = getLastInputValueByName('deliveryOs');
		if (shippingType != '' ) {
			order += '&shippingType='+shippingType;
		}
		// Parameter added when reverting back from higher step to lower one :: ends
		//alert('request sent.');
		$.get("onepageCheckout_ajax.php", order, function(theResponse){
			//alert('response sent.');
			$('#onePageCheckout').html(theResponse);
		});
	}
}

function justpayout( chooseoptionval, guest_id ){
	
	order = 'step=1&chooseoption='+chooseoptionval;
	if ( chooseoptionval == 'guest' ) {
		if (guest_id != '' && typeof (guest_id) == 'string' ) {
			order += '&gid='+guest_id;
		}
	}
	$.get("onepageCheckout_ajax.php", order, function(theResponse){
		$('#onePageCheckout').html(theResponse);
	});
	return false;
	//return true;
}

function validateCheckoutOption( guest_id ){
	flag = false;	
	chooseoptionval = getSelectedRadioValueByName('chooseoption');
	if (typeof chooseoptionval != 'undefined' && chooseoptionval != ''){
		flag = true;
	}	
	
	if (!flag){
		alert("Selezionare un metodo di pagamento.");
		return false;	
	}
	
	order = 'step=1&chooseoption='+chooseoptionval;
	if ( chooseoptionval == 'guest' ) {
		if (guest_id != '' && typeof (guest_id) == 'string' ) {
			order += '&gid='+guest_id;
		}
	}
	$.get("onepageCheckout_ajax.php", order, function(theResponse){
		$('#onePageCheckout').html(theResponse);
	});
	return false;
	//return true;
}

function getSelectedComboValueById( name ){
	//return $( "#"+name+" :selected" ).val();
	val = $( "#"+name+" :selected" ).val();
	if ( typeof val == 'undefined' ){
		return '';
	} else {
		return val;
	}
}

function getSelectedRadioValueByName( name ){
	//return $("input[name="+name+"]:checked").val();
	val = $("input[name="+name+"]:checked").val();
	if ( typeof val == 'undefined' ){
		return '';
	} else {
		return val;
	}	
}

function getLastInputValueByName( name ){
	//return takelast($("input[name="+name+"]"));
	val = takelast($("input[name="+name+"]"));
	if ( typeof val == 'undefined' ){
		return '';
	} else {
		return val;
	}
}

function setFocusToElementByName( name ){
	$("input[name="+name+"]").focus();
}

function takelast( obj ){
	var returnVal = '';
	for (i = 0; i < obj.length; i++){
		returnVal = obj[i].value;
	}
	return returnVal;
}

// Order History
function orderHistory(start, max, total){
	var order = 'case=orderhistroy';

	order += '&start='+start;
	order += '&max='+max;
	order += '&total='+total;
	
	$.post("ajax.php", order, function(theResponse){
		$('#myOrderHistory').html(theResponse);
	});
}

// Recently Viewed
function RecentlyViewed(start, max, total){
	var order = 'case=recentlyViewed';

	order += '&start='+start;
	order += '&max='+max;
	order += '&total='+total;
	
	$.ajaxSetup({
	   url: "ajax.php",
	   type: "POST",
	   dataType:"html",
	   contentType: "application/x-www-form-urlencoded;charset=ISO-8859-1"
	});

	$.post("ajax.php", order, function(theResponse){
		$('#recentlyViewed').html(theResponse);
	});
}

// Recently Viewed Section
function RecentlyViewedSection(start, max, total){
	var order = 'case=RecentlyViewedSection';

	order += '&start='+start;
	order += '&max='+max;
	order += '&total='+total;
	
	$.ajaxSetup({
	   url: "ajax.php",
	   type: "POST",
	   dataType:"html",
	   contentType: "application/x-www-form-urlencoded;charset=ISO-8859-1"
	});
	
	$.post("ajax.php", order, function(theResponse){
		$('#product-box-center-recent-viewed').html(theResponse);
	});
}

// Cross Sell Products Section
function CrossSellProductSection(start, max, total, CSPIDs){
	var order = 'case=CrossSellProductSection';

	order += '&start='+start;
	order += '&max='+max;
	order += '&total='+total;
	order += '&CSPIDs='+CSPIDs;
	
	$.post("ajax.php", order, function(theResponse){
		$('#product-box-center-cross-sell-products').html(theResponse);
	});
}

// Search
function Search(start, max, total, q){
	var order = 'case=search';

	order += '&start='+start;
	order += '&max='+max;
	order += '&total='+total;
	order += '&q='+q;
	
	$.post("ajax.php", order, function(theResponse){
		$('#searchResult').html(theResponse);
	});
}

// Create a reorder
function reorder(orderX){
	var order = 'case=reorder';

	order += '&orderX='+orderX;
	
	$.post("ajax.php", order, function(theResponse){
		if ( theResponse == 1 ){
			if ( confirm("Il carrello è stato creato per il corrente ordine.\nVolete procedere al pagamento adesso.") ){
				$(window.location).attr('href', 'shoppingbasket.aspx');
			}
		} else if ( theResponse == 2 ) {
			if ( confirm("Il carrello è stato creato per il corrente ordine ma alcuni prodotti non sono.\nVolete procedere al pagamento adesso?") ){
				$(window.location).attr('href', 'shoppingbasket.aspx');
			}
		} else {
			alert( 'Aggiungere altri prodotti al vostro carrello.' );
		}
		return false;
	});
}

// Check empty search
function checkEmptySearch(ObjId){
	if ( $('#'+ObjId).val() == '' ){
		alert('Inserire la vostra parola chiave.');
		$('#'+ObjId).focus();
		return false;
	}
}

//IsEmpty
function IsEmpty( obj ){
	if ( $("#"+obj).val() == '' ){
		return true;
	}
	return false;
}
