function SubmitLoginForm(submitForm)
{
	var password = document.login_form.password.value;
	var login = Trim(document.login_form.login.value);
	
	if (login.length < MIN_LOGIN_LENGTH)
	{
		alert("Your login name is too small.");
		return false;
	}

	if (password.length < MIN_PASSWORD_LENGTH)
	{
		alert("Your password is too small.");
		return false;
	}
	
	if (submitForm)
	{
		document.login_form.submit();
	}
	return true;
}

function RetrievePassword()
{
	var login = Trim(GetElementValue("retrievePasswordLogin"));
	var privateKey = Trim(GetElementValue("private_key"));

	var isReturn = true;
	if (!login || login.length < MIN_LOGIN_LENGTH)
	{
		alert("The size of your name must be greater than " + MIN_LOGIN_LENGTH);
	}
	else if (IsEmpty(privateKey))
	{
		alert("Please enter private key");
	}
	else
	{
		isReturn = false;
	}
	
	if (isReturn)
	{
		return false;
	}
	
	var publicKey = window.capt.document.captcha.public_key.value;
	
	var parameterNames = 
	[
		"login",
		"private_key",
		"public_key"
	];
	var parameterValues = 
	[
		login,
		privateKey,
		publicKey
	];
	var queryString = UriSerialize(parameterNames, parameterValues);
	
	DisableButton("btnSendEmail");
	
	var retrievePasswordScript = "/retrieve_password.php";
	ajaxpage(retrievePasswordScript, OnSendEmail, queryString);
}

function OnSendEmail(result)
{
	result = parseInt(result);
	switch(result)
	{
		case ERR_IMAGE_CHECK_PASS:
			alert("Please retype characters on image!");
			break;
		case ERR_LOGIN_INCORRECT:
			alert("You've entered incorrect login.");
			break;
		case ERR_UNKNOWN_USER:
			alert("The specified username does not exists.");
			break;
		case ERR_SEND_EMAIL:
			alert("Can't send email!");
			break;
		case ERR_UNCONFIRMED_EMAIL:
			alert("Can't send email to unconfirmed email!");
			break;
		case S_OK:
			alert("Instructions for resetting your password have been emailed to you.");
			break;
	}
	EnableButton("btnSendEmail");
}

function SwitchForms()
{
	SwitchVisibility("loginFormContainer");
	SwitchVisibility("retrievePasswordFormContainer");
	var formTitle = GetElement("formTitle");
	if (GetElement("loginFormContainer").style.display == "")
	{
		formTitle.innerHTML = "Sign In";
		SetFirstFieldFocus();
	}
	else
	{
		formTitle.innerHTML = "Forgot Password";
		SetFirstFieldFocus();
	}
}

$(document).ready(
	function ()
	{
		$("#login").setLimit(MAX_LOGIN_LENGTH).setCounter(COUNTER_LABEL);
	}
);