var buttonstate = false;

$(document).ready(function() {
	
	if ($("#expired").length) {
		alert("Sorry, your session has expired. All items from your basket have been dropped.");
	}

	$(".lessinfo").text('More information');
	$(".moreinfo").hide();
	$(".supplementaryticket").hide();
	
	$("#chpassfields").hide();
	$("#chpass").show();
	$("#chpassfields input").attr("disabled", true);
	
	$("#chpass").click(function(e) {
		$("#chpass").hide();
		$("#chpassfields input").removeAttr("disabled");
		$("#chpassfields").show();
	});
	
	if ($("#country").val() != 'GB') {
		$("#county").attr('disabled', true);
	}
	
	$("#country").change(function() {
		if ($(this).val() != 'GB') {
			$("#county").val('');
			$("#county").attr('disabled', true);
		}
		else {
			$("#county").removeAttr('disabled');
		}
	});
	
	$(".lessinfo").click(function(e) {
		e.preventDefault();
		var moreinfo = $(this).closest(".event,.highlightevent").find(".moreinfo");
		
		var eventid = $(this).closest(".event,.highlightevent").attr('id').substr(5);

		moreinfo.find(".subevents").load('_subevents.php?event=' + eventid);
		
		moreinfo.toggle();
		
		if (moreinfo.is(":visible")) {
			$(this).text('Less information');
		} else {
			$(this).text('More information');
		}
	});
	
	$(".buy").live('click', function(e) {
		e.preventDefault();
		$.post("cart.php",
				{ ajax: 1,
				  howmany:  $(this).siblings(".howmany").val(),
				  ticketid: $(this).siblings(".ticketid").val()
				},
				function(data) {
					var totals = data.split("\n");
					if (totals.length < 3 || ! isnum(totals[0])) { alert(data); }
					else {
						$("#totals").text(number_format(totals[1], 2));
						$("#icount").text(totals[0]);
						if (totals[0] > 1) { $("#iplural").show(); } else { $("#iplural").hide(); }
						$("#iprice").html("&pound;" + number_format(totals[1], 2));
						alert(totals[2]);
					}
			  });
	});
	
	$(".quantity").change(function() {
		var ticketid = $(this).attr('id').substr(1);
		var qty = $(this).val();
		
		// update cart, update totals
		$.post("cart.php",
				{ ajax: 1,
				  change: 1,
				  howmany:  qty,
				  ticketid: ticketid
				},
				function(data) {
						var totals = data.split("\n");
						if ( ! isnum(totals[0])) { alert(data); }
						else {
						
							if (qty == 0) {
								$("#cartrow"+ticketid).fadeOut();
							}
							else {
								$("#amount"+ticketid).text(number_format($("#price"+ticketid).text()*qty, 2));
							}
						
							$("#totals").text(number_format(totals[1], 2));
							$("#icount").text(totals[0]);
							if (totals[0] > 1) { $("#iplural").show(); } else { $("#iplural").hide(); }
							$("#iprice").html("&pound;" + number_format(totals[1], 2));
						}
			  });
	});
	
	$(".remove").click(function() {
		var ticketid = $(this).attr('id').substr(1);
	
		// update cart, update totals
		$.post("cart.php",
				{ ajax: 1,
				  change: 1,
				  howmany:  0,
				  ticketid: ticketid
				},
				function(data) {
						var totals = data.split("\n");
						if ( ! isnum(totals[0])) { alert(data); }
						else {
							$("#cartrow"+ticketid).fadeOut();
							$("#totals").text(number_format(totals[1], 2));
							$("#icount").text(totals[0]);
							if (totals[0] > 1) { $("#iplural").show(); } else { $("#iplural").hide(); }
							$("#iprice").html("&pound;" + number_format(totals[1], 2));
						}
			  });
	});
	
	
	$(".becomeafriend").click(function() {
		$("#becomeafriend").hide();
	});
	
	$(".resend").click(function(e){
		e.preventDefault();
		var orderid = $(this).attr('id').substr(6);
		$.post("resend.php",
				{ ajax: 1,
				  order: orderid
				},
				function(data) {
					alert(data);
			});
	});
	
	$(".printconfirmation").click(function(e) {
		e.preventDefault();
		window.open($(this).attr('href'));
	});
	
	$("#cart").submit(function() {
		if ($("#cart #recalculate").length) {
			return false;
		}
	});
	
});

function isnum(input)
{
   return (input - 0) == input && input.length > 0;
}