/***********************************************************************/
/*Changes here need to be made to qjscrmain001 and qjscreels001 as well*/
/***********************************************************************/
var EventManager = {
    _registry: null,

    Initialise: function()
    {
        if (!this._registry) {
            this._registry = [];
            // Register the cleanup handler on page unload.
            EventManager.Add(window, "unload", this.CleanUp);
        }
    },

    /**
     * Registers an event and handler with the manager.
     *
     * @param  obj         Object handler will be attached to.
     * @param  type        Name of event handler responds to.
     * @param  fn          Handler function.
     * @param  useCapture  Use event capture. False by default.
     *                     If you don't understand this, ignore it.
     *
     * @return True if handler registered, else false.
     */
    Add: function(obj, type, fn, useCapture)
    {
        // If a string was passed in, it's an id.
        if (typeof obj == "string") {
            obj = document.getElementById(obj);
        }
        if (obj == null || fn == null) {
            return false;
        }
        if (obj.addEventListener) {
            obj.addEventListener(type, fn, useCapture);
            this._registry.push({obj: obj, type: type, fn: fn, useCapture: useCapture});
            return true;
        }
        if (obj.attachEvent && obj.attachEvent("on" + type, fn)) {
            this._registry.push({obj: obj, type: type, fn: fn, useCapture: false});
            return true;
        }

        return false;
    },
    /**
     * Removes all events attached to an object.
     *
     * @param  objIn         Object handler was attached to.
     * @param  typeIn       Name of event handler responds to (optional).
     * @param  fnIn         Handler function (optional).
     * 
     * If only the Object is supplied, then all registered events will be removed.  
     * If type or fn are supplied then only events matching the supplies values will be removed.
     */
    Remove: function (objIn, typeIn, fnIn, useCaptureIn){
         var returnValue = false;
         var o;
         for (var i = EventManager._registry.length -1; i >= 0 ; --i) {
             o = EventManager._registry[i];
            if (objIn == o.obj) {
                if ((!typeIn || typeIn == o.type) && (!fnIn || fnIn == o.fn)) {
                    try {
                        if (o.obj.removeEventListener) {
                            o.obj.removeEventListener(o.type, o.fn, o.useCapture);
                            returnValue = true;
                            EventManager._registry.splice(i, 1);
                        } else if (o.obj.detachEvent){ 
                            o.obj.detachEvent("on" + o.type, o.fn);
                            returnValue = true;
                            EventManager._registry.splice(i, 1);
                        }
                    } catch (ex) {}
                }
            }
        }
         if (!returnValue && typeof objIn == "object") { //remove the event even if it was not a registered event 
             try {
                 if (objIn.removeEventListener) { 
                     objIn.removeEventListener(typeIn, fnIn, useCaptureIn);
                 } else if (objIn.detachEvent) {
                     objIn.detachEvent("on" + typeIn, fnIn);
                 }
             } catch (ex){}
             
         }
         return returnValue;
    },

    /**
     * Cleans up all the registered event handlers.
     */
    CleanUp: function()
    {
        var o;
        if (EventManager._registry) {
            for (var i = 0; i < EventManager._registry.length; i++) {
                try {
                    o = EventManager._registry[i];
                    if (o.obj.removeEventListener) {
                        o.obj.removeEventListener(o.type, o.fn, o.useCapture);
                    } else if (o.obj.detachEvent) {
                        o.obj.detachEvent("on" + o.type, o.fn);
                    }
                } catch (ex) {}
            }
    
            // Kill off the registry itself to get rid of the last remaining
            // references.
    
            EventManager._registry = null;
        }
    }
};
EventManager.Initialise();

function addEvent(obj, type, fn, useCapture){
    EventManager.Add(obj, type, fn, useCapture);
}
function removeEvent(obj, type, fn){
    EventManager.Remove(obj, type, fn);
}

