// zpTapbleSort.js: base code is by Zvonko on Experts-Exchange.com
//
// Usage:
// <body onLoad='initTable("table0", {hdrRows:1, numeric:".3.4.5.", desc:"", html:""}); initTable("table1");'>
// <table id="table0"> ....
//
// Crude secondary sorting added for first and second columns.

var selectedColor = "blue";
var defaultColor = "black";

function initTable(tabName, sortRules){
  if ( ! testagent() ) exit;

  hdrRows = 1;
  numeric = '..';
  desc = '..';
  html = '..';
  for(rule in sortRules){
    window[rule] = sortRules[rule];
  }
  var theTab = document.getElementById(tabName);
  for(r=0;r<hdrRows;r++)
   for(c=0;c<theTab.rows[r].cells.length;c++)
     if((r+theTab.rows[r].cells[c].rowSpan)>hdrRows)
       hdrRows=r+theTab.rows[r].cells[c].rowSpan;
  for(r=0;r<hdrRows; r++){
    colNum = 0;
    for(c=0;c<theTab.rows[r].cells.length;c++, colNum++){
      if(theTab.rows[r].cells[c].colSpan<2){
        theCell = theTab.rows[r].cells[c];
        rTitle = theCell.innerHTML.replace(/<[^>]+>|&nbsp;/g,'');
        if(rTitle>""){
	  theCell.innerHTML += "<IMG class='sortimg' src='images/blank18w.png'>";
          theCell.title = rTitle;
          theCell.onmouseover = function(){setCursor(this, "selected")};
          theCell.onmouseout = function(){setCursor(this, "default")};
          var sortParams = 7;
          if(numeric.indexOf("."+colNum+".")>-1) sortParams -= 1;
          if(desc.indexOf("."+colNum+".")>-1) sortParams -= 2;
          if(html.indexOf("."+colNum+".")>-1) sortParams -= 4;
          theCell.onclick = new Function("sortTable(this,"+(colNum+r)+","+hdrRows+","+sortParams+")");
        }
      } else {
        colNum = colNum+theTab.rows[r].cells[c].colSpan-1;
      }
    }
  }
}

function setCursor(theCell, mode){
  rTitle = theCell.innerHTML.replace(/<[^>]+>|&nbsp;|\W/g,'');
  if(mode=="selected"){
    if(theCell.style.color!=selectedColor) 
      defaultColor = theCell.style.color;
    theCell.style.color = selectedColor;
    theCell.style.cursor = "pointer";
    window.status = "Click to sort by '"+rTitle+"'";
  } else {
    theCell.style.color = defaultColor;
    theCell.style.cursor = "";
    window.status = "";
  }
}

function sortTable(theCell, colNum, hdrRows, sortParams){
  var num = !(sortParams & 1);
  sDir = !(sortParams & 2);
  var html = !(sortParams & 4);
  var tBody = theCell.parentNode;
  while(tBody.nodeName!="TBODY"){
    tBody = tBody.parentNode;
  }
  var tabOrd = new Array();
  if(tBody.rows[0].sCol==colNum) sDir = !tBody.rows[0].sDir;
  tBody.rows[0].sCol = colNum;
  tBody.rows[0].sDir = sDir;

  var BLANK = "<IMG class='sortimg' src='images/blank18w.png'>";
  var ARROW = "";
  if(sDir){
    ARROW = "<IMG class='sortimg' src='images/up18w.png'>";
  } else {
    ARROW = "<IMG class='sortimg' src='images/down18w.png'>";
  }
  var oldSort=/<IMG class="?sortimg"? src="?.+"?>/i;
  for(r=0;r<hdrRows;r++){
    theRow = tBody.rows[r];
    for(i=0;i<theRow.cells.length;i++){
      theRow.cells[i].innerHTML = theRow.cells[i].innerHTML.replace(oldSort, BLANK);
    }
  }
  theCell.innerHTML = theCell.innerHTML.replace(oldSort, ARROW);

  for(i=0,r=hdrRows;r<tBody.rows.length;i++,r++){
    colCont = tBody.rows[r].cells[colNum].innerHTML;
    if ( colNum == 0 || colNum == 1 ) colCont2 = tBody.rows[r].cells[colNum+1].innerHTML;
    else colCont2 = '';
    if(html) colCont = colCont.replace(/<[^>]+>/g,'');
    if(num) {
      colCont*=1;
      if(isNaN(colCont)) colCont = 0;
    }
    tabOrd[i] = [r, tBody.rows[r], colCont, colCont2];
  }
  tabOrd.sort(compRows);
  for(i=0,r=hdrRows;r<tBody.rows.length;i++,r++){
    tBody.insertBefore(tabOrd[i][1],tBody.rows[r]);
  } 
  setCursor(theCell, "default");
  window.status=""; 
}

function compRows(a, b){
  if(sDir){
    if(a[2]>b[2]) return -1;
    if(a[2]<b[2]) return 1;
    if (a[3] && b[3]) {
      if(a[3]>b[3]) return 1;
      if(a[3]<b[3]) return -1;
    }
  } else {
    if(a[2]>b[2]) return 1;
    if(a[2]<b[2]) return -1;
    if (a[3] && b[3]) {
      if(a[3]>b[3]) return 1;
      if(a[3]<b[3]) return -1;
    }
  }
  return 0;
}

function testagent(){
    var agent=navigator.userAgent.toLowerCase();

    var macie52 = (agent.indexOf("msie 5.2") != -1) &&
                  (agent.indexOf("mac") != -1);

    if ( macie52 ) return 0;
    else return 1;
}
