/**
 * Convenience method for loading a DOM element by id
 * @param id the id string for a page element that you wish to load
 * @return a reference to the DOM element with the given id or null if
 * no such element exists
 */
function $(id)
{
    return document.getElementById(id);
}

/**
  * Adds a trim function to the standard javascript string
  * @return returns the trimmed string
  */
String.prototype.trim = function ()
{
    return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
};

/**
 * Adds the ability to include a script from within another script.
 * @param script_filename the filename of the script to include
 * @return returns true if script was newly included, false if script was previously included
 */
var st_base = {
    included_files: new Array()
}

function include(script_filename) {
    // If the given script has already been included, return false
    for (var i = 0; i < st_base.included_files.length; i++) {
        if (st_base.included_files[i] == script_filename) return false;
    }

    // Otherwise, include the script
    var head = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    head.appendChild(js);

    // Record inclusion for future reference and return true
    st_base.included_files[st_base.included_files.length] = script_filename;
    return true;
}

/**
 * Add the given function to the list of functions being called on page load.
 * @param new_function a reference to the function to be called on load
 */
function addOnLoad(new_function) {
    var oldOnLoad = window.onload;
    window.onload = function () {
        if(typeof oldOnLoad == 'function') oldOnLoad();
        new_function();
    }
}
