//*****************************************************************************
// Commonly used code and functions.
//*****************************************************************************

// This code is necessary for browsers that don't reflect the DOM constants.
if (document.ELEMENT_NODE == null)
{
	document.ELEMENT_NODE = 1;
	document.TEXT_NODE    = 3;
}

//=============================================================================
// Code to add/remove style classes to elements.
//=============================================================================

//-----------------------------------------------------------------------------
// Returns true if the given element currently has the specified style class.
//-----------------------------------------------------------------------------
function hasClassName(el, name)
{
	var list = el.className.split(" ");
	for (var i = 0; i < list.length; i++)
		if (list[i] == name)
			return true;

	return false;
}

//-----------------------------------------------------------------------------
// Adds the specified class name to the given element.
//-----------------------------------------------------------------------------
function addClassName(el, name)
{
	if (!hasClassName(el, name))
		el.className += (el.className.length > 0 ? " " : "") + name;
}

//-----------------------------------------------------------------------------
// Removes the specified class name from the given element.
//-----------------------------------------------------------------------------
function removeClassName(el, name)
{
	if (el.className == null)
		return;

	var newList = new Array();
	var curList = el.className.split(" ");
	for (var i = 0; i < curList.length; i++)
		if (curList[i] != name)
			newList.push(curList[i]);
	el.className = newList.join(" ");
}

//=============================================================================
// Code for positioning elements.
//=============================================================================

//-----------------------------------------------------------------------------
// Returns the coordinates of the given element relative to the page.
//-----------------------------------------------------------------------------
function getPageOffset(el)
{
	// Find the page coordinates of the element.
	var x = 0, y = 0;

	// For IE, add up the border widths of any containing elements (except for
	// BODY, HTML and TABLE tags).
	if (document.all != null && window.opera == null)
	{
		var tempEl = el;
		while (tempEl != null && tempEl.tagName != null) {
			if (tempEl.tagName != "BODY" && tempEl.tagName != "HTML" && tempEl.tagName != "TABLE")
			{
				x += tempEl.clientLeft;
				y += tempEl.clientTop;
			}
			tempEl = tempEl.parentNode;
		}
	}

	// Add up the left and top offsets of all positioned containing elements.
	do
	{
		x += el.offsetLeft;
		y += el.offsetTop;
		el = el.offsetParent;
	} while (el != null);

	// Return the coordinates.
	return new Point(x, y);
}

//-----------------------------------------------------------------------------
// Defines an object for holding x- and y- coordinates.
//-----------------------------------------------------------------------------
function Point(x, y)
{
	// Set the point coordinates.
	this.x = x;
	this.y = y;
}
