function resetFields ( whichForm ) {
//alert( "resetFields invoked. whichForm.elements.length="+whichForm.elements.length);
	if ( !document.getElementById ) return false;

	for ( var i=0; i<whichForm.elements.length; i++ ) {
		var element = whichForm.elements[i];
		if ( element.type == 'submit' ) continue;
		if ( element.type == 'select-one' ) {
			element.onfocus = function () {
				this.style.color = highlightColor;
				this.style.borderColor = highlightBorderColor;
				this.style.backgroundColor = highlightBackgroundColor;
				if ( $( this.id+'Label' )) $( this.id + 'Label' ).style.color = highlightColor;
				//this.style.fontWeight = 'bold';
				//elementFocused = this;
				hideError();
			}
			element.onblur = function () {
				this.style.color = origColor;
				this.style.borderColor = origBorderColor;
				this.style.backgroundColor = origBackgroundColor;
				if ( $( this.id+'Label' )) $( this.id + 'Label').style.color = origColor;
				if ( $( this.id+'Label' )) $( this.id + 'Label' ).style.color = origColor;
				//this.style.fontWeight = 'normal';
			}
			continue;
		}
		if ( element.type == 'textarea' ) {
			element.onfocus = function () {
				this.style.color = highlightColor;
				this.style.borderColor = highlightBorderColor;
				this.style.backgroundColor = highlightBackgroundColor;
				if ( $( this.id+'Label' )) $( this.id + 'Label' ).style.color = highlightColor;
				this.style.fontWeight = 'bold';
				if ( this.value == this.defaultValue ) this.value = '';
				//elementFocused = this;
				hideError();
			}
			element.onblur = function () {
				this.style.color = origColor;
				this.style.borderColor = origBorderColor;
				this.style.backgroundColor = origBackgroundColor;
				if ( $( this.id+'Label' )) $( this.id + 'Label' ).style.color = origColor;
				this.style.fontWeight = 'normal';
				if ( this.value=='' && this.defaultValue ) this.value = this.defaultValue;	
			}
			continue;
		}
		if (element.type=='text' ) {
			element.onfocus = function () {
				this.style.color = highlightColor;
				this.style.borderColor = highlightBorderColor;
				this.style.backgroundColor = highlightBackgroundColor;
				if ( $( this.id+'Label' )) $( this.id + 'Label' ).style.color = highlightColor;
				this.style.fontWeight = 'bold';
				if ( this.value == this.defaultValue ) this.value = '';
				//elementFocused = this;
				hideError();
			}
			element.onblur = function () {
				this.style.color = origColor;
				this.style.borderColor = origBorderColor;
				this.style.backgroundColor = origBackgroundColor;
				if ( $( this.id+'Label' )) $( this.id + 'Label' ).style.color = origColor;
				this.style.fontWeight = 'normal';
				if ( this.value=='' && this.defaultValue ) this.value = this.defaultValue;			
			}		
			continue;
		}
		if ( element.type=='radio' ) {
			element.onfocus = function () {
				if ( $( this.name + 'Label' )) $( this.name + 'Label' ).style.color = highlightColor;
				if ( $( this.name + this.value )) $( this.name + this.value ).style.color = highlightColor;
				//elementFocused = this;
				hideError();
			}
			element.onblur = function () {
				if ( $( this.name + 'Label' )) $( this.name + 'Label' ).style.color = origColor;
			}
			element.onclick = function () {
				setRadioFeedback( this.form, this.name );
			}
			continue;		
		}
		if (element.type=='file' ) {
			element.onfocus = function () {
				this.style.borderColor = highlightBorderColor;
				if ( $( this.id+'Label' )) $( this.id + 'Label' ).style.color = highlightColor;
				//elementFocused = this;
				hideError();
			}
			element.onblur = function () {
				this.style.borderColor = origBorderColor;
				if ( $( this.id+'Label' )) $( this.id + 'Label' ).style.color = origColor;
			}		
			continue;
		}		
	}
}
function setRadioFeedback( whichForm, name ) {
//alert("setRadioFeedback invoked. whichForm="+whichForm.name+" name="+name);
	for ( var i=0; i<whichForm.elements.length; i++ ) {
		var element = whichForm.elements[i];
		if ( element.type != 'radio' ) continue; 
		if ( element.name == name ) {
			if ( !element.checked ) {
				if ( $( element.name+element.value )) $( element.name+element.value+"Label" ).style.color = origColor;
			} else {
				if ( $( element.name+element.value )) $( element.name+element.value+"Label" ).style.color = highlightColor;
			}
		}
	}
}
function focusLabels() {
	if ( !document.getElementsByTagName ) return false;
	
	var labels = document.getElementsByTagName( 'label' );
	for ( var i=0; i<labels.length; i++ ) {
		if( !labels[i].getAttribute( 'for' )) continue;
		labels[i].onclick = function () {
			var id=this.getAttribute( 'for' );
			if ( !$( id )) { return false } else { $( id ).focus() }
		};
	};
};
function isFilled( field ) {
// Assumptions: 
//	* field exists and is an input text field
	if ( field.value.length <1 || field.value == field.defaultValue ) {
		return false;
	} else {
		return true;
	};
};
function isEmail( field ) {
// Assumptions: 
//	* field exists and is an input text field
	if ( field.value.indexOf( '@' ) == -1 || field.value.indexOf( '.' ) == -1 ) {
		return false;
	} else {
		return true;
	};
};
function selectValue( field ) {
// Assumptions:
//	* The Zero index of the select field means 'nothing has been selected'
	var value = field.options[ field.selectedIndex ].text;
	if ( field.selectedIndex == 0 ) {
		return null;
	} else {
		return value;
	};
};
function buttonValue( whichForm, groupName ) {
// Assumptions: 
//	* whichForm exists and has the specified radio button group
//	* groupName exists and is a radio button group
	var elements = whichForm.elements;
	for ( var i=0; i<whichForm.elements.length; i++ ) {
		if ( elements[ i ].name==groupName && elements[ i ].checked ) return elements[ i ].value;
	};
	return null;
};
function showError( formElement ) {
// Assumptions: 
//	* formElement exists and is an input text field
	formElement.value = formElement.defaultValue;
	formElement.style.color = errorColor;
	formElement.style.fontWeight = 'bold';
	formElement.style.borderColor = errorBorderColor;
	$( formElement.id + 'Label' ).style.color = errorColor;
	formError.style.display='block';
	//elementFocused.blur();
};
function hideError () {
	formError.style.display='none';
};
function showSelectError( formElement ) {
// Assumptions: 
//	* formElement exists and is a selection box
	formElement.style.color = errorColor;
	formElement.style.borderColor = errorBorderColor;
	formElement.style.fontWeight = 'bold';
	$( formElement.id + 'Label' ).style.color = errorColor;
	formError.style.display = 'block';
	//elementFocused.blur();
};
function showRadioError( formElement ) {
// Assumptions: 
//	* formElement exists and is a radio button
	if ( $( formElement.name + 'Label' )) $( formElement.name + 'Label' ).style.color = errorColor;
	formError.style.display = 'block';
	//elementFocused.blur();
};
function showFileError( formElement ) {
// Assumptions: 
//	* formElement exists and is an file upload text field
	formElement.style.borderColor = errorBorderColor;
	$( formElement.id + 'Label').style.color = errorColor;
	formError.style.display='block';
	//elementFocused.blur();
};
function validateForm( whichForm ) {
// Assumptions: 
//	* whichForm exists
//	* Required elements have 'required' somewhere in their className
// 	* Radio groups have only one button labelled 'required'
	for ( var i=0; i<whichForm.elements.length; i++ ) {
		var element = whichForm.elements[ i ];
		if ( element.className.indexOf( 'required' ) == -1 ) continue;
		switch ( element.type ) {
			case 'text':
				if ( !isFilled( element )) { showError( element ); return false; }
				if ( element.className.indexOf( 'email' ) != -1) {
					if ( !isEmail( element )) { showError( element ); return false; }
				};
				break;
			case 'select-one':
				if ( selectValue( element ) == null ) { showSelectError( element ); return false; }
				break;
			case 'radio':
				if ( buttonValue( element.form, element.name ) == null ) { showRadioError( element ); return false; }
				break;
			case 'file':
				if ( !isFilled( element )) { showFileError( element ); return false; }
				break;
		};
	};
	return true;
};
function prepareForms( whichForm ) {
//alert('prepareForms invoked. whichForm='+whichForm);
	if ( !document.getElementById ) return false;
	if ( !$( whichForm )) return false;
	var thisForm = $( whichForm );
	resetFields( thisForm );
	thisForm.onsubmit = function () {
		return validateForm( this );
	};
	// Store the location of the error message
	formError = $( 'formError' );
};
// Variables used
var formError; 								// DIV with error message
var origColor = '#666666'; 					// The original text color of the form elements
var origBorderColor = '#CCCCCC'; 			// The original border color of the form elements
var origBackgroundColor ='#FFFFFF';			// The original background color of the form elements
var highlightColor = '#000000';				// The highlight text color of the form elements
var highlightBorderColor = '#000000';		// The highlight border color of the form elements
var highlightBackgroundColor = '#eceff4';	// The highlight background color of the form elements
var errorColor = '#FF0000';					// The error text color of the form elements
var errorBorderColor = '#FF0000';			// The error border color of the form elements

function invokeFormValidation () {
	prepareForms( 'contactForm' );
	focusLabels();
};
