/*
 * Utility function definitions
 */

/*
 * getInteger() --
 *	to return an integer corresponding to the input value
 *
 *	input			: input value, which can be a number, or a string
 *	defaultValue	: default value, if specified, while the input is null
 */
function getInteger(input, defaultValue)
{
	var iDefault = null;
	var iValue = null;

	if (defaultValue)
	{ iDefault = getInteger(defaultValue); }

	if ((null == input) && (null != defaultValue))
	{ iValue = iDefault; }
	else
	{
		if ("number" != typeof(input))
		{
			iValue = parseInt(input, 10);
		}
		else
		{
			iValue = Math.floor(input);
		}
	}
	return iValue;
}

/*
 * getString() --
 *	to return a string corresponding to the input value
 *
 *	input			: input value, which can be a number, or a string
 *	defaultValue	: default value, if specified, while the input is null
 */
function getString(input, defaultValue)
{
	var sDefault = null;
	var sValue = null;

	if (defaultValue)
	{ sDefault = getString(defaultValue); }

	if ((null == input) && (null != defaultValue))
	{ sValue = sDefault; }
	else
	{
		if ("string" != typeof(input))
		{
			sValue = new String(input)
		}
		else
		{
			sValue = input
		}
	}

	return sValue;
}

/*
 * addEvent() --
 *	to set an event handler on a given object's specified event
 *
 *	objectPath	: the target object path
 *	event		: the event name
 *	handler		: the handler function
 *	isAppend	: 'true' to append the new handler
 *	isReplace	: 'true' to replace with the new handler
 */
function addEvent(objectPath, event, handler, isAppend, isReplace)
{
	var sEventPath = getString(objectPath) + "." + getString(event).toLowerCase();
	var objEvent = eval(sEventPath);
	var sEvent = (objEvent)? objEvent.toString() : "";
	var sHandler = (handler)? handler.toString() : "";

	var sOldCmd = sEvent.substring(sEvent.indexOf("{") + 1, sEvent.lastIndexOf("}"));
	var sNewCmd = sHandler.substring(sHandler.indexOf("{") + 1, sHandler.lastIndexOf("}"));

	var sCmd = null;

	if ((null != isReplace) && (true == isReplace))
	{ sCmd = sNewCmd; }
	else if (true == isAppend)
	{ sCmd = sOldCmd + sNewCmd; }
	else
	{ sCmd = sNewCmd + sOldCmd; }

	sCmd += "\n";

	eval(sEventPath + "= new Function(sCmd)");
	return true;
}