function sortTable(id, col) {
 if(byId(id).firstChild.tagName.toLowerCase() == "tbody")
  var curTable = byId(id).firstChild;
 else
  var curTable = byId(id);
 var content = new Array();
 var numeric = new Array();
 var curRow, curCell;
 for(var i=1; curRow = curTable.childNodes[i]; ++i) {
  content[i] = new Array();
  for(var j=0; curCell = curRow.childNodes[j]; ++j)
   content[i][j] = curCell.innerHTML;
 }
 for(var j=0; j<content[1].length; ++j) {
  numeric[j] = true;
  for(var i=1; i<content.length; ++i)
   if(!content[i][j].match(/^-?\d+([\.,]\d+)?[^\d]*$/))
    numeric[j] = false;
 }
 quicksort(content,1,content.length, col, numeric[col]);
 var colCell = curTable.childNodes[0].childNodes[col];
 var colStyle = colCell.className.substr(0,3);
 for(var j=0; curCell = curTable.childNodes[0].childNodes[j]; ++j)
  if(curCell.className.substr(0,3) == "dsc" || curCell.className.substr(0,3) == "asc")
   curCell.className = curCell.className.substr(4);
 if(colStyle != "dsc" && numeric[col] || colStyle == "asc" && !numeric[col]) {
  content.reverse();
  content.unshift(new Array());
  colCell.className = "dsc " + colCell.className;
 } else {
  colCell.className = "asc " + colCell.className;
 }
 for(var i=1; curRow = curTable.childNodes[i]; ++i)
  for(var j=0; curCell = curRow.childNodes[j]; ++j)
   curCell.innerHTML = content[i][j];
}

function quicksort(inArray, bottom, top, col, numeric) {
 if(bottom + 1 < top) {
  swap(inArray, bottom, Math.floor((top+bottom)/2));
  var i = bottom + 1;
  var j = top - 1;
  var z = 0;
  do {
   if(numeric)
    while(i < top && parseFloat(inArray[i][col]) < parseFloat(inArray[bottom][col])) ++i;
   else
    while(i < top && inArray[i][col].toUpperCase() < inArray[bottom][col].toUpperCase()) ++i;
   if(numeric)
    while(j > bottom && parseFloat(inArray[j][col]) >= parseFloat(inArray[bottom][col])) --j;
   else
    while(j > bottom && inArray[j][col].toUpperCase() >= inArray[bottom][col].toUpperCase()) --j;
   if(i < j)
    swap(inArray, i, j);
  } while(i < j && ++z < inArray.length/2);
  swap(inArray, i - 1, bottom);
  quicksort(inArray, bottom, i - 1, col, numeric);
  quicksort(inArray, i, top, col, numeric);
 }
}

function swap(inArray, first, second) {
 for(var i=0; i<inArray[first].length; ++i) {
  var temp = inArray[first][i];
  inArray[first][i] = inArray[second][i];
  inArray[second][i] = temp;
 }
}
