var User =
{
	focusLoginField : function (refField)
	{
		if ($("#" + refField).val () == $("input[type='hidden'][rel='" + refField + "']").val ())
		{
			$("#" + refField).val ("");
		}
	},

	blurLoginField : function (refField)
	{
		if ($("#" + refField).val () == "")
		{
			$("#" + refField).val ($("input[type='hidden'][rel='" + refField + "']").val ());
		}
	},

	login : function ()
	{
		var email = $("#txtUsername").val ();
		var password = $("#txtPassword").val ();
		var remember = $("#chkbRemember").is (":checked");
		$.ajax
		(
			{
				type : "POST",
				url : mainUrl + "mods/user/receiver.php",
				data : "req=login&email=" + encodeURIComponent (email) + "&password=" + encodeURIComponent (password) + "&remember=" + remember,
				dataType : "json",
				success : function (response)
				{
					if (response.existing == 1)
					{
						if (response.cookie != "")
						{
							General.createCookie ("si", response.cookie, 180);
						}
						document.location = mainUrl + currLang + "/all-promotions";
					}
					else
					{
						$("#logininfo").text ("The credentials that you specified are not valid");
					}
				}
			}
		);
	},

	register : function ()
	{
		var firstname = $("#txtFirstName").val ();
		var lastname = $("#txtLastName").val ();
		var email = $("#txtEmail").val ();
		var password1 = $("#txtPassword1").val ();
		var password2 = $("#txtPassword1").val ();
		var country = $("#slctCountry").val ();
		var city = $("#txtCity").val ();
		var newsletter = $("#chkbNewsletter").is (":checked");
		if (email.match (/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i) == null)
		{
			$("#signupinfo").text ("Please enter a valid email address");
			return;
		}
		if (password1.length < 6 || password1 != password2)
		{
			$("#signupinfo").text ("Please choose a password at least 6 characters long and retype it correctly");
			return;
		}

		$.ajax
		(
			{
				type : "POST",
				url : mainUrl + "mods/user/receiver.php",
				data : "req=signup&firstname=" + encodeURIComponent (firstname) + "&lastname=" + encodeURIComponent (lastname) + "&email=" + encodeURIComponent (email) + "&password=" + encodeURIComponent (password1) + "&country=" + encodeURIComponent (country) + "&city=" + encodeURIComponent (city) + "&newsletter=" + newsletter,
				dataType : "json",
				success : function (response)
				{
					if (response == 1)
					{
						//document.location = mainUrl + currLang + "/my-account";
						$("#signupinfo").text ("Please click on the link in the confirmation email that was sent to you.");
					}
					else
					{
						$("#signupinfo").text (response.error);
					}
				}
			}
		);
	},

	remoteLogin : function (refRemote)
	{
		childWindow = window.open(mainUrl + 'mods/user/remote.php?remote=' + refRemote, 'remoteLogin', 'width=800,height=500,location=0')
		if (childWindow.opener == null)
		{
			childWindow.opener = self;
		}
	},

	logout : function ()
	{
		$.ajax
		(
			{
				type : "POST",
				url : mainUrl + "mods/user/receiver.php",
				data : "req=logout",
				success : function (response)
				{
					General.eraseCookie ("si");
					document.location = mainUrl;
				}
			}
		);
	},

	getUnreadMsg : function ()
	{
		$.ajax
		(
			{
				type : "POST",
				url : mainUrl + "mods/message/receiver.php",
				data : "req=unreadcnt",
				success : function (response)
				{
					$("#hrefMsgCnt").text (response);
					if (response > 0)
					{
						$("#hrefMsgCnt").css ({"background" : "#a00"});
					}
				}
			}
		);
	},

	accountPanel : function (refPanel)
	{
		$("#divAccountPanel div.subpanel").hide ();
		$("#divAccountPanel div[rel='panel_" + refPanel + "']").show ();
		$("#ulAccountPanel li").removeClass ("selected");
		$("#ulAccountPanel li[rel='panel_" + refPanel + "']").addClass ("selected");
	},

	changeInfo1 : function (refElement)
	{
		$("#divAccountPanel li[rel='" + refElement + "'] span").hide ();
		$("#divAccountPanel li[rel='" + refElement + "'] input").show ();
		$("#divAccountPanel li[rel='" + refElement + "'] a").text ("save");
		$("#divAccountPanel li[rel='" + refElement + "'] a").attr ("href", "javascript:User.saveInfo1 ('" + refElement + "');");
	},

	saveInfo1 : function (refElement)
	{
		$("#divAccountPanel li[rel='" + refElement + "'] input").hide ();
		$("#divAccountPanel li[rel='" + refElement + "'] span").show ();
		$("#divAccountPanel li[rel='" + refElement + "'] span").text ($("#divAccountPanel li[rel='" + refElement + "'] input").val ());
		$("#divAccountPanel li[rel='" + refElement + "'] a").text ("change");
		$("#divAccountPanel li[rel='" + refElement + "'] a").attr ("href", "javascript:User.changeInfo1 ('" + refElement + "');");

		var userData = "&ref_1=" + refElement + "&val_1=" + encodeURIComponent ($("#divAccountPanel li[rel='" + refElement + "'] input").val ());
		$.ajax
		(
			{
				type : "POST",
				url : mainUrl + "mods/user/receiver.php",
				data : "req=updateuser" + userData,
				success : function (response)
				{
					alert ("Your details were successfully updated!");
				}
			}
		);
	},

	changeEmail : function ()
	{
		if (confirm ("Please note: changing your email will change your login credentials. Are you sure you want to proceed?") === true)
		{
			$("span[rel='email']").hide ();
			$("input[rel='email']").show ();
			$("a[rel='email']").text ("save");
			$("a[rel='email']").attr ("href", "javascript:User.saveEmail ();");
		}
	},

	saveEmail : function ()
	{
		$("input[rel='email']").hide ();
		$("span[rel='email']").show ();
		$("span[rel='email']").text ($("input[rel='email']").val ());
		$("a[rel='email']").text ("change");
		$("a[rel='email']").attr ("href", "javascript:User.changeEmail ();");

		var userData = "&ref_1=email&val_1=" + encodeURIComponent ($("input[rel='email']").val ());
		$.ajax
		(
			{
				type : "POST",
				url : mainUrl + "mods/user/receiver.php",
				data : "req=updateuser" + userData,
				success : function (response)
				{
					alert ("Your details were successfully updated!");
				}
			}
		);
	},

	changePassword : function ()
	{
		if (confirm ("Please note: changing your password will change your login credentials. Are you sure you want to proceed?") === true)
		{

		}
	},

	updateAccount : function (refPanel)
	{
		var userData = "";
		var i = 0;
		$("div[rel='panel_" + refPanel + "'] input[type='text'], div[rel='panel_" + refPanel + "'] textarea, div[rel='panel_" + refPanel + "'] select").each (function ()
		{
			if ( $(this).attr ("id") != null)
			{
				i++;
				userData += "&ref_" + i + "=" + $(this).attr ("id") + "&val_" + i + "=" + encodeURIComponent ($(this).val ());
			}
		});
		$("div[rel='panel_" + refPanel + "'] input[type='checkbox']").each (function ()
		{
			if ( $(this).attr ("id") != null)
			{
				i++;
				userData += "&ref_" + i + "=" + $(this).attr ("id") + "&val_" + i + "=" + ($(this).is (":checked") === true ? 1 : 0);
			}
		});
		$.ajax
		(
			{
				type : "POST",
				url : mainUrl + "mods/user/receiver.php",
				data : "req=updateuser" + userData,
				success : function (response)
				{
					alert ("Your details were successfully updated!");
				}
			}
		);
	}
}
