// Determines if a string holds a valid number.
//
// Parameters:
//     number:   The string to check.
//     currency: True if the number can have a currency symbol in front.
function isValidNumber(number, currency)
{
    if (!number.match(/^\$?(\d[\d,]*(\.\d*)?|\.\d+)$/))
        return false;
        
    if (currency != null && currency)
        return true;
    
    return !number.match(/^\$/);
}

// Reformats a number by inserting commas and padding out the number of digits
// and decimal places.
//
// Parameters:
//     number:        The number to format. All non-numeric characters are
//                    stripped out first.
//     digits:        The minimum number of digits to the left of the decimal
//                    point. The extra places are padded with zeros.
//     decimalPlaces: The number of places after the decimal point, or zero to
//                    omit the decimal point.
//     withCommas:    True to insert commas every 3 places, false to omit them.
function formatNumber(number, digits, decimalPlaces, withCommas)
{
        number       = number.toString();
    var simpleNumber = '';
    
    // Strips out the dollar sign and commas.
    for (var i = 0; i < number.length; ++i)
    {
        if ("0123456789.".indexOf(number.charAt(i)) >= 0)
            simpleNumber += number.charAt(i);
    }
    
    number = parseFloat(simpleNumber);
    
    if (isNaN(number))      number     = 0;
    if (withCommas == null) withCommas = false;
    if (digits     == 0)    digits     = 1;
    
    var integerPart = (decimalPlaces > 0 ? Math.floor(number) : Math.round(number));
    var string      = "";
    
    for (var i = 0; i < digits || integerPart > 0; ++i)
    {
        // Insert a comma every three digits.
        if (withCommas && string.match(/^\d\d\d/))
            string = "," + string;
    
        string      = (integerPart % 10) + string;
        integerPart = Math.floor(integerPart / 10);
    }
    
    if (decimalPlaces > 0)
    {
        number -= Math.floor(number);
        number *= Math.pow(10, decimalPlaces);
        
        string += "." + formatNumber(number, decimalPlaces, 0);
    }
    
    return string;
}

// Determines if a string contains a valid date.
//
// Parameters:
//     date: The string to check.
function isValidDate(date)
{
    date = date.toString().toLowerCase();

    if (date == "today" || date == "yesterday" || date == "tomorrow")
        return true;
    
    with (new Date(date))
        return !isNaN(getDate()) && getFullYear() >= 1970 && getFullYear() < 2038;
}

// Formats the date as Month Day, Year.
function formatDate(date)
{
    date = date.toString().toLowerCase();

    if      (date == "today")     date = new Date();
    else if (date == "yesterday") date = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 1);
    else if (date == "tomorrow")  date = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() + 1);
    else                          date = new Date(date);
    
    with (date)
        return MONTHS[getMonth()] + " " + getDate() + ", " + getFullYear();
}
