/******************************************
Name:			base.js
Description:	Base Javascript file
Date:			07/06/2006
Author:			James Condliffe
******************************************/

/****
Used for executing multiple functions on page load.
****/
function addLoadEvent(func)
{
	var oldonload = window.onload;

	if (typeof window.onload != 'function')
		window.onload = func;
	else
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}

/****
Disables event bubbling on the passed object
****/
function stopPropagation(e) 
{ 
    e = e || event;/* get IE event ( not passed ) */ 
    e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true; 
}

/****
Creates a message window on the document and displays the message.
Used for debugging
****/
function message(msgstring)
{
	// Create if it's the first use
	if (!document.getElementById("msgdiv"))
	{
		// Create message div
		var msgdiv = document.createElement("div");
		msgdiv.setAttribute("id","msgdiv");

		// Append to document
		var docbody = document.getElementsByTagName("body");
		docbody[0].appendChild(msgdiv);
		
		// Add styling
		msgdiv.style.position = "absolute";
		msgdiv.style.top = "0";
		msgdiv.style.right = "0";
		msgdiv.style.border = "1px solid black";
		msgdiv.style.backgroundColor = "#CCCCCC";
		msgdiv.style.minWidth = "200px";
		msgdiv.style.maxWidth = "300px";
		msgdiv.style.padding = "5px";
		msgdiv.style.overflow = "scroll";
		msgdiv.style.maxHeight = "300px";

		// Add a heading
		msgdiv.innerHTML = "<h4 style=\"margin:3px;\">Javascript Logging</h4>";
	}
	
	// Update the message div
	document.getElementById("msgdiv").innerHTML += msgstring + "<br />";
}


/****
Returns the height in pixels of the entire document
****/
function getDocumentHeight()
{	
	var docHeight;

	if (typeof document.height != 'undefined' && document.height != 0)
	{
		docHeight = document.height;
	}
	else if (document.compatMode && document.compatMode != 'BackCompat') 
	{
		docHeight = document.documentElement.scrollHeight;
	}
	else if (document.body && typeof document.body.scrollHeight != 'undefined')
	{
		docHeight = document.body.scrollHeight;
	}

	return docHeight;
}


/****
Returns an array of elements specified by the parameters

Written by Jonathan Snook, http://www.snook.ca/jonathan
Add-ons by Robert Nyman, http://www.robertnyman.com
****/
function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements);
}


/****
Return the Y position of an element on the page
****/
function findPosY(obj) 
{
    var curtop = 0;
    if (obj.offsetParent) 
    {
        while (obj.offsetParent) 
        {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

/****
Return the X position of an element on the page
****/
function findPosX(obj) 
{
  var curleft = 0;
  if (obj.offsetParent) 
  {
    while (obj.offsetParent) 
        {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}