/*
    Array Browser / Table display function
    --------------------------------------
    Copyright 2001-2004, DeskWeb consultancy
    All rights reserved
*/
function dispArBrowser(PasValues, PsGlobname, PsHeadclass, PsSortedheadclass, PsCellclass, PsName, PnSortcol, PsWidth, PnItemsPerPage, PnPage) {
    document.write ("<div id='" + PsName + "'>");

    if (! PnSortcol)
        PnSortcol = 0;

    LsHTML = getArBrowser(PasValues, PsGlobname, PsHeadclass, PsSortedheadclass, PsCellclass, PnSortcol, PsName, PsWidth, PnItemsPerPage, PnPage);
    document.write (LsHTML);

    document.write ("</div>");
}

function getArBrowser(PasValues, PsGlobname, PsHeadclass, PsSortedheadclass, PsCellclass, PnSortcol, PsName, PsWidth, PnItemsPerPage, PnPage) {
    resortAr(PasValues, PnSortcol);

    LsRetval = "";

    LsRetval += "<table cellpadding=1 cellspacing=1 border=0" + (PsWidth ? " width='" + PsWidth + "'" : "") + ">";

    LnCurPage = PnPage;

    if (PnItemsPerPage >= (PasValues.length - 2)) {
        PnItemsPerPage = 0;
    }

    // Header row ...
    LsRetval += "\n<TR>";
    LasAlign = new Array();
    LnVisibleColumns = 0;

    for (i=0; i < PasValues[0].length; i++) {
        // Determine alignment
        LsTemp = PasValues[0][i];

        if (LsTemp != "") {
            LsAlign = "";
            LbSort  = true;
            LnVisibleColumns++;

            if (LsTemp.substring(0, 1) == '<') {
                LsAlign = " align=left ";
                LsTemp  = LsTemp.substring(1);
            }
            else if (LsTemp.substring(0, 1) == '>') {
                LsAlign = " align=right ";
                LsTemp  = LsTemp.substring(1);
            }
            else if (LsTemp.substring(0, 1) == '+') {
                LsAlign = " align=center ";
                LsTemp  = LsTemp.substring(1);
            }
            else if (LsTemp.substring(0, 1) == '-') {
                LsAlign = " align=center ";
                LsTemp  = LsTemp.substring(1);
                LbSort  = false;
            }
            else {
                LsAlign = " align=left ";
            }

            LasAlign[i] = LsAlign;

            if (PnSortcol == i) {
                LnSortcol = i - 100;
                LsClass   = PsSortedheadclass;
            }
            else if (PnSortcol == (i - 100)) {
                LnSortcol = i;
                LsClass   = PsSortedheadclass;
            }
            else {
                LnSortcol = i;
                LsClass   = PsHeadclass;
            }

            if (LbSort) {
                LsClick  = " onclick='javascript:sortArBrowser(" + PsGlobname + ",\"" + PsGlobname + "\",\"" + PsHeadclass + "\",\"" +
                    PsSortedheadclass + "\",\"" + PsCellclass + "\", " + LnSortcol + ",\"" + PsName + "\", \"" + PsWidth + "\", " +
                    PnItemsPerPage + ", " + PnPage + ");' ";
            }
            else {
                LsClick  = "";
            }

            LsRetval += "<td class='" + LsClass + "' width='" + PasValues[1][i] + "'" + LsAlign + " " + LsClick + ">";
            LsRetval += "<a href='#' class='" + LsClass + "'>" + LsTemp + "</a>";
            LsRetval += "</td>";
        }
    }
    LsRetval += "</TR>";

    LbPaging = (PnItemsPerPage ? true : false);
    LbItem   = 0;

    // Data rows ...
    for (j=2; j < PasValues.length; j++) {
        if (LbPaging) {
            LbAddline = false;

            if (PnPage == 0) {
                LbAddline = true;
            }
        }
        else {
            LbAddline = true;
        }

        if (LbAddline) {
            LsLine = "<TR>";

            for (i=0; i < PasValues[0].length; i++) {
                if (LasAlign[i]) {
                    LsLine += "<td class='" + PsCellclass + "'" + LasAlign[i] + "  width='" + PasValues[1][i] + "'>";
                    LsLine += PasValues[j][i];
                    LsLine += "</td>";
                }
            }

            LsLine += "</TR>";

            LsRetval += LsLine;
        }

        if (LbPaging) {
            LbItem++;
            if (LbItem == PnItemsPerPage) {
                LbItem = 0;

                PnPage--;

                if (PnPage < 0) {
                    break;
                }
            }
        }
    }

    // Add page navigation (if necessary)
    if (LbPaging) {
        LnTotPages = Math.round((PasValues.length - 2) / PnItemsPerPage);
        if ((LnTotPages * PnItemsPerPage) < (PasValues.length - 2)) {
            LnTotPages++;
        }

        LsNavigation = "<TR><TD COLSPAN=" + LnVisibleColumns + " ALIGN=RIGHT CLASS='" + PsHeadclass + "'>";

        LsTable       = "<table width=100% cellspacing=1 cellpadding=1 border=0><td>";
        LsTable      += "<td class='" + PsHeadclass + "'>&nbsp;</td>";

        if (LnCurPage > 0) {
            LsPageLeft  = " onclick='javascript:sortArBrowser(" + PsGlobname + ",\"" + PsGlobname + "\",\"" + PsHeadclass + "\",\"" +
                PsSortedheadclass + "\",\"" + PsCellclass + "\", " + PnSortcol + ",\"" + PsName + "\", \"" + PsWidth + "\", " +
                PnItemsPerPage + ", " + (LnCurPage - 1) + ");' ";

            LsTable      += "<td width=30 align=center class='" + PsHeadclass + "'><span " + LsPageLeft + " class='" + PsHeadclass + "'>&lt;&lt;</span></td>";
        }
        else {
            LsTable      += "<td width=30 align=center class='" + PsHeadclass + "'>&nbsp;</td>";
        }

        LsTable      += "<TD WIDTH=80 ALIGN=CENTER CLASS='" + PsHeadclass + "'>Page " + (LnCurPage + 1) + "/" + LnTotPages + "</TD>";

        if (LnCurPage < (LnTotPages - 1)) {
            LsPageRight  = " ONCLICK='javascript:sortArBrowser(" + PsGlobname + ",\"" + PsGlobname + "\",\"" + PsHeadclass + "\",\"" +
                PsSortedheadclass + "\",\"" + PsCellclass + "\", " + PnSortcol + ",\"" + PsName + "\", \"" + PsWidth + "\", " +
                PnItemsPerPage + ", " + (LnCurPage + 1) + ");' ";

            LsTable      += "<TD WIDTH=30 ALIGN=CENTER CLASS='" + PsHeadclass + "'><SPAN " + LsPageRight + " CLASS='" + PsHeadclass + "'>&gt;&gt;</SPAN></TD>";
        }
        else {
            LsTable      += "<TD WIDTH=30 ALIGN=CENTER CLASS='" + PsHeadclass + "'>&nbsp;</TD>";
        }

        LsTable      += "</TD></TABLE>";
        LsNavigation += LsTable + "</TR>";

        LsRetval += LsNavigation;
    }


    LsRetval += "</TABLE>";

    return (LsRetval);
}

function sortArBrowser(PasValues, PsGlobname, PsHeadclass, PsSortedheadclass, PsCellclass, PnSortcol, PsName, PsWidth, PnItemsPerPage, PnPage) {
    document.all(PsName).innerHTML = getArBrowser(PasValues, PsGlobname, PsHeadclass, PsSortedheadclass, PsCellclass, PnSortcol, PsName, PsWidth, PnItemsPerPage, PnPage);
}

function resortAr(PasValues, PnSortcol) {
    LnumCount = PasValues.length;

    if (PnSortcol >= 0) {
        // Special... if the next column has an empty header, use that column to sort by...
        PnSortcol = adjustSortcol(PasValues, PnSortcol);

        // Sort ascending
        for (i=2; i < LnumCount - 1; i++) {
            for (j=i+1; j < LnumCount; j++) {
                if (PasValues[i][PnSortcol] > PasValues[j][PnSortcol]) {
                    PasTemp = PasValues[i];
                    PasValues[i] = PasValues[j];
                    PasValues[j] = PasTemp;
                }
            }
        }
    }
    else {
        PnSortcol += 100;

        // Special... if the next column has an empty header, use that column to sort by...
        PnSortcol = adjustSortcol(PasValues, PnSortcol);

        // Sort descending
        for (i=2; i < LnumCount - 1; i++) {
            for (j=i+1; j < LnumCount; j++) {
                if (PasValues[i][PnSortcol] < PasValues[j][PnSortcol]) {
                    PasTemp = PasValues[i];
                    PasValues[i] = PasValues[j];
                    PasValues[j] = PasTemp;
                }
            }
        }
    }
}

function adjustSortcol(PasValues, PnSortcol) {
    LnSortcol = PnSortcol;

    if (PnSortcol < PasValues[0].length) {
        LsTemp = PasValues[0][PnSortcol + 1];

        if (LsTemp == "") {
            LnSortcol = PnSortcol + 1;
        }
    }

    return (LnSortcol);
}
