/**
* $Id$
*/

/**
* Init UI
*/
$(document).ready(function() {

	// Add handler to print button
	$('#btn-print-basket').click(function() {
		window.focus();
		window.print();
	});

	// Convert "remove" buttons to graphics
	$('input[rel=remove]').each(function(i, el) {
		$(el).replaceWith('<a href="" onclick="removeItem(\''+$(el).attr('name')+'\');return false;" title="remove"><span>remove</span></a>');
	});

	// Convert "apply discount" button
	$('input[rel=discount]').replaceWith('<a href="" onclick="applyDiscount();return false;" title="apply discount"><span>apply</span></a>');

	// Add handler to "zone" dropdown that will refresh the basket automatically
	// upon change. To mimic pressing the "update basket" button a hidden field
	// is create called "update"
	$('select[name=zone]').change(function() {
		$('<input type="hidden" name="update" value="Update" />').appendTo($('#basket-form'));
		$('#basket-form').submit();
	});
});

/**
* Create a hidden field in the basket form, give it the specified name and
* submit the form.
* This may look convoluted, but it's in keeping with the non-javascript version
* of the form.
*
* @param string Element name (format: "remove[item-ref]")
* @return void
*/
function removeItem(elName) {
	$('<input type="hidden" name="'+elName+'" value="Remove" />').appendTo($('#basket-form'));
	$('#basket-form').submit();
}

/**
* Create a hidden field named "applydiscount" and submit the form.
*
* @return void
*/
function applyDiscount() {
	$('<input type="hidden" name="applydiscount" value="Apply" />').appendTo($('#basket-form'));
	$('#basket-form').submit();
}
