$(document).ready(function() {
	/* This is for the add to cart function on the category page */
	$('input.buyItem').click(function() {
		var theid = $(this).attr('rel');
		$.post("/pages/components/include_cart.php", "price_id="+ theid +"&cartaction=add&qty=1", function(data) {
			$(".shopCart .update").html(data);
			pulseCart();
		});
	});

	$('input.popinBuyItem').click(function() {
		var theid = $(this).attr('rel');
		$.post("/pages/components/include_cart.php", "price_id="+ theid +"&cartaction=add&qty=1", function(data) {
			parent.$(".shopCart .update").html(data);
			parent.$.fancybox.close();
			pulseCart();
		});
	});
	
	/* calculate postage */
	$('input#calculatePostage').click(function() {
		var thePostcode = $("#enterPostcode").val();
		$.post("/pages/components/include_cart.php", "cartaction=calculatePostage&postcode=" + thePostcode, function(data) {
			var returnData = eval("(" + data + ")");
			if(returnData['error'] == "true") {
				$("#calculatedShipping").html('Calculate');
				$("#totalIncGST").html('Calculate');
				$("#shippingPostcode").val('');
				$(".details").hide();
				alert(returnData['errorMessage']);
			}
			else {
				$("#calculatedShipping").html(returnData['shipping']);
				$("#totalIncGST").html(returnData['total']);
				$("#shippingPostcode").val(thePostcode);
				
				/* autofill the shipping form where possible */
				/*
				var shippingSuburbField = $("#shippingSuburb");
				var shippingStateField = $("#shippingState");
				
				if(shippingSuburbField.val() == "") shippingSuburbField.val(returnData['suburb']);
				if(shippingStateField.val() == "") shippingStateField.val(returnData['state']);
				*/
				$(".details").show();
			}
		});
	});
	
	$('input#checkPostcode').click(function() {
		var thePostcode = $("#postcode").val();
		$.post("/pages/components/include_cart.php", "cartaction=calculatePostage&postcode=" + thePostcode, function(data) {
			var returnData = eval("(" + data + ")");
			if(returnData['error'] == "true") {
				$("#deliveryMessage").html(returnData['errorMessage']);
			}
			else {
				$("#deliveryMessage").html('Great, we can deliver to you in zone ' + returnData['zone']);
			}
		});
	});
});

function changeQuantity(qty, id) {
	$.post("/pages/components/include_cart.php", "cartaction=quantity&qty=" + qty + "&key=" + id, function(data) {
		$(location).attr('href','/cart/');
	});
}

function removeItem(id, target) {
	if(confirm("Remove Item from cart?")){
		$.post("/pages/components/include_cart.php", "cartaction=remove&price_id=" + id, function(data) {
			$(".shopCart .update").html(data);
			if(target == "cart") {
				$(location).attr('href','/cart/');
			}
			else if(target == "payment") {
				$(location).attr('href','/payment/');
			}
			else {
				$(location).attr('href','/');
			}
		});
	}
}

function copyBilling(form) {
	if(form.shippingSame.checked) {
		form.shippingFirstName.value = form.billingFirstName.value;
		form.shippingLastName.value = form.billingLastName.value;
		form.shippingStreetAddress.value = form.billingStreetAddress.value;
		form.shippingSuburb.value = form.billingSuburb.value;
		form.shippingState.selectedIndex = form.billingState.selectedIndex;
		form.shippingPostcode.value = form.billingPostcode.value;
	}
}

function pulseCart() {
	/*
	$('.shopCart .update').css('backgroundColor', '#ff9900');
	$('.shopCart .update').animate({ backgroundColor: "#ff7d00"}, 500);
	*/
	$('#confirmCart').css('display', 'block').delay(1500).fadeOut(300);
	
}

function confirmLogout() {
	var logoutAnswer = confirm("Are you sure you want to log out?");
	if(logoutAnswer == true) {
		$(location).attr('href', '/logout/');
	}
}

function processOrder(){	
		$('#submitPayment').attr('disabled', 'disabled'); 
		$('#ajaxLoader').show();
		$.post("/eway/process_payment_eway.php", $("#paymentForm").serialize(), function(data) {

			if(data.substring(0,2)=="00"){
				$('#ewaySuccess').show().html(data.substring(2));
				$('#payment-content').hide();
			}
			else{
				$('#ewayError').show().html(data.substring(2));
				$('#submitPayment').removeAttr('disabled');
				$('#ajaxLoader').hide();
			}
		});
		
	}

