/*************************************************************************************
Name: formHighlight.js
Description: Highlights active form elements
Author: John Kuiphoff

Instructions: 
1. To initialize this class, use the following syntax:
   new FormHighlight('IdOfForm');
************************************************************************************/

// FORM HIGHLIGHT CLASS
var FormHighlight = Class.create(
{
	// CONSTRUCTOR (input: ID of form)
	initialize: function(formID)
	{
		// Declare Variables
		this.formID = formID;
		this.highlightColor = '#3399ff';
		this.unhighlightColor = '#cccccc';
		
		// Get All Form Elements
		this.formElements = $(this.formID).getElements();
		
		// Create Event Listeners
		this.createEvents();
	},
	
	// CREATE EVENT LISTENERS FOR EACH FORM ELEMENT
	createEvents: function()
	{
		// Loop Through All Form Elements
		for(var i=0; i<this.formElements.length; i++)
		{
			// Check For The Following Types: text, select, password, textarea
			if((this.formElements[i].type == 'text') || (this.formElements[i].type == 'select') || (this.formElements[i].type == 'password') || (this.formElements[i].type == 'textarea'))
			{
				// Initialize Event Listers For Each Form Element
				$(this.formElements[i]).observe('focus', this.highlightElement.bindAsEventListener(this, this.formElements[i]));	
				$(this.formElements[i]).observe('blur', this.unhighlightElement.bindAsEventListener(this, this.formElements[i]));
			}
		}
	},
	
	// DESTROY EVENT LISTENERS FOR EACH FORM ELEMENT
	destroyEvents: function()
	{
		// Loop Through All Form Elements
		for(var i=0; i<this.formElements.length; i++)
		{
			// Check For The Following Types: text, select, password, textarea
			if((this.formElements[i].type == 'text') || (this.formElements[i].type == 'select') || (this.formElements[i].type == 'password') || (this.formElements[i].type == 'textarea'))
			{
				// Destroy Event Listers For Each Form Element
				$(this.formElements[i]).stopObserving('focus', this.highlightElement.bindAsEventListener(this, this.formElements[i]));	
				$(this.formElements[i]).stopObserving('blur', this.unhighlightElement.bindAsEventListener(this, this.formElements[i]));
			}
		}
	},
	
	// HIGHLIGHTS ELEMENT
	highlightElement: function(e, formElement)
	{
		// Set the Border Color To the Color Defined in the Constructor Method
		$(formElement).setStyle({ borderColor: this.highlightColor });
	},
	
	// UNHIGHLIGHTS ELEMENT
	unhighlightElement: function(e, formElement)
	{
		// Set the Border Color To the Color Defined in the Constructor Method
		$(formElement).setStyle({ borderColor: this.unhighlightColor });	
	}
	
});