<!-- Begin JavaScript
/*
Global JavaScript event registry used by event functions below.
*/
var __eventListeners = [];

/*
Attribution:
Original Code By Bret Taylor -- http://ajaxcookbook.org/
License Info: http://creativecommons.org/licenses/by/2.5/

Description:
Cross browser function to add an event listener to a target.

Example:
var elem = document.getElementById("elem");
var listener = addEventListener(elem, "click", function() {
    alert("You clicked me!");
});
*/
function addEventListener(instance, eventName, listener) {
    var listenerFn = listener;
    if (instance.addEventListener) {
        instance.addEventListener(eventName, listenerFn, false);
    } else if (instance.attachEvent) {
        listenerFn = function() {
            listener(window.event);
        }
        instance.attachEvent("on" + eventName, listenerFn);
    } else {
        throw new Error("Event registration not supported");
    }
    var event = {
        instance: instance,
        name: eventName,
        listener: listenerFn
    };
    __eventListeners.push(event);
    return event;
}

/*
Attribution:
Original Code By Bret Taylor -- http://ajaxcookbook.org/
License Info: http://creativecommons.org/licenses/by/2.5/

Description:
Cross browser function which removes a registered event 
from an target.

Example:
var elem = document.getElementById("elem");
var listener = addEventListener(elem, "click", function() {
    alert("You clicked me!");
});
removeEventListener(listener);
*/
function removeEventListener(event) {
    var instance = event.instance;
    if (instance.removeEventListener) {
        instance.removeEventListener(event.name, event.listener, false);
    } else if (instance.detachEvent) {
        instance.detachEvent("on" + event.name, event.listener);
    }
    for (var i = 0; i < __eventListeners.length; i++) {
        if (__eventListeners[i] == event) {
            __eventListeners.splice(i, 1);
            break;
        }
    }
}

/*
Attribution:
Original Code By Bret Taylor -- http://ajaxcookbook.org/
License Info: http://creativecommons.org/licenses/by/2.5/

Description:
Cross browser function which removes all registered events 
from the page. Should help prevent memory leaks in IE.

Example:
<body onunload="unregisterAllEvents()">
*/
function unregisterAllEvents() {
    while (__eventListeners.length > 0) {
        removeEventListener(__eventListeners[0]);
    }
}

/*
Attribution:
Original Code By Bret Taylor -- http://ajaxcookbook.org/
License Info: http://creativecommons.org/licenses/by/2.5/

Description:
Cross browser function which prevents events from the 
target from propogating up the DOM.

Example:
var link = document.getElementById("link");
link.onclick = stopEvent;
*/
function stopEvent(e) {
    if (!e) e = window.event;
    if (e.stopPropagation) {
        e.stopPropagation();
    } else {
        e.cancelBubble = true;
    }
}

/*
Attribution:
Original Code By Bret Taylor -- http://ajaxcookbook.org/
License Info: http://creativecommons.org/licenses/by/2.5/

Description:
Cross browser function which stops processing of the event by 
the original target.

Example:
var link = document.getElementById("link");
link.onclick = function(e) {
    cancelEvent(e);
}
-----
To stop any processing of the event, combine cancel event 
with stop event as so:
var link = document.getElementById("link");
link.onclick = function(e) {
    cancelEvent(e);
    stopEvent(e);
}
*/
function cancelEvent(e) {
    if (!e) e = window.event;
    if (e.preventDefault) {
        e.preventDefault();
    } else {
        e.returnValue = false;
    }
}

/*
Attribution:
Original Code By Bret Taylor -- http://ajaxcookbook.org/
License Info: http://creativecommons.org/licenses/by/2.5/

Description:
Allows you to use a method of a specific instance of an object (prototype) as a callback function.

Example:
function Adder() {  this.value = 0;}
Adder.prototype.addOne = function() { this.value++; }

var adder = new Adder();
var button = document.getElementById("button");
button.onclick = callback(adder, adder.addOne);
*/
function callback(instance, method) {
    return function() {
        method.apply(instance, arguments);
    }
}
// End JavaScript -->