﻿/**
FORM script
Author: Matthew Butt
Version: 2.1
Date: 09-11-2005
Description:
	This script is intended as a complete and centralised set of form-processing scripts.
To do:
	Include scripting to dynamically remove default class from elements once they no longer show the default value. Apply this to select elements.
*/
function clearValue(){
	// Called by various form elements on focus
	if(this.value == this.origValue) {
		// If this field still shows its original value, clear it
		this.value = "";
	}
}
function resetValue(){
	// Called by various form elements on blur
	if(this.value == "") {
		// If the field is left empty, reinstate the original value
		this.value = this.origValue;
	} 
}
function formInit(){
	// Process INPUT tags
	// Get all inputs
	var inputs = document.getElementsByTagName("input");
	// Loop over inputs
	for(var i=0;i<inputs.length;i++){
		// Check that the input is of type text, is marked as containing the default value and is not protected
		if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf("default") >= 0 && inputs[i].className.indexOf("protect") < 0){
			// Save a copy of the original value
			inputs[i].origValue = inputs[i].value;
			// Set onfocus action
			inputs[i].onfocus = clearValue;
			// Set onblur action
			inputs[i].onblur = resetValue;
		}
	}
	// Process TEXTAREA tags
	// Get all textareas
	var textareas = document.getElementsByTagName("textarea");
	// Loop over textareas
	for(var i=0;i<textareas.length;i++){
		// Check that the textarea is marked as containing the default value and is not protected
		if(textareas[i].className.indexOf("default") >= 0 && textareas[i].className.indexOf("protect") < 0){
			// Save a copy of the original value
			textareas[i].origValue = textareas[i].value;
			// Set onfocus action
			textareas[i].onfocus = clearValue;
			// set onblur action
			textareas[i].onblur = resetValue;
		}
	}
}
