﻿function FormatCurrency (fCurrency)
{
    var sDisplay = parseFloat (Math.round(fCurrency*100)/100).toString (); 
    
    var iIndexOfDecimal = sDisplay.indexOf ('.');
    if (iIndexOfDecimal >= 0)
    {
	    if(sDisplay.indexOf('.') == (sDisplay.length - 2)) 
	    { 
	        sDisplay += '0'; 
	    }        
	    sDisplay = sDisplay.substring (0, iIndexOfDecimal+3);
    }
    else
    {
        if (sDisplay.indexOf ("e") < 0)
        {
            sDisplay += '.00';
        }
    }
    
    //strip off the leading '-' so that comma's won't be incorrectly placed.
    if (fCurrency < 0)
    {
        sDisplay = sDisplay.substr(1);
    }

    // add the commas in
    iIndexForComma = sDisplay.indexOf ('.') - 3;
    while (iIndexForComma > 0)
    {
        sDisplay = sDisplay.substring (0, iIndexForComma) + "," + sDisplay.substr (iIndexForComma);
        iIndexForComma -= 3;
    }
    
    //Format Negative Numbers with ($#####.##)
    if (fCurrency < 0) 
        sDisplay = '($' + sDisplay + ')'; 
        //sDisplay = '($' + sDisplay.substr(1) + ')'; 
    else
        sDisplay = "$" + sDisplay;
        
    return sDisplay;
}

function FormatFloat (fFloat)
{    
    var sDisplay = parseFloat (Math.round(fFloat*100)/100).toString (); 
    var iIndexOfDecimal = sDisplay.indexOf ('.');
    if (iIndexOfDecimal >= 0)
    {
    	if(sDisplay.indexOf('.') == (sDisplay.length - 2)) 
	    { 
	        sDisplay += '0'; 
	    }  
        sDisplay = sDisplay.substring (0, iIndexOfDecimal+3);
    }
    else
    {
        if (sDisplay.indexOf ("e") < 0)
        {
            sDisplay = sDisplay + ".00";    
        }
    }
    
    return sDisplay;
}

function FormatDate (sISO8601Date)
{
    // dates are always in the XML as ISO8601 dates (YYYY-MM-DDTHH:MM:SS)... let's format it as 'MM/DD/YYYY' for display
    return (sISO8601Date.length > 0) ? parseInt(sISO8601Date.substring (5, 7), 10) + '/' + parseInt(sISO8601Date.substring (8, 10), 10) + '/' + sISO8601Date.substring (0, 4) : "";
}

function FormatISO8601Date (dDate)
{
    var sMonth = (dDate.getMonth() + 1).toString();
    if (sMonth.length == 1)
    {
        sMonth = "0" + sMonth;
    }
    
    var sDay = dDate.getDate().toString();
    if (sDay.length == 1)
    {
        sDay = "0" + sDay;
    }
    
    var sYear = dDate.getFullYear().toString();
    while (sYear.length < 4)
    {
        sYear = "0" + sYear;
    }
    
    return sYear + '-' + sMonth + '-' + sDay + 'T00:00:00';
}

function FormatISO8601DateTime (dDate)
{
    var sMonth = (dDate.getMonth() + 1).toString();
    if (sMonth.length == 1)
    {
        sMonth = "0" + sMonth;
    }
    
    var sDay = dDate.getDate().toString();
    if (sDay.length == 1)
    {
        sDay = "0" + sDay;
    }
    
    var sYear = dDate.getFullYear().toString();
    while (sYear.length < 4)
    {
        sYear = "0" + sYear;
    }
    
    var sHours = dDate.getHours().toString();
    if (sHours.length == 1)
    {
        sHours = "0" + sHours;
    }
    
    var sMinutes = dDate.getMinutes().toString();
    if (sMinutes.length == 1)
    {
        sMinutes = "0" + sMinutes;
    }

    var sSeconds = dDate.getSeconds().toString();
    if (sSeconds.length == 1)
    {
        sSeconds = "0" + sSeconds;
    }
  
    
    return sYear + '-' + sMonth + '-' + sDay + 'T' + sHours + ":" + sMinutes + ":" + sSeconds;
}


function FormatDateTime (sISO8601DateTime)
{
    var dDateTime = new Date ();
 
    dDateTime.setFullYear (parseInt(sISO8601DateTime.substring (0, 4), 10));
    dDateTime.setMonth (parseInt(sISO8601DateTime.substring (5, 7)-1, 10));
    dDateTime.setDate (parseInt(sISO8601DateTime.substring (8, 10), 10));
    
    dDateTime.setHours (parseInt(sISO8601DateTime.substring (11, 13), 10));
    dDateTime.setMinutes (parseInt(sISO8601DateTime.substring (14, 16), 10));
    dDateTime.setSeconds (parseInt(sISO8601DateTime.substring (17, 19), 10));
    
    return dDateTime;
}

function TrimLeadingZeroes (s)
{
    while ((s.length > 1) && (s.charAt (0) == "0"))
    {
        s = s.substr (1);
    }
    
    return s;
}

function Trim(stringToTrim) 
{
    if (stringToTrim != null)
        // REG - 8/7/07 - fixed 
        //return stringToTrim.replace(/^\s+|$/g,"");
        return stringToTrim.replace(/^\s+|\s+$/g,"");
    else
        return stringToTrim;
}

function LTtrim(stringToTrim)
{
    if (stringToTrim != null)
        return stringToTrim.replace(/^\s+/,"");
    else
        return stringToTrim;
}

function RTrim(stringToTrim)
{
    if (stringToTrim != null)
    	return stringToTrim.replace(/\s+$/,"");
    else
        return stringToTrim;
}

function FormatUTCDateTime (sISO8601UTCDateTime)
{
    var dDateTime = new Date ();
    
    dDateTime.setUTCFullYear (parseInt(TrimLeadingZeroes(sISO8601UTCDateTime.substring (0, 4)), 10));
    dDateTime.setUTCMonth (parseInt(TrimLeadingZeroes(sISO8601UTCDateTime.substring (5, 7))-1, 10));
    dDateTime.setUTCDate (parseInt(TrimLeadingZeroes(sISO8601UTCDateTime.substring (8, 10)), 10));
    
    dDateTime.setUTCHours (parseInt(TrimLeadingZeroes(sISO8601UTCDateTime.substring (11, 13)), 10));
    dDateTime.setUTCMinutes (parseInt(TrimLeadingZeroes(sISO8601UTCDateTime.substring (14, 16)), 10));
    dDateTime.setUTCSeconds (parseInt(TrimLeadingZeroes(sISO8601UTCDateTime.substring (17, 19)), 10));
    
    return dDateTime;
}



function FormatTime (sTime)
{
    // times are always in the XML as ISO8601 dates (YYYY-MM-DDTHH:MM:SS) ...
    if(sTime.length>0)
    {
        var dTime = new Date (0);
        dTime.setHours (parseInt(TrimLeadingZeroes(sTime.substring (11, 13)), 10));
        dTime.setMinutes (parseInt(TrimLeadingZeroes(sTime.substring (14, 16)), 10));
        dTime.setSeconds (parseInt(TrimLeadingZeroes(sTime.substring (17, 19)), 10));
        
        return dTime.toLocaleTimeString();
    }

    return "";
}


function XMLEncode (s)
{
    if (s != null && isNaN(s)) 
    {
        s = s.replace (/&/g, "&amp;");
        s = s.replace (/"/g, "&quot;");
        s = s.replace (/</g, "&lt;");
        s = s.replace (/>/g, "&gt;");
    }
    
    return s;
}

function HTMLEncode (s)
{
    if (s != null && isNaN(s)) 
    {
        s = s.replace (/&/g, "&amp;");
        s = s.replace (/"/g, "&quot;");
        s = s.replace (/</g, "&lt;");
        s = s.replace (/>/g, "&gt;");
        s = s.replace (/'/g, "&apos;");
    }
    
    return s;
}