
$(document).ready(function() {
	$('#checkout-customer-address-copy').click(function() {
		var i, fields = ['name', 'phone', 'email', 'address-street', 'address-suburb', 'address-postcode', 'address-state', 'address-country'];
		if (('' + $('#shipping-name').val() + $('#shipping-address-street').val()).length && !confirm('Are you sure you wish to overwrite the shipping details?'))
			return;
		for (i = 0; i < fields.length; i++)
			$('#shipping-' + fields[i]).val($('#customer-' + fields[i]).val());
	});
	
	$('#cart-region-select').change(function(event) {
		$('#product-cart-view-form input.update-order').trigger('click');
	});
	
	var $quants = $('input.quantity').attr('autocomplete', 'off').keydown(function(event) {
		var up = 38, down = 40, val = this.value;
		if (val.indexOf('-') >= 0)
			val = 0;//assume negative
		else {
			val = parseInt(val.replace(/\..*|\D+/g, ''), 10);
			if (isNaN(val))
				val = 0;
		}
		if (event.which == up) {
			event.preventDefault();
			val++;
		}
		else if (event.which == down) {
			event.preventDefault();
			val--;
		}
		if (val < 1)
			val = $(this).hasClass('removable') ? 0 : 1;
		if (this.value != val)
			this.value = val;
	});
	$quants.hasAncestor('.add-to-cart').after('<div class="description">You can use your up and down keys in this box to adjust the quantity.</div>');
	$quants.attr('title', 'You can use your up and down keys in this box to adjust the quantity.');
	
	var $payMethods = $('#checkout-payment-method input:radio:visible');
	if ($payMethods.length == 1)
		$payMethods.click();
});

