/**
  ** Title: Form Validation
  ** Description: Validates all text fields highlighting and focusing when found empty.
  ** Author: Josh Hill
  ** Date: 19/11/2007
  **/
function validateForm(form, namesOfElementsToIgnore, showAlerts, invalidColour, defaultColour) 
{ 
	// set default param values if undefined when method was called
	showAlerts = typeof(showAlerts) != 'undefined' ? showAlerts : 0;
	invalidColour = typeof(invalidColour) != 'undefined' ? invalidColour : 'pink';
	defaultColour = typeof(defaultColour) != 'undefined' ? defaultColour : '#FFFFA0';
	
	// loop through all elements in supplied form
	var length=form.length;
	var firstEmptyField=null;
	for (var i=0; i<length; i++) 
	{
		// check element exists and is of type 'text'
		if (form.elements[i] && form.elements[i].type == "text" && namesOfElementsToIgnore.indexOf(form.elements[i].name) < 0)
		{
			// if empty
			if (form.elements[i].value=='') 
			{
				// store first empty element to set focus on later
				if (firstEmptyField==null)
				{
					firstEmptyField = form.elements[i];
				}
				
				// highlight element using color stored in invalidColour variable
				highlightBackground(form.elements[i], invalidColour);
			}
			// set elements background colour back to default colour
			else
			{
				highlightBackground(form.elements[i], defaultColour);
			}
		}
	}
	
	// check if any empty fields were found
	if (firstEmptyField != null)
	{
		// display alert
		if (showAlerts == 1)
		{
			alert('Please fill in the \'' + invalidColour + '\' fields..');
		}
		firstEmptyField.focus();
		return false;
	}
	return true;
}

function highlightBackground(obj,col)
{
	//just set the bgcolor of obj to col
	obj.style.backgroundColor = col; 
}