////////////////////////////////////////////////////////////////////////////////////////////////////
// This is a collection of very useful javascript functions that simply
// many repetitive mundane tasks
////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
// $
//
// The famous dollar function is useful for obtaining a reference to a DOM
// element by its id or to grab an array of elements by their id
// Examples:
//            var obj = $('id');
//            var objA = document.getElementById('objA');
//            var objB = document.getElementById('objB');
//            var objs = $('id1', objA, objB, 'id2');
////////////////////////////////////////////////////////////////////////////////////////////////////
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 only one argument passed in, return a direct reference to the
        // object instead of returning a 1 element array
        if (arguments.length == 1)
            return element;

        elements.push(element);
    }

    return elements;
}// $
////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
// getCookie
//
// Retrieves the value of a cookie with a specified name.  If the cookie does not exist, the return
// value is null
// Example:
//          alert(getCookie("username"));
////////////////////////////////////////////////////////////////////////////////////////////////////
function getCookie(name) {
    var start = document.cookie.indexOf(name + "=");
    if (start == -1) return null;
    
    start = start + name.length + 1;

    var end = document.cookie.indexOf(';', start);
    if (end == -1) end = document.cookie.length;
    
    return decodeURI(document.cookie.substring(start, end));
}// getCookie
////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
// setCookie
//
// Creates or overwrites a cookie.  Optional parameters include expires, path, domain, and secure.
// The expires parameter is the number of days.
// Example:
//          setCookie("username", "user", 365); // set a one year cookie
////////////////////////////////////////////////////////////////////////////////////////////////////
function setCookie( name, value, expires, path, domain, secure ) {
    var today = new Date();
    today.setTime(today.getTime());
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );
    document.cookie = name + '=' + encodeURI(value)
                    + ((expires) ? ';expires=' + expires_date.toGMTString() : '')
                    + ((path) ? ';path=' + path : '')
                    + ((domain) ? ';domain=' + domain : '')
                    + ((secure) ? ';secure' : '');
}// setCookie
////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
// deleteCookie
//
// Removes a cookie from the cookie collection by setting the expiration date to the past
// Example:
//          deleteCookie("username");
////////////////////////////////////////////////////////////////////////////////////////////////////
function deleteCookie( name, path, domain ) {
    if (getCookie(name)) {
        document.cookie = name + '='
                        + ((path) ? ';path=' + path : '')
                        + ((domain) ? ';domain=' + domain : '')
                        + ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
    }
}// deleteCookie
////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
// addLoadHandler
//
// Adds a windows.onload event handler without overwriting any existing onload handlers already
// assigned to the window.onload event.  Previous handlers will be invoked first.
// Example:
//          function initVariables() {
//              g_Image = $('theImage');
//              g_Table = $('theTable');
//          }
//
//          addLoadHandler(initVariables);
//
//          addLoadHandler(function() { alert('Loading is done!'); });
//
////////////////////////////////////////////////////////////////////////////////////////////////////
function addLoadHandler(handler) {
    var parentHandler = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = handler;
    }
    else {
        window.onload = function() {
            parentHandler();
            handler();
        }
    }
}// addLoadHandler
////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
// String.trim (prototype)
//
// Trims both leading and trailing white space characters from a string.
// This particular algorithm is extremely fast. Refer to this website for more details...
// http://blog.stevenlevithan.com/archives/faster-trim-javascript
//
// Example: 
//			var s = "  a string with leading and trailing spaces   ";
//          s = s.trim();

String.prototype.trim = function() {
	return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}// trim
////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
// String.rtrim (prototype)
//
// Trims trailing white space characters from a string.
// This particular algorithm is extremely fast. Refer to this website for more details...
// http://blog.stevenlevithan.com/archives/faster-trim-javascript
//
// Example: 
//			var s = "a string with trailing spaces   ";
//          s = s.rtrim();

String.prototype.rtrim = function() {
	return this.replace(/\s\s*$/, '');
}// trim
////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
// String.ltrim (prototype)
//
// Trims leading white space characters from a string.
// This particular algorithm is extremely fast. Refer to this website for more details...
// http://blog.stevenlevithan.com/archives/faster-trim-javascript
//
// Example: 
//			var s = "  a string with leading spaces";
//          s = s.ltrim();

String.prototype.ltrim = function() {
	return this.replace(/^\s\s*/, '');
}// trim
////////////////////////////////////////////////////////////////////////////////////////////////////
