String.whiteSpace = [];
String.whiteSpace[0x0009] = true;
String.whiteSpace[0x000a] = true;
String.whiteSpace[0x000b] = true;
String.whiteSpace[0x000c] = true;
String.whiteSpace[0x000d] = true;
String.whiteSpace[0x0020] = true;
String.whiteSpace[0x0085] = true;
String.whiteSpace[0x00a0] = true;
String.whiteSpace[0x1680] = true;
String.whiteSpace[0x180e] = true;
String.whiteSpace[0x2000] = true;
String.whiteSpace[0x2001] = true;
String.whiteSpace[0x2002] = true;
String.whiteSpace[0x2003] = true;
String.whiteSpace[0x2004] = true;
String.whiteSpace[0x2005] = true;
String.whiteSpace[0x2006] = true;
String.whiteSpace[0x2007] = true;
String.whiteSpace[0x2008] = true;
String.whiteSpace[0x2009] = true;
String.whiteSpace[0x200a] = true;
String.whiteSpace[0x200b] = true;
String.whiteSpace[0x2028] = true;
String.whiteSpace[0x2029] = true;
String.whiteSpace[0x202f] = true;
String.whiteSpace[0x205f] = true;
String.whiteSpace[0x3000] = true;

/*
* Trim spaces from a string on the left and right.
*/

String.prototype.trim = function()
{
var str = this;
var n = str.length;
var s;
var i;

if (!n)
return str;
s = String.whiteSpace;
if (n && s[str.charCodeAt(n-1)])
{
do
{
-n;
}
while (n && s[str.charCodeAt(n-1)]);
if (n && s[str.charCodeAt(0)])
{
i = 1;
while (i < n && s[str.charCodeAt(i)])
++i;
}
return str.substring(i, n);
}
if (n && s[str.charCodeAt(0)])
{
i = 1;
while (i < n && s[str.charAt(i)])
++i;
return str.substring(i, n);
}
return str;
};

var bOfficeSort = 1;
var bProductSort = 1;


function compareDate(a,b)
{
    var aTime = Date.parse(a["date"].trim() + " " + a["time"].trim().substr(0,2) + ":" + a["time"].trim().substr(2,2));
    var bTime = Date.parse(b["date"].trim() + " " + b["time"].trim().substr(0,2) + ":" + b["time"].trim().substr(2,2));

    if (aTime < bTime)
        return 1;
    else if (aTime > bTime)
        return -1;
    else
        return 0;
}

function sortOffice()
{
    bProductSort = 1;
    bOfficeSort = (bOfficeSort)?0:1;
    var bigger = (bOfficeSort)?-1:1;
    var smaller = (bOfficeSort)?1:-1;
    var arrData = extractTableData();
    arrData.sort(
        function(a,b)
        {
            if (a["office"].trim() < b["office"].trim())
                return smaller;
            else if (a["office"].trim() > b["office"].trim())
                return bigger;
            else
            {
                return compareDate(a,b);
            }
        }
                );

    rebuildTable(arrData);
}

function sortProduct()
{
    bOfficeSort = 1;
    bProductSort = (bProductSort)?0:1;
    var bigger = (bProductSort)?-1:1;
    var smaller = (bProductSort)?1:-1;
    var arrData = extractTableData();

    arrData.sort(
        function(a,b)
        {
            if (a["product"] < b["product"])
                return smaller;
            else if (a["product"] > b["product"])
                return bigger;
            else
            {
                return compareDate(a,b);
            }
        }
                );
    rebuildTable(arrData);
}

function rebuildTable(arrData)
{
    var oTbody = document.getElementById('sortable');
    for (var i=0; i<arrData.length; i++)
    {
        var oTR = document.createElement("tr");
        var oTD = document.createElement('td');
        oTD.appendChild(document.createTextNode(arrData[i]["date"]));
        oTR.appendChild(oTD);
        oTD = document.createElement('td');
        oTD.appendChild(document.createTextNode(arrData[i]["time"]));
        oTR.appendChild(oTD);
        oTD = document.createElement('td');
        oTD.appendChild(document.createTextNode(arrData[i]["office"]));
        oTR.appendChild(oTD);
        oTD = document.createElement('td');
        oTD.innerHTML = arrData[i]["productURL"];
        oTR.appendChild(oTD);

        oTbody.appendChild(oTR);
    }
    
    oTbody.style.display = "";
}

function extractTableData()
{
    var oTbody = document.getElementById('sortable');
    var arrRow = oTbody.getElementsByTagName('tr');
    var arrData = new Array;
    
    oTbody.style.display = "none";
    
    for (var i=arrRow.length-1; i>=0; --i)
    {

        arrCells = arrRow[i].getElementsByTagName('td');
        var oTemp = new Object;
        oTemp.date = getInnerText(arrCells[0]);
        oTemp.time = getInnerText(arrCells[1]);
        oTemp.office = getInnerText(arrCells[2]);
        oTemp.productURL = arrCells[3].innerHTML;

        var oAnchor = arrCells[3].getElementsByTagName('a');
        if (oAnchor.length)
        {
            oTemp.product = getInnerText(oAnchor[0]);
        }
        else
        {
            oTemp.product = "";
        }

        arrData.push(oTemp);

        oTbody.removeChild(arrRow[i]);
    }
    return arrData;
}

function getInnerText(obj)
{
    if (obj.childNodes)
    {
        for (var i=0; i<obj.childNodes.length; i++)
        {
            if (obj.childNodes[i].nodeType == 3)
            {
                return obj.childNodes[i].nodeValue;
            }
        }
    }
    return "";
}