function addLoadEvent(func)
{
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

/**
 * @param int gap Delay in ms.
 * @return void
*/
function sleep(gap)
{
    var then,now;
    then = new Date().getTime();
    now = then;
    while ((now - then) < gap) {
        now = new Date().getTime();
    }
}

if (self.Node && self.Node.prototype) {
    Node.prototype.swapNode = function(node)
    {
        // current
        var thisNode = this;
        var thisNextSibling = thisNode.nextSibling;
        var thisParentNode = thisNode.parentNode;
        
        // one that this is being swapped with
        var thatNode = node;
        var thatNextSibling = thatNode.nextSibling;
        var thatParentNode = thatNode.parentNode;
        
        if (thatNode == thisNextSibling) {
            thisParentNode.removeChild(thatNode);
            thisParentNode.insertBefore(thatNode, thisNode);
        } else {
            thatParentNode.replaceChild(thisNode, thatNode);
            if (thisNextSibling) {
                thisParentNode.insertBefore(thatNode, thisNextSibling);
            } else {
                thisParentNode.appendChild(thatNode);
            }
        }
    }
}

/**
 * Trims whitespace from beginning and end of string.
*/
String.prototype.trim = function()
{
    return this.replace(/^\s+|\s+$/, '');
}

/**
 * Trims whitespace from beginning of string.
*/
String.prototype.lTrim = function()
{
    return this.replace(/^\s*(.*)/, "$1");
}

/**
 * Trims whitespace from end of string.
*/
String.prototype.rTrim = function()
{
    return this.replace(/(.*)\s*$/, "$1");
}

/**
 * A list; like 'foo,bar,baz'.
 * @var string list The list itself.
 * @var string delim The delimeter.
 * @param string str The list itself.
*/
function List(str)
{
    this.list = new String(str);
    this.delim = ',';
    if (arguments[1] != null) this.delim = arguments[1]
        
    /**
     * @return int How many items are in the list.
    */
    this.len = function()
    {
        return this.list.split(this.delim).length;
    }
    
    /**
     * @param int pos The position of the element in the list.
     * @return string The element at the specified position.
    */
    this.getAt = function(pos)
    {
        var tempArray = this.list.split(this.delim);
        if (pos >= tempArray.length) {
            return false;
        } else {
            return tempArray[pos];
        }
    }
    
    /**
     * @param int pos The position of the element in the list.
     * @param string val The new value.
     * @return string The new list property.
    */
    this.setAt = function(pos, val)
    {
        var tempArray = this.list.split(this.delim);
        tempArray[pos] = val;
        this.list = tempArray.join(this.delim);
        return this.list;
    }
}


function listLen(list)
{
    if (arguments[1] == null)    list = new List(list);
    else                         list = new List(list, arguments[1]);
    
    return list.len();
}

function listGetAt(list, pos)
{
    if (arguments[2] == null)    list = new List(list);
    else                         list = new List(list, arguments[2]);
    
    return list.getAt(pos);
}

function listSetAt(list, pos, val)
{
    if (arguments[3] == null)    list = new List(list);
    else                         list = new List(list, arguments[3]);
    
    return list.setAt(pos, val);
}

function print_r(arr)
{
    var str = "[ ";
    
    for (i in arr) {
        if (typeof arr[i] == "object") str += print_r(arr[i])
        else str += i + " => " + arr[i] + ", ";
    }
    
    str = str.substr(0, str.length - 2) + " ]";
    
    if (arguments.caller != null) str += ", ";
    else str += " ";
    
    return str;
}


/**
 * This function creates makes the body's font-size larger
 *         if the user has an absurdly small font-size chosen.
 * 
 * It does this by creating a div, appending it to the body,
 * and measuring its size. 
 * 
*/
function fixSmallText()
{
    if (document.createElement && document.createTextNode) {
        body = document.getElementsByTagName('body')[0];
        
        div = document.createElement('div');
        div.id = 'emsTest';
        div.style.position = 'absolute';
        div.style.visibility = 'hidden';
        
        for (var i = 0; i < 5; i++) {
            div.appendChild(document.createTextNode(' '));
            div.appendChild(document.createElement('br'));
        }
        
        body.appendChild(div);
        
        var scaling = 100;
        var modifier = 80;
        if ((navigator.platform == "Win32") && (navigator.appName == "Microsoft Internet Explorer")) scaling = 110;
        var h = 999;
        if (document.getElementById('emsTest').clientHeight) h = parseInt(document.getElementById('emsTest').clientHeight);
        else if (document.getElementById('emsTest').offsetHeight) h = parseInt(document.getElementById('emsTest').offsetHeight);
        if (h < 100) document.body.style.fontSize = Math.round(scaling * modifier / h) + "%";
        body.removeChild(div);
    }
}
