function srtPrg() {
  top.controlFrame.document.getElementById("progress").innerHTML = "Sorting...";
  top.controlFrame.document.getElementById("progress").style.visibility = "visible";
}

function drawTable(tbody, C) {
  top.virginTbl &= top.contentFrame.s == 0;
  var ticker = "";
  var lurl = contentFrame.location.toString().toLowerCase();
  var pos = lurl.indexOf("?");
  if (pos != -1)
    ticker = lurl.substring(pos+1);
  var c1 = "#fff", c2 = "#eee", c3 = "#ffffcc", c4 = "#fcc";
  var even = false;
  var evenColor, oddColor;
  var tr, td;
  for (var i = 0; i < C.length; i++) {
    if (C[i].t == "Sector") {
      evenColor = c3;
      oddColor = c3;
    }
    else if (C[i].t.toLowerCase() == ticker) {
      evenColor = c4;
      oddColor = c4;
    }
    else {
      evenColor = c1;
      oddColor = c2;
    }
    tr = tbody.rows[i];
    tr.style.backgroundColor = even ? evenColor : oddColor;
    td = tr.cells[0];
    td.innerHTML = C[i].t;
    td = tr.cells[1];
    td.innerHTML = C[i].n;
    td = tr.cells[2];
    td.innerHTML = C[i].m.toFixed(C[i].m == 100 ? 0 : 1) + "%";
    td = tr.cells[3];
    td.innerHTML = top.addCommas(C[i].d.toFixed(0));
    td = tr.cells[4];
    td.innerHTML = C[i].s.toFixed(2);
    td = tr.cells[5];
    td.innerHTML = C[i].t == "Sector" ? "n/a" : C[i].r.toFixed(0);
    td = tr.cells[6];
    td.innerHTML = C[i].v.toFixed(0);
    td = tr.cells[7];
    td.innerHTML = C[i].x.toFixed(1) + "%";
    td = tr.cells[8];
    td.innerHTML = C[i].y.toFixed(1) + "%";
    td = tr.cells[9];
    td.innerHTML = C[i].z.toFixed(1) + "%";
    td = tr.cells[10];
    td.innerHTML = C[i].q.toFixed(1) + "%";
    td = tr.cells[11];
    td.innerHTML = C[i].h == 1 ? "High" : C[i].h == -1 ? "Low" : "-";
    td = tr.cells[12];
    td.innerHTML = C[i].b == -999 ? "n/a" : C[i].b.toFixed(2);
    td = tr.cells[13];
    td.innerHTML = C[i].b == -999 ? "n/a" : C[i].c.toFixed(2);
    td = tr.cells[14];
    td.innerHTML = C[i].b == -999 ? "n/a" : C[i].e.toFixed(1) + "%";
    even = !even;
  }
  top.showOrd();
}

// Sorting function dispatcher (invoked by table column links)
function srtTbl(type, tbody, C)
{
  if (top.sTyp == type)
    top.sOrd *= -1;
  else {
    top.delOrd();
    top.sOrd = 1;
    top.sTyp = type;
  }
  setTimeout("top.controlFrame.document.getElementById('progress').style.visibility='hidden'", 300);
  switch (type) {
    case 0 : C.sort(sortByTicker); break;
    case 1 : C.sort(sortByName); break;
    case 2 : C.sort(sortByMCap); break;
    case 3 : C.sort(sortByADV); break;
    case 4 : C.sort(sortByFlow); break;
    case 5 : C.sort(sortByStkRank); break;
    case 6 : C.sort(sortByVol); break;
    case 7 : C.sort(sortByChg1); break;
    case 8 : C.sort(sortByChg5); break;
    case 9 : C.sort(sortByChg20); break;
    case 10 : C.sort(sortByChg60); break;
    case 11 : C.sort(sortByHigh); break;
    case 12 : C.sort(sortByBeta); break;
    case 13 : C.sort(sortByCorr); break;
    case 14 : C.sort(sortByVols); break;
  }
  drawTable(tbody, C)
}

// Sorting functions (invoked by sortTable())
function sortByTicker(x, y) {
  a = x.t.toLowerCase();
  b = y.t.toLowerCase();
  return ((a < b) ? -top.sOrd : ((a > b) ? top.sOrd : 0));
}
function sortByName(x, y) {
  a = x.n.toLowerCase();
  b = y.n.toLowerCase();
  return ((a < b) ? -top.sOrd : ((a > b) ? top.sOrd : 0));
}
function sortByStkRank(a, b) {
  if (b.t == "Sector")
    return -1;
  if (a.t == "Sector")
    return 1;
  return (a.r - b.r) * top.sOrd;
}
function sortByMCap(a, b) {
  return (b.m - a.m) * top.sOrd;
}
function sortByBeta(a, b) {
  return (b.b - a.b) * top.sOrd;
}
function sortByCorr(a, b) {
  return (b.c - a.c) * top.sOrd;
}
function sortByChg1(a, b) {
  return (b.x - a.x) * top.sOrd;
}
function sortByChg5(a, b) {
  return (b.y - a.y) * top.sOrd;
}
function sortByChg20(a, b) {
  return (b.z - a.z) * top.sOrd;
}
function sortByChg60(a, b) {
  return (b.q - a.q) * top.sOrd;
}
function sortByFlow(a, b) {
  if (b.t == "Sector")
    return 1;
  if (a.t == "Sector")
    return -1;
  return (b.s - a.s) * top.sOrd;
}
function sortByVol(a, b) {
  return (b.v - a.v) * top.sOrd;
}
function sortByADV(a, b) {
  return (b.d - a.d) * top.sOrd;
}
