﻿// Trims leading and trailing white-space from a string.
String.prototype.trim = function()
{
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

// Tests if a variable can be treated as numeric. Empty strings are treated as non-numeric.
function isNumeric(num)
{
	return (num >= 0 || num < 0) && num !== '';
}

// Adds a function to the onload handler. Multiple onload functions will be chained.
function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			if (oldonload)
			{
				oldonload();
			}
			func();
		}
	}
}

function formatCurrency(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g, '');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue * 100 + 0.50000000001);
	intMinor = dblValue % 100;
	strMinor = intMinor.toString();
	dblValue = Math.floor(dblValue / 100).toString();
	if (intMinor < 10)
		strMinor = "0" + strMinor;
	for (var i = 0; i < Math.floor((dblValue.length - (1 + i)) / 3); i++)
		dblValue = dblValue.substring(0, dblValue.length - (4 * i + 3)) + ',' +
				dblValue.substring(dblValue.length - (4 * i + 3));
	return (((blnSign) ? '' : '-') + '£' + dblValue + '.' + strMinor);
}

function bind(context, func)
{
	return function()
	{
		return func.apply(context, arguments);
	}
}

function roundNumber(num, dec)
{
	var result = Math.round(Math.round(num * Math.pow(10, dec + 1)) / Math.pow(10, 1)) / Math.pow(10, dec);
	return result;
}

function isDate(sDate)
{
    try
    {
        var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
        if (re.test(sDate))
        {
            var dArr = sDate.split("/");
            var d = Date.parseLocale(sDate);
            return d.getMonth() + 1 == dArr[1] && d.getDate() == dArr[0] && d.getFullYear() == dArr[2];
        }
        else
        {
            return false;
        }
    }
    catch (e)
    {
        return false;
    }
}
                

