//PURPOSE:	Contains JS for the login page

function fnSuccess_Error(strMessage, textStatus)												{
	$('#loginWaiting').hide();
	$('#loginErrorBox').show().text(strMessage);
	$("#INP_username").removeClass("inputError");
	$("#INP_password").removeClass("inputError");
}
function fnError(XMLHttpRequest, textStatus, errorThrown)								{
	strMessage	= "An unknown error has occured - "+textStatus+' - '+errorThrown;
 	fnSuccess_Error(strMessage, textStatus);
}

function frm_login_submit()																							{
//PURPOSE:	Validates input and then calls AJAX send
	$('#loginErrorBox').hide();
	$("#INP_username").removeClass("inputError");
	$("#INP_password").removeClass("inputError");
	
	var strUsername = $("#INP_username").val();
	var strPassword = $("#INP_password").val();
	
	if 	("" == strUsername || "" == strPassword)													{
		$('#loginErrorBox').attr("style", "");
		$('#loginErrorBox').text("Username or Password are missing.  Please complete");
		
		if (	"" == strUsername)
			$("#INP_username").addClass("inputError");
		if (	"" == strPassword)
			$("#INP_password").addClass("inputError");
		return true;
	}
	$('#loginWaiting').show();
	
	$.ajax(	{	url: 			"/index/login"
					,	type: 			'POST'
					,	data: { 		"Username"	: strUsername
					 					, 	"Password"	: hex_md5(strPassword)
										,		"format"		: "json"
									}
					,	success: 	function(data, textStatus)
									{
										if("true" == data.success && data.redirect)				{
											objLoc			= window.location;
											strRedirect	= objLoc.protocol+"//"+objLoc.host+"/"+data.redirect;
											window.location.replace(strRedirect);
										}
										else																							{
											fnSuccess_Error(data.Exception.Message[0], textStatus);
										}
									}
				, error: 		function(XMLHttpRequest, textStatus, errorThrown)
									{
											fnError(XMLHttpRequest, textStatus, errorThrown);
									}
					});

		return false;
}
