/*
Add/Remove classes
*/
function addClass(aClass,obj) {
	if (obj.className.indexOf(aClass) != -1) return;
	obj.className = obj.className + ' ' + aClass;
}
function removeClass(aClass,obj) {
	aClass = (obj.className.indexOf(' ') != -1) ? (' ' + aClass) : aClass;
	obj.className = obj.className.replace(aClass,'');
}

/*
Find the form element labeled by a particular label
*/
function getLabeled(aLabel) {
	return document.getElementById(aLabel.getAttribute('for'));
}

/*
Change cursor
*/
function cursor(obj,cursorStyle) {
	if (!cursorStyle) cursorStyle = 'default';
	obj.style.cursor = cursorStyle;
}

/*
Form behaviors: mostly makes up for lack of similar functionality via 
CSS in Internet Explorer
*/
function forms() {
	
	var labels = document.getElementsByTagName('label');
	if (labels) {
		for (var i=0; i<labels.length; i++) {
			
			labels[i].onmouseover = function() {
				cursor(this,'pointer');
				addClass('hover',this);
				addClass('hilite',getLabeled(this));
			};
			labels[i].onmouseout = function() {
				cursor(this);
				removeClass('hover',this);
				removeClass('hilite',getLabeled(this));
			};
			labels[i].onfocus = function() {
				addClass('focus',getLabeled(this));
			}
		}
	}
	
	var inputs = document.getElementsByTagName('input');
	if (inputs) {
		for (var i=0; i<inputs.length; i++) {
			
			var type = inputs[i].getAttribute('type');
			
			// buttons
			if (type == 'submit' || type == 'button' || type == 'reset') {
				addClass('button',inputs[i]);
				inputs[i].onmouseover = function() {
					cursor(this,'pointer');
					addClass('hover',this);
				};
				inputs[i].onmouseout = function() {
					cursor(this);
					removeClass('hover',this);
				};
			}
			
			// text/password fields
			if (type == 'text' || type == 'password') {
				inputs[i].onfocus = function() {
					addClass('focus',this);
				};
				inputs[i].onblur = function() {
					removeClass('focus',this);
				};
				if (inputs[i].getAttribute('title') != '' && inputs[i].getAttribute('title') != null) {
					inputs[i].onmouseover = function() {
						cursor(this,'help');
					};
				}
			}
			
		} // end for
	}
	
	var textareas = document.getElementsByTagName("textarea");
	if (textareas) {
		for (var i=0; i<textareas.length; i++) {
			textareas[i].onfocus = function() {
				addClass('focus',this);
			};
			textareas[i].onblur = function() {
				removeClass('focus',this);
			};
			if (textareas[i].getAttribute('title') != '' && textareas[i].getAttribute('title') != null) {
				textareas[i].onmouseover = function() {
					cursor(this,'help');
				};
			}
		}
	}	
}


/*
Table behaviors: same reason as form behaviors
*/
function tables() {
	var tbodies = document.getElementsByTagName('tbody');
	for (var b=0; b<tbodies.length; b++) {
		var rows = tbodies[b].getElementsByTagName('tr');
		for (var r=0; r<rows.length; r++) {
			rows[r].onmouseover = function() {
				addClass('hover',this);
			};
			rows[r].onmouseout = function() {
				removeClass('hover',this);
			};
		}
	}
}



/*
Attach events
*/
if (typeof window.addEventListener != 'undefined') {
	window.addEventListener('load',forms,false);
	window.addEventListener('load',tables,false);
} else if (typeof document.addEventListener != 'undefined') {
	document.addEventListener('load',forms,false);
	document.addEventListener('load',tables,false);
} else if (typeof window.attachEvent != 'undefined') {
	window.attachEvent('onload',forms);
	window.attachEvent('onload',tables);
}

