// Reloads the current page.
function ReloadCurrentPage ()
{
	window.location.reload ();
}

// Returns the event object to be used
function GetEvent ( oEvent )
{
	return ( window.event || oEvent );
}

// Returns the key code from a keyPress event.
function GetKeyCodeFromEvent ( oEvent )
{
	if ( window.event ) // IE
	{
		return ( oEvent.keyCode );
	}
	else if ( oEvent.which ) // Firefox
	{
		return ( oEvent.which );
	}

	return ( -1 );
}

function OnTextBoxKeyPress ( oEvent )
{
	oEvent = GetEvent ( oEvent );
	
	var lCode = GetKeyCodeFromEvent ( oEvent );
	switch ( lCode )
	{
		case 60:
		case 62:
			return ( false );
	}

	return ( true );
}

function ShowOverlay ()
{
	var oOverlayDiv = document.getElementById ( 'divOverlay' );

	if ( oOverlayDiv == null )
	{
		oOverlayDiv = document.createElement ( 'div' );
		if ( oOverlayDiv != null )
		{
			oOverlayDiv.setAttribute ( 'id', 'divOverlay' );
			oOverlayDiv.setAttribute ( 'name', 'divOverlay' );

			SetClass ( oOverlayDiv, 'Overlay' );
			oOverlayDiv.onclick = RemoveOverlay;

			document.body.appendChild ( oOverlayDiv );
		}
	}

	return ( oOverlayDiv );
}

function RemoveOverlay ()
{
	var oOverlayDiv = document.getElementById ( 'divOverlay' );

	if ( oOverlayDiv != null )
		document.body.removeChild ( oOverlayDiv );
}

function StartWSCall ( sServiceName, sParams, oSuccessFunc, oFailureFunc )
{
	var oTmpRequest = null;

	if ( ( sServiceName != null ) && ( sParams != null ) )
	{
		var oSFunc, oFFunc;

		if ( oSuccessFunc == null )
			oSFunc = function () { return; };
		else
			oSFunc = oSuccessFunc;
			
		if ( oFailureFunc == null )
			oFFunc = function () { ShowMessage ( 'An error occured', 'There was an error while completing your request.' ); };
		else
			oFFunc = oFailureFunc;

		oTmpRequest = new Ajax.Request ( sServiceName,
			{ method: 'post', postBody: sParams, onSuccess: oSFunc, onFailure: oFFunc } );
	}

	return ( oTmpRequest );
}

function MessageDialog_OnClick ( oEvent )
{
	oEvent = GetEvent ( oEvent );
	if ( oEvent )
	{
		if ( IsIE () )
		{
			oEvent.returnValue = false;
			oEvent.cancelBubble = true;
		}
		else
		{
			oEvent.stopPropagation ();
			oEvent.preventDefault ();
		}
	}

	return ( false );
}

function ShowMessage ( sTitle, sMessage )
{
	var oOverlay = ShowOverlay ();
	
	if ( oOverlay != null )
	{
		var oDialogDiv = document.createElement ( 'div' );

		if ( oDialogDiv != null )
		{
			oDialogDiv.setAttribute ( 'id', 'dlgMessage' );
			oDialogDiv.setAttribute ( 'name', 'dlgMessage' );
			
			SetClass ( oDialogDiv, 'MessageDialog' );
			oDialogDiv.onclick = MessageDialog_OnClick;

			var oTitleDiv = document.createElement ( 'div' );

			if ( oTitleDiv != null )
			{
				SetClass ( oTitleDiv, 'MessageDialogTitle' );

				var oTitleText = document.createElement ( 'p' );

				if ( oTitleText != null )
				{
					SetClass ( oTitleText, 'MessageDialogTitleText' );
					SetInnerText ( oTitleText, sTitle );

					oTitleDiv.appendChild ( oTitleText );
				}

				oDialogDiv.appendChild ( oTitleDiv );
			}

			var oContentDiv = document.createElement ( 'div' );

			if ( oContentDiv != null )
			{
				SetClass ( oContentDiv, 'MessageDialogContent' );

				var oContentText = document.createElement ( 'p' );

				if ( oContentText != null )
				{
					SetClass ( oContentText, 'NormalText' );
					SetInnerText ( oContentText, sMessage );

					oContentDiv.appendChild ( oContentText );
				}

				oDialogDiv.appendChild ( oContentDiv );
			}

			oOverlay.appendChild ( oDialogDiv );
			oDialogDiv.focus ();
		}
	}
}
