var blnAllowPostBack = false;

function validate()
{
	try
	{
		var txtEmail = document.getElementById("txtEmail");
		var txtPassword = document.getElementById("txtPassword");

		if (txtEmail.value == "")
		{
			alert("Please enter your email address");
			txtEmail.focus();
			return false;
		}

		if (txtPassword.value == "")
		{
			alert("Please enter your password");
			txtPassword.focus();
			return false;
		}

		if (validateEmail(txtEmail.value) == false)
		{
			alert("The email address entered is invalid");
			txtEmail.focus();
			return false;
		}

		return true;
	}
	catch (exc)
	{
		showError(exc);
		return false;
	}
}

function validateEmail(strEmail)
{
	try
	{
		//there must be an @
		if (strEmail.indexOf("@") == -1)
		{
			return false;
		}

		//there must be a .
		if (strEmail.indexOf(".") == -1)
		{
			return false;
		}

		//there must be a . after the @
		if (strEmail.indexOf(".", strEmail.indexOf("@")) == -1)
		{
			return false;
		}

		//the . cannot be the last character
		if (strEmail.indexOf(".") == strEmail.length - 1)
		{
			return false;
		}

		//the . cannot be the first character
		if (strEmail.indexOf(".") == 0)
		{
			return false;
		}

		//the @ cannot be the first character
		if (strEmail.indexOf("@") == 0)
		{
			return false;
		}
		return true;
	}
	catch (exc)
	{
		showError(exc);
		return false;
	}
}

function login(btnLogin)
{
	try
	{
		var txtEmail = document.getElementById("txtEmail");
		var txtPassword = document.getElementById("txtPassword");
		var encEmail = document.getElementById("encEmail");
		var encPassword = document.getElementById("encPassword");
		var btnForgotPassword = document.getElementById(strRemindButton);
		var btnRegister = document.getElementById(strRegisterButton);

		if (btnLogin.innerHTML == " Please Wait ... ") 
		{
			alert("Please wait, your request is being processed!");
			return false;
		}

		if (!validate())
		{
			return false;
		}

		btnLogin.disabled = true;
		txtEmail.disabled = true;
		txtPassword.disabled = true;
		if (btnForgotPassword) {btnForgotPassword.style.display = "none";}
		if (btnRegister) {btnRegister.style.display = "none";}

		encEmail.value = hex_md5(txtEmail.value.toLowerCase());
		encPassword.value = hex_md5(txtPassword.value);

		txtEmail.value = "";
		txtPassword.value = "";

		btnLogin.innerHTML = " Please Wait ... ";

		postBack("login")
		return true;
	}
	catch (exc)
	{
		showError(exc);
		return false;
	}
}

function forgotPassword(btnForgotPassword)
{
	try
	{
		if (btnForgotPassword.innerHTML == strSendPasswordReminder)
		{
			return sendPassword(btnForgotPassword);
		}

		var btnLogin = document.getElementById(strLoginButton);
		var passwordRow = document.getElementById("passwordRow");
		var txtEmail = document.getElementById("txtEmail");
		var btnRegister = document.getElementById(strRegisterButton);
		var btnCancel = document.getElementById("btnCancel");

		btnLogin.style.display = "none";
		passwordRow.style.display = "none";
		if (btnRegister) {btnRegister.style.display = "none";}
		btnCancel.style.display = "";

		btnForgotPassword.innerHTML = strSendPasswordReminder;

		txtEmail.focus();
		return false;
	}
	catch (exc)
	{
		showError(exc);
		return false;
	}
}

function sendPassword(btnForgotPassword)
{
	try
	{
		var txtEmail = document.getElementById("txtEmail");
		var btnLogin = document.getElementById(strLoginButton);
		var encEmail = document.getElementById("encEmail");
		var encPassword = document.getElementById("encPassword");
		var txtPassword = document.getElementById("txtPassword");

		if (btnForgotPassword.innerHTML == " Please Wait ... ") 
		{
			alert("Please wait, your request is being processed!");
			return false;
		}

		if (txtEmail.value == "")
		{
			alert("Please enter your email address");
			txtEmail.focus();
			return false;
		}

		if (validateEmail(txtEmail.value) == false)
		{
			alert("The email address entered is invalid");
			txtEmail.focus();
			return false;
		}

		encEmail.value = hex_md5(txtEmail.value.toLowerCase());
		encPassword.value = "";

		txtEmail.value = "";
		txtPassword.value = "";


		btnLogin.disabled = true;
		txtEmail.disabled = true;
		txtPassword.disabled = true;
		btnForgotPassword.disabled = true;
		btnForgotPassword.innerHTML = " Please Wait ... ";

		postBack("remind");
		return true;
	}
	catch (exc)
	{
		showError(exc);
		return false;
	}
}

function register(btnRegister)
{
	try
	{
		if (btnRegister.innerHTML == strSendRegister)
		{
			return registerMe(btnRegister);
		}

		var btnLogin = document.getElementById(strLoginButton);
		var passwordRow = document.getElementById("passwordRow");
		var btnForgotPassword = document.getElementById(strRemindButton);
		var txtEmail = document.getElementById("txtEmail");
		var btnCancel = document.getElementById("btnCancel");

		btnLogin.style.display = "none";
		btnForgotPassword.style.display = "none";
		passwordRow.style.display = "none";
		btnCancel.style.display = "";

		btnRegister.innerHTML = strSendRegister;

		txtEmail.focus();
		return false;
	}
	catch (exc)
	{
		showError(exc);
		return false;
	}
}

function registerMe(btnRegister)
{
	try
	{
		var txtEmail = document.getElementById("txtEmail");
		var btnLogin = document.getElementById(strLoginButton);
		var encEmail = document.getElementById("encEmail");
		var encPassword = document.getElementById("encPassword");
		var txtPassword = document.getElementById("txtPassword");
		var btnForgotPassword = document.getElementById(strRemindButton);

		if (btnRegister.innerHTML == " Please Wait ... ") 
		{
			alert("Please wait, your request is being processed!");
			return false;
		}

		if (txtEmail.value == "")
		{
			alert("Please enter your email address");
			txtEmail.focus();
			return false;
		}

		if (validateEmail(txtEmail.value) == false)
		{
			alert("The email address entered is invalid");
			txtEmail.focus();
			return false;
		}

		encEmail.value = txtEmail.value;
		encPassword.value = "";

		txtEmail.value = "";
		txtPassword.value = "";

		btnLogin.disabled = true;
		txtEmail.disabled = true;
		txtPassword.disabled = true;
		btnForgotPassword.disabled = true;
		btnRegister.innerHTML = " Please Wait ... ";
		btnRegister.disabled = true;

		postBack("register");
		return true;
	}
	catch (exc)
	{
		showError(exc);
		return false;
	}
}

function resetForm()
{
	try
	{
		var txtEmail = document.getElementById("txtEmail");
		var txtPassword = document.getElementById("txtPassword");
		var encEmail = document.getElementById("encEmail");
		var encPassword = document.getElementById("encPassword");
		var btnLogin = document.getElementById(strLoginButton);
		var btnForgotPassword = document.getElementById(strRemindButton);
		var passwordRow = document.getElementById("passwordRow");
		var btnRegister = document.getElementById(strRegisterButton);
		var btnCancel = document.getElementById("btnCancel");

		btnLogin.disabled = false;
		btnLogin.innerHTML = "Login";
		btnLogin.style.display = "";

		btnCancel.style.display = "none";

		txtEmail.disabled = false;
		txtPassword.disabled = false;

		if (btnForgotPassword) 
		{
			btnForgotPassword.disabled = false;
			btnForgotPassword.innerHTML = strPasswordReminder;
			btnForgotPassword.style.display = "";
		}

		if (btnRegister) 
		{
			btnRegister.innerHTML = strRegister;
			btnRegister.style.display = "";
		}

		if (passwordRow){passwordRow.style.display = "";}

		//txtEmail.value = "";
		txtPassword.value = "";
		encEmail.value = "";
		encPassword.value = "";

		txtEmail.focus();
		return false;
	}
	catch (exc)
	{
		showError(exc);
		return false;
	}
}

function postBack(strMode)
{
	try
	{
		var txtMode = document.getElementById("txtMode");
		txtMode.value = strMode;

		blnAllowPostBack = true;

		return true;
	}
	catch (exc)
	{
		showError(exc);
		return false;
	}
}

function confirmPostBack()
{
	try
	{
		var txtMode = document.getElementById("txtMode");
		var encEmail = document.getElementById("encEmail");
		var encPassword = document.getElementById("encPassword");

		if (txtMode.value == ""){ return false; }
		if (encEmail.value == ""){ return false; }
		if (encPassword.value == ""){ return false; }

		return blnAllowPostBack;
	}
	catch (exc)
	{
		showError(exc);
		return false;
	}
}

function showError(exc)
{
	alert("Sorry an error has occurred;\n" + exc);
	return false;
}
