$(document).ready(function()
{
	$("#cartOperation").click(function()
	{
		var productSN = $(this).attr("name");
		$.ajax(
		{
			type: "POST",
			url: "/transaction/add_to_cart.jsp",
			data: "productSN="+encodeURIComponent(productSN),
			success: function(cartModuleHTML)
			{
				if($("#shoppingCartModule").length < 1)
				{
					$("#sidebar").prepend("<div class=\"module\" id=\"shoppingCartModule\"><div class=\"modTitle\"><h1>&raquo; Shopping Cart</h1></div><div class=\"modBody\"></div></div>");
				}
				$("#shoppingCartModule .modBody").empty();
				$("#shoppingCartModule .modBody").append(cartModuleHTML);
				$("#shoppingCartModule #clearCart").click(function()
				{
					$.ajax(
					{
						type: "GET",
						url: "/transaction/clear_all.jsp",
						success: function()
						{
							$("#shoppingCartModule").remove();
						}
					});
				});
				$("#shoppingCartModule #checkout").click(function(event)
				{
					event.preventDefault();
					window.location = "https://"+window.location.hostname+"/checkout.jsp";
				});
				$("#shoppingCartModule .removeButton").click(function()
				{
					var productSN = $(this).parent().attr("id").substring(8);
					$.ajax(
					{
						type: "POST",
						url: "/transaction/remove_from_cart.jsp",
						data: "productSN="+encodeURIComponent(productSN),
						success: function(cartModuleHTML)
						{
							if(cartModuleHTML.substring(0,5)!="<ul/>")
							{
								$("#shoppingCartModule .modBody").empty();
								$("#shoppingCartModule .modBody").append(cartModuleHTML);
								$("#shoppingCartModule #clearCart").click(function()
								{
									$.ajax(
									{
										type: "GET",
										url: "/transaction/clear_all.jsp",
										success: function()
										{
											$("#shoppingCartModule").remove();
										}
									});
								});
								$("#shoppingCartModule #checkout").click(function(event)
								{
									event.preventDefault();
									window.location = "https://"+window.location.hostname+"/checkout.jsp";
								});
								$("#shoppingCartModule .removeButton").click(function()
								{
									var productSN = $(this).parent().attr("id").substring(8);
									$.ajax(
									{
										type: "POST",
										url: "/transaction/remove_from_cart.jsp",
										data: "productSN="+encodeURIComponent(productSN),
										success: function(cartModuleHTML)
										{
											if(cartModuleHTML.substring(0,5)!="<ul/>")
											{
												$("#shoppingCartModule .modBody").empty();
												$("#shoppingCartModule .modBody").append(cartModuleHTML);
											}
											else
											{
												$("#shoppingCartModule").remove();
											}
										}
									});
								});
							}
							else
							{
								$("#shoppingCartModule").remove();
							}
						}
					});
				});
			}
		});
	});
	
	$("a.neverAccessed").click(function(event)
	{
		event.preventDefault();
		var originalURI = $(this).attr("href");
		$.ajax(
		{
			type: "GET",
			url: "/license.html",
			success: function(html)
			{
				maskViewport();
				viewportWidth = $("html").innerWidth();
				$("body").append("<div id=\"termsPopup\" style=\"position: fixed; z-index: 10; left: "+((viewportWidth/2)-310)+"px; top: 100px;\"><div id=\"termsWindow\">"+html+"</div></div>");
				$("#termsPopup").append("<input type=\"button\" id=\"declineTerms\" value=\"&laquo; I DO NOT agree to the above terms\" /><input type=\"button\" id=\"acceptTerms\" value=\"I agree to the above terms &raquo;\" />");
				$("#termsPopup #declineTerms").click(function()
				{
					$("#termsPopup").remove();
					unmaskViewport();
				});
				$("#termsPopup #acceptTerms").click(function()
				{
					$("#termsPopup").remove();
					createWaitMessage("Updating our records...");
					$.ajax(
					{
						type: "GET",
						url: "/transaction/accept_license.jsp",
						success: function(response)
						{
							$("a.neverAccessed").unbind("click");
							destroyMessage();
							unmaskViewport();
							window.location.pathname = originalURI;
						}
					});
				});
			}
		});
	});
	$("a.inPageLink").click(function(event)
	{
		event.preventDefault();
		var targetID = $(this).attr("href");
		targetID = targetID.substr(1);
		visibleScrollTo(targetID);
	});
	$("#printThisPage").click(function()
	{
		window.print();
	});
	$("#emailThisPage").click(function(event)
	{
		event.preventDefault();
		maskViewport();
		viewportWidth = $("html").innerWidth();
		$.ajax(
		{
			type: "GET",
			url: "/emailpageform.html",
			success: function(html)
			{
				$("body").append("<div id=\"emailPageOptions\" style=\"position: fixed; z-index: 10; left: "+((viewportWidth/2)-310)+"px; top: 100px;\">"+html+"</div>");
				$("#emailPageOptions").append("<input type=\"button\" id=\"cancelSend\" value=\"Cancel\" /><input type=\"button\" id=\"sendMail\" value=\"Send\" />");
				$("#emailAddresses").focus();
		
				$("#emailAddresses").blur(function()
				{
					var filter=/^(?:((?:[a-zA-Z0-9_\.\-])+\@(?:(?:[a-zA-Z0-9\-])+\.)+(?:[a-zA-Z0-9]{2,4}))+(?:[^a-zA-Z0-9])*)+$/;
					if(!filter.test($(this).val())) $(this).addClass("invalidEntry");
					else $(this).removeClass("invalidEntry");
				});
				
				$("#cancelSend").click(function()
				{
					$("#emailPageOptions").remove();
					unmaskViewport();
				});
				$("#sendMail").click(function()
				{
					if($("#emailAddresses").hasClass("invalidEntry")) $("#emailAddresses").focus();
					else
					{
						var emailString = $("#emailAddresses").val();
						var message = $("#emailMessage").val();
						$("#emailPageOptions").remove();
						createWaitMessage("Sending e-mail...");
						var pattern=/((?:[a-zA-Z0-9_\.\-])+\@(?:(?:[a-zA-Z0-9\-])+\.)+(?:[a-zA-Z0-9]{2,4}))/g;
						$.ajax(
						{
							type: "POST",
							url: "/mailpage.jsp",
							data: "title="+encodeURIComponent(document.title)+"&message="+message+"&mailto="+encodeURIComponent(emailString.match(pattern)),
							success: function()
							{
								createMessage("Your message has been sent.<br /><br />");
								$("#messageWindow").append("<input type=\"button\" id=\"closeButton\" value=\"Close\" />");
								$("#closeButton").click(function()
								{
									destroyMessage();
									unmaskViewport();
								});
							}
						});
					}
				});
			}
		});
	});
});