// This is the globally available eventDetail array (see below)
var eventDetail = null;

// The array of event handler functions that are registered with the framework.
var eventHandlers = null;

// This is the main event handling method.  This decomposes the event into 
// a global array called "eventDetail", which is accessible to every JavaScript section.
// The array contains the following hash elements:
// event: the actual event object.
// browserType: either "IE" or "other" string literals.
// target: the target of the event, sometimes the event itself.
// tagName: the name of the tag/html element.  Eg: 'A' for anchors.
// type: the target element's type.  Useful for determining submit events for 'INPUT' tags, for example.
// id: the id of the tag/html element, if available.
// Some additional information is available when the tagName is 'A' (anchor) or 'IMG' (image):
// href: (anchors only) the href link.
// hrefTarget: (anchors only) the target of the href, i.e. '_blank'
// src: (images only) the image's src attribute.
//select:(select only): when user hit select box, it is not empty.(when user select an option in select box, IE return two 'select', firefox return 'select' and 'option')
function handle(e) {
	var target = null;
	var tagName = null;
	var type = null;
	var id = null;
	
	eventDetail = new Array();

    eventDetail["event"] = e;
    
	// determine the browser-specific event parameters
	if (document.all) {
	    eventDetail["browserType"] = "IE";
		eventDetail["target"] = e.srcElement;
		eventDetail["tagName"] = e.srcElement.tagName;
		eventDetail["type"] = e.srcElement.type;
		eventDetail["id"] = e.srcElement.id;
	} else {
	    eventDetail["browserType"] = "OTHER";
		eventDetail["target"] = e.target;
		eventDetail["tagName"] = e.target.tagName;
		eventDetail["type"] = e.target.type;
		eventDetail["id"] = e.target.id;
	}
	
	// add additional href info for anchor tags
	if (eventDetail["tagName"] == 'A') {
	    if (document.all) {
			eventDetail["href"] = e.srcElement.href;
			eventDetail["hrefTarget"] = e.srcElement.target;
		} else {
			eventDetail["href"] = e.target.href;
			eventDetail["hrefTarget"] = e.target.target;
		}
	}
	
	// add additional src info for image tags.
	if (eventDetail["tagName"] == 'IMG') {
	    if (document.all) {
	        eventDetail["src"] = e.srcElement.src;
	    } else {
	        eventDetail["src"] = e.target.src;
	    }
	}
	if(eventDetail['tagName'] == 'OPTION' || eventDetail['tagName']== 'SELECT' )
    {
		if(e.srcElement && e.srcElement.id){
	        eventDetail["select"] = e.srcElement.id;
		}
    }
    else
    {
        eventDetail["select"] = "";
    }
	
	// loop through our registered event hander functions and call each one in turn.
	if (eventHandlers != null) {
	    for (var i = 0; i < eventHandlers.length; i++) {
	        eventHandlers[i].call(eventDetail);
	    }
	}
}

// Call this method to initialize this framework.  This registers the handle method with the browser's event hierarchy.
function initializeBrowserEvents() {
    womAdd('DoOnLoad()');
    womOn();
}

function DoOnLoad() {
        if (document.all) {
            // this is for IE's little "quirks"...
	        document.attachEvent('onclick', handle);
        } else {
	        // and hopefully, "everything else..."
	        window.addEventListener('click', handle, false);
        }
    }

// Call this method to register your event handlers, and pass in your literal function as the parameter.
// for example, if you have the following:
// function myHandler() {
//     ...
// }
// to register myHandler with the event framework do the following:
// registerEventHandler(myHandler);
function registerEventHandler(theHandler) {
    if (eventHandlers == null) {
        eventHandlers = new Array();
    }
    
    eventHandlers[eventHandlers.length] = theHandler;
}
