//
// This module contains functions that allow you to redefine keyboard actions
// and perform a button click when key event occurs.
//
// Note :
//	Functions in this module should be called from the OnLoad event of the document
//	or anytime after OnLoad event.
//	This makes sure that button object names and ids are defined.
//
// Limitations:
//	If the button you want to map is a submit type, be sure NOT to use an onsubmit 
//	handler for the form. This will cause the page to be submitted twice in netscape
//	and the onsubmit handler will be called twice in IE.
// Solution:
//	Change the type of the button to a button type and define a handler for the onclick
//	event. This will work fine in both IE and netscape.

NS4 = (document.layers);
IE4 = (document.all);
ver4 = (NS4 || IE4);
IE5 = (IE4 && navigator.appVersion.indexOf("5.")!=-1);
isMac = (navigator.appVersion.indexOf("Mac") != -1);

var eventCount;					// this is the number of event handlers defined
var buttonObject;				// this object holds the reference to the action button
var keyValue;					// this key value is the key code expected from user	

//
// This function determines if the browser is Netscape or IE
//
function IsNetscape()
{
	return window.navigator.appName == "Netscape";
}

//
// this function assigns the event handler globally for the whole window object
// this function should be called from the onload event of the document
// INPUT:
//		winobject - a window object which is the scope of the event handling.
//				  - use the top value if you want all frames to handle the event
//		butobject - a button object which carries the action when the event is
//					triggered.
//					note:
//						the button object should be part of a form to work in 
//						netscape.
//		keyval    - the key value you expect from the user to trigger the event.
//				  - the default value is 13 (ENTER key)
function AttachKeyHandler(winobject, butobject, keyval)
{
	var defaction;
	if (butobject != null && typeof butobject == "object")
	{
		defaction = funcHandleKey;
		if (typeof winobject == "object")
		{
			if (eventCount == null)
			{
				keyValue = new Array();
				buttonObject = new Array();
				eventCount = 0;
			}
			else
				eventCount++;		
			if (typeof keyval == "number")
			{
				keyValue[eventCount] = keyval;
			}
			else	// default key assignments
			{
				keyValue[eventCount] = 13;	// assigns RETURN key to the button
				buttonObject[eventCount++] = butobject;
				keyValue[eventCount] = 3;	// assigns the ENTER key to the button
			}
				
			buttonObject[eventCount] = butobject;

			if (winobject.frames.length > 0)
			{
				for (var i=0; i < winobject.frames.length; i++)
				{
					if (IsNetscape())
					{
						if (keyval == 27)	// use onkeyup for ESC key
						{
							winobject.frames[i].document.captureEvents(Event.KEYUP);
							winobject.frames[i].document.onkeyup = defaction;
						}
						else
						{
							winobject.frames[i].document.captureEvents(Event.KEYPRESS);
							winobject.frames[i].document.onkeypress = defaction;
						}
					}
					else
						winobject.frames[i].document.onkeypress = defaction;
				}
			}
			else
			{
				if (IsNetscape())
				{
					if (keyval == 27)	// use onkeyup for ESC key
					{
						winobject.document.captureEvents(Event.KEYUP);
						winobject.document.onkeyup = defaction;
					}
					else
					{
						winobject.document.captureEvents(Event.KEYPRESS);
						winobject.document.onkeypress = defaction;
					}
				}
				else
					winobject.document.onkeypress = defaction;
			}
		}
	}	
}

//
// This function handles the key event by calling the click method of the
// action button.
//
function funcHandleKey(evt)
{
	var keyval;
	if (IsNetscape())
	{
		keyval = evt.which;
	}
	else
		keyval = this.parentWindow.event.keyCode;
	for (var i=0; i <= eventCount; i++)
	{
		if (keyval == keyValue[i])
		{
			if (IsNetscape())
			{
				buttonObject[i].focus(); // this is necessary for netscape to work properly
				buttonObject[i].click();
			}
			else
			{
				if (isMac && ((buttonObject[i].type).toUpperCase() == "SUBMIT"))
					buttonObject[i].submit();
				else
					buttonObject[i].click();
			}
			break;
		}
	}
}

//
// This function gets a button object from the current document
// returns null if no button object exist.
// The return value can be used as a parameter to the AttachKeyHandler function
//
function getButtonObject(buttonName, thisdoc)
{
	var butObject;

	if (typeof thisdoc == "undefined")
		thisdoc = "document"
	if (eval(thisdoc + ".forms.length") > 1)
	{
		for (var i=0; i<eval(thisdoc + ".forms.length"); i++)
		{
			butObject = eval(thisdoc + ".forms[" + i + "]." + buttonName);
			if (butObject != null)
				break;
		}
	}
	else
		butObject = eval(thisdoc + ".forms[0]." + buttonName);
	if (butObject != null && typeof butObject != "undefined")
	{
		if ((butObject.type).toUpperCase() != "BUTTON" && (butObject.type).toUpperCase() != "SUBMIT" && (butObject.type).toUpperCase() != "IMAGE")
			return null;
	}
	else
		butObject = eval(thisdoc + ".links('" + buttonName + "')");
	return butObject;
}
