   // tnx to David Andersson <http://liorean.web-graphics.com/>
function getIFrameDocRef(ref)
{
   return (typeof ref.contentDocument != 'undefined')
          ? ref.contentDocument
          : (typeof ref.contentWindow != 'undefined')
             ? ref.contentWindow.document
             : (typeof ref.document != 'undefined')
                ? ref.document
                : null;
}

   // for use with event.keyCode
function getKey(keyCode)
{
   switch(keyCode)
   {
      case  27: return 'escape';

      case 112: return 'f1';
      case 113: return 'f2';
      case 114: return 'f3';
      case 115: return 'f4';
      case 116: return 'f5';
      case 117: return 'f6';
      case 118: return 'f7';
      case 119: return 'f8';
      case 120: return 'f9';
      case 121: return 'f10';
      case 122: return 'f11';
      case 123: return 'f12';

      case  19: return 'pause';
      case  44: return 'printscreen';
      case 145: return 'scrolllock';

      case   8: return 'backspace';
      case  45: return 'insert';
      case  46: return 'delete';
      case  36: return 'home';
      case  35: return 'end';
      case  33: return 'pageup';
      case  34: return 'pagedown';

      case  37: return 'left';
      case  38: return 'up';
      case  39: return 'right';
      case  40: return 'down';

      case  13: return 'enter';
      case  20: return 'capslock';
      case   9: return 'tab';
      case  32: return 'space';

      default:  return null;
   }
}

function stopClick(event)
{
   if (BrowserDetect.browser == 'explorer')
   {
      getXploderEvent().returnValue  = false;
      getXploderEvent().cancelBubble = true;
   }
   else
   {
      event.preventDefault(); 
      event.stopPropagation();
   }
}

function getXploderEvent()
{
/* xPloder's event belongs to the frame rather than the top window
   so first I have to figger out the calling frame [sigh]
*/
   for (var i = 0; i < top.frames.length; i++)
      if (top.frames[i].event)
         return top.frames[i].event;

   return window.event;
}

   // flwg function removes spurious textnodes & comments in w3c compliant browsers [tnx to John Resig]
function cleanWhitespace(element)
{
   element = element || document;

   var temp;
   var currentElement = element.firstChild;
   while (currentElement != null)
   {
      temp = currentElement.nextSibling;

   // the node is a text node and contains nothing but whitespace/linefeeds or is a comment
      if (currentElement.nodeType == 3 && !/\S/.test(currentElement.nodeValue) || currentElement.nodeType == 8)
         element.removeChild(currentElement);
      else if (currentElement.nodeType == 1)
         cleanWhitespace(currentElement);

      currentElement = temp;
   }
}

   // tnx to Simon Willison <http://www.sitepoint.com/blog-post-view.php?id=171578>
function addLoadEvent(fn)
{
   var oldonload = window.onload;
   if (typeof window.onload != 'function')
      window.onload = fn;
   else
      window.onload = function() { oldonload(); fn(); };
}

function addUnloadEvent(fn)
{
   var oldonunload = window.onunload;
   if (typeof window.onunload != 'function')
      window.onunload = fn;
   else
      window.onunload = function() { oldunonload(); fn(); };
}

function getScreenX(windowObj)
{
   if (!windowObj)
      windowObj = self;
   return windowObj.Left ? windowObj.Left : screenX;
}

function getScreenY(windowObj)
{
   if (!windowObj)
      windowObj = self;
   return windowObj.Top ? windowObj.Top : screenY;
}

function getOuterWidth(windowObj)
{
   if (!windowObj)
      windowObj = self;
// all except Explorer
   if (windowObj.outerWidth)
      return windowObj.outerWidth;
// Explorer 6 Strict Mode
   else if (windowObj.document.documentElement && windowObj.document.documentElement.offsetWidth)
      return windowObj.document.documentElement.offsetWidth;
// other Explorers
   else if (windowObj.document.body)
      return windowObj.document.body.offsetWidth;
   return null;
}

function getOuterHeight(windowObj)
{
   if (!windowObj)
      windowObj = self;
// all except Explorer
   if (windowObj.outerHeight)
      return windowObj.outerHeight;
// Explorer 6 Strict Mode
   else if (windowObj.document.documentElement && windowObj.document.documentElement.offsetHeight)
      return windowObj.document.documentElement.offsetHeight;
// other Explorers
   else if (windowObj.document.body)
      return windowObj.document.body.offsetHeight;
   return null;
}

   // flwg 6 functions tnx to <http://www.quirksmode.org/viewport/compatibility.html>
function getInnerWidth(windowObj)
{
   if (!windowObj)
      windowObj = self;
// all except Explorer
   if (windowObj.innerWidth)
      return windowObj.innerWidth;
// Explorer 6 Strict Mode
   else if (windowObj.document.documentElement && windowObj.document.documentElement.clientWidth)
      return windowObj.document.documentElement.clientWidth;
// other Explorers
   else if (windowObj.document.body)
      return windowObj.document.body.clientWidth;
   return null;
}

function getInnerHeight(windowObj)
{
   if (!windowObj)
      windowObj = self;
// all except Explorer
   if (windowObj.innerHeight)
      return windowObj.innerHeight;
// Explorer 6 Strict Mode
   else if (windowObj.document.documentElement && windowObj.document.documentElement.clientHeight)
      return windowObj.document.documentElement.clientHeight;
// other Explorers
   else if (windowObj.document.body)
      return windowObj.document.body.clientHeight;
   return null;
}

function getPageXOffset(windowObj)
{
   if (!windowObj)
      windowObj = self;
// all except Explorer
   if (windowObj.pageXOffset)
      return windowObj.pageXOffset;
// Explorer 6 Strict
   else if (windowObj.document.documentElement && windowObj.document.documentElement.scrollLeft)
      return windowObj.document.documentElement.scrollLeft;
// all other Explorers
   else if (windowObj.document.body)
      return windowObj.document.body.scrollLeft;
   return null;
}

function getPageYOffset(windowObj)
{
   if (!windowObj)
      windowObj = self;
// all except Explorer
   if (windowObj.pageYOffset)
      return windowObj.pageYOffset;
// Explorer 6 Strict
   else if (windowObj.document.documentElement && windowObj.document.documentElement.scrollTop)
      return windowObj.document.documentElement.scrollTop;
// all other Explorers
   else if (windowObj.document.body)
      return windowObj.document.body.scrollTop;
   return null;
}

function getPageWidth(windowObj)
{
// all but Explorer Mac
   if (document.body.offsetWidth < document.body.scrollWidth)
      return document.body.scrollWidth;
// Explorer Mac; would also work in Explorer 6 Strict, Mozilla and Safari
   else
      return document.body.offsetWidth;
   return null;
}
         
function getPageHeight(windowObj)
{
// all but Explorer Mac
   if (document.body.offsetHeight < document.body.scrollHeight)
      return document.body.scrollHeight;
// Explorer Mac; would also work in Explorer 6 Strict, Mozilla and Safari
   else
      return document.body.offsetHeight;
   return null;
}
         
   // flwg 2 functions tnx to <http://www.quirksmode.org/js/findpos.html>
function getPosX(obj)
{
   var trueLeft = 0;
   while (obj.offsetParent)
   {
      trueLeft += obj.offsetLeft;
      obj       = obj.offsetParent;
   }
   return trueLeft;
}

function getPosY(obj)
{
   var trueTop = 0;
   while (obj.offsetParent)
   {
       trueTop += obj.offsetTop;
       obj      = obj.offsetParent;
   }
   return trueTop;
}

   // flwg 2 function tnx to <http://www.quirksmode.org/js/events_properties.html>
function getMouseX(event)
{
   event = event || getXploderEvent();
   if (event.pageX)
      return event.pageX;
   else if (event.clientX)
      return event.clientX + document.body.scrollLeft;
   return null;
}

function getMouseY(event)
{
   event = event || getXploderEvent();
   if (event.pageY)
      return event.pageY;
   else if (event.clientY)
      return event.clientY + document.body.scrollTop;
   return null;
}

   // flwg 2 functions prevent popups and tooltips from disappearing under viewport edges
function fitObjInViewportWidth(Xpos, obj, windowObj)
{
   windowObj = windowObj || window;

   var totalWidth = Xpos + obj.offsetWidth;
   var pageWidth  = getPageXOffset(windowObj) + getInnerWidth(windowObj) - getScrollbarWidth(windowObj);

   var newXpos = Xpos;
   if (pageWidth < totalWidth)
      newXpos = pageWidth - obj.offsetWidth;     // correct for right underlap
   if (newXpos < getPageXOffset(windowObj))
      newXpos = getPageXOffset(windowObj);       // correct for left underlap

   return newXpos;
}

function fitObjInViewportHeight(Ypos, obj, windowObj)
{
   windowObj = windowObj || window;

   var totalHeight = Ypos + obj.offsetHeight;
   var pageHeight  = getPageYOffset(windowObj) + getInnerHeight(windowObj) - getScrollbarWidth(windowObj);

   var newYpos = Ypos;
   if (pageHeight < totalHeight)
      newYpos = pageHeight - obj.offsetHeight;   // correct for bottom underlap
   if (newYpos < getPageYOffset(windowObj))
      newYpos = getPageYOffset(windowObj);       // correct for top underlap

   return newYpos;
}

function getScrollbarWidth(windowObj)
{
   windowObj = windowObj || window;

   return BrowserDetect.browser == 'explorer'   // xPloder is unreliable in quirks mode: document.body.offsetWidth does not change when a scrollbar is necessitated by the doc's width
          ? windowObj.document.documentElement
             ? document.documentElement.offsetWidth - document.documentElement.clientWidth
             : document.body.offsetWidth - document.body.clientWidth
          : windowObj.innerWidth - document.documentElement.clientWidth;
}

function setOpacity(obj, newOpacity)
{
        if ('opacity' in obj.style)      obj.style.opacity      = newOpacity;
   else if ('MozOpacity' in obj.style)   obj.style.MozOpacity   = newOpacity;
   else if ('KhtmlOpacity' in obj.style) obj.style.KhtmlOpacity = newOpacity;
   else if ('filter' in obj.style)       obj.style.filter       = 'alpha(opacity=' + (newOpacity * 100) + ')';
}

function setCookie(name, value, days)
{
   if (days)
   {
      var date = new Date();
      date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
      var expires = '; expires=' + date.toGMTString();
   }
   else
      var expires = '';

   document.cookie = name + '=' + value + expires + '; path=/';
}

function getCookie(name)
{
   var nameEQ = name + '=';

   var ca = document.cookie.split(';');
   for(var i = 0; i < ca.length; i++)
   {
      var c = ca[i];
      while (c.charAt(0) == ' ')
         c = c.substring(1, c.length);

      if (c.indexOf(nameEQ) == 0)
         return c.substring(nameEQ.length, c.length);
   }

   return null;
}

function delCookie(name)
{
   setCookie(name, '', -1);
}

