addEvent(window, "load", preparetableforheadings);
addEvent(window, "load", preparehref);
addEvent(window, "load", preparefiltertriggers);

function preparehref() {
  var ref;
  var thisRef;
  var loop;

  if (!document.getElementsByTagName) return;

  refs = document.getElementsByTagName("a");

  //Go through all the inputs
  for (loop=0;loop<refs.length;loop++) {
    thisRef = refs[loop];
    //For all refs that have a class of return
    if ((' '+thisRef.className+' ').indexOf("return") != -1) {
      thisRef.addEventListener("click", jumpAndFollow, 0);
    }
  }
}

function preparetableforheadings() {
  var rowcount = 0;
  var colcount = 0;

  if (!document.getElementsByTagName) return;
  tbls = document.getElementsByTagName("table");
  //Go through all the tables
  for (ti=0;ti<tbls.length;ti++) {
    thisTbl = tbls[ti];

    //For all tables that are marked as headings run header on them
    if ((' '+thisTbl.className+' ').indexOf("headings") != -1) {
      var rows = thisTbl.rows.length;
      if (thisTbl.rows[0]) {
        var cols = thisTbl.rows[0].cells.length;
        for(rowcount = 1; rowcount < rows; rowcount++) {
          //debug("in row" + rowcount);
          //Check to see if there is no heading.  If there is no heading then no need to add events.
          for (colcount = 0; colcount < thisTbl.rows[rowcount].cells.length; colcount++) {
            if ((' '+thisTbl.rows[rowcount].getAttribute("class")+' ').indexOf("data") != -1) {
            //if (thisTbl.rows[rowcount].getAttribute("class") == 'data') {
              //Make sure the cell actually exists, incase table rows are not all the same size
              if (thisTbl.rows[rowcount].cells[colcount]) {
                if (thisTbl.rows[rowcount].cells[colcount].addEventListener) {
                  thisTbl.rows[rowcount].cells[colcount].addEventListener("mouseover", tableHeader,0);
                } else if (thisTbl.rows[rowcount].cells[colcount].attachEvent) {
                  thisTbl.rows[rowcount].cells[colcount].attachEvent("onmouseover", tableHeader);
                } else {
                  alert("Can't add events");
                }

                if (thisTbl.rows[rowcount].cells[colcount].addEventListener) {
                  thisTbl.rows[rowcount].cells[colcount].addEventListener("mouseout", tableHeader,0);
                } else if (thisTbl.rows[rowcount].cells[colcount].attachEvent) {
                  thisTbl.rows[rowcount].cells[colcount].attachEvent("onmouseout", tableHeader);
                } else {
                  alert("Can't add events");
                }
              }
            }
          }
        }
      }
    }
  }
}

function preparefiltertriggers(startElement) {
  var inputs;
  var thisInput;
  var loop;

  if (!startElement) {
  } else {
    startElement = document;
  }

  //Skip if this is an empty document
  if (!startElement.getElementsByTagName) return;

  inputs = startElement.getElementsByTagName("input");

  //Go through all the inputs
  for (loop=0;loop<inputs.length;loop++) {
    thisInput = inputs[loop];
    //For all inputs that have a class of claimQty
    if ((' '+thisInput.className+' ').indexOf("filterfield") != -1) {

      if (thisInput.addEventListener) {
        thisInput.addEventListener("change", filterTable, 0);
      } else if (thisInput.attachEvent) {
        thisInput.attachEvent("onchange", filterTable);
      } else {
        alert("Can't add event in prepare Filter Triggers");
      }
    }
  }
}

function filterTable(e) {
  var theEvent = e || event;
  var theInput = theEvent.srcElement || theEvent.target;

  if (!filterTable.box) {
    filterTable.box = document.createElement("div");
    document.body.appendChild(filterTable.box);
  }

  windowwidth = window.innerWidth;
  windowheight = window.innerHeight;
  displaywidth = 400;
  displayheight = 80;
  displayleft = (windowwidth / 2) - (displaywidth / 2);
  displaytop = (windowheight / 2)  - (displayheight / 2);

  filterTable.box.setAttribute("style","display: block; position: fixed; top: " + displaytop + "px; left: " + displayleft + "px; width: " + displaywidth + "px;height: " + displayheight + "px; padding: 5px; margin: 10px; z-index: 100; color: black; border-style: solid; border-color: #41478e; border-width: 5px; background: #5864ff; font: 24px Verdana, sans-serif; text-align: center;");

  filterTable.box.innerHTML = "Please wait.  Filtering Table";

  filterTable_work.theEvent = theEvent;
  filterTable_work.theInput = theInput;

  window.setTimeout(filterTable_work, 100);
}

function filterTable_work() {

  if (!filterTable_work.loop) {
    filterTable_work.loop = 0;
  }

  var loop = filterTable_work.loop;
  var thisLoop = 0;
  var row;
  theEvent = filterTable_work.theEvent;
  theInput = filterTable_work.theInput;

  var idArray = theInput.id.split(";");
  var tableId = idArray[1];
  var column = idArray[2];

  var theTable = document.getElementById(tableId);

  if (theInput.value.length > 0) {
    while (loop < theTable.rows.length) {
      row = theTable.rows[loop];

      if (((' '+row.className+' ').indexOf("data") != -1) || ((' '+row.className+' ').indexOf("hiddenRow") != -1)) {
        if ((' ' + row.innerHTML + ' ').toLowerCase().indexOf(theInput.value.toLowerCase()) == -1) {
          row.setAttribute("class", "hiddenRow");
        } else {
          row.setAttribute("class", "data");
        }
      } else {
        debug(row.className + "  " + theTable.rows.length + " " + tableId + row.innerHTML);
      }

      loop++;
      thisLoop++;

      if (thisLoop > 100) {
        filterTable_work.loop = loop;
        window.setTimeout(filterTable_work, 10);
        percentdone = loop / theTable.rows.length;
        percentdone = percentdone * 100;
        filterTable.box.innerHTML = "Please Wait, Filtering...<br />" + Math.round(percentdone) + " percent done";
        return;
      }
    }
  } else {
    while (loop < theTable.rows.length) {
      row = theTable.rows[loop];

      if ((' '+row.className+' ').indexOf("hiddenRow") != -1) {
        row.setAttribute("class", "data");
      }

      loop++;
      thisLoop++;

      if (thisLoop > 100) {
        filterTable_work.loop = loop;
        window.setTimeout(filterTable_work, 10);
        percentdone = loop / theTable.rows.length;
        percentdone = percentdone * 100;
        filterTable.box.innerHTML = "Please Wait, Removing Filter...<br />" + Math.round(percentdone) + " percent done";
        return;
      }
    }
  }

  filterTable_work.loop = 0;
  filterTable.box.setAttribute("style","display: none;");
}


function filterTable_workBACKUP() {
  var loop;
  var row;
  theEvent = filterTable_work.theEvent;
  theInput = filterTable_work.theInput;

  var idArray = theInput.id.split(";");
  var tableId = idArray[1];
  var column = idArray[2];

  var theTable = document.getElementById(tableId);

  for (loop = 1; loop < theTable.rows.length; loop++) {
    row = theTable.rows[loop];

    if ((' ' + row.innerHTML + ' ').toLowerCase().indexOf(theInput.value.toLowerCase()) == -1) {
      row.setAttribute("class", "hiddenRow");
    } else {
      row.setAttribute("class", "data");
    }
  }

  filterTable.box.setAttribute("style","display: none;");
}

function tableHeader(e) {
  var e = e || event;
  var theobject = e.srcElement || e.target;

  if (e.type == "mouseover") {
    show = true;
  } else {
    show = false;
  }

  var thecolumn;
  if (!tableHeader.box) {
    tableHeader.box = document.createElement("div");
    document.body.appendChild(tableHeader.box);
  }
  if (show) {
    tableHeader.box.setAttribute("class","tableHeader");

    var therow = getParent(theobject, "tr");

    if (therow.rowIndex) {
      var thetable = getParent(theobject, "table");

      if ((thetable.tagName.toUpperCase()) == "TABLE") {
        if(thetable.rows && thetable.rows.length > 0) {
          if (therow.cells.length != thetable.rows[0].cells.length) {
            var firstRow = thetable.rows[1];
          } else {
            var firstRow = thetable.rows[0];
          }
          thecolumn = firstRow.cells[theobject.cellIndex];
        }
      }

      if (thecolumn) {
        if (thecolumn.childNodes.length > 0) {
          if (thecolumn.childNodes[0].tagName) {
            if (thecolumn.childNodes[0].tagName.toLowerCase() == "a") {
              thecolumn = thecolumn.childNodes[0];
            }
          }
        }

        tableHeader.box.innerHTML = thecolumn.innerHTML;
      }
    }

  } else {
    tableHeader.box.setAttribute("class","invisible");
  }
}

//Jump to the TD containing the link and then follow the link
function jumpAndFollow(e) {
  var theTD;
  var theEvent = e || event;
  var theRef = theEvent.srcElement || theEvent.target;

  theTD = getParent(theRef, "TD");

  window.location.hash=theTD.id;

  return true;
}