﻿// Anti name-mangling
var _masterPageID = 'ctl00';
var _placeholders = new Array();
function initialiseAntiNameMangling(masterPageID)
{
    _masterPageID = masterPageID
    registerContentPlaceholder('');
    
    document.newGetElementById = document.getElementById;
    document.getElementById = function(elementID)
    {
        var firstTry;

        firstTry = document.newGetElementById(elementID);
        if (firstTry)
        {
            return firstTry;
        }
        else
        {
            // Try each registered content placeholder
            for (var i = 0; i < _placeholders.length; i++)
            {
                var el = document.newGetElementById(_placeholders[i] + elementID);
                if (el)
                {
                    return el;
                }
            }
        }
    }
}

// Registers a content placeholder's ID so it can be found by it's unmangled name by getElementById.
function registerContentPlaceholder(placeholderID)
{
    if (placeholderID != '')
        _placeholders.push(_masterPageID + '_' + placeholderID + '_');
    else
        _placeholders.push(_masterPageID + '_');
}

/// Prototype.js $ function
function $()
{
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++)
    {
        var element = arguments[i];
        if (typeof element == 'string')
            element = document.getElementById(element);
        if (arguments.length == 1)
            return element;
        elements.push(element);
    }
    return elements;
}

// 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);
}

