function setFocus ( whichForm, name ) {
	if ( !document.getElementById ) return false;
	$( 'name' ).focus();
}
function resetFields ( whichForm ) {
//alert( "resetFields invoked. whichForm.name="+whichForm.name);
	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 = highlightLabelColor;
				//this.style.fontWeight = 'bold';
				//elementFocused = this;				
				hideError();
				show( this, 'highlight' );
			}
			element.onblur = function () {
				this.style.color = origColor;
				this.style.borderColor = origBorderColor;
				this.style.backgroundColor = origBackgroundColor;
				if ( $( this.id+'Label' )) $( this.id + 'Label').style.color = origLabelColor;
				//this.style.fontWeight = 'normal';
				
				show( this, 'orig' );
			}
			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 = highlightLabelColor;
				//this.style.fontWeight = 'bold';
				if ( this.value == this.defaultValue ) this.value = '';
				//elementFocused = this;
				hideError();
				
				show( this, 'highlight' );
			}
			element.onblur = function () {
				//this.style.color = origColor;
				//this.style.borderColor = origBorderColor;
				//this.style.backgroundColor = origBackgroundColor;
				//if ( $( this.id+'Label' )) $( this.id + 'Label' ).style.color = origLabelColor;
				//this.style.fontWeight = 'normal';
				//if ( this.value=='' && this.defaultValue ) this.value = this.defaultValue;
				if( this.value=='' && this.defaultValue ) {
					this.value = this.defaultValue;
					show( this, 'orig' );
				} else {
					if( validateForm( this.form, this.name )) {
						show( this, 'valid' );
					};
				};
			}
			continue;
		}
		if (element.type=='text' ) {
			element.onfocus = function () {
				//this.style.color = highlightColor;
				//this.style.borderColor = highlightBorderColor;
				//this.style.backgroundColor = '#00ff00';
				//if ( $( this.id+'Label' )) $( this.id + 'Label' ).style.color = highlightLabelColor;
				//this.style.fontWeight = 'bold';
				if ( this.value == this.defaultValue ) this.value = '';
				//elementFocused = this;
				hideError();
				
				show( this, 'highlight' );
			}
			element.onblur = function () {
				if( this.value=='' && this.defaultValue ) {
					this.value = this.defaultValue;
					show( this, 'orig' );
				} else {
					if( validateForm( this.form, this.name )) {
						show( this, 'valid' );
						//showValid( this );
					};
				};
			}
			continue;
		}
		if ( element.type=='radio' ) {
			element.onfocus = function () {
				if ( $( this.name + 'Label' )) $( this.name + 'Label' ).style.color = highlightLabelColor;
				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 = origLabelColor;
			}
			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 = highlightLabelColor;
				//elementFocused = this;
				hideError();
			}
			element.onblur = function () {
				this.style.borderColor = origBorderColor;
				if ( $( this.id+'Label' )) $( this.id + 'Label' ).style.color = origLabelColor;
			}		
			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 = origLabelColor;
			} else {
				if( $( element.name+element.value )) $( element.name+element.value+"Label" ).style.color = highlightLabelColor;
			};
		};
	};
};
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 ) { // unused
// 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 = errorLabelColor;
	if( showFormError ) formError.style.display='block';
	//elementFocused.blur();
}
function show( el, state ) {
// Assumptions: 
//	* formElement exists and is an input text field
	//formElement.style.color = validColor;
	//formElement.style.fontWeight = 'bold';
	//formElement.style.borderColor = validBorderColor;
	switch( state ) {
		case 'error':
			//el.value = el.defaultValue;
			el.style.color = errorColor;
			el.style.fontWeight = 'bold';
			el.style.borderColor = errorBorderColor;
			$( el.id + 'Label' ).style.color = errorLabelColor;
			if( showFormError ) formError.style.display='block';
			el.style.fontWeight = 'bold';
			break;
		case 'highlight':
			el.style.color = highlightColor;
			el.style.borderColor = highlightBorderColor;
			el.style.backgroundColor = highlightBackgroundColor;
			if ( $( el.id+'Label' )) $( el.id + 'Label' ).style.color = highlightLabelColor;
			el.style.fontWeight = 'bold';
			break;
		case 'valid':
			el.style.color = validColor;
			el.style.borderColor = validBorderColor;
			el.style.backgroundColor = validBackgroundColor;
			if( $( el.id+'Label' )) $( el.id + 'Label' ).style.color = validLabelColor;
			el.style.fontWeight = 'bold';
			break;
		default:
			el.style.color = origColor;
			el.style.borderColor = origBorderColor;
			el.style.backgroundColor = origBackgroundColor;
			if( $( el.id+'Label' )) $( el.id + 'Label' ).style.color = origLabelColor;
			el.style.fontWeight = 'normal';
			break;
	};
}

function showValid( el ) { // unused
// Assumptions: 
//	* formElement exists and is an input text field
	//formElement.style.color = validColor;
	//formElement.style.fontWeight = 'bold';
	//formElement.style.borderColor = validBorderColor;
	el.style.color = validColor;
	el.style.borderColor = validBorderColor;
	el.style.backgroundColor = validBackgroundColor;
	if( $( el.id+'Label' )) $( el.id + 'Label' ).style.color = validLabelColor;
	el.style.fontWeight = 'bold';
}
function showOriginal( el ) { // unused
// Assumptions: 
//	* formElement exists and is an input text field
	el.style.color = origColor;
	el.style.borderColor = origBorderColor;
	el.style.backgroundColor = origBackgroundColor;
	if( $( el.id+'Label' )) $( el.id + 'Label' ).style.color = origLabelColor;
	el.style.fontWeight = 'normal';
}

function hideError () {
	if( showFormError ) formError.setStyle( 'display', 'none' );
}
function showSelectError( el ) {
// Assumptions: 
//	* formElement exists and is a selection box
	//el.style.color = errorColor;
	//el.style.borderColor = errorBorderColor;
	//el.style.fontWeight = 'bold';
	//$( el.id + 'Label' ).style.color = errorLabelColor;
	if( showFormError ) formError.style.display = 'block';
	//elementFocused.blur();
	
	show( el, 'error' );
}
function showRadioError( formElement ) {
// Assumptions: 
//	* formElement exists and is a radio button
	if ( $( formElement.name + 'Label' )) $( formElement.name + 'Label' ).style.color = errorLabelColor;
	if( showFormError ) 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 = errorLabelColor;
	if( showFormError ) formError.style.display='block';
	//elementFocused.blur();
}
function validateForm( whichForm, name ) {
//alert('validateForm invoked. whichForm='+whichForm.id+', name='+name);
// Assumptions: 
//	* whichForm exists
// 	* If name is specified, name exists
//	* Required elements have 'required' in their class
// 	* 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;
		if( name!=null && element.name != name ) continue;
		switch( element.type ) {
			case 'text':
				if( element.className.indexOf( 'email' ) != -1 ) {
					if ( !isEmail( element )) {
						show( element, 'error' );
						//showError( element ); 
						return false;
					};
				};
				if( !isFilled( element )) { 
					show( element, 'error' );
					//showError( element ); 
					return false; 
				};
				break;
			case 'textarea':
				if( !isFilled( element )) { 
					show( element, 'error' );
					//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 ) {
	if ( !document.getElementById ) return false;
	
	var thisForm = $( whichForm );
	resetFields( thisForm );
	thisForm.onsubmit = function () {
		return validateForm( this );
	};
	// Store the location of the error message
	//formError = $( 'formError' );
};

