/*

Project:	Input Placeholder Text
Title:		Automatic population of form fields with contents of title attributes
Created:	13 August 2005 by Jon Gibbins (aka dotjay)
Modified:	02 March 2006 by Jon Gibbins (aka dotjay)

Notes:

Add the following classes to text inputs or textareas to get the desired behaviour:

auto-select
	Will pre-populate the input with the title attribute and select the text when it receives focus.

auto-clear
	Will pre-populate the input with the title attribute and clear the text when it receives focus.
	Note: if auto-select and auto-clear are set, auto-select takes precedence.

populate
	Will just populate the input with the title attribute.

*/

window.onload = function () {

	if (!document.getElementsByTagName) return true;

	ourForms = document.getElementsByTagName('form');

	// go through each form
	for (var i=0;i<ourForms.length;i++) {

		// go through each form element
		for (var j=0;j<ourForms[i].elements.length;j++) {
		
			// if we got a text type input
			if (ourForms[i].elements[j].type == "text") {
				// only populate if we want it to
				// note: might want title attribute but no pre-population of inputs
				var ourClassName = ourForms[i].elements[j].className;
				if (ourClassName.match('auto-select') || ourClassName.match('auto-clear') || ourClassName.match('populate')) {
					// only populate if empty
					if (ourForms[i].elements[j].value == '') ourForms[i].elements[j].value = ourForms[i].elements[j].title;
				}

				// add auto select if class contains auto-select
				// note: else if below so auto-select takes precedence (assuming select is better than clear)
				if (ourForms[i].elements[j].className.match('auto-select')) {
					ourForms[i].elements[j].onfocus = function () {
						if (this.value == this.title) this.select();
					}
					if (ourForms[i].elements[j].captureEvents) ourForms[i].elements[j].captureEvents(Event.FOCUS);
				}

				// add auto clear if class contains auto-clear
				else if (ourForms[i].elements[j].className.match('auto-clear')) {
					ourForms[i].elements[j].onfocus = function () {
						if (this.value == this.title) this.value = '';
					}
					if (ourForms[i].elements[j].captureEvents) ourForms[i].elements[j].captureEvents(Event.FOCUS);

					ourForms[i].elements[j].onblur = function () {
						if (this.value == '') this.value = this.title;
					}
					if (ourForms[i].elements[j].captureEvents) ourForms[i].elements[j].captureEvents(Event.BLUR);
				}
			}

			// if we got a textarea
			if (ourForms[i].elements[j].type == "textarea") {
				// only populate if we want it to
				// note: might want title attribute but no pre-population of inputs
				var ourClassName = ourForms[i].elements[j].className;
				if (ourClassName.match('auto-select') || ourClassName.match('auto-clear') || ourClassName.match('populate')) {
					// only populate if empty
					if (ourForms[i].elements[j].value == '') ourForms[i].elements[j].value = ourForms[i].elements[j].title;
				}

				// add auto select if class contains auto-select
				if (ourForms[i].elements[j].className.match('auto-select')) {
					ourForms[i].elements[j].onfocus = function () {
						if (this.value == this.title) this.select();
					}
					if (ourForms[i].elements[j].captureEvents) ourForms[i].elements[j].captureEvents(Event.FOCUS);
				}

				// add auto clear if class contains auto-clear
				else if (ourForms[i].elements[j].className.match('auto-clear')) {
					ourForms[i].elements[j].onfocus = function () {
						if (this.value == this.title) this.value = '';
					}
					if (ourForms[i].elements[j].captureEvents) ourForms[i].elements[j].captureEvents(Event.FOCUS);

					ourForms[i].elements[j].onblur = function () {
						if (this.value == '') this.value = this.title;
					}
					if (ourForms[i].elements[j].captureEvents) ourForms[i].elements[j].captureEvents(Event.BLUR);
				}
			}

		}

	}

}

