/*

 Creates cross-browser compatible methods for binding DOM events to JavaScript functions.
 --Takes into account the W3C and Microsoft models' differing interpretations of "this" 
   and inclusion/exclusion of "on" in event name

*/

// Bind a given object's event to a given function
// param HTMLElementObject obj: the element object to which an event handler will be added
// param string type: the type of event to be handled (ie "click", "load")
// param string fn: the name of the function that will handle the event (note: without "()"!)
function addEvent( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		var nameKey = type+fn;
		var eventKey = "e"+nameKey;
	
		obj[eventKey] = fn;
		obj[nameKey] = function() { obj[eventKey]( window.event ); }
		obj.attachEvent( "on"+type, obj[nameKey] );
	}
}

// Remove a given object's event binding to a given function
// param HTMLElementObject obj: the element object from which an event handler will be removed
// param string type: the type of event handled (ie "click", "load")
// param string fn: the name of the function to remove (note: without "()"!)
function removeEvent( obj, type, fn )
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		var nameKey = type+fn;
		
		obj.detachEvent( "on"+type, obj[nameKey] );
		obj[nameKey] = null;
		obj["e"+nameKey] = null;
	}
}