addEvent(window, "onload", function() { initScroll(); });
// this function will initialize the scrolling tables by -
// 1) Synchronizing each header element with its respective body element
// 2) Searching for a "resizable" div element and adding a function to the window's
//    onresize event that will resize the div elements each time the window is resized
//    **Note: Only the first "resizable" div element found will by resized.
//        Multiple "resizable" divs are not supported.
//    After the event is attached, the next lines are a copy of the code from the function
//    that was attached to the event, that serves to resize the div elements immediately.
function initScroll()
{
  var coll;
  var coll2;
  var header;
  var body;
  coll = document.getElementsByTagName("DIV");
  if (coll!=null)
  {
    for (var i=0; i<coll.length; i++)
    {
      if (coll[i].getAttribute('control') == "QuadScroll")
      {
        coll2 = coll[i].children;
        for (var j=0; j<coll2.length; j++)
        {
          if (coll2[j].getAttribute('val') == "Header")
          {
            header = coll2[j];
          }
          else if (coll2[j].getAttribute('val') == "Body")
            body = coll2[j];
        }
        if (typeof(header) == "object" && typeof(body) == "object")
        {
        	addScrollSynchronization(header, body, "horizontal");
        }
      }
    }
  }
  coll = document.getElementsByTagName("DIV");
  if (coll != null)
  {
    for (var i=0; i<coll.length; i++)
    {
      if (coll[i].getAttribute('control') == "QuadScroll")
      {
        coll2 = coll[i].children;
        for (var j=0; j<coll2.length; j++)
        {
          if (coll2[j].getAttribute('resizeOffset') != null)
          {
            scrollObj = coll[i];
            scrollBodyObj = coll2[j];
            var params = new Array();
            params[0] = coll[i];	// main div surrounding the scrolling table
            params[1] = parseInt(coll2[j].getAttribute('resizeOffset')) - 20;	// account for the header ~ 20px
            params[2] = coll2[j];	// inner div that actually does the scrolling
            params[3] = coll2[j].getAttribute('resizeOffset');
            addEvent(window, "onresize", getOnResizable(params));
            // resize the inner div first - otherwise the headers disappear in Gecko browsers
            params[2].style.height = (document.body.offsetHeight-params[3]);
            params[0].style.height = (document.body.offsetHeight-params[1]);
            break;
          }
        }
      }
    }
  }
}

function scrollGeckoHeader()
{
	geckoHeaderDiv.style.left = -geckoBodyDiv.scrollLeft;
	geckoHeaderDiv.style.overflow = "visible";
}

var geckoHeaderDiv;
var geckoBodyDiv;
// This is a function that returns a function that is used in the event listener
function getOnScrollFunction(oElement, bodyElement) 
{
   return function () 
   {
      if (oElement._scrollSyncDirection == "horizontal" || oElement._scrollSyncDirection == "both")
      {
         // we need to use setTimeout to call the function to scroll the header
         // because in Gecko browsers the scrollLeft attribute is not set to the
         // scrolled position of the div until after the onscroll event has fired
        if (is_gecko && !is_safari) 
        {	// mozilla & netscape on pc & mac
            geckoBodyDiv = event.srcElement.parentNode.parentNode;
            geckoHeaderDiv = oElement;
            setTimeout("scrollGeckoHeader()", 0); // this must be the last statement
        }
				else if (is_mac && is_ie) {	// ie on a mac
         	var obj = bodyElement;
         	var totalOffsetLeft = 0;
         	// totalOffsetLeft will represent the distance between the left edge
         	// of the element and the left edge of the HTML BODY element
         	while (obj.nodeName.toUpperCase() != "BODY")
         	{
         		totalOffsetLeft += obj.offsetLeft;
         		obj = obj.offsetParent;
         	}
         	// the clientWidth measures from the left edge of the element up to,
         	// but not including, the vertical scrollbar
      	    if ((totalOffsetLeft + bodyElement.clientWidth) > event.x) {	// if vertical scrollbar is not clicked
         	   // as of mac ie version 5.1 the only apparent way to scroll the
         	   // header is to use style.left
         	   // the amount by which to move the header equals the amount the "body" moved
         	   // which is the left offset of the div contained within this div (bodyElement)

         	   // the totalOffsetLeft is added because of an apparent bug in ie 5.0
         	   // where the positioning of the header element is measured relative
         	   // to the frame instead of relative to its containing element
               if (is_ie50)
                  oElement.style.left = bodyElement.firstChild.nextSibling.offsetLeft + totalOffsetLeft;
               else
                  oElement.style.left = bodyElement.firstChild.nextSibling.offsetLeft;

               oElement.style.overflow = "visible";
            }
         } else {	// ie on a pc
            oElement.scrollLeft = event.srcElement.scrollLeft;
         }
      }
      // vertical scrolling has not been used or tested
      // it will likely need modifications similar to the ones above for
      // horizontal scrolling across all browsers
      if (oElement._scrollSyncDirection == "vertical" || oElement._scrollSyncDirection == "both")
      {
				if (is_safari || is_firefox)
				{
					oElement.scrollTop = event.srcElement.scrollTop;
				}
         else if (is_gecko)
            oElement.style.top = event.target.scrollTop;
         else if (is_mac && is_ie)
            oElement.style.left = -event.offsetY;
         else
            oElement.scrollTop = event.srcElement.scrollTop;
      }
   };
}

// This function adds scroll syncronization for the fromElement to the toElement
// this means that the fromElement will be updated when the toElement is scrolled
function addScrollSynchronization(fromElement, toElement, direction) 
{
  removeScrollSynchronization(fromElement);
   
  fromElement._syncScroll = getOnScrollFunction(fromElement, toElement);
  fromElement._scrollSyncDirection = direction;
  fromElement._syncTo = toElement;
  if (is_mac && is_ie)
		toElement.onscroll = fromElement._syncScroll;
  else
		addEvent(toElement, "onscroll", function() { fromElement._syncScroll(); });
}

// removes the scroll synchronization for an element
function removeScrollSynchronization(fromElement) 
{
   if (fromElement._syncTo != null)
      if (is_mac && is_ie)
         fromElement.onscroll = "";
      else
         fromElement._syncTo.detachEvent("onscroll", fromElement._syncScroll);

   fromElement._syncTo = null;;
   fromElement._syncScroll = null;
   fromElement._scrollSyncDirection = null;
}