$(document).ready(function() {

	function clearConsole() {
		if (window.console && window.console.firebug) {
			console.clear()
		}
	}

	$('.continue').click(function(){
		if ($("#checkout-form").valid()) {
			if ( $('#fMinimumVal').length ) {
				if ( parseInt($('#totalPrice').val()) < parseInt($('#fMinimumVal').val()) ) {
					$('#error-check').empty();
					$('#error-check').show();
					$('#error-check').append('<strong>Please note:</strong> Minimum order value <strong>EXCLUDING DELIVERY</strong> must be more than <strong>&pound;' + $('#fMinimumVal').val() + '</strong>');
					return false;
				}
			}

			$('#checkout-container').empty();
			$('#checkout-container').load('/resources/vb/checkout-step-1.asp');
			clearConsole();
		};
		return false;
	});

	if ( $('#fPostcode').length ) {
		$('#fPostcode').change(function(){
			updatePrices($(this).val(),$('#totalPrice').val());
			//clearConsole();
		});

		function updatePrices(id,total) {
			$('.postcode-response').empty();
			$('.postcode-response').load('/resources/vb/post-get-delivery.asp',{id:id,total:total});
		}
		updatePrices($('#fPostcode').val(),$('#totalPrice').val());
	}

	// update quantities within basket
	$('.button-update').click(function(e){
		e.preventDefault();
		id = $(this).attr('id').replace('prodId-','');
		q = $('#quantity-'+id);
		qv = q.val();
		var min = $('#min-'+id).attr('title');

		if (qv != '') {
			if ( parseInt(qv) != 0 && parseInt(qv) < parseInt(min) ) {
				q.addClass('error');
				alert('minimum order must be more than '+min);
				return false;
			} else {
				$.post('/resources/vb/post-update-item.asp',{id:id,quantity:qv}, function(response) {
					if (response=='nocookie') {
						window.location.href='/';
					} else {
						q.removeClass('error');
						window.location.reload();
					}
				});
			}
		} else {
			q.addClass('error');
		}
		clearConsole();
	});

	// delete item from basket
	$('.i-delete').click(function(e){
		e.preventDefault();
		id = $(this).attr('id').replace('delete-','');
		$.post('/resources/vb/post-update-item.asp',{id:id,quantity:0}, function(response) {
			if (response=='nocookie') {
				window.location.href='/';
			} else {
				window.location.reload();
			}
		});
		clearConsole();
	});

});
