
/**
Provides 'auto-hiding' default values in text and textarea fields.
The default values are obtained from the HTML using the alt attribute.  When
this script is run, default values are added to every form text input and
text area element that has an alt attribute.

Adding default values is as simple as including this file and adding non-empty
alt tags to your text input and textarea elements.
*/
(function()
{
	// Initialisation -- have init() run once page has finished loading
	if( window.addEventListener)
		window.addEventListener('load', init, false);
	else if( window.attachEvent)
		window.attachEvent('onload', init);

	/**
	Add event handlers to all form elements in document that have alt attributes
	*/
	function init()
	{
		// Loop through all forms in document
		for( var i=0; i<document.forms.length; i++)
		{
			needsValidation = false;
			var form = document.forms[i];

			// Loop through elements in current form
			for( var j=0; j < form.elements.length; j++ )
			{
				var elem = form.elements[j];

				// We care about text, and Textarea elements
				if( elem.type == 'text' || elem.type == 'textarea')
				{
					var alt = elem.getAttribute('alt');
					if( alt )
					{
						// Add default text (if empty)
						if( ! elem.value )
							elem.value = alt;

						// Install event handlers
						elem.onfocus = onFocusHandler;
						elem.onblur = onBlurHandler;
					}
				}
			}
		}
	}

	/**
	Remove default text from form element on focus
	*/
	function onFocusHandler()
	{
		var alt = this.getAttribute('alt');
		if( this.value == alt)
			this.value = '';
	}

	/**
	Restore default text on form element on blur, if empty
	*/
	function onBlurHandler()
	{
		var alt = this.getAttribute('alt');
		if( ! this.value )
			this.value = alt;
	}

})();
