var headerArray = new Array();
var toMenumove, toScale, tools, tDummyTop;
var _scaleInit  = false;
var toolsGroup  = null;
var gridHeader  = null;
var gHInflate   = null;
var gridBar     = null;
var gridBody    = null;
var headerTable = null;
var tableHeader = null;
var tableBody   = null;
var header      = null;
var toolsDummy  = null;
var offsetHeader=0;     // accounts for the gridBar and tableHeader margins

function scaleInit() {
  if (isNS4) { return; }
  if (_scaleInit) return;
  searchParam= new getObj('searchparams');
  toolsGroup = new getObj('toolsGroup');
  gridHeader = new getObj('gridHeader');
  gridBody   = new getObj('gridBody');
  gridBar    = new getObj('gridBar');
  headerTable= new getObj('headerTable');
  tableHeader= new getObj('tableHeader');
  tableBody  = new getObj('tableBody');
  header     = new getObj('header');
  toolsDummy = new getObj('toolsDummy');
  gHInflate  = new getObj('gHInflate');  // dummy object use as a cushion between
                                         // searchparams and gridBody

  // performance improvement by caching the object into an array
  for (i=headerList.length;i--;) {
    headerArray[headerList[i]+'_b'] = new getObj(headerList[i]+'_b');
    headerArray[headerList[i]+'_h'] = new getObj(headerList[i]+'_h');
  }

  if (tableBody.obj) {
    gridHeader.style.width =  tableBody.obj.offsetWidth + "px";
  }
  offsetHeader =  gridHeader.obj.offsetHeight - (gridBar.obj.offsetHeight + tableHeader.obj.offsetHeight);

  _scaleInit=true;
}
  
function scale()
{
  if (isNS4) return;
  scaleInit();
  if (!tableBody.obj) return;  // if table is empty don't scale
  if (toScale) clearTimeout(toScale);

  if (!document.all && !document.getElementById) {
//    alert('The script on this page does\'t work in your browser');
    return;
  }
  var extra = 0;
  if (document.all) extra = 2;
  extra +=4; // +2 per point of cellpadding
             // make this dependent on browsers
             // some browsers a 2 will be ok
             // i.e. mozilla and netscape a value of 4 seems to work
             //      opera 2 (except the last two columns)
             //      ie 2

  headerTable.style.width = tableBody.obj.offsetWidth + "px";
  gridHeader.style.width  = tableBody.obj.offsetWidth + "px";
  gridBar.style.width     = tableBody.obj.offsetWidth + "px"; //opera

  //loops through the column list resizing them
  for (i=headerList.length;i--;) {
    x = headerArray[headerList[i]+'_b'].obj.offsetWidth - extra;
    headerArray[headerList[i]+'_h'].style.width= x + "px"
  }

  // These two lines are no longer necessary as gridHeader is positioned inline at start
  // but keep them just in case ...
  //gridHeader.style.left = gridBody.obj.offsetLeft;
  //if (isNS && isMacOS) gridHeader.style.left = findLeft('header') + "px";  // bug in NS Mac version
                                                                           // absolute divs don't 
                                                                           // inherite properties
//  header.obj.style.marginBottom = gridHeader.obj.offsetHeight + "px";

  toScale = setTimeout('scale()',1000);  //gets the new scaling from a change of font size via ctrl-+ or - E.g. firefox, netscape etc
}

function movemenu()
{
  if (isNS4) return;
  if (!tableBody.obj) return;  // if table is empty don't move 
  if (toMenumove) clearTimeout(toMenumove);
  var pos = 0;  // declare the variable or otherwise IE will
                // not handle big numbers

  //find top position in page
  if (window.innerHeight) {
    pos = window.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop) {
    pos = document.documentElement.scrollTop;
  } else if (document.body) {
    pos = document.body.scrollTop;
  }

  // In order to fix the tools menu we use the
  // toolsDummy object as a reference
  tDummyTop = findTopObj(toolsDummy);
  //debug("tDummyTop :" + tDummyTop + " and pos : " + pos);
  // if we scroll pass the toolDummy object then start moving the toolsGroup
  if ( pos > tDummyTop) {
    toolsGroup.style.top = pos + "px";
  } else {
    toolsGroup.style.top = tDummyTop + "px";
  }

  // header headerList
  var headerHeight = header.obj.offsetHeight;
  // the findTopObj will find the top most distance from the object
  // to the window ... while the obj.offsetTop for some browsers will
  // be the top relative to the parent object
  // So the difference will help me to position the header bar correctly
  var topOffset = findTopObj(header) - header.obj.offsetTop;

  var ghPosInit = findTopObj(gridBody) - gridBar.obj.offsetHeight - 2 *offsetHeader;

  var spHeight=0;
  //calculate search Params height
  if (searchParam.obj && searchParam.obj.offsetHeight) {
    spHeight=searchParam.obj.offsetHeight;
  }
    
  if ( pos > (findTopObj(header) + headerHeight +  gridBar.obj.offsetHeight + spHeight)) {
//    alert(gridHeader.obj.offsetHeight + "px");
    tableHeader.style.display="block";
    getTHEADS("tableBody","rowHidden");
    gridHeader.style.position="absolute";
    gridHeader.style.top = pos - topOffset + "px";  // topOffset is for IE
                                                    // as in IE there is some margin from the body to the header object

    gHInflate.style.height=gridHeader.obj.offsetHeight + "px";
  } else {
    gHInflate.style.height="0px";
    gridHeader.style.position="";    //browser defaults
    tableHeader.style.display="none";
    getTHEADS("tableBody","rowHeader");
  }
//alert("isIE :" + isIE + ": " +  findTopObj(gridHeader) + "; pos:" + pos + "; topoffset:" + topOffset);
    
  toMenumove = setTimeout('movemenu()', 200);
}

//work around for IE but works in all browsers
function getTHEADS(table_id, _action) {
  var _table = document.getElementById(table_id);
  var _theads = _table.getElementsByTagName('TR');
  var i=0;
//    _theads.item(i).className = _action;
    if (_action == "rowHidden") {
      _theads.item(i).style.visibility = "hidden";
    } else {
      _theads.item(i).style.backgroundColor = "#C0C0C0";
      _theads.item(i).style.visibility = "";  //browser defaults
    }
}

function readParams () {
  var buffHead="";
  var buffBody="";
  var buffBoxes="";
  var pos = 0;  // declare the variable or otherwise IE will
  // not handle big numbers
  if (window.innerHeight) {
    pos = window.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop) {
    pos = document.documentElement.scrollTop;
  } else if (document.body) {
    pos = document.body.scrollTop;
  }

  for (i=0;i<headerList.length;i++) {
    y = new getObj(headerList[i]+'_b');
    z = new getObj(headerList[i]+'_h');
    bodyElemWidth = y.obj.offsetWidth; 
    headElemWidth = z.obj.offsetWidth;
    buffHead += headerList[i] + " ::" + headElemWidth + " ";
    buffBody += headerList[i] + " ::" + bodyElemWidth + " ";
  }
  var woffset = gridHeader.obj.offsetHeight - header.obj.offsetHeight;
  buffBoxes += "\n gridHeader top:" + gridHeader.obj.offsetTop + "; gridHeader height:" + gridHeader.obj.offsetHeight + ' ';
  buffBoxes += "\n gridHeader top (findTop):" + findTop("gridHeader") + ' ';
  buffBoxes += "\n header top:" + header.obj.offsetTop + ' ';
  buffBoxes += "\n header top (findTop):" + findTop("header") + '\n ';
  var wgb = new getObj('gridBody');
  buffBoxes += "gridBody Height:" + wgb.obj.offsetHeight + ' ';
  buffBoxes += "\nheader Top:" + header.obj.offsetTop + "; header height:" + header.obj.offsetHeight + ' ';
  buffBoxes += "\nheader - gridHeader offset :" + woffset;
  buffBoxes += "\npos :" + pos ; 

  alert ( buffHead + "\n" + buffBody + "\n" + buffBoxes );
}

var showMode = 'table-cell';
if (document.all) showMode='block';
function toggleVis(btn){
  var fieldName= btn;
  btn   = document.forms['RecordSet'].elements[btn];
  cells = document.getElementsByName('_'+fieldName);
  mode = btn.checked ? showMode : 'none';
  for(j = 0; j < cells.length; j++) cells[j].style.display = mode;
//debug("mode:" + cells);
}

//  window.onresize = scale;

