
// -------------------------------------------------------------------
// shouldSubmit()
//  This function verifies if the password field should be submitted.
// -------------------------------------------------------------------
function shouldSubmit(e){
	var username = getElement('username').value;
	var password = getElement('password').value;
	
	if(username != "" 
		&& username.length > 5 
		&& password != "" 
		&& password.length > 5){
		
		if(e.which == 13){
			document.staffLoginForm.submit();
		}
	}
}

// ----------------------------------------------------------------------------
//	getElement(id)
//		Returns a single element
// ----------------------------------------------------------------------------
function getElement(id){
	return document.getElementById(id);
}

// -------------------------------------------------------------------
// attachEventListener(object, eventName, functionName)
//  Attaches an event to an object
// -------------------------------------------------------------------
function attachEventListener (object, eventName, functionName){
	if(!object){
		return ;
	}
	
	// Must be backwards compatible
	var objectId = "#" + object.id;
	
	$(objectId).unbind(eventName, theFunctionToFire);
	
	var theFunctionToFire = functionName;
	
	$(objectId).bind(eventName, theFunctionToFire);
}

// -------------------------------------------------------------------
// initLoginPage()
//  This function adds a listener to the password box.
// -------------------------------------------------------------------
function initLoginPage(){
	attachEventListener(
		getElement('username'),
		'keypress',
		function(e){
			shouldSubmit(e);
			
		});	
	
	attachEventListener(
		getElement('password'),
		'keypress',
		function(e){
			shouldSubmit(e);
		});	
}

// -------------------------------------------------------------------
// Initializes handlers
// -------------------------------------------------------------------
$(document).ready(function(){
	initLoginPage();
});
// -------------------------------------------------------------------
