/*
 * EventHandlerUtils.js
 * Copyright (c) 2005 MetroSpark LLC  All rights reserved.
 */
/**
 * Utilities related to adding and removing event handlers to specified 
 * elements.  Supports browsers with DOM2 implementations and IE.
 *
 * Written By: Sunder Srinivasan | sunder@metrospark.com
 */
 
/**
 * Function will add an event handler function to the specified "on" event of the
 * specified DOM element.
 *
 * param elementRef   DOM element object to which the event handler
 *                    function should be added.
 * param eventType    Event type excluding "on" prefix.
 * param functionRef  Name of event handler function.
 */
function addOnEventHandler(elementRef, eventType, functionRef) {
    // Add the event listener to the element object
    if (typeof elementRef.addEventListener != "undefined") {
        // DOM2
        elementRef.addEventListener(eventType, functionRef, false);
    } else if (typeof elementRef.attachEvent != "undefined") {
        // IE
        elementRef.attachEvent("on"+eventType, functionRef);
    }
}

/**
 * Function adds a handler that will run a specified function on page load.
 *
 * param functionRef  Name of the function to run on page load 
 */
function addOnLoadEventHandler(functionRef) {
    addOnEventHandler(window, "load", functionRef);
}

/**
 * Function adds a handler that will run a specified function when the mouse
 * is clicked on the specified element.
 *
 * param elementRef  DOM element object to which the onClick event
 *        handler function should be added.
 * param functionRef  Name of event handler function. 
 */
function addOnClickEventHandler(elementRef, functionRef) {
    addOnEventHandler(elementRef, "click", functionRef);
}

/**
 * Removes the specified handler function from the specified "on" event of the
 * provided DOM element.
 *
 * param elementRef  DOM element object from which the event handler
 *        function should be removed.
 * param eventType  Event type excluding "on" prefix.
 * param functionRef  Name of event handler function.
 */
function removeOnEventHandler(elementRef, eventType, functionRef) {
    // Remove the event listener from the element object
    if (typeof elementRef.removeEventListener != "undefined") {
        // DOM2
        elementRef.removeEventListener(eventType, functionRef, false);
    } else if (typeof elementRef.detachEvent != "undefined") {
        // IE
        elementRef.detachEvent("on"+eventType, functionRef);
    }
}
