function LoginForm()
{
	new Ajax.Request("Controller.jsp", 
		{
			method: "get",
			parameters: "req=LoginForm",
			onComplete: _results
		});
	
	function _results(REQ)
	{
		var loginForm = document.getElementById("OutputForm");
		loginForm.innerHTML = REQ.responseText;
		loginForm.onsubmit = function() {
			Controller("Login",loginForm.Username.value,loginForm.Password.value);
			return false;
		}
	}
}

function Login(UN,PW)
{
	var params="req=Login&username=" + UN + "&password=" + PW;
	new Ajax.Request("Controller.jsp", 
		{
			method: "get",
			parameters: params,
			onComplete: _results
		});
		
		function _results(REQ)
		{
			if (REQ.responseText.indexOf("failed") == -1)
			{
				UsersName = REQ.responseText;
				StartApp();
			}
			else
			{
				document.getElementById("BadLogin").style.display="block";
				document.getElementById("Username").select();
				document.getElementById("Username").className="Highlighted";
				setTimeout("document.getElementById('BadLogin').style.display='none'",3000);
			}
		}
}

var AutoLogoutTimer = null;
function StartAutoLogoutTimer()
{
	var sessionTime = 15;
	clearTimeout(AutoLogoutTimer);
	AutoLogoutTimer = setTimeout("Logout(true)", sessionTime * 1000); //15 seconds
}


function Logout(AUTO)
{
	document.getElementById("OutputForm").innerHTML = "";
	new Ajax.Request("Controller.jsp", 
		{
			method: "get",
			parameters: "req=Logout",
			onComplete: _results
		});
		
	function _results(REQ)
	{
		EndApp();
		if (AUTO)
		{
			alert("You have been logged out due to inactivity.");
		}
	}
}

function CheckLogin()
{
	new Ajax.Request("Controller.jsp", 
		{
			method: "get",
			parameters: "req=LoggedIn",
			onComplete: _results
		});
	
	function _results(REQ)
	{
		if (REQ.responseText.indexOf("failed") == -1)
		{
			UsersName = REQ.responseText;
			StartApp();
		}
		else
		{
			Controller("LoginForm");
		}
	}
}
