/** Hide an html element given its id */
function hideElementById(id)
{
    // hide the element by setting css attribute
    document.getElementById(id).style.display = 'none'
}

// Show the subversion revision number in the page title
function showRevision()
{
    var title = document.title;
    title = title + revisionNumber;
    document.title = title;
}

// Load a CSV file and convert to a string of formatted HTML
// @param filename the file containing the CSV input
// @param targetDiv the div element in which the HTML should be placed.
// @param sortOrder true if the items are shown in ascending order by time
function loadLog(filename, targetDiv, sortOrder)
{
    // Asynchronous file read of unified log data
    loadJSON("dynamicdata/" + filename, function(response)
    {
        // Split the file into rows
        var allRows = response.split(/\r?\n|\r/);
        targetDiv.innerHTML = formatTable(allRows, sortOrder);
    }
    );
}

// Format the csv data into an HTML table
function formatTable(allRows, sortOrder)
{
    var table = '<table>';
    if (sortOrder) // ascending order of time (most recent last)
    {
        // Put the last log entry at the BOTTOM of the table
        for (var singleRow = 0; singleRow <= allRows.length-1; singleRow++)
        {
            // split each row into fields
            var fields = allRows[singleRow].split(',');
            table += formatRow(fields)
        }
    }
    else  // descending order of time (most recent first)
    {
        // Put the last log entry at the TOP of the table
        for (var singleRow = allRows.length-1; singleRow >= 0; singleRow--)
        {
            // split each row into fields
            var fields = allRows[singleRow].split(',');
            table += formatRow(fields)
        }
    }
    
    table += '</table>';
    return table;
}

// Format a single row of fields into a table row
function formatRow(rowCells)
{
        var msg_type = ""; // color white is default

        // trimming white space in the type field 
        // ingore the empty line 
        if (rowCells.length > 1) 
        {
            rowCells[1] = rowCells[1].trim();
        }
        var label = String(rowCells[1]);
        var info = String(rowCells[3]).trim();
        // Implement ticket #190 
        // Checking for the type of logging information 
        if (label.startsWith("CAD")) {
            msg_type = "class=\"CAD\"";
            if (info.startsWith("Detail")) {
                msg_type = "class=\"CADdetail\"";
            }
        } else if (label.startsWith("Activity")) {
            msg_type = "class=\"Activity\"";
        //} else if (rowCells[1] == "CMS Activated.") {
        } else if (label.startsWith("CMS")) {
            msg_type = "class=\"CMS\"";
        } else if (label.startsWith("Eval")) {
            msg_type = "class=\"Evaluation\"";
        } 

        // add the message type class to that row 
        var row = '<tr ' + msg_type + '>';
        for (var rowCell = 0; rowCell < rowCells.length; rowCell++)
        {
            row += '<td>';
            row += rowCells[rowCell];
            row += '</td>';
        }
        row += '</tr>';
        return row;
}
