function hasClass(el,cls) {
	return el.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
function addClass(el,cls) {
	if (!this.hasClass(el,cls)) el.className += " "+cls;
}
function removeClass(el,cls) {
	if (hasClass(el,cls)) {
		var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		el.className = el.className.replace(reg,' ');
	}
}
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}
function initFormPlaceholders() {
	if (!document.getElementsByTagName) return true;
	ourForms = document.getElementsByTagName('form');
	var numForms = ourForms.length;
	for (var i=0;i<numForms;i++) {
		var numFormElements = ourForms[i].elements.length;
		for (var j=0;j<numFormElements;j++) {
			var el = ourForms[i].elements[j];
			if (el.type == "submit") continue;
			if ((el.type == "text") || (el.type == "password") || (el.type == "textarea")) {
				var ourClassName = el.className;
				if (ourClassName.match('auto-select') || ourClassName.match('placeholder') || ourClassName.match('populate')) {
					if (el.value == '') {
						addClass(el, "placeholder");
						el.value = el.title;
					}
				}
				if (el.className.match('auto-select')) {
					el.onfocus = function () {
						if (this.value == this.title) {
							removeClass(this, "placeholder");
							this.select();
						}
					}
					if (el.captureEvents) el.captureEvents(Event.FOCUS);
					el.onblur = function () {
						if (this.value == '') {
							this.value = this.title;
						}
						if (this.value == this.title) {
							addClass(this, "placeholder");
						}
					}
					if (el.captureEvents) el.captureEvents(Event.BLUR);
				}
				else if (el.className.match('placeholder')) {
					el.onfocus = function () {
						if (this.value == this.title) {
							removeClass(this, "placeholder");
							this.value = '';
						}
					}
					if (el.captureEvents) el.captureEvents(Event.FOCUS);
					el.onblur = function () {
						if (this.value == '') {
							this.value = this.title;
						}
						if (this.value == this.title) {
							addClass(this, "placeholder");
						}
					}
					if (el.captureEvents) el.captureEvents(Event.BLUR);
				}
			}
		}
	}
}
addLoadEvent(initFormPlaceholders);
