/* ----------------------------------------------------------------------------
Description: Private eDB JavaScript framework functions for use by eDB logic
             only.  No user code should directly access anything in this file.
Copyright (c) 2000, by Competitive Computing, Inc.
-----------------------------------------------------------------------------*/

var _strEventArgsClosingTag = '</EventArgs>';
var _strEventArgsXML = '<EventArgs>' + _strEventArgsClosingTag;


window.onload = _onLoad


function _onLoad()
{
	document.frm.xml.value = _strEventArgsXML;
	_callOptionalEventHandler('onLoadPage')
}


function _isDefined(strObject)
{
	return eval('typeof ' + strObject) != 'undefined';
}


function _callRequiredEventHandler(strHandler)
{
	if (!_isDefined(strHandler))
	{
		var strMsg = 'Event handler ' + strHandler + ' is not defined.\n';
		strMsg += 'A JavaScript function with this name is required.';
		alert(strMsg);
		return;
	}
	eval(strHandler + '();');
}


function _callOptionalEventHandler(strHandler)
{
	if (_isDefined(strHandler))
		eval(strHandler + '();');
}

var blnNetscape = false

//If Netscape, need extra code to capture events
if(!(document.all))
{
	document.captureEvents(Event.KEYPRESS)
	blnNetscape = true
}

document.onkeypress = _keyPressHandler

function _keyPressHandler(evt)
{

	if (blnNetscape)
	{
		if	(evt.which == 13)
		{
			_callOptionalEventHandler('enterKeyPressed');
			return false;		
		}
		else
		{
			return true;
		}		
	}	
	else
	{
		if (event.keyCode == 13)
		{
			_callOptionalEventHandler('enterKeyPressed');
			return false;
		}
		else
		{
			return true;
		}
	}
}



function _onChange()
{
	g_blnPageDirty = true;
}


// Attach an argument to the Event Args XML fragment, but only if it doesn't
// already exist.  
// This is done by first removing the closing tag, appending the new argument,
// and then replacing the closing tag.
// The xml form field is then updated from the new string.
//
function _setEventArg(strArgName, strArgValue)
{
	if ( _notExistsEventArg(strArgName) )
	{
		strArgValue = strArgValue + ""

		var regexp1 = /</g;
		var regexp2 = /&[^(#\d{2,3};)(\w{2,3};)]/g;
		var regexp3 = />/g;
		strArgValue = strArgValue.replace(regexp1,"&lt;");
		strArgValue = strArgValue.replace(regexp2,"&amp;");
		strArgValue = strArgValue.replace(regexp3,"&gt;");
		var strXML = _strEventArgsXML.slice(0, _strEventArgsClosingTag.length * -1);
		strXML += '<' + strArgName + '>' + strArgValue + '</' + strArgName + '>' + _strEventArgsClosingTag;
		_strEventArgsXML = strXML;
		document.frm.xml.value = _strEventArgsXML;
	}
}


// Look in the Event Argument list to see if this argument is already there.  If it is
// not there return a True indication, otherwise False.  A duplicate argument is an
// indication of a redundant submission and the arguments should be ignored.
//
function _notExistsEventArg( strArgName )
{
	var strSearchExp = '<' + strArgName + '>';
	var regexp = new RegExp(strSearchExp, "g");
	
	if (_strEventArgsXML.match(regexp) == null)
	{
		return true;
	}
	else
	{
		return false;
	}
}


// Clear both the local EventArgs string and the FORM xml string back to an
// empty EventArgs list.
//
function _clearEventArgs()
{
	_strEventArgsXML = '<EventArgs>' + _strEventArgsClosingTag;
	document.frm.xml.value = _strEventArgsXML;
}

function _logicError(strFunction, strError)
{
	alert('Function ' + strFunction + '() detected a logic error.\n' + strError);
}
