/*Exposes the element but turns it invisible and takes it out of the flow so it can be measured. */
Element.implement({
	expose: function(){
		if( this.getStyle('display') != 'none' ) return $empty;
		var before = {};
		var styles = { visibility:'hidden', display:'block', position:'absolute' };
		//use this method instead of getStyles
		$each(styles, function(value, style){
			before[style] = this.style[style]||'';
		}, this);
		//this.getStyles('visibility', 'display', 'position');
		this.setStyles(styles);
		return (function(){ this.setStyles(before); }).bind(this);
	}
});

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 () {
				//elementFocused = this;
				hideError();
				show( this, 'highlight' );
			};
			element.onblur = function () {
				show( this, 'valid' );
			};
			continue;
		};
		if ( element.type == 'textarea' ) {
			element.onfocus = function () {
				if ( this.value == this.defaultValue ) this.value = '';
				//elementFocused = this;
				hideError();				
				show( this, 'highlight' );
			}
			element.onblur = function () {				
				if( this.value=='' && this.defaultValue ) {
					show( this, 'orig' );
					this.value = this.defaultValue;
				} else {
					if( validateForm( this.form, this.name )) {
						show( this, 'valid' );
					};
				};
			}
			continue;
		}
		if( element.type=='text' ) {
			element.onfocus = function () {
				if ( this.value == this.defaultValue ) this.value = '';
				//elementFocused = this;
				hideError();				
				show( this, 'highlight' );
			};
			element.onblur = function () {
				if( this.value=='' && this.defaultValue ) {
					show( this, 'orig' );
					this.value = this.defaultValue;
				} else {
					if( validateForm( this.form, this.name )) show( this, 'valid' );
				};		
			};		
			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=='checkbox' ) {
			element.onfocus = function () {
				hideError();
			}
			element.onblur = function () {
			}
			element.onclick = function () {
				setCheckboxFeedback( this.form, this.name );
			}
// FORM SPECIFIC -- MOVE THIS OUTSIDE CHECKBOX
			if( element.className.test( 'toggler' )) {
				var rvl = $( element.id + 'Reveal' );
				var restore = rvl.expose();
				var size = rvl.getSize();
				restore();
				var h = size.y;
				//alert('size.y='+h);
				rvl.store( 'xHeight', h );
				rvl.setStyle( 'overflow', 'hidden' );
				rvl.store( 'revealer', new Fx.Morph( rvl ));
				rvl.retrieve( 'revealer' ).set({
					'height': 0,
					'opacity': 0,
					duration: 'long', 
					transition: 'bounce:out'
				});
				$( element ).addEvent( 'click', function(e){
					//e.stop();
					var rvl = $( this.id + 'Reveal' );
					var h = rvl.retrieve( 'xHeight' );
					var rvlOpen = false;
					if( rvl.getSize().y > 0 ) { rvlOpen = true; };
					if( this.checked ) {
						if( !rvlOpen ) rvl.retrieve( 'revealer' ).start({
							'height': h,
							'opacity': 1,
							duration: 'long', 
							transition: 'sine:out'
						});
					} else {
						if( rvlOpen ) rvl.retrieve( 'revealer' ).start({
							'height': 0,
							'opacity': 0,
							duration: 'long', 
							transition: 'sine:out'
						});
					};
				});
			};
			continue;
		}
		if (element.type=='file' ) {
			element.onfocus = function () {
				this.style.borderColor = highlightBorderColor;
				if ( $( this.id+'Label' )) $( this.id + 'Label' ).setStyle( 'color', highlightLabelColor );
				//elementFocused = this;
				hideError();
			}
			element.onblur = function () {
				this.style.borderColor = origBorderColor;
				if ( $( this.id+'Label' )) $( this.id + 'Label' ).setStyle( 'color', origLabelColor );
			}		
			continue;
		}		
	}
}
function setRadioFeedback( whichForm, name ) {
//alert("setRadioFeedback invoked. whichForm="+whichForm.id+" 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' ).setStyle( 'color', origLabelColor );
			} else {
				if ( $( element.name+element.value )) $( element.name+element.value+'Label' ).setStyle( 'color', highlightLabelColor );
			}
		}
	}
}
function setCheckboxFeedback( whichForm, name ) {
//alert("setCheckboxFeedback invoked. whichForm="+whichForm.id+" name="+name);
var result = false;
	for ( var i=0; i<whichForm.elements.length; i++ ) {
		var element = whichForm.elements[ i ];
		if( element.type != 'checkbox' ) continue; 
		if( element.name == name ) {
			if ( element.checked ) {
				$( element.id+'Label' ).setStyle( 'color', highlightLabelColor );
				result = true;
			} else {
				$( element.id+'Label' ).setStyle( 'color', origLabelColor );
			};
		};
	};
	if( result ) {
		$( name+'Label' ).setStyle( 'color', highlightLabelColor );
	} else {
		$( name+'Label' ).setStyle( 'color', origLabelColor );
	};
}
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 showFormErrorDiv( el ) {
	if( showFormError ) {
		formError.style.display='block';
		formError.inject( el, 'before' );
	};
}
function showError( el ) {
// Assumptions: 
//	* formElement exists and is an input text field
	el.value = el.defaultValue;
	el.style.color = errorColor;
	el.style.fontWeight = 'bold';
	el.style.borderColor = errorBorderColor;
	$( el.id + 'Label' ).style.color = errorLabelColor;
	
	showFormErrorDiv( el );
	//elementFocused.blur();
}
function hideError () {
	if( showFormError ) {
		formError.setStyle( 'display', 'none' );
		formError.dispose();
	};
}
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 = errorLabelColor;
	showFormErrorDiv( el );
	//elementFocused.blur();
}
function showRadioError( el ) {
// Assumptions: 
//	* el exists and is a radio button
	if ( $( el.name + 'Label' )) $( el.name + 'Label' ).style.color = errorLabelColor;
	showFormErrorDiv( el );
	//elementFocused.blur();
}
function showFileError( el ) {
// Assumptions: 
//	* el exists and is an file upload text field
	el.setStyle( 'borderColor', errorBorderColor );
	$( el.id + 'Label' ).setStyle( 'color', errorLabelColor );
	showFormErrorDiv( el );
	//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' 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;
		if( name!=null && element.name != name ) continue;
		switch( element.type ) {
			case 'text':
				if( element.className.indexOf( 'email' ) != -1 ) {
					if ( !isEmail( element )) {
						show( element, 'error' );
						return false;
					};
				};
				if( !isFilled( element )) { 
					show( element, 'error' );
					return false; 
				};
				break;
			case 'textarea':
				if( !isFilled( element )) { 
					show( element, 'error' );
					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 show( el, state ) {
// Assumptions: 
//	* formElement exists and is an input text field
	switch( state ) {
		case 'error':
			//el.value = el.defaultValue;
			el.style.color = errorColor;
			el.style.borderColor = errorBorderColor;
			el.style.backgroundColor = errorBackgroundColor;
			el.style.fontWeight = 'bold';
			if ( $( el.id+'Label' )) {
				$( el.id + 'Label' ).style.color = errorLabelColor;
				$( el.id + 'Label' ).style.fontWeight = 'bold';
			};
			if( showFormError ) formError.style.display='block';
			break;
		case 'highlight':
			el.style.color = highlightColor;
			el.style.borderColor = highlightBorderColor;
			el.style.backgroundColor = highlightBackgroundColor;
			el.style.fontWeight = 'bold';
			if ( $( el.id+'Label' )) {
				$( el.id + 'Label' ).style.color = highlightLabelColor;
				$( el.id + 'Label' ).style.fontWeight = 'bold';
			};
			break;
		case 'valid':
			el.style.color = validColor;
			el.style.borderColor = validBorderColor;
			el.style.backgroundColor = validBackgroundColor;
			el.style.fontWeight = 'bold';
			if ( $( el.id+'Label' )) {
				$( el.id + 'Label' ).style.color = validLabelColor;
				$( el.id + 'Label' ).style.fontWeight = 'bold';
			};
			break;
		default:
			el.style.color = origColor;
			el.style.borderColor = origBorderColor;
			el.style.backgroundColor = origBackgroundColor;
			el.style.fontWeight = 'normal';
			if ( $( el.id+'Label' )) {
				$( el.id + 'Label' ).style.color = origLabelColor;
				$( el.id + 'Label' ).style.fontWeight = 'bold';
			};
			break;
	};
}
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 );
	};
	// Remove error message from display
	if( showFormError ) {
		formError = $( 'formError' );
		formError.dispose();
	};
}
function setBeginningFocus () {
	if ( !document.getElementById ) return false; 
	$( 'name' ).focus();
}
// Variables used
//var elementFocused;						// Last element that had focus
var formError; 								// DIV with error message
var showFormError = false;					// Boolean to display DIV 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 origLabelColor ='#666666';				// The original label 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 highlightLabelColor = '#000000';		// 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
var errorBackgroundColor = '#eceff4';		// The highlight background color of the form elements
var errorLabelColor = '#FF0000';			// The highlight background color of the form elements
var validColor = '#194534';
var validBorderColor = '#194534';
var validBackgroundColor = '#FFFFFF';
var validLabelColor = '#194534';

function invokeContact() {
	prepareForms( 'contactForm' );
	focusLabels();
};