
function changeCurrency(  )
{
	var currency = $("#currency").val();
	$.post("ajax/change-currency.asp", { currency: currency }, function(result) {
		location.reload();
	});
}

function logout()
{
	return confirm("Logging out will clear your cart and shopping session. Are you sure you want to continue?");
}

function changeDeliveryCountry()
{
	$.post("cart.asp", { time: new Date() }, function(html) {
		$("#orderform_cart").html(html);
	});
}

function toggleBilling()
{
	var d = $("#billing_details");
	var period = 350;
	if( d.is(":hidden") ) {
		d.fadeIn(period);
		$("#payoption_account").attr("checked", true);
	} else {
		d.fadeOut(period);
		$("#payoption_creditcard").attr("checked", true);
	}
}

function toggleDeliveryConditions()
{
	var d = $("#delivery_conditions");
	var period = 350;
	if( d.is(":hidden") ) {
		$("#delivery_companyname").attr("disabled", true).css("color", "#999");
		d.fadeIn(period);
	} else {
		$("#delivery_companyname").attr("disabled", false).css("color", "white");
		d.fadeOut(period);
	}
}

function switchPayment( method ) 
{
	if( method == "account" ) {
		if( ! $("#different_billing").attr("checked") )
			$("#different_billing").click();
	} else if( method == "creditcard" ) {
		if(  $("#different_billing").attr("checked") )
			$("#different_billing").click();
	}
}

function isBlank(str)
{
	return String(str).replace(/\s/g, "") == "";
}

function isEmail( addy )
{
	var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	return isBlank(addy) || ! String(addy).match(emailRegex);	
}

function checkSubmission()
{
	//check the email
	var fname = $("#contact_fname");
	var lname = $("#contact_lname");
	var email = $("#contact_email");
	if( isBlank( fname.val() ) ) {
		alert("Please enter your contact first name");
		fname.focus();
		return false;
	} else if( isBlank( lname.val() ) ) {
		alert("Please enter your contact last name");
		lname.focus();
		return false;
	} else if( isEmail( email.val() ) ) {
		alert("Please enter your contact email");
		email.focus();
		return false;
	}

	//delivery
	var attention = $("#delivery_attention");
	var business = $("#delivery_companyname");
	var address = $("#delivery_address");
	var suburb = $("#delivery_suburb");
	var state = $("#delivery_state");
	var phone = $("#delivery_phone");
	var country = $("#delivery_country");
	var postcode = $("#delivery_postcode");

	var phonere = /([0-9] | \+ | \( | \) | \s)/

	if( isBlank( attention.val() ) ) {
		alert("Please enter the delivery attention.");
		attention.focus();
		return false;
	} else if( ! $("#delivery_residential").attr("checked") && isBlank( business.val() ) ) {
		alert("Please enter the delivery business name.");
		business.focus();
		return false;
	} else if( isBlank( phone.val() ) || phone.val().match(/[a-z]/i) ) {
		alert("Please enter the delivery phone number.");
		phone.focus();
		return false;
	} else if( isBlank( address.val() ) ) {
		alert("Please enter the delivery address.");
		address.focus();
		return false;
	} else if( isBlank( suburb.val() ) ) {
		alert("Please enter the delivery city or suburb.");
		suburb.focus();
		return false;
	} else if( isBlank( state.val() ) ) {
		alert("Please enter the delivery state or province.");
		state.focus();
		return false;
	} else if( isBlank( country.val() ) ) {
		alert("Please enter the delivery country.");
		country.focus();
		return false;
	} else if( isBlank( postcode.val())) {// || ! Boolean(postcode.val().match(/\d/)) ) {
		alert("Please enter a valid delivery postcode.");
		postcode.focus();
		return false;
	} else if( $("#different_billing").attr("checked") ) {
		//billing checks
		email = $("#billing_email");
		attention = $("#billing_attention");
		business = $("#billing_email");
		address = $("#billing_address");
		suburb = $("#billing_suburb");
		state = $("#billing_state");
		country = $("#billing_country");
		postcode = $("#billing_postcode");

		if( isBlank( attention.val() ) ) {
			alert("Please enter the billing attention.");
			attention.focus();
			return false;
		} else if( isEmail( email.val()) ) {
			alert("Please enter the billing email.");
			email.focus();
			return false;
		} else if( isBlank( address.val() ) ) {
			alert("Please enter the billing address.");
			address.focus();
			return false;
		} else if( isBlank( suburb.val() ) ) {
			alert("Please enter the billing suburb or city");
			suburb.focus();
			return false;
		} else if( isBlank( state.val() ) ) {
			alert("Please enter the billing state.");
			state.focus();
			return false;
		} else if( isBlank( country.val() ) ) {
			alert("Please enter the billing country.");
			country.focus();
			return false;
		} else if( isBlank( postcode.val() ) || ! Boolean(postcode.val().match(/\d/)) ) {
			alert("Please enter a valid billing postcode.");
			postcode.focus();
			return false;
		}
	}

	if( $("#delivery_residential").attr("checked") && ! $("#accept-delivery-conditions").attr("checked") ) {
		alert("You must accept the delivery conditions or deliver to a business address");
		return false;
	}
	return confirm("Are you sure you want to submit your order?");
}

function showimage(imagename) {
				$.facebox("<img src='images/xlimage/"+imagename+"?v=1' alt='"+imagename+"' />");
			}
