/*
 *  Hook events for all cart pages
 */
$(
	function() {
		/*  Product details */
		$("form.addProductToCart").bind("submit", handleAddToCart);
	}
);


function receiveOrderStep( response ) {
	$("#order").html(response);
	applyOrderBindings();
	if( $('table#orderSteps').pngFix ) {
		$('table#orderSteps').pngFix();
	}
}


function applyOrderBindings() {
	/*  Order steps, cart input */
	$("table.cartView .cartColQuantity input[type='text']").unbind().bind("keydown", function(event){
		$("#btnOrder").hide();
		$("#btnUpdate").show();
	
		/*  IE doesn't want to submit a form if the submit button is hidden
		 * (and this happens when we hide the Order button and display the Update button)
		 * so handle that case here. If the user hits ENTER then update the cart.
		 */
		if( event.keyCode == 13) {
			handleUpdateCart( event );
		}
	});

	$("#btnUpdate").unbind().click(handleUpdateCart);

	$("#cartForm").unbind().bind("submit", function() {
		jQuery.ajax({
					type:'post',
					url:p_web_root_lang + "/shop/order/index.cfm",
					data:$("#cartForm").serialize(),
					success:receiveOrderStep,
					error:function(response) {
						$("#order").html(response.responseText);
					}
				});
		return false;
	});
	
	$("table#orderSteps a").unbind().click(function(event) {
		jQuery.ajax({
					type:'post',
					url:p_web_root_lang + "/shop/order/index.cfm?fuseaction=" + $(event.target.parentNode).attr("data-fuseaction"),
					data:{},
					success:receiveOrderStep,
					error:function(response) {
						$("#order").html(response.responseText);
					}
				});
		return false;
	});
}


/*
 * Add the product and update the cart summary.
 */
function handleAddToCart( event ) {
	var params = new Object();

	params["fuseaction"] = "add_product";
	params["id"]         = event.target.id.value;
	params["f_quantity"] = event.target['f_quantity'+event.target.id.value].value;

	if( params["f_quantity"] == '' || isNaN(params["f_quantity"]) || params["f_quantity"] < 0 ) {
		alert( "Please enter a number greater then 0!" );
		return false;
	}
	
	/*  Use POST method: http://www.cs.tut.fi/~jkorpela/forms/methods.html  */
	jQuery.ajax({
				type:'post',
				url:p_web_root_lang + "/shop/index.cfm",
				data:params,
				success:function( response ){
					$("#cart_hold_summary").html(response);		
					//alert(event.target.id.value);
					$("form#formid" + event.target.id.value + " #f_quantity" + event.target.id.value).effect("transfer", {to: "#cart_hold_summary"}, 600);
				}
			});

	/*  Clear the quantity the user entered  */
	event.target['f_quantity'+event.target.id.value].value = '';

	return false;
}


/*
 *  Called after the user changes product quantity when viewing the cart.
 */
function handleChangeQuantity( event ) {
	$("#btnOrder").hide();
	$("#btnUpdate").show();

	/*  IE doesn't want to submit a form if the submit button is hidden
	 * (and this happens when we hide the Order button and display the Update button)
	 * so handle that case here. If the user hits ENTER then update the cart.
	 */
	if( event.keyCode == 13) {
		handleUpdateCart( event );
	}
}


/*
 *  Called when the user clicks on the button to update cart quantities
 * The product table prices is updated via received Javascript (so we evaluate javascript
 * in the reponse)
 */
function handleUpdateCart( event ) {
	var requestString  = $("#cartForm").serialize().replace(/fuseaction=[^&]*/, "fuseaction=update_quantities");

	jQuery.ajax({
			type:'post',
			dataType:'script',
			url:p_web_root_lang + "/shop/index.cfm",
			data:requestString,
			success:function( response ) {
				$("#btnOrder").show();
				$("#btnUpdate").hide();
			}
		});

	return false;
}
