//-----------------------------------------------------------------------------
// MyTable
//-----------------------------------------------------------------------------
function MyTableCell(value, sortValue)
{
    this.value = value;
    this.sortValue = sortValue;
}

function MyTableRow(table)
{
    this.table = table;
    this.cells = new Array();
}

function MyTable(id, defSortList)
{
    this.id = id;
    this.rows = new Array();
    this.titleList = '';
    this.sortOrder = 0;  // ascending = 0, descending = 1
    this.sortCol = 0;
    this.defSortList = defSortList;
    this.preSortCol = 0;
    this.customDrawTitleCell = null;
}

MyTable.prototype.header = function()
{
    return document.getElementById(this.id + "_header");
}

MyTable.prototype.body = function()
{
    return document.getElementById(this.id + "_bodyTable");
}

MyTable.prototype.onClick = function(event)
{
    var cell = this;
    var self = cell.myTable;
    if (self.sortCol == cell.colIndex) {
        self.sortOrder = 1 - self.sortOrder;
    }
    else {
        self.sortOrder = 0;
        self.sortCol = cell.colIndex;
    }

    self.redraw();
}

MyTable.prototype.drawTitleCell = function(col)
{
    if (this.customDrawTitleCell != null) {
        if (this.customDrawTitleCell(col)) {
            return;
        }
    }

    var cell = this.header().rows[0].cells[col];

    cell.innerHTML = "<u>" + this.titleList[col] + "</u>";
    if (col == this.sortCol) {
        cell.innerHTML += "&nbsp;";
        if (this.sortOrder == 0) {
            cell.innerHTML += '<img src="CapEst_Files/triangle_up.png">';
        }
        else {
            cell.innerHTML += '<img src="CapEst_Files/triangle_down.png">';
        }
    }
}

MyTable.prototype.initialize = function(titleList)
{
    this.titleList = titleList;

    var div = document.getElementById(this.id);
    div.className = "MyTable";

    var header = document.createElement("table");
    div.appendChild(header);
    header.id = this.id + "_header";
    header.className = "MyTableHeader";
    header.width = "100%";

    var body = document.createElement("div");
    div.appendChild(body);
    body.id = this.id + "_body";
    body.className = "MyTableBody";
    body.width = "100%";

    var bodyTable = document.createElement("table");
    body.appendChild(bodyTable);
    bodyTable.id = this.id + "_bodyTable";
    bodyTable.className = "MyTableBodyTable";
    bodyTable.width = "100%";

    var footer = document.createElement("table");
    div.appendChild(footer);
    footer.id = this.id + "_footer";
    footer.className = "MyTableFooter";
    footer.width = "100%";
    var row = footer.insertRow(0);
    var cell = row.insertCell(0);

    // configure header
    row = header.insertRow(0);
    row.style.cursor = "pointer";
    //row.id = "MyTableHeader";
    var i;
    for (i = 0; i < titleList.length; i++) {
        cell = row.insertCell(i);
        cell.id = this.id + "_header_" + i;
        //cell.align = "center";
        this.drawTitleCell(i);
        cell.myTable = this;
        cell.colIndex = i;
        cell.onclick = this.onClick;
    }
}

MyTable.prototype.empty = function()
{
    while (this.rows.length > 0) {
        this.rows.pop();
    }
}

MyTable.prototype.addRow = function(list)
{
    this.rows.push(list);
}

MyTable.prototype.compare = function(a, b)
{
    var col = a.table.sortCol;
    if (a.table.sortOrder != 0) {
        var tmp = b;
        b = a;
        a = tmp;
    }

    var diff = a.cells[col].sortValue - b.cells[col].sortValue;
    if (diff == 0) {
        var c;
        for (c in a.table.defSortList) {
            if (c != col) {
                diff = a.cells[c].sortValue - b.cells[c].sortValue;
                if (diff != 0) {
                    return diff;
                }
            }
        }
    }
    return diff;
}

MyTable.prototype.redraw = function()
{
    this.rows.sort(this.compare);

    var i;
    var table = this.body();
    for (i = table.rows.length-1; i >= this.rows.length; i--) {
        table.deleteRow(i);
    }

    for (i = 0; i < this.rows.length; i++) {
        var rowIsNew = false;;
        if (i >= table.rows.length) {
            table.insertRow(i);
            rowIsNew = true;
        }
        var tRow = table.rows[i];
        var row = this.rows[i];
        var j;
        for (j = 0; j < row.cells.length; j++) {
            if (rowIsNew) {
                tRow.insertCell(j);
            }
            var cell = tRow.cells[j];
            cell.id = this.id + "_body_" + j;
            cell.align = "right";
            cell.innerHTML = row.cells[j].value + "&nbsp";
        }
    }

    if (this.preSortCol != this.sortCol) {
        this.drawTitleCell(this.preSortCol);
        this.preSortCol = this.sortCol;
    }
    this.drawTitleCell(this.sortCol);
}
