function TablesSort(el) {
  var i,temp,textA,textB,j;
  var deep = true;
  var flag = false;
  var trObj;
  var sortColumnIndex = 0;
  var sortedColumnElements = new Array(0);

  //------------------------------------------------------------------------------
  var TablesSortAscending = function (a,b) {
    var c, d;
    c = parseFloat(a[1]);
    d = parseFloat(b[1]);
    if (!isNaN(c) && !isNaN(d)) {
      a[1] = c;
      b[1] = d;
    }
    if (a[1] == b[1]) return 0;
    if (a[1] > b[1]) return -1;
    return 1;
  }
  var TablesSortDescending = function (b,a) {
    var c, d;
    c = parseFloat(a[1]);
    d = parseFloat(b[1]);
    if (!isNaN(c) && !isNaN(d)) {
      a[1] = c;
      b[1] = d;
    }
    if (a[1] == b[1]) return 0;
    if (a[1] > b[1]) return -1;
    return 1;
  }
  var TablesSortGetTextNode = function (el) {
    var currentObj = el;
    var childObj = currentObj.firstChild;
    while (childObj.nodeType != 3) {
      currentObj = childObj;
      childObj = currentObj.firstChild;
    }
    // removing preceding special characters so the sort works correctly
    
    var text = childObj.nodeValue;
    try {
      text = text.replace(/\#/g, "");
      text = text.replace(/,/g, "");
      text = text.replace(/\//g, "");
      text = text.replace(/^\$/, "");
      text = text.replace(/^0/, "");
    }
    catch(e) {
      //ignore error
    }
    return text;
  }
  var TablesSortGetCurrentRow = function (el) {
    var currentObj = el;
    var parentObj = currentObj.parentNode;
    while (parentObj.tagName != "TABLE") {
      currentObj = parentObj;
      parentObj = currentObj.parentNode;
      if (currentObj.tagName == "TR") {
        var currentRow = currentObj;
        continue;
      }
    }
    return currentRow;
  }
  var TablesSortGetCurrentTable = function (el) {
    var currentObj = el;
    var parentObj = currentObj.parentNode;
    while (parentObj.tagName != "TABLE") {
      currentObj = parentObj;
      parentObj = currentObj.parentNode;
    }
    var currentTable = currentObj.parentNode;
    return currentTable;
  }
  //------------------------------------------------------------------------------

  var trObj = TablesSortGetCurrentRow(el);
  var tableObj = TablesSortGetCurrentTable(el);
  var tBodyObj = tableObj.tBodies.item(0);
  
  // determine if we should sort ascending or descending
  var sortDirection = el.getAttribute("sort");
  
  for (i=0; i < trObj.cells.length; i++) {
    trObj.cells[i].className = '';
    // this is a check to see if we need to remove the sort indicator image
    if (trObj.cells[i].lastChild.src) {
       trObj.cells[i].removeChild(trObj.cells[i].lastChild)
    }
    
    var elToCompare = el;
    while (elToCompare.tagName != "TD") {  // this is to ensure we are comparing the tr child nodes with a td node
      elToCompare = elToCompare.parentNode;
    }
    if (trObj.cells[i] == elToCompare) {
      sortColumnIndex = i;
      trObj.cells[i].className = 'bgSort';
      if (sortDirection == "ascending") {
       var imgObj = new Image();
       imgObj.src = "http://msn.foxsports.com/fe/img/arrow_down.gif";
       imgObj.border = 0;
       el.parentNode.appendChild(imgObj);
      } else {
       var imgObj = new Image();
       imgObj.src = "http://msn.foxsports.com/fe/img/arrow_up.gif";
       imgObj.border = 0;
       el.parentNode.appendChild(imgObj);
      }
    }
  }
  
  // use rows and cells so we do not have to worry about whitespace and future html comments
  for (i=0; i < tBodyObj.rows.length; i++) {
    var text = TablesSortGetTextNode(tBodyObj.rows[i].cells[sortColumnIndex]);
    sortedColumnElements[i] = new Array(i,text);
  }
  
  if (sortDirection == "ascending") {
    sortedColumnElements.sort(TablesSortAscending);
    el.setAttribute("sort","descending");
  }
  else {
    sortedColumnElements.sort(TablesSortDescending);
    el.setAttribute("sort","ascending");
  }
  
  var clonedTableObj = tableObj.cloneNode(true);
  var clonedTBodyObj = clonedTableObj.tBodies.item(0);
  
  var prevSortColumnIndex = trObj.getAttribute("prevSortColumnIndex");
  
  for (i=0; i < sortedColumnElements.length; i++) {
    clonedRow = clonedTBodyObj.rows[sortedColumnElements[i][0]].cloneNode(true);
    // child nodes will not work here because of the whitespace in FF
    /*
    if (prevSortColumnIndex != null) clonedRow.childNodes[prevSortColumnIndex].className = 'bgC';
    clonedRow.childNodes[sortColumnIndex].className = 'bgSort';
    */
    // cells will not work here because IE will not support that collection for the cloned node
    /*
    if (prevSortColumnIndex != null) clonedRow.cells[prevSortColumnIndex].className = 'bgC';
    clonedRow.cells[sortColumnIndex].className = 'bgSort';
    */
    // maybe we should use child nodes here but go ahead and remove the whitespace
    for (j=0; j < clonedRow.childNodes.length; j++) {
      if (clonedRow.childNodes[j].nodeType != 1) {
        clonedRow.removeChild(clonedRow.childNodes[j]);
        j--;
      }
    }
    if (prevSortColumnIndex != null) clonedRow.childNodes[prevSortColumnIndex].className = 'bgC';
    clonedRow.childNodes[sortColumnIndex].className = 'bgSort';

    tBodyObj.replaceChild(clonedRow, tBodyObj.rows[i]);
  } 
  
  trObj.setAttribute("prevSortColumnIndex",sortColumnIndex);
}





