var _authenticate = new Authentication();

function Authentication()
{
	this.ID = "";
}

/***********************************Login Methods*************************************/
Authentication.prototype.Login = function(sUsernameFieldID, sPasswordFieldID, sRememberMeField)
{
	//Clear any error messages
	this.ClearErrorMessage();
	this.ClearInfoMessage();
	
	//Validate that the email and password were entered
	var txtUsername = document.getElementById(sUsernameFieldID);
	var txtPassword = document.getElementById(sPasswordFieldID);
	var chkRememberMe = document.getElementById(sRememberMeField);

	if(txtUsername != null && txtPassword != null)
	{
		var sUsername = Trim(txtUsername.value);
		var sPassword = Trim(txtPassword.value);
		if(sUsername == "")
		{
			alert("Please enter a valid username.");
			txtUsername.style.backgroundColor = "#ecb9af";
			txtUsername.focus();
			return;
		}
		else if(sPassword == "")
		{
			alert("Please enter a valid password.");
			txtPassword.style.backgroundColor = "#ecb9af";
			txtPassword.focus();
			return;
		}
		
		//Check the remember me field
		var sRememberMe = "&rememberme=0"
		if(chkRememberMe.checked != null)
			sRememberMe = "&rememberme=1"
		
		this.ShowLoading();
		//Package up the data and create the account.
		var sURL = "private/loginWS.php?action=login&username="+sUsername+"&password="+sPassword + sRememberMe;
		//Post the ajax call
		$.ajax({ 
			   type: "POST", 
			   url: sURL,  
			   success: this.LoginComplete,
			   error:this.LoginError} 
			 );
		
		_authenticate = this;
	}
}

Authentication.prototype.LoginComplete = function(response)
{
	switch(Trim(response))
	{
		case "true":
			_authenticate.HideLoading();
			_authenticate.ShowInfoMessage("Credentials valid. Logging in....");
			window.location = "private/my-support.php";
			break;
		case "The username and password combination was not found.":
			_authenticate.HideLoading();
			_authenticate.ShowErrorMessage(response);
			break;
	}
	_authenticate.HideLoading();
}

Authentication.prototype.LoginError = function(response)
{
	alert("Error logging in. Response was " + response.text);
}

Authentication.prototype.ShowLoading = function()
{
	var divLoading = document.getElementById("divLoading");
	var divLoginButton = document.getElementById("divLoginButton");
	divLoading.style.display = "block";
	divLoginButton.style.display = "none";
}

Authentication.prototype.HideLoading = function()
{
	var divLoading = document.getElementById("divLoading");
	var divLoginButton = document.getElementById("divLoginButton");
	divLoading.style.display = "none";
	divLoginButton.style.display = "block";
}

Authentication.prototype.ShowErrorMessage = function(sMsg)
{
	var parLoginError = document.getElementById("parLoginMessage");
	parLoginError.style.color = "red";
	parLoginError.innerHTML = sMsg;
}

Authentication.prototype.ClearErrorMessage = function(sMsg)
{
	var parLoginError = document.getElementById("parLoginMessage");
	parLoginError.innerHTML = "";
}

Authentication.prototype.ShowInfoMessage = function(sMsg)
{
	var parLoginInfo = document.getElementById("parLoginMessage");
	parLoginInfo.style.color = "green";
	parLoginInfo.innerHTML = sMsg;
}

Authentication.prototype.ClearInfoMessage = function(sMsg)
{
	var parLoginInfo = document.getElementById("parLoginMessage");
	parLoginInfo.innerHTML = "";
}


