// JavaScript Document


	
	$(document).ready ( function ()
	{
	
		$('#formAccountDetails').validate({
			submitHandler: function(form) {				
			
			
				var data = $("#formAccountDetails").serialize();
//		alert(data);

	
				$.ajax( {
					url : "iwa/login/ajax/update-details.php",
					data : data,
					async : false,
					type: 'POST',
					success : function (response) {
		//				alert ("L" + response + "L");
						responseSplit = response.split("@@");
						
						if (responseSplit[0] == "OK")
						{
							alert(responseSplit[1]);
							window.location.href="?section=home";
						}
						else
						{
							alert("ERROR\n\n" + responseSplit[1]);
							$("#firstname").focus();
							
						}
						
					},
						error: function(data)
						{
							alert("Error: " + data.status);
						}
					
				 });
		
				return false;
			
			}					
		});


		$('#formAccountCommunications').validate({
			submitHandler: function(form) {				
			
			var data = $("#formAccountCommunications").serialize();
	
		
			$.ajax( {
				url : "iwa/login/ajax/update-communication-prefs.php",
				data : data,
				async : false,
				type: 'POST',
				success : function (response) {
	//				alert ("L" + response + "L");
					responseSplit = response.split("@@");
					
					if (responseSplit[0] == "OK")
					{
						alert(responseSplit[1]);
						window.location.href="?section=home";
					}
					else
					{
						alert("ERROR\n\n" + responseSplit[1]);
						$("#firstname").focus();
						
					}
					
				},
					error: function(data)
					{
						alert("Error: " + data.status);
					}
				
			 });
	
	
						
			
		
				return false;
			
			}					
		});


		$('#formAccountPassword').validate({
			submitHandler: function(form) {				
			

			if ($(".myaccount #passwordNew").val() != $(".myaccount #passwordNewConfirm").val())
				{
					alert("ERROR\nYour new password do not match.");	
					return;
				}
		
				var data = "oldPassword=" + $(".myaccount #passwordCurrent").val() + "&newPassword=" + $(".myaccount #passwordNew").val();
			
				$.ajax( {
					url : "iwa/login/ajax/change-password.php",
					data : data,
					async : false,
					type: 'POST',
					success : function (response) {
						responseSplit = response.split("@@");
						
						if (responseSplit[0] == "ERROR")
						{
							alert("ERROR\n\n" + responseSplit[1]);
							$("#passwordCurrent").focus();
						}
						else
						{
							alert(responseSplit[1]);
							window.location.href="?section=home";
							
						}
						
					},
						error: function(data)
						{
							alert("Error: " + data.status);
						}
					
				 });
	
						
			
		
				return false;
			
			}					
		});






	$(".account_panel_home_menu").click ( function () {
		window.location.href = $(this).find("a").attr("href");
	});
		

	$(".btnMyAccount").live( "click","",  function () {
		window.location.href="http://www.airportcars-uk.com/account.php";
	});





	
	//Expand lost password panel
	$(".lost-pwd").click( function () {
		$("#panelRegister").fadeOut("fast", "", function () {
			$("#panelResetPassword").fadeIn("fast");
		});
		
//		$(".panelResetPassword").slideDown("fast");
		
	});

	$(".lost-pwd-cancel").click( function () {
		$("#panelResetPassword").fadeOut("fast", "", function () {
			$("#panelRegister").fadeIn("fast");
		});
		
	});
	
	
	// Expand Panel
	$("#loginSummary_hello a, .loginShortcut").click(function(){

		$(".pre_header").css("visibility", "hidden");
		$("div#panel").slideDown("slow");

		$("#toggle a").css("display", "none");
		
		
		$("#close").css("display","block");
	
	});	

	$("#logout, .btnLogout").click(function(){
		$(".pre_header").css("visibility", "hidden");
		$("div#panel").slideDown("slow");
		$("#toggle a").css("display", "none");
		
		$("#close").css("display","block");

		//Clear any my account content in the main page
		$(".myaccount").html("<h2>You are now logged out.</h2>");
		

		$.ajax( {
			url : "iwa/login/ajax/logout.php",
			success : function (response) {
//				alert( response);

				$("#loginSummary_hello").html("Hello Guest" );

				$("#logout").removeClass("relevant" );
				$("#open").addClass("relevant" );

				$("#loggedOff").css("display", "block");
				$("#loggedOn").css("display", "none");

				$("#loggedOnDetail_h1").html("");
				$("#loggedOnDetail_body").html("");
				
				if (refreshOnLoginStatusChange)
				{
					window.location.href = window.location.href.replace("#", "");
					return false;
				}
				
			}
		 });
		
//		$("div#panel").slideDown("slow");
	//	$("#toggle a").toggle();
	
	});	
		
		
		$("#userLogin").submit( function () {
			$.ajax({
				async : false,
				type: 'POST',
				url: "iwa/login/ajax/login_process.php",
				data : $(this).serialize(),
				success : function (response)
				{
					//alert(response);

					$("#username").val("");
					$("#password").val("");
					
					responseSplit = response.split("@@");
					
					if (responseSplit[0] == "OK")
					{
//						alert("ok");
						$("#loginSummary_hello").html("Hello " + responseSplit[1]);
					//	$("#open").html("Logout");
						
						$("#loggedOnDetail_h1").html(responseSplit[1] + "'s Airport Cars UK");
						$("#loggedOnDetail_body").html(responseSplit[2]);
						
						$("#loggedOff").css("display", "none");
						$("#loggedOn").css("display", "block");

						$("#open").removeClass("relevant");
//						$("#open").css("display", "none");
						
						$("#logout").addClass("relevant");
	//					$("#logout").css("display", "block");


						if (refreshOnLoginStatusChange)
						{
//						alert(window.location.href.replace("#", "") );
							window.location.href = window.location.href.replace("#", "");
//							alert("done");
							return false;
						}
						
						
					}

					else 
					{
						$("#username").focus();
						
						alert("Sorry, we could not log you in:\n\n" + responseSplit[1]);
						
					}
					

					
				},
				error: function(data)
				{
					alert("Error: " + data.status);
				}
			});
			
			return false;			
			
		});

		$("#register").submit( function () {

			$.ajax({
				async : false,
				type: 'POST',
				url: "iwa/login/ajax/register.php",
				data : $(this).serialize(),
				success : function (response)
				{
				//	alert(response);
					
					responseSplit = response.split("@@");
					
					if (responseSplit[0] == "OK")
					{
						alert(responseSplit[1]);
					}
					else
					{
						alert("ERROR\n" + responseSplit[1]);
					}
					
					$("#username").focus();
					
				//	$("#userLogin").html(response);
					
				},
				error: function(data)
				{
					alert("Error: " + data.status);
				}
			});
			
			$(this).children("#email").val("");
			$(this).children("#firstname").val("");
			$(this).children("#lastname").val("");
			
			return false;			
			
		});



		$("#resetPassword").submit( function () {
			
			$.ajax({
				async : false,
				type: 'POST',
				url: "iwa/login/ajax/reset_password.php",
				data : $(this).serialize(),
				success : function (response)
				{
					$("#panelResetPassword").fadeOut("fast", "", function () {
						$("#panelRegister").fadeIn("fast");
					});

					alert("Thank you. We have emailed you a new password if the entered email address was recognised.");
				},
				error: function(data)
				{
					alert("Error: " + data.status);
				}
			});
			
			return false;			
			
		});


		
	});
	
