function Logout(e) {
	var url = "./scripts/logout.ajax.php";
	var LoginPanel = $('tertiaryContent');
	var LoginTitle = $('LoginStatus');
	var LoginInfo = $('login');
	var LoginForm = $('form1');
	var LoginStatus = $('message');
	
	/**
	*	Intermitten progress stuff...
	*/
	LoginTitle.setHTML("Attempting logout...");
	LoginInfo.addClass("loading");
	
	var jSonRequest = new Json.Remote(url, {onComplete: LogoutResponse}).send();
}

/**
*	When the user presses the login button this is the action
*	that is triggered to actually perform the AJAX login.
*/
function Login() {
	try {
		$('form1').addEvent('submit', Submit);
	} catch (e) {
		
	}
}

function LoginResponse(result) {
	var LoginTitle = $('LoginStatus');
	var LoginInfo = $('message');
	var LoginForm = $('form1');
	var LoginProgress = $('login');
	
	LoginTitle.empty();
	LoginProgress.removeClass("loading");
	
	if(!result.success) {
		LoginTitle.setText("Error");
		LoginInfo.setText(result.error);
		LoginForm.removeClass("invisible");
	} else {
		LoginTitle.setText("Welcome Back, " + result.username);
		LoginProgress.empty();
		LoginInfo.setHTML('<p>'+result.message+'</p><a href="javascript:Logout(this)">Logout</a>');
	}
		
}

function LogoutResponse(result) {
	var LoginTitle = $('LoginStatus');
	var LoginInfo = $('message');
	var LoginForm = $('form1');
	var LoginProgress = $('login');
	
	LoginTitle.empty();
	LoginProgress.removeClass("loading");
	
	if(result.success) {
		LoginInfo.empty();
		LoginTitle.empty();
		LoginTitle.setText("Client Login");
		CreateLoginHTML();
	}
}

function Submit(e) {
	/**
	* Prevent the submit event
	*/
	new Event(e).stop();
	
	var url = "./scripts/login.ajax.php";
	
	var LoginPanel = $('tertiaryContent');
	var LoginTitle = $('LoginStatus');
	var LoginInfo = $('login');
	var LoginForm = $('form1');
	var LoginStatus = $('message');
	var username = $('username').getValue();
	var password = $('password').getValue();
	
	/*alert("Attempting login as: " + username + " with encrypted pass of: " + hex_md5(password));*/
	
	/**
	*	Clear everything
	*/
	LoginTitle.empty();
	LoginForm.addClass("invisible");
	LoginStatus.empty();
	
	/**
	*	Intermitten progress stuff...
	*/
	LoginTitle.setHTML("Attempting login...");
	LoginInfo.addClass("loading");
	
	/**
	*	Make the JSON request to the server.
	*/
	var jSonRequest = new Json.Remote(url, {onComplete: LoginResponse}).send({'hjuythj': username, 'ewrwerd': hex_md5(password)});
}

/**
*	Function creates the login panel HTML
*/
function CreateLoginHTML() {
	var LoginPanel = $('tertiaryContent');
	var LoginTitle = $('LoginStatus');
	var LoginInfo = $('login'); /*	This is where the actual form goes to */
	$('response').empty();
	
	LoginInfo.setHTML('<form id="form1" method="post" action="#"><fieldset><legend>Sign-In</legend><label for="username">Client ID:</label><input id="username" type="text" name="username" value="" /><label for="password">Password:</label><input id="password" type="password" name="password" value="" />  <input id="inputsubmit1" type="submit" name="inputsubmit1" value="Sign In" /><p><a href="register.php">Register</a> | <a href="password.php">Forgot your password?</a></p></fieldset></form>');
	
	$('form1').addEvent('submit', Submit);
}