function boxIsChecked(form, elements) {																// Returns true if one of a group of same-named checkboxes is checked.
	var bChecked = false;

	for (var i = 0; i < form[elements].length; i++) {
		if (form[elements][i].checked == true) {
			bChecked = true;
			break;
		}
	}

	return bChecked;
}

function isEmpty(formField) {																		// isEmpty() returns true if a text box is empty.
	return formField.value == null || formField.value.length == 0 ? true : false;
}

function moveCursor(form, elem, keyCode, width, elemsBefore, elemsAfter) {
	/*
	Author: (c) Aidan Whitehall <mailto:aidanwhitehall@fairbanks.co.uk>
	Feel free to distribute, but please retain this comment block.
	
	Script function: Allows the user to navigate around a grid-like matrix of form fields using the arrow keys.
	It also traps the Enter and Cancel keys and prevents the form from being automatically submitted or reset
	when they are pressed, forcing the user to click them with the mouse.
	
	Instructions: Add the following code to all <input type="text"> form fields in your grid of text inputs:
	   onkeypress="moveCursor(this.form, x, event.keyCode);"
	In each text input field, replace x with the position of that element in the forms.elements array. As an
	example, imagine we have a form that has two hidden inputs, a 3 wide by 2 high grid of text inputs (6 text
	input fields in total) and two buttons after the form, a Submit and a Reset, as in this HTML:
	
	   <form action="somepage.html" method="post">
	   <input type="hidden" name="var1" value="someValue">
	   <input type="hidden" name="var2" value="anotherValue">
	   <input type="text" onkeypress="moveCursor(event.keyCode, this.form, 2);">
	   <input type="text" onkeypress="moveCursor(event.keyCode, this.form, 3);">
	   <input type="text" onkeypress="moveCursor(event.keyCode, this.form, 4);"><br />
	   <input type="text" onkeypress="moveCursor(event.keyCode, this.form, 5);">
	   <input type="text" onkeypress="moveCursor(event.keyCode, this.form, 6);">
	   <input type="text" onkeypress="moveCursor(event.keyCode, this.form, 7);"><br />
	   <input type="submit"> <input type="reset">
	
	As JavaScript arrays start at zero, the two hidden inputs are fields 0 and 1. Therefore, in the first text
	input you have to replace x with 2 and continue from there.
	
	For this form, you would then need to modify the three variables declared at the start of the script from their
	default values to
	   width = 3;
	   elemsBefore = 2;
	   elemsAfter = 2;
	
	width is the number of columns in your text input grid. Our example grid was three columns wide, so we set width to 3.
	elemsBefore is the number of form inputs before your grid. In our example, we had two hidden form inputs before the grid, so we set elementsBefore to 2.
	elemsAfter is the number of form elements after your grid. In our example, we had two buttons, so we also set elementsAfter to 2.
	*/

	if (typeof width == 'undefined') width = 4;														// The number of cells wide in the form (defaults to 4).
	if (typeof elemsBefore == 'undefined') elemsBefore = 0;											// The number of form elements before the navigable part of the form (defaults to 0).
	if (typeof elemsAfter == 'undefined') elemsAfter = 2;											// The number of form elements after the navigable part of the form (defaults to 2, for Submit and Cancel buttons).
	var length = form.elements.length;																// The number of elements in the form.
	var key;																						// Use key to track 

	if (keyCode == 37)							key = "left";										// Left arrow.
	else if (keyCode == 38)						key = "up";											// Up arrow.
	else if (keyCode == 39)						key = "right";										// Right arrow.
	else if (keyCode == 13 || keyCode == 40)	key = "down";										// Enter or down arrow.

	var bFirst = (elem - elemsBefore == 0) ? true : false;											// Is the current cell the very first cell in the grid? 
	var bLast = (elem == length - 1 - elemsAfter) ? true : false;									// Is the current cell the very last cell in the grid?
	var eFirst = elemsBefore;																		// Reference to the fist cell.
	var eLast = length - 1 - elemsAfter;															// Reference to the last cell.

	switch (key) {																					// Determine which code to run based on the key pressed.
		case "left":
			if (bFirst)																				// If we're on the first cell, wrap to the end of the form.
				elem = eLast;
			else																					// Otherwise just go to the previous cell.
				elem--;
			break;
		case "up":
			if (((elem + 1 - elemsBefore) / width) <= 1)											// If we're on the top row, wrap to the bottom.
				if (bFirst)																			// If on first cell, wrap to the last.
					elem = eLast;
				else
					elem = length - elemsAfter - (width - (elem - elemsBefore)) - 1;
			else																					// Otherwise, just move one row up.
				elem -= width;
			break;
		case "right":
			if (bLast)																				// If we're on the last cell, wrap to the first.
				elem = eFirst;
			else																					// Otherwise just go to the next cell.
				elem++;
			break;
		case "down":
			if (elem >= (length - elemsAfter - width)) {											// if we're on the bottom row, wrap to the top.
				if (bLast) {
					elem = eFirst;																	// If on last cell, wrap to the first.
				} else {
					elem = elemsBefore + (elem + 1 - elemsBefore) % width;
				}
			} else {																				// Otherwise, just move one row down.
				elem += width;
			}
			break;
	}

	if (key || keyCode == 27) {																		// If they pressed an arrow key, enter or Escape,
		form.elements[elem].focus();																// put focus on the new form input
		return false;																				// and return false to prevent the form being submitted or reset.
	}
}

function submitOnce(form) {																			// submitOnce() disables a submit button once pressed.
   if (document.all || document.getElementById) {													// If it's IE 4+ or NS 6+
      for (i = 0; i < form.length; i++) {															// Loop through every element in the form, and hunt down Submit and Reset
         var tempobj = form.elements[i];
         if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset") {
            tempobj.disabled = true
         }
      }
   }
}

function validateLogin(form) {																		// Checks the login form to ensure that a user has supplied both the username and password.
	if (isEmpty(form.username)) {
		alert("Please enter your username.")
		form.username.focus();
		return false;
	} else if (isEmpty(form.password)) {
		alert("Please enter your password.")
		form.password.focus();
		return false;
	} else {
		submitOnce(form);
		return true;
	}
}