//******************************************************************************************//
// A browser-aware function to get an HTML element by its ID.
//******************************************************************************************//
function GetElementById(e) {
	if (typeof(e) != "string")
		return e;
	if (document.getElementById)
		e = document.getElementById(e);
	else if (document.all)
		e = document.all[e];
	else
		e = null;
	return e;
}

//******************************************************************************************//
// A utility function to return an array of elements with the specified class.
//******************************************************************************************//
function getElementsByClass(searchClass, node, tag) {
	var classElements = new Array();
	if (node == null)
		node = document;
	if (tag == null)
		tag = "*";
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
	for (var i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

//******************************************************************************************//
// A function to add a function to the window.onload event handler "stack".
//******************************************************************************************//
function addLoadEvent(func) {
	var oldLoad = window.onload;
	if (typeof window.onload != "function") {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldLoad();
			func();
		}
	}
}

//******************************************************************************************//
// A function to add a function to the window.onresize event handler "stack".
//******************************************************************************************//
function addResizeEvent(func) {
	var oldResize = window.onresize;
	if (typeof window.onresize != "function") {
		window.onresize = func;
	}
	else {
		window.onresize = function() {
			oldResize();
			func();
		}
	}
}

//******************************************************************************************//
// A function to retrieve the named cookie from the request.  Returns an empty string if
// the cookie is not found.
//******************************************************************************************//
function getCookie(c_name)
{
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) { 
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1) c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start, c_end));
		}
	}
	return "";
}

//******************************************************************************************//
// This function returns the current width of the browser "window".
//******************************************************************************************//
function getWindowWidth() {
	var size = 0;
	if (document.body && document.body.clientWidth)
		size = document.body.clientWidth;
	else if (window.innerWidth && window.innerWidth > 0) 
		size = window.innerWidth;
	return size;	
}

//******************************************************************************************//
// This function returns the current width of an HTML element.  If the element cannot be
// found then this function returns the current browser "window" width.
//******************************************************************************************//
function getElementWidth(id) {
	var size = 0;
	if (document.body && document.body.clientWidth)
		size = document.body.clientWidth;
	else if (window.innerWidth && window.innerWidth > 0) 
		size = window.innerWidth;
		
	var content = document.getElementById(id);
	if (content && content.clientWidth && content.clientWIdth > 0)
		size = content.clientWidth;
		
	return size;	
}

//******************************************************************************************************//
// Utility function to set a global variable if it is not already set.  
// WARNING!!! Any variable set with this function will be visible to the entire page.
//******************************************************************************************************//
function setDefault(name, val) {
	if (typeof(window[name]) == "undefined" || window[name] == null) {
		window[name] = val;
	}
}

//******************************************************************************************************//
// A browser-aware function to cancel an event (prevent the
// event from propagating).
//******************************************************************************************************//
function cancel(e)
{
	if (e && e.preventDefault)
	{
		e.preventDefault(); // DOM style
	}
	else if (window.event)
	{
		window.event.returnValue = false;
		window.event.cancelBubble = true;
	}	
	return false; // IE style
}

