// All praise be to jQuery
$(function() {
	
	// Apply tablesorters
	$(".simplelist").each(function() {
		$(this).tablesorter();
	});

	$("#menu").find("a").each(function() {
		if ("/"+$(this).attr("href")==window.location.pathname || $(this).attr("href")==window.location.href) {
			$(this).addClass("active");
		}
	});

	$(".confirm").click(function(e) {
		return(confirm("Really?"));
	});

	$(".delete").click(attachAjaxDeleteEvent);

	function attachAjaxDeleteEvent(e) {
		e.preventDefault();
		var parentRow = $(this).parents("tr")[0];
		if (confirm("Really Delete?")) {
			$.get($(this).attr("href"), function(d) {
				if (d=="Success") {
					$(parentRow).hide();
				}
				else alert("UNEXPECTED RESPONSE FROM SERVER: "+d);
			});
		}
	}

	$("#loginForm").find("#user_name").focus();

	$("#timesheetForm").submit(function() {
		return(confirm("Submit Timesheet?"));
	});


	$("form").submit(function() {
		var alertMessage = "";
		$(this).find(".required").each(function() {
			if ($(this).val()=="") {
				alertMessage += $(this).attr("rel")+" is required\n";
			}
		});
		if ($(this).find("#re_password").length!=0 && $(this).find("#re_password").val()!=$(this).find("#password").val()) {
			alertMessage += "Passwords do not match\n";
		}
		if ($(this).find("input[name=paymentMethod]").length && $(this).find("input[name=paymentMethod]:checked").length==0) {
			alertMessage += "You must select a Payment Type\n";
		}
		if ($(this).find("input[name=postageMethod]").length && $(this).find("input[name=postageMethod]:checked").length==0) {
			alertMessage += "You must select a Postage Method\n";
		}
		if ($(this).find(".iAgree").length != $(this).find(".iAgree:checked").length) {
			alertMessage += "You must agree to our Terms and Conditions\n";
		}
		if ($(this).find("input[name=paymentMethod]:checked").val()=="Credit Card") {
			if ($("#cardType").val()=="") alertMessage += "Card Type is required\n";
			if ($("#cardNumber").val()=="") alertMessage += "Card Number is required\n";
			if ($("#cardExpiryMonth").val()=="") alertMessage += "Card Expiry Month is required\n";
			if ($("#cardExpiryYear").val()=="") alertMessage += "Card Expiry Year is required\n";
			if ($("#cardName").val()=="") alertMessage += "Name on Card is required\n";
		}
		if (alertMessage.length) {
			alert(alertMessage);
			return false;
		}
		return true;
	});



	$(".ajaxLink").click(function(e) {
		e.preventDefault();
		$.post($(this).attr("href"), function(d) {
			$("#ajaxDialog").html(d);
			$("#ajaxDialog").fadeIn(500);
			attachAjaxDialogEvents();
		});
	});

	// Update cart
	$("#updateCart").click(function(e) {
		e.preventDefault();
		$("#cartForm").submit();
	});
	$(".removeItemFromCart").click(function(e) {
		e.preventDefault();
		$($(this).siblings("input")[0]).val(0);
		$("#cartForm").submit();
	});

	// Order page
	$("#sameAsAbove").click(function() {
		if ($(this).attr("checked")==false) 
		{
			$("input[type=text],select").each(function() {
				if ($(this).attr("rev")) {
					$(this).val("");
				}
			});
		}
		else {
			$("input[type=text],select").each(function() {
				if ($(this).attr("rev")) {
					$(this).val($("input[name="+$(this).attr("rev")+"]").val());
				}
			});
		}
	});

}); 

function attachAjaxDialogEvents() {
	$(".closeDialog").click(function(e) {
		e.preventDefault();
		$("#ajaxDialog").fadeOut(500);
	});
}