/*
 * Set the user agent
 */
if (navigator.userAgent.indexOf('MSIE 6') > 0) {
	window.userAgent = 'IE6';
} else if (navigator.userAgent.indexOf('MSIE 7') > 0) {
	window.userAgent = 'IE7';
} else if (navigator.userAgent.indexOf('MSIE 8') > 0) {
	window.userAgent = 'IE8';
} else if (navigator.userAgent.indexOf('Firefox') > 0) {
	window.userAgent = 'FF';
} else if (navigator.userAgent.indexOf('Chrome') > 0) {
	window.userAgent = 'GC';
} else if (navigator.userAgent.indexOf('Opera') >= 0) {
	window.userAgent = 'O';
}

/**
 * Get an element from the document.
 * 
 * @param id
 * @return element
 */
function $(id) {
	return document.getElementById(id);
}

/**
 * Get elements by name.
 * 
 * @param name
 * @return array elements
 */
function $name (name) {
	return document.getElementsByName(name);
}

/**
 * Append a child element to a parent. Return the parent when done.
 * 
 * @param parent
 * @param child
 * @return parent
 */
function ac (parent, child) {
	parent.appendChild(child);
	return parent;
}

/**
 * Create an element and return it.
 * 
 * @param tag
 * @return element
 */
function ce (tag) {
	return document.createElement(tag);
}

/**
 * Create a text node and return it.
 * 
 * @param text
 * @return node
 */
function ctn (text) {
	return document.createTextNode(text);
}

/**
 * Go to a URL.
 * 
 * @param url
 * @return void
 */
function gotoURL (url) {
	window.location = url;
}

/**
 * Get a value from an array of form elements (like radio buttons).
 * 
 * @param array
 * @return value
 */
function getRadioValFromArray (array) {
	for (var i = 0; i < array.length; ++i) {
		if (array[i].checked) {
			return array[i].value;
		}
	}
	
	return null;
}

/**
 * Check an input element. Cross-browser support.
 * @param element
 * @return void
 */
function check (event, element) {
	if (element == null || element == undefined) {
		return false;
	}
	
	event = event || window.event;
	if (!element.disabled) {
		if (navigator.appVersion.indexOf('MSIE 6') > 0) {
			if (event.srcElement.tagName != 'INPUT' || event.srcElement.getAttribute('type').toUpperCase() != 'CHECKBOX') {
				if (element.getAttribute('type').toUpperCase() == 'CHECKBOX') {
					if (element.checked) {
						element.checked = false;
					} else {
						element.checked = true;
					}
				} else {
					element.checked = true;
				}
			}
		} else {
			var el = event.target || event.srcElement;
			var t = (el.type ? el.type.toUpperCase() : null);
			if (el != element || t == 'CHECKBOX') {
				return false;
			}
			if (element.getAttribute('type').toUpperCase() == 'CHECKBOX') {
				if (element.checked) {
					element.checked = '';
				} else {
					element.checked = "CHECKED";
				}
			} else {
				element.checked = "CHECKED";
			}
		}
	}
	
	return false;
}

/**
 * Load a help text blurb overtop of an element in the DOM tree.
 * 
 * @param parent Containing node which is the container for the mouse; if the mouse leaves this node the help blurb is hidden
 * @param hoverover The help text is displayed directly overtop of this node
 * @param text Help text
 * @return void
 */
function loadHoverHelp (parent, hoverover, text) {
	var a = [0,0];
	getCoords(hoverover, a);
	var div = $('helpBlurb');
	if (!div) {
		div = ce('div');
		div.id = 'helpBlurb';
		div.style.opacity = '0.01';
		div.style.filter = 'alpha(opacity=1)';
		div.className = 'helpBlurb';
		div.style['position'] = 'absolute';
		document.body.appendChild(div);
	} else {
		div.innerHTML = '';
		div.style.width = '';
		div.style.opacity = '.01';
		div.style.filter = 'alpha(opacity=1)';
	}
	div.innerHTML = text;
	div.style.display = 'block';
	div.style.width = '300px';
	div.style.top = (a[1] - div.offsetHeight) + 'px';
	div.style.left = a[0] + "px";
	div.style.opacity = '1';
	div.style.filter = 'alpha(opacity=100)';
	
	document.onmousemove = function (event) { event = event || window.event; observeHoverHelp (event, parent, hoverover, div); };
}

/**
 * Observe the movement of the mouse and make sure that the help blurb is only displayed when the mouse is in the proper parent node.
 * 
 * @param event Event object
 * @param parent Containing node which is the container for the mouse; if the mouse leaves this node the help blurb is hidden
 * @param element The help text is displayed directly overtop of this node
 * @param help text to be displayed
 * @return void
 */
function observeHoverHelp (event, parent, element, help) {
	event = event || window.event;
	
	if (!isInHoverElement(parent, event.target || event.srcElement)) {
		help.style.display = 'none';
		document.onmousemove = null;
	}
}

/**
 * Checks whether or not the mouse is in the parent node.
 * 
 * @param parent Parent containing node.
 * @param element Source/Target element which generated the event.
 * @return boolean true if inside, false otherwise.
 */
function isInHoverElement (parent, element) {
	if (element == parent) {
		return true;
	}
	
	if (element.parentNode) {
		return isInHoverElement(parent, element.parentNode);
	} else {
		return false;
	}
	
	if (element.tagName.toUpperCase() == 'BODY') {
		return false;
	}
}

/**
 * Get the left offset of an element.
 * 
 * @param Element e
 * @return Offset value
 */
function getLeft (e) {
	if (e.offsetParent) {
		return e.offsetLeft - e.scrollLeft + getLeft(e.offsetParent);
	}
	
	return e.offsetLeft;
}

/**
 * Get the top offset of an element.
 * 
 * @param Element e
 * @return Offset value
 */
function getTop (e) {
	if (e.offsetParent) {
		return e.offsetTop - e.scrollTop + getTop(e.offsetParent);
	}
	
	return e.offsetTop;
}

/**
 * Get the coords of an element.
 * @param e
 * @param a
 * @return
 */
function getCoords (e, a) {
	getCoordsR(e,a);
	if (window.userAgent == 'O') {
		a[0] -= window.scrollX;
		a[1] -= window.scrollY;
	}
	return a;
}

function getCoordsR (e,a) {
	//alert (e.tagName);
	a[0] += e.offsetLeft - e.scrollLeft;
	a[1] += e.offsetTop - e.scrollTop;
	if (e.offsetParent && e.offsetParent.tagName != 'BODY' && e.offsetParent.tagName != 'HTML') {
		return getCoordsR(e.offsetParent, a);
	}
	return;
}
