/**
 *	Copyright (c) 2005-2009 H2O Solutions (http://www.h2osolutions.org) 
 *
 *	@brief		Login validation using client-side scripting.
 *	@version	20081113
 *
 *	@author		Mathew Schofield
 */


/**
 *	@brief	Determines if the login form has been filled correctly.
 *
 *	The username and password must both contain data that is at 
 *	least 5 characters and at most 15 characters.
 *
 *	The error ?
 *
 *	@retval true 	The form is valid.
 *	@retval false	A part of the form is invalid.
 */
function validate_login()
{
	var errors = Array();
	var txtUsername = document.getElementById("username");
	var txtPassword = document.getElementById("password");
	var length = 0;
	
	// clear our current errors
	clear_errors();
	
	// validate the username
	length = txtUsername.value.length;
	
	if (length < 5 || length > 15)
		errors.push("Username is invalid.");
	
	// validate the password
	length = txtPassword.value.length;
	
	if (length < 5 || length > 15)
		errors.push("Password is invalid.");
	
	if (errors.length != 0) {
		// display our errors
		for(var i = 0; i < errors.length; ++i)
			add_error(errors[i]);
		
		// return our failure
		return false;
	}
	
	// success!
	return true;
};


/**
 *	@todo Documentation.
 */
function loader()
{
	// attach a submit event to the recover form
	attach_event_submit(document.getElementById("loginForm"), validate_login);
	
	// attach a click event to the reset button
	attach_event_click(document.getElementById("resetLogin"), clear_errors);
};


// attach a load event to this page
attach_event_load(loader);