$(document).ready(function(){
	// Postcodes
	$('.node-postcodes, .node-postcodes h2').css('cursor', 'pointer');
	$('.node-postcodes').click(function(event){
		if ($(this).attr('class').indexOf('active') <= -1) $(this).addClass('active');
		showhide = ($(this).find('h2 a').html() == "View Regions") ? "Hide Regions" : "View Regions";
		$(this).find('div').slideToggle(500);
		$(this).find('h2 a').html(showhide);
		event.preventDefault();
	});
	
	// Promos
	$("#updatePromo").click(function(event){
		var pc = $("#promo_code").val();
		if (pc) {
			$("#updatePromo").attr("disabled", "disabled");
			$("#updatePromo").addClass("disabled");
			$("#promoLoader").show();
			$.getJSON(webRoot + "/basket/", 
				{offer_code:pc, fuseaction:"processoffer"},
				function(data) {
					$("#promoLoader").hide();
					$(".promo #msg").show();
					if (data.SUCCESS) {
						$("#promo_code").removeClass("error");
						$("#promo_code").attr("readonly");
						$("#updatePromo").hide();
						$(".promo #msg").html("Promotion code applied.");
						$(".promo #remove").show();
						// Update table
						var subtotal = $("#subtotal-val").html().replace(/[^0-9.]/, "");
						var vat = ($("#vat-val").html()) ? $("#vat-val").html().replace(/[^0-9.]/, "") : 0;
						var grosstotal = (Number(subtotal) + Number(vat)).toFixed(2);
						var discount = ((grosstotal / 100) * data.DATA.SPECIALOFFERDISCOUNT).toFixed(2);
						var newSubtotal = (grosstotal - discount).toFixed(2);
						var delivery = $("#delivery-val").html();
						var deliverySurcharge = $("#deliverySurcharge-val").html();
						var total = (Number(newSubtotal) + Number(delivery) + Number(deliverySurcharge)).toFixed(2);
						$("#delivery-tr").before("<tr id='discount-tr'><td colspan='3' style='text-align:right;'><strong>" + data.DATA.OFFERTITLE + ":</strong></td><td style='text-align:right;'><strong>- &pound;" + discount + "</strong></td></tr>");
						$("#total-val").html(total);
					} else {
						$("#updatePromo").removeAttr("disabled");
						$("#updatePromo").removeClass("disabled");
						$("#promo_code").addClass("error");
						$("#promo_code").focus();
						$(".promo #msg").html("<span>" + data.ERRORS + "</span>");
					};
				}
			);
		} else {
			$("#promo_code").addClass("error");
			$("#promo_code").focus();
			$(".promo #msg").html("<span>Please enter a valid promotion code.</span>");
			$(".promo #msg").show();
		}
		event.preventDefault();
	});
	
	$("#remove").click(function(event){
		$("#updatePromo").removeAttr("disabled");
		$("#updatePromo").removeClass("disabled");
		$("#updatePromo").show();
		$("#promo_code").val("");
		$("#promo_code").removeAttr("readonly");
		$("#promo_code").focus();
		$(".promo #msg").hide();
		$("#remove").hide();
		// Update table
		var subtotal = $("#subtotal-val").html().replace(/[^0-9.]/, "");
		var vat = ($("#vat-val").html()) ? $("#vat-val").html().replace(/[^0-9.]/, "") : 0;
		var delivery = $("#delivery-val").html();
		var deliverySurcharge = $("#deliverySurcharge-val").html();
		var total = (Number(subtotal) + Number(vat) + Number(delivery) + Number(deliverySurcharge)).toFixed(2);
		$("#discount-tr").remove();
		$("#total-val").html(total);
		event.preventDefault();
	});
});
