/************************************** Splendid *************************************
* Created By:		Steve Doggett
* Creation Date:	22nd July 2009
* Edited ----------------------------------------------------------------------------
*      By:              On:
* Description -----------------------------------------------------------------------
*      This is a collection of little utility functions that come in handy.
*      Uses JQuery.
*
* Functions -------------------------------------------------------------------------
*       getTopOffset()              // Get the offest distance that the browser has scrolled down from the top.
*       checkForDodgyIE6Elements()  // Hides drop down, checkbox or radio button in ie6
*       String.format()             // Same as in .Net String.format("something with {0} and {2}", arg1, arg2);
**************************************************************************************/


/****
* Get the offest distance that the browser has scrolled down from the top.
****/
function getTopOffset() {
    if (document.all) {
        if (!document.documentElement.scrollTop)
            return parseInt(document.body.scrollTop);
        else
            return parseInt(document.documentElement.scrollTop);
    } else {
        return parseInt(window.pageYOffset);
    }

    return 0;
}

/****
* If a div is placed over a drop down, checkbox or radio button in ie6, then it shows though the div,
* so just hide them all on the page for ie6 to make sure it's ok.
****/
function checkForDodgyIE6Elements(strVisibility) {
    if ($.browser.msie && $.browser.version == "6.0") {
        $("select").css("visibility", strVisibility);
        $("input:radio").css("visibility", strVisibility);
        $("input:checkbox").css("visibility", strVisibility);
    }
}

/****
 * Same as in .Net String.format("something with {0} and {2}", arg1, arg2);
 ****/
String.format = function( text )
{
    //check if there are two arguments in the arguments list
    if ( arguments.length <= 1 )
    {
        //if there are not 2 or more arguments there’s nothing to replace
        //just return the original text
        return text;
    }

    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for( var token = 0; token <= tokenCount; token++ )
    {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace( new RegExp( "\\{" + token + "\\}", "gi" ), arguments[ token + 1 ] );
    }

    return text;
};

