// <![CDATA[
/******************************************************************************************
* Project: Content Connect Africa
* Module: validate.login.js
* Description: Make sure login details are filled in
* 
* Copyright: 2005 - this.year() SI Works Internet
* 
* Email:  support@siworks.co.za
* Author: Greg Shiers, Jarratt Ingram, Roland Lovelock
* Note
* error[] comes from errors/errors.js store them in a common place
******************************************************************************************/

/**
* Function to initialize validate() and add the eventListener to the form
* @param 
* @version 0.4
* @returns 
* @type 
* @author Greg Shiers
*/
function initialize ( ) {
	var form = $('login_form');
	// Attatch event listeners to buttons using attatchEventListener()
	// First check if the element exsists, then add the event listener if it is
	if ( form ) { attachEventListener (  form , "submit" , validate  , false); }
}
/**
* Fucntion to validate that the login form elements have been filled in
* @param (event) event to check for window.event
* @version 1.0.14
* @returns Form errors
* @type Boolean
* @author Greg Shiers
*/
function validate ( event ) {
	// set the form to be validated
	var form = $('login_form');
	// Check for event and define if not there
	if (typeof event == "undefined") {
    	event = window.event;
	}
	// Set the variables for the script
	var msg = [], focus_el = null;
	// Set form fields to true or false, we can easily turn on and off validation this way
	var name 		= true,
		pass		= true;
	// Trim all values of text fields
	form.name.value = form.name.value.trim()
	form.pass.value = form.pass.value.trim()
	
	// Check that the first name is entered
	if ( name && validation.empty ( $('name') ) ) {
		msg += error[0];
		focus_el = focus_el || $('name');
	}
	// Check that the last name is entered
	if ( pass && validation.empty ( $('pass') ) ) {
		msg += error[1];
		focus_el = focus_el || $('pass');
	}
	// If there is an error
	if ( msg != '' ) {
		// Send the errors to the screen in the errors wrapper
		$('validate').innerHTML = "<p class=\"error\">" + msg + "</p>";
		// Focus on the first element with an error
		if ( focus_el.focus ) {
			focus_el.focus();
		}
		// Stop the default action
		stopDefaultAction( event );
	}
	else {
		$('validate').innerHTML = '';
		// If there are no errors, go ahead and send the form through.
		loadProgressBar( "validate" , false , "Logging in, please wait...");
	}
}

/**
* Use the AddLoadListner function to add an onsubmit action to the 
* form that needs to be validated
*/
addLoadListener ( initialize  );
// ]]>