// JavaScript
// This file handles all form functions including validation, additional styling, etc.

// The following functions are for hiding and showing over labels. 
function initOverLabels () {
	if (!document.getElementById) return;      

	var labels, id, field;

	// Set focus and blur handlers to hide and show 
	// labels with 'overlabel' class names.
	labels = document.getElementsByTagName('label');
	for (var i = 0; i < labels.length; i++) {

		if (labels[i].className == 'overlabel') {

			// Skip labels that do not have a named association
			// with another field.
			id = labels[i].htmlFor || labels[i].getAttribute('for');
			if (!id || !(field = document.getElementById(id))) {
				continue;
			} 

			// Change the applied class to hover the label 
			// over the form field.
			labels[i].className = 'overlabel-apply';

			// Hide any fields having an initial value.
			if (field.value !== '') {
				hideLabel(field.getAttribute('id'), true);
			}
			
			// Set handlers to show and hide labels.
			field.onfocus = function () {
				hideLabel(this.getAttribute('id'), true);
				addClass(this, 'highlight');
				this.focus = true;
			};
			
			field.onblur = function () {
				if (this.value === '') {
					hideLabel(this.getAttribute('id'), false);
					removeClass(this, "highlight");
					this.focus = false;
				}
			};
			
			field.onmouseover = function() {
				addClass(this, 'highlight');
			};
			field.onmouseout = function() {
				removeClass(this, "highlight");
			};
			
			// Handle clicks to label elements (for Safari).
			labels[i].onclick = function () {
				var id, field;
				id = this.getAttribute('for');
				if (id && (field = document.getElementById(id))) {
					field.focus();
				}
			};

		}
	}
};

function hideLabel (field_id, hide) {
	var field_for;
	var labels = document.getElementsByTagName('label');
	for (var i = 0; i < labels.length; i++) {
		field_for = labels[i].htmlFor || labels[i].getAttribute('for');
		if (field_for == field_id) {
			labels[i].style.textIndent = (hide) ? '-1000px' : '0px';
			return true;
		}
	}
}

addLoadEvent(setTimeout(initOverLabels, 50));
// end of hiding and showing overlobels.

// begin hover and focus functions

function initForm () {
	var inputs = document.getElementsByTagName('input');
	var textinputs = new Array();
	var textareas = document.getElementsByTagName('textarea');
	
	for (var i=0; i < inputs.length; i++) {
		if (inputs[i].getAttribute("type") == "text") {
			textinputs.push(inputs[i]);
		}
	}
	
	formHoverFocus(textinputs, "hover", "focus");
	formHoverFocus(textareas, "hover", "focus");
}

function formHoverFocus(elems_array, hover_class, focus_class) {
	for (var i=0; i < elems_array.length; i++) {
		elems_array[i].onmouseover = function() {
			if (!findWord(hover_class, this.className)) {
				addClass(this, hover_class);
			}
		}

		elems_array[i].onmouseout = function() {
			if (findWord(hover_class, this.className)) {
				removeClass(this, hover_class);
			} else { 
				return; 
			}
		}

		elems_array[i].onfocus = function() {
			if (!findWord(focus_class, this.className)) {
				addClass(this, focus_class);
			}
		}

		elems_array[i].onblur = function() {
			if (findWord(focus_class, this.className)) {
				removeClass(this, focus_class);
			} else {
				return;
			}
		}
	}
}

// addLoadEvent(initForm);

// end hover and focus functions


// The following functions are for form validation.
