//
// eventwrapper.js
// Paul Dragicevich, 31 Oct 2004
//
// Wrap calls to event objects to make them
// the same for both browsers.
//

// Check for DEBUG cookie.
var EVENTWRAPPER_DEBUG = false;
try
{
	EVENTWRAPPER_DEBUG = DebugCookie();
}
catch ( e )
{
	EVENTWRAPPER_DEBUG = false;
}

function EventWrapper ( p_GeckoEvent )
{
	// Very simplistic here.
	// If there's an argument, assume it's a gecko event object,
	// and therefore assume a gecko browser.
	// Otherwise, assume IE.
	// example of usage:
	// function eventHandler ( e )
	// {
	//     var ew = new EventWrapper ( e );
	// }
	this.geckoEvent = null;
	if ( typeof(p_GeckoEvent) == "object" )
	{
		this.geckoEvent = p_GeckoEvent;
	}
	
	
	this.getSourceElement = function ()
	{
		if ( this.geckoEvent )
		{
			return this.geckoEvent.target;
		}
		else
		{
			return event.srcElement;
		}
	}
	
	this.getSourceTagName = function ()
	{
		var tagName = null;
		var tag = this.getSourceElement();
		if ( tag && tag.tagName )
		{
			tagName = tag.tagName.toLowerCase();
		}
		return tagName;
	}
	
	this.getKeyCode = function ()
	{
		if ( this.geckoEvent )
		{
			return this.geckoEvent.keyCode;
		}
		else
		{
			return event.keyCode;
		}
	}
	
	this.cancelEvent = function ()
	{
		if ( this.geckoEvent )
		{
			if ( EVENTWRAPPER_DEBUG )
				ClientDebug(this.geckoEvent.cancelable);
			if ( this.geckoEvent.cancelable )
 				this.geckoEvent.preventDefault();
		}
		else
		{
     		event.returnValue = false;
   	   event.cancel = true;
		}
		
	}
	
	//
	// Event Phase is a mozilla concept.
	// In IE, events bubble up from the source element.
	// In gecko browsers, events capture down, then bubble up.
	// 0 CAPTURING_PHASE
	// 1 AT_TARGET
	// 3 BUBBLING_PHASE
	// 
	this.getEventPhase = function ()
	{
		if ( this.geckoEvent )
			return this.geckoEvent.eventPhase;
		else
			return 3;  // in IE, events only bubble.
	}
	
	//
	// checkModifiers
	// Given a flag:  1 SHIFT, 2 ALT, 4 CTRL
	// check if the modifiers are set.
	//
	this.checkModifiers = function ( p_Flag )
	{
		var check = true;
		if ( p_Flag > 0 )
		{
			var ctrlOk = false;
			var altOk = false;
			var shiftOk = false;
			
			if ( (p_Flag & 1) == 1 )  // require SHIFT key
			{
				if ( this.geckoEvent )
					shiftOk = this.geckoEvent.shiftKey;
				else
					shiftOk = event.shiftKey;
			}
			else
				shiftOk = true;
			
			if ( (p_Flag & 2) == 2 )  // require ALT key
			{
				if ( this.geckoEvent )
					altOk = this.geckoEvent.altKey;
				else
					altOk = event.altKey;
			}
			else
				altOk = true;

			if ( (p_Flag & 4) == 4 )  // require CTRL key
			{
				if ( this.geckoEvent )
					ctrlOk = this.geckoEvent.ctrlKey;
				else
					ctrlOk = event.ctrlKey;
			}
			else
				ctrlOk = true;
				
			check = shiftOk && altOk && ctrlOk;
		}
		return check;
	}
}

