//  Developed by Roshan Bhattarai 
//  Visit http://roshanbh.com.np for this script and more.
//  This notice MUST stay intact for legal use

$(document).ready(function() {

	//Get the page name of the current page
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);

	// Expand Panel
	$("#open").click(function() {
		$("div#panel").slideDown("slow");
	});
	// Collapse Panel
	$("#close").click(function() {
		$("div#panel").slideUp("slow");
	});
	// Switch buttons from "Log In | Register" to "Close Panel" on click
	$("#toggle a").click(function() {
		$("#toggle a").toggle();
	});

	$("#login_form").submit(function() {

		$("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
		$.post("/ajax/login/", { emailaddress: $('#emailaddress').val(), password: $('#password').val(), securitycode: $('#security_login').val(), remember: $('#rememberlogin').val(), secvars: $('#hidSecVarsLogin').val(), rand: Math.random() }, function(data) {
			if (data == 'yes') {
				$("#msgbox").fadeTo(200, 0.1, function() {
					$(this).html('Logging in.....').addClass('messageboxok').fadeTo(900, 1,
						function() {
							//perform the next action now that logged in
							if ($.cookie('nextaction') != "" && $.cookie('nextaction') != null) {
								var url = $.cookie('nextaction');
								$.cookie('nextaction', '');
								window.location = url;
							} else if (requestMethod == "POST" || sPage == "/ajax/login/") {
								window.location = "/account/home/";
							} else {
								window.location.reload(false);
							}
						}
					);
				});
			}
			else if (data == 'sec') {
				$("#msgbox").fadeTo(200, 0.1, function() //start fading the messagebox
				{
					$(this).html('Security code entered is incorrect.<br>[<a style=\"font-size:8pt;color:blue;font-weight:normal\" href=\"javascript:DisplayCaptchaHelp()\">help me</a>] [<a style=\"font-size:8pt;color:blue;font-weight:normal\" href=\"javascript:refreshPage()\">refresh page</a>]').addClass('messageboxerror').fadeTo(900, 1);
				});
			}
			else if (data == 'pno') {
				$("#msgbox").fadeTo(200, 0.1, function() //start fading the messagebox
				{
					$(this).html('Password does not match email address.').addClass('messageboxerror').fadeTo(900, 1);
				});
			}
			else {
				$("#msgbox").fadeTo(200, 0.1, function() //start fading the messagebox
				{
					$(this).html('Invalid Login Info.').addClass('messageboxerror').fadeTo(900, 1);
				});
			}

		});
		return false; //not to post the  form physically
	});

	$("#signup_form").submit(function() {
		//remove all the class add the messagebox classes and start initial fading
		$("#msgbox").removeClass().addClass('messagebox').text('Validating Email....').fadeIn(1000);
		$.post("/ajax/signup/", { emailaddress: $('#emailaddress_signup').val(), securitycode: $('#security_signup').val(), closestcity: $('#closest_city').val(), secvars: $('#hidSecVarsSignup').val(), rand: Math.random() }, function(data) {
			var sData = new String(data);
			if (data == 'yes') //if correct login detail
			{
				$("#msgbox").fadeTo(200, 0.1, function() {
					$(this).html('Thank You For Signing Up. A password<br>has just been e-mailed to you so you can log in.').addClass('messageboxok').fadeTo(900, 1);
				});
			}
			else if (data == 'bno') {
				$("#msgbox").fadeTo(200, 0.1, function() {
					$(this).html('The email address entered is invalid.').addClass('messageboxerror').fadeTo(900, 1);
					$("#emailaddress_signup").focus();
				});
			}
			else if (data == 'ncs') {
				$("#msgbox").fadeTo(200, 0.1, function() {
					$(this).html('Please select the closest city to you.').addClass('messageboxerror').fadeTo(900, 1);
					$("#closest_city").focus();
				});
			}
			else if (sData.substring(0, 3) == 'sno') {
				$("#msgbox").fadeTo(200, 0.1, function() {
					if (data == 'snob') {
						//$(this).html('Please enter the security code. [<a style=\"font-size:8pt;color:blue;font-weight:normal\" href=\"javascript:DisplayCaptchaHelp()\">help me</a>]').addClass('messageboxerror').fadeTo(900, 1);
						$(this).html('Please enter the security code.<br>[<a style=\"font-size:8pt;color:blue;font-weight:normal\" href=\"javascript:DisplayCaptchaHelp()\">help me</a>] [<a style=\"font-size:8pt;color:blue;font-weight:normal\" href=\"javascript:refreshPage()\">refresh page</a>]').addClass('messageboxerror').fadeTo(900, 1);
					} else {
						//$(this).html('The security code entered is incorrect.').addClass('messageboxerror').fadeTo(900, 1);
						$(this).html('Security code entered is incorrect.<br>[<a style=\"font-size:8pt;color:blue;font-weight:normal\" href=\"javascript:DisplayCaptchaHelp()\">help me</a>] [<a style=\"font-size:8pt;color:blue;font-weight:normal\" href=\"javascript:refreshPage()\">refresh page</a>]').addClass('messageboxerror').fadeTo(900, 1);
					}
					$("#security_signup").focus();
				});
			}
			else {
				$("#msgbox").fadeTo(200, 0.1, function() {
					$(this).html('The email address entered already exists.<br><a style=\"color:blue;font-weight:normal\" href=\"/account/lostpassword/\">Remind Me of My Password &raquo;</a>').addClass('messageboxerror').fadeTo(900, 1);
				});
			}

		});
		return false;
	});

	$("#logout_form").submit(function() {
		$("#msgbox").removeClass().addClass('messageboxok').text('Logging Out. See You Again Soon!').fadeIn(1000);
		var novars = null;
		$.post("/ajax/logout/", { emailaddress: $('#emailaddress').val(), password: $('#password').val(), rand: Math.random() }, function(data) {
			$("#msgbox").fadeTo(200, 0.1, function() {
				window.location = "/home/";
			});
		});
		return false;
	});

	$("#lost_pwd_form").submit(function() {

		//remove all the class add the messagebox classes and start initial fading
		$("#pwdmsg").removeClass().addClass('genmsgbox').text('Processing Request....').fadeIn(1000);
		$.post("/ajax/lostpassword/", { emailaddress: $('#emailaddress_lostpwd').val(), securitycode: $('#security_lostpwd').val(), rand: Math.random() }, function(data) {
			var sData = new String(data);
			if (data == 'yes') //if correct login detail
			{
				$("#pwdmsg").fadeTo(200, 0.1, function() {
					document.getElementById("lostpwd").style.display = 'none';
					$(this).html('Your password has been e-mailed to you.').addClass('genmsgboxok').fadeTo(900, 1);
				});
			}
			else if (data == 'sec') //if correct login detail
			{
				$("#pwdmsg").fadeTo(200, 0.1, function() {
					$(this).html('The security code entered is incorrect.').addClass('genmsgboxerror').fadeTo(900, 1);
				});
			}
			else {
				$("#pwdmsg").fadeTo(200, 0.1, function() {
					$(this).html('The email address entered could not be found.  Please be sure you typed it in correctly.').addClass('genmsgboxerror').fadeTo(900, 1);
				});
			}

		});
		return false;
	});

});
