/******************************************************************
* Determines whether or not the browser is supported.  The browser
* must support the DOM (Document Object Model) standard.
******************************************************************/
function isSupportedBrowser()
{
  return (document.getElementById ? true : false);
}

/******************************************************************
* Because Microsoft still has a difficult time following rules and
* standards, this method is required to retrieve an event source.
******************************************************************/
function getEventSource(e)
{
  e = e || window.event;
  return e.target || e.srcElement;
}

/****************************************************************************
* Microsoft still has a difficult time following rules and standards, this
* method is required to create a universal way to handle events in browsers.
****************************************************************************/
function getEvent(e)
{
  e = e || window.event;
  e.aim = e.target || e.srcElement;

  if (!e.preventDefault)
    e.preventDefault = function() { return false; }

  if (!e.stopPropagation)
    e.stopPropagation = function() { if (window.event) window.event.cancelBubble = true; }

  return e;
}

/*****************************************************************************
* There is no standard for the parentElement issue so have to do it this way.
*****************************************************************************/
function getParentElement(oElement)
{
  return oElement.parentElement || oElement.parentNode;
}

/*****************************************
* Attaches an event-handler to an object.
*****************************************/
function addEvent(oTarget, sEvent, fpEventHandler, bBubble)
{
  bBubble = bBubble || false;
  
  if (oTarget.addEventListener)
    oTarget.addEventListener(sEvent, fpEventHandler, bBubble);
  else if (oTarget.attachEvent)
    oTarget.attachEvent("on" + sEvent, fpEventHandler);
}

/******************************************
* Removes an event-handler from an object.
******************************************/
function removeEvent(oTarget, sEvent, fpEventHandler, bBubble)
{
  bBubble = bBubble || false;
  
  if (oTarget.removeEventListener)
    oTarget.removeEventListener(sEvent, fpEventHandler, bBubble);
  else if (oTarget.detachEvent)
    oTarget.detachEvent(sEvent, fpEventHandler);
}

/********************************
* Gets the key that was pressed.
********************************/
function getKeyCode(e)
{
  e = e || window.event;
  return e.keyCode || e.which;
}

/***************************************
* Gets the width of the browser window.
***************************************/
function getWindowWidth()
{
  return document.documentElement.clientWidth || window.innerWidth || document.body.clientWidth;
}

/****************************************
* Gets the height of the browser window.
****************************************/
function getWindowHeight()
{
  return document.documentElement.clientHeight || window.innerHeight || document.body.clientHeight;
}

/*******************************************************************
* Gets the X position of the mouse cursor relative to the document.
*******************************************************************/
function getMouseX(e)
{
  e = e || window.event;
  return e.pageX || e.x || 0;
}

/*******************************************************************
* Gets the Y position of the mouse cursor relative to the document.
*******************************************************************/
function getMouseY(e)
{
  e = e || window.event;
  return e.pageY || e.y || 0;
}

/******************************************************************
* Gets the X position of the mouse cursor relative to the element.
******************************************************************/
function getMouseOffsetX(e)
{
  e = e || window.event;
  return e.offsetX || 0;
}

/******************************************************************
* Gets the Y position of the mouse cursor relative to the element.
******************************************************************/
function getMouseOffsetY(e)
{
  e = e || window.event;
  return e.offsetY || 0;
}

/*****************************************************************
* Gets the X position of the mouse cursor relative to the window.
*****************************************************************/
function getMouseWindowX(e)
{
  e = e || window.event;
  return e.clientX || 0;
}

/*****************************************************************
* Gets the Y position of the mouse cursor relative to the window.
*****************************************************************/
function getMouseWindowY(e)
{
  e = e || window.event;
  return e.clientY || 0;
}

/********************************
* Gets the height of an element.
********************************/
function getElementHeight(oElement)
{
  return oElement.style.height || oElement.scrollHeight || oElement.clientHeight;
}

/*******************************
* Gets the width of an element.
*******************************/
function getElementWidth(oElement)
{
  return oElement.style.width || oElement.scrollWidth || oElement.clientWidth;
}

/**************************************************************
* Gets the X position of the element relative to offsetParent.
**************************************************************/
function getOffsetLeft(oElement)
{
  var iOffsetLeft = 0;

  if (oElement.offsetParent)
  {
    iOffsetLeft = oElement.offsetLeft;
    
    while (oElement = oElement.offsetParent)
      iOffsetLeft += oElement.offsetLeft;
  }
  
  return iOffsetLeft;
}

/**************************************************************
* Gets the Y position of the element relative to offsetParent.
**************************************************************/
function getOffsetTop(oElement)
{
  var iOffsetTop = 0;

  if (oElement.offsetParent)
  {
    iOffsetTop = oElement.offsetTop;
    
    while (oElement = oElement.offsetParent)
      iOffsetTop += oElement.offsetTop;
  }
  
  return iOffsetTop;
}

/*******************************
* Gets the text of the element.
*******************************/
function getElementText(oElement)
{
  if (oElement.innerText)
    oElement.innerText;
  else
  {
    for (var iNodes = 0; iNodes < oElement.childNodes.length; iNodes++)
    {
      if (oElement.childNodes[iNodes].nodeType == 3)
        return oElement.childNodes[iNodes].nodeValue;
    }
    
    return null;
  }
}

/*******************************
* Gets the text of the element.
*******************************/
function setElementText(oElement, sValue)
{
  if (oElement.innerText)
    oElement.innerText = sValue;
  else if (!oElement.hasChildNodes())
    oElement.appendChild(document.createTextNode(sValue));
  else
  {
    for (var iNodes = 0; iNodes < oElement.childNodes.length; iNodes++)
    {
      if (oElement.childNodes[iNodes].nodeType == 3)
        oElement.childNodes[iNodes].nodeValue = sValue;
    }
    
    oElement.insertBefore(document.createTextNode(sValue), oElement.firstChild);
  }
}