//=====================================================================================
// project : placeholder
// version : 0.1
// author  : Joseph R. Hughes Jr.
// date	   : 10/30/08
// usage   : add the attribute of placeholder="your text here" to any text input
//	the available styles for placeholder are utilized by changing the 
//	class type i.e. class="placeholder-clear" ... valid class types are 
//	as follows: palceholdter-select, placeholder-clear, placeholder-populate
// todo    : add event listener to submit and clear out all 
//	inputs with a placeholder attached to them
//=====================================================================================

window.onload = function () {

	if (!document.getElementsByTagName) return true;

	el_forms = document.getElementsByTagName('form');

	var forms = el_forms.length;
	for (var i=0;i<forms;i++) {
		var el_length = el_forms[i].elements.length;
		for (var j=0;j<el_length;j++) {
			var el = el_forms[i].elements[j];

			if (el.type == "submit") continue; // ignore submits

			if (el.type == "text") {
				var classname = el.className;
				
				if (classname.match('placeholder-select') || classname.match('placeholder-clear') || classname.match('placeholder-placeholder-populate')) {
					if (el.value == '') {
						el.style.color = '#ccc';
						el.value = el.getAttribute('placeholder');
					}
				}				
				if (el.className.match('placeholder-select')) {
					el.onfocus = function () {
						if (this.value == this.getAttribute('placeholder')) {
							this.select();
						}
					}
					if (el.captureEvents) el.captureEvents(Event.FOCUS);
				}
				else if (el.className.match('placeholder-clear')) {
					el.onfocus = function () {
						if (this.value == this.getAttribute('placeholder')) { 
							this.style.color = '#000';
							this.value = '';
						}
					}
					if (el.captureEvents) el.captureEvents(Event.FOCUS);

					el.onblur = function () {
						if (this.value == '') { 
							this.style.color = '#ccc';
							this.value = this.getAttribute('placeholder'); 
						}
					}
					if (el.captureEvents) el.captureEvents(Event.BLUR);
				}
			}
			
			if (el.type == "textarea") {
				var classname = el.className;
				if (classname.match('placeholder-select') || classname.match('placeholder-clear') || classname.match('placeholder-populate')) {
					if (el.value == '') {
						el.style.color = '#ccc';
						var raw = el.getAttribute('placeholder');
						var raw2 = raw.replace("<br>", "\n");
						el.value = raw2;
					}
				}

				if (el.className.match('placeholder-select')) {
					el.onfocus = function () {
						if (this.value == this.getAttribute('placeholder')) 
							this.select();
					}
					if (el.captureEvents) el.captureEvents(Event.FOCUS);
				}

				else if (el.className.match('placeholder-clear')) {
					el.onfocus = function () {
						if (this.value == this.getAttribute('placeholder')) {
							this.style.color = "#000";
							this.value = '';
						}
					}
					if (el.captureEvents) el.captureEvents(Event.FOCUS);

					el.onblur = function () {
						if (this.value == '') {
							this.style.color = '#ccc';
							this.value = this.getAttribute('placeholder');
						}
					}
					if (el.captureEvents) el.captureEvents(Event.BLUR);
				}
			}
		}
	}
}
