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

	
	/* calculate postage */
	$('input#calculatePostage').click(function() {
		var theShipping = $("#enterShipping").val();
		if(theShipping==""){
			alert('Please select a shipping destination');	
			return false;
		}
		$.post("/pages/components/include_cart.php", "cartaction=calculatePostage&shipping="+theShipping, function(data) {
			$("#cart_details").html(data);
			window.location.href = '/cart/';

		});
	});
	
	$('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) {
		$("#cart_details").html(data);
		window.location.href = '/cart/';
	});
}

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

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/');
	}
}


