		$(function() {
			/**
			 * Init
			 **/

			$(".lavaLampWithImage").lavaLamp({
				fx: "backout",
				speed: 700,
				click: function(event, menuItem) {
				}
			});

			var openDays = {
				1: [], // Jan
				2: [], // Feb
				3: [], // Mar
				4: [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,24,25], // Apr
				5: [1,2,3,8,9,15,16,22,23,29,30,31], // May
				6: [1,2,3,4,5,6,9,12,13,16,19,20,23,26,27,28,29,30], // Jun
				7: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31], // Jul
				8: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31], // Aug
				9: [1,2,3,4,5,8,11,12,18,19,25,26], // Sep
				10: [23,24,25,26,27,28,29,30,31], // Oct
				11: [], // Nov
				12: [] // Dec
			};

			function isParkOpen(date){
				var currentDate = new Date();
				var currentDayofMonth = currentDate.getDate();
				var currentMonth = currentDate.getMonth() + 1;
				var dayofMonth = date.getDate();
				var month = date.getMonth() + 1;
				var openDaysInCurrentMonth = openDays[month];

				if(month < currentMonth){
					return [0, ''];
				}

				if(month == currentMonth && dayofMonth < currentDayofMonth){
					return [0, ''];
				}

				if($.inArray(dayofMonth, openDaysInCurrentMonth) !== -1){
					return [1, ''];
				} else {
					return [0, ''];
				}
			}

			$("#fsVisitDate").css("display", "none");
			$("#fsDatedTicket").css("display", "none");
			$("#fsOpenTicket").css("display", "none");
			$("#fsSeasonTicket").css("display", "none");

			$("#ticketsForm").accordion({
				animated: 'easeslide',
				autoHeight: false,
				event:false
			});

			$("#tipticket").dialog({
				autoOpen: false,
				bgiframe: true,
				modal: true,
				buttons: {
					Ok: function() {
						$(this).dialog('close');
					}
				}
			});

			$("#rgDOSTicket").j3ssw({off:"statedown-sort", on:"stateup-sort", def:"statedef-sort", mode:1, index: -1});

			$("#visitDate").datepicker({
				minDate: new Date(2010, 0, 1),
				maxDate: new Date(2010, 11, 31),
				firstDay: 1,
				dateFormat: 'dd/mm/yy',
				appendText: ' (dd/mm/yyyy)',
				hideIfNoPrevNext: true,
				constrainInput: true,
				beforeShowDay: isParkOpen,
				onSelect: function(){
					$(".nextButton:eq(0)").attr("disabled", "");
				}
			});

			function showTooltip(el, msg){
				var pos = $("#"+el).position();
				var width = $("#"+el).width();

				$("#fsAddressInfo").append("<div class='tooltip' id='tip"+el+"'>"+msg+"</div>");

				var my_tooltip = $("#tip"+el);

				my_tooltip.css({opacity:0.8, display:"none", position: "absolute", top: pos.top, left: pos.left + width + 20}).fadeIn(400);
			}

			/**
			 * Flow
			 **/

			$(".nextButton").click(function(){
				var nextSectionIndex = $(".nextButton").index(this) + 1;
				var valid = true;

				$(".tooltip").remove();

				$("#fsAddressInfo input").each(function(i){
					if($(this).attr("type") == "text"){
						$("#"+$(this).attr("name")).css("border", "1px solid #ccc");
					}
				});

				switch(nextSectionIndex){
					case 1:
						valid = validateTicketType();
						break;
					case 2:
						valid = validateTickets();
						break;
					case 3:
						valid = validateAddress();
						break;
				}

				if(!valid){
					return false;
				}

				$("#ticketsForm").accordion('activate', nextSectionIndex);
			});

			$(".prevButton").click(function(){
				var prevSectionIndex = $(".prevButton").index(this);

				$(".tooltip").remove();

				$("#fsAddressInfo input").each(function(i){
					if($(this).attr("type") == "text"){
						$("#"+$(this).attr("name")).css("border", "");
					}
				});

				if(prevSectionIndex == 0){
					$("#orderSubTotal span").html("0.00");
					$("#orderTotal span").html("0.00");
					$("#orderSubDiscount span").html("0.00");
					$("#orderDiscount span").html("0.00");
					$("#voucherCode").val("");

					$("#fsTicket input").each(function(i){
						var fieldName = $(this).attr("name");

						$("#"+fieldName).val("");
					});
				}

				$("#ticketsForm").accordion('activate', prevSectionIndex);
			});

			/**
			 * Stage 1
			 **/

			$("#datedTicket").click(function(){
				$("#fsVisitDate").show();
				$("#visitDate").val("");
				$("#fsDatedTicket").show();
				$("#fsOpenTicket").hide();
				$("#fsSeasonTicket").hide();
			});

			$("#openTicket").click(function(){
				$("#fsVisitDate").hide();
				$("#visitDate").val("");
				$("#fsDatedTicket").hide();
				$("#fsOpenTicket").show();
				$("#fsSeasonTicket").hide();
			});

			$("#seasonTicket").click(function(){
				$("#fsVisitDate").hide();
				$("#visitDate").val("");
				$("#fsDatedTicket").hide();
				$("#fsOpenTicket").hide();
				$("#fsSeasonTicket").show();
			});

			function validateTicketType(){
				var dated  = $("#datedTicket:checked").val();
				var open   = $("#openTicket:checked").val();
				var season = $("#seasonTicket:checked").val();

				if(dated == undefined && open == undefined && season == undefined){
					$("#tipticket p").html("Please make a selection");

					$("#tipticket").dialog('open');

					return false;
				}

				if(dated != undefined){
					if($("#visitDate").val() == ""){
						$("#tipticket p").html("Please select a date");

						$("#tipticket").dialog('open');

						return false;
					}

					if(/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/i.test($("#visitDate").val()) === false)
					{
						$("#tipticket p").html("Please insert a date in dd/mm/yyyy format");

						$("#tipticket").dialog('open');

						return false;
					}
				}

				return true;
			}

			/**
			 * Stage 2
			 **/

			function validateTickets(){
				var valid = true;
				var totalTickets = 0;
				var season = $("#seasonTicket:checked").val();

				$("#fsTicket input").each(function(i){
					var fieldName = $(this).attr("name");
					var value = $.trim($(this).val());

					$("#"+fieldName).css("border", "");

					if(isNaN(value)){
						valid = false;

						$("#tipticket p").html("Please enter a valid number");

						$("#tipticket").dialog('open');

						$("#"+fieldName).css("border", "1px solid red");
					} else if(value !== "" && value <= 0) {
						valid = false;

						$("#tipticket p").html("Please enter a number above zero");

						$("#tipticket").dialog('open');

						$("#"+fieldName).css("border", "1px solid red");
					} else {
						totalTickets += value;
					}
				});

				if(season != undefined){
					if($("#ticketSeasonValid").val() == ""){
					    valid = false;

						$("#tipticket p").html("Please select a season");

						$("#tipticket").dialog('open');

						$("#ticketSeasonValid").css("border", "1px solid red");
					}
				}

				if(totalTickets == 0){
					valid = false;

					$("#tipticket p").html("Please enter number of tickets you would like");

					$("#tipticket").dialog('open');
				}

				//if child under 3 make sure at least 1 other ticket is purchased
				if($("#ticket3").val() > 0){
					if($("#ticket1").val() == 0 && $("#ticket2").val() == 0 &&
					   $("#ticket4").val() == 0 && $("#ticket5").val() == 0 &&
					   $("#ticket6").val() == 0){
					    valid = false;

						$("#tipticket p").html("Please select an adult ticket");

						$("#tipticket").dialog('open');

						$("#ticket3").css("border", "1px solid red");
					}
				}

				return valid;
			}

			$("#fsTicket input").keyup(function(){
				if($(this).val() <= 100)
				{
					var postData = $("#ticketsForm").serialize();

					$.post("/tickets/getOrderTotal", postData, function(data){
						if(data.success == true){
							$("#orderSubTotal span").html(data.orderTotal);
							$("#orderTotal span").html(data.orderTotal);
							$("#orderSubDiscount span").html(data.orderDiscount);
							$("#orderDiscount span").html(data.orderDiscount);
						} else {
							$("#tipticket p").html("There was a problem while trying to calculate a price for your order");

							$("#tipticket").dialog('open');
						}
					},"json");
				} else {
					$(this).val(0);

					$("#orderSubTotal span").html("0.00");
					$("#orderTotal span").html("0.00");

					$("#tipticket p").html("Please enter a value less than 100");

					$("#tipticket").dialog('open');

					$(this).css("border", "1px solid red");
				}
			});

			var getOrderTotal = function(){
				var postData = $("#ticketsForm").serialize();

				$.post("/tickets/getOrderTotal", postData, function(data){
					if(data.success == true){
						$("#orderSubDiscount span").html(data.orderDiscount);
						$("#orderDiscount span").html(data.orderDiscount);
						$("#orderSubTotal span").html(data.orderTotal);
						$("#orderTotal span").html(data.orderTotal);
						if(data.voucherFound == true){
							$("#voucherCode").css("border", "1px solid green");
						} else {
							$("#voucherCode").css("border", "1px solid red");
						}
					}
				},"json");				
			}

//			$("#voucherCode").keyup(getOrderTotal);
			$("#voucherCode").blur(getOrderTotal);

			/**
			 * Stage 3
			 **/

			function validateAddress(){
				var valid = true;

				$("#fsAddressInfo input").each(function(i){
					if($(this).attr("type") == "text"){
						var fieldName = $(this).attr("name");
						var fieldValue = $.trim($(this).val());
						var requiredFields = ["customerTitle", "customerFirstName", "customerLastName", "contactEmail", "address1", "addressTown", "addressCounty", "addressPostcode"];
						var msg = "";

						$("#"+fieldName).css("border", "1px solid #ccc");

						if($.inArray(fieldName, requiredFields) !== -1){
							if(fieldValue == ""){
								valid = false;
								msg += "<p>This is a required field</p>";
								$("#"+fieldName).css("border", "1px solid red");
							}

							switch(fieldName){
								case "contactEmail":
									//crazy regex borrowed from http://bassistance.de/jquery-plugins/jquery-plugin-validation/
									if(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(fieldValue) === false){
										valid = false;
										msg += "<p>Please enter a valid email address</p>";
										$("#"+fieldName).css("border", "1px solid red");
									}
									break;
							}
						}

						if(msg != ""){
							showTooltip(fieldName, msg);
						}
					}
				});

				if(valid){
					//processOrder
					var postData = $("#ticketsForm").serialize();

					$.post("/tickets/processOrder", postData, function(data){
						if(data.success == true){
							$("#staleHashId").val(data.freshHash);
							if (data.page)
							{
								$("#confirmOrder").html(data.page);
							}
							
						} 
						// else {
						// 	$("#staleHashId").val(data.freshHash);
						// }
					},"json");
				}

				return valid;
			}

			/**
			 * Stage 4
			 **/

//			$("#ticketsForm").submit(function(){
////				var postData = $("#ticketsForm").serialize();
////
////				$.post("/tickets/processOrder", postData, function(data){
////					if(data.success == true){
////						alert("success");
////					} else {
////						alert("fail");
////					}
////				},"json");
////
////				return false;
//			});
		});