// Add functionality to String object.
/********************************************************************
Remove whitespace from begining of string.
********************************************************************/
String.prototype.ltrim = function()
{
	//Match spaces at beginning of text and replace with a null string
	return this.replace(/^\s+/,'');
}
/********************************************************************
Remove trailing whitespace from string.
********************************************************************/
String.prototype.rtrim = function()
{
	//Match spaces at end of text and replace with a null string
    return this.replace(/\s+$/,'');
}
/********************************************************************
Remove leading and trailing whitespace from string.
********************************************************************/
String.prototype.trim = function()
{
    return this.ltrim().rtrim();
}


/********************************************************************
Adds an event listener to an html element.
********************************************************************/
function rsweb_addEventListener(obj,eventName,callbackFunction,flag)
{ 
	if (obj.addEventListener)
		obj.addEventListener(eventName,callbackFunction,flag);
	else if (obj.attachEvent)
		obj.attachEvent("on"+eventName,callbackFunction);
	else
		eval("obj.on"+eventName+"="+callbackFunction);
}

/********************************************************************
Set focus to specified control. Handles Infragistics controls.
********************************************************************/
function rsweb_SetFocus(ctrl)
{
	// First try Infragistic edit controls.
	try
	{
		var edit = igedit_getById(ctrl);
		if(edit != null);
		{
			edit.elem.focus();
			return ;
		}
	}
	catch (e)
	{
	}

	// Now try our account control.
	try
	{
		var acct = rsaccount_GetAcctObjByID(ctrl);
		if (acct != null && !acct.disabled)
		{
			// Set focus to 1st edit control.
			acct.focus(0);
			return ;
		}
	}
	catch (e)
	{
	}
	
	// Next try standard control.
	var control = document.getElementById(ctrl);
	if (control.disabled)
		control = rsweb_getNextElement(control);
		
	control.focus();
}

/********************************************************************
Get next control in the TAB sequence. Based on code taken from
	http://www.faqts.com/knowledge_base/view.phtml/aid/995/fid/129
	
Parameters:
	field		Control to which you wish to return the next tab
				control.
********************************************************************/
function rsweb_getNextElement(field)
{
	var fieldFound = false;
	for (var e = 0; e < document.all.length; e++)
	{
		if (fieldFound && document.all[e].type != 'hidden' && !document.all[e].isDisabled && document.all[e].isTextEdit)
			return document.all[e];
		if (field.id == document.all[e].id)
			fieldFound = true;
	}
	return field;
}

/********************************************************************
Hide controls and display "Please wait..." message when a button
has been clicked.

Parameters:
	hideID		ID of control to make hidden.
	messageID	ID of control to display "Please wait..." message.
	
Needed variables:
	TxtPleaseWait	"Please wait..."
********************************************************************/
function rsweb_ShowPleaseWait(hideID, messageID)
{
	document.getElementById(hideID).style.visibility = "hidden";
	var lblMessage = document.getElementById(messageID);
	lblMessage.className = CSSPleaseWait;
	lblMessage.innerText = TxtPleaseWait;
}

/********************************************************************
Map the <Enter> key to a button press.

Parameters:
	buttonID			button control ID to press

********************************************************************/
function rsweb_KeyDownHandler(buttonID)
{ 
	// Process only the Enter key 
	if (event.keyCode == 13 || event.keyCode == 10)
	{
		if (event.srcElement.type != 'textarea' || event.ctrlKey)
		{
			// Cancel the default submit 
			event.returnValue=false;
			event.cancel = true; 
			// Submit the form by programmatically clicking the specified button 
			var button=document.getElementById(buttonID);
			button.click();
		}
	} 
}

/********************************************************************
Cancel the current event

Parameters:
	e			event object

********************************************************************/
function rsweb_CancelEvent(e)
{
	if (!e) var e = window.event;
	
	if(document.all)
	{
		e.cancelBubble=true;
		e.returnValue=false;
		return true;
	}
	else
	{
		e.stopPropagation();
		e.preventDefault();
		return false;
	}
}

function rsweb_OnSubmit()
{
	if (showClockOnSubmit)
	{
		// Make page invisible.
		document.forms[0].style.display='none';

		// If new page takes more than 2 seconds to display,
		// display the please wait form.
		setTimeout('rsweb_DisplayPleaseWaitForm()', 2000);
	}
}

function rsweb_DisplayPleaseWaitForm()
{
	if (document.forms['PleaseWaitForm'] != null)
		document.forms['PleaseWaitForm'].style.display='inline';
}

function rsweb_Submit()
{
	rsweb_OnSubmit();

	this._submit();
}

/********************************************************************
Global variables.
********************************************************************/
var CSSPleaseWait = "RSPleaseWait";
var CSSMessage = "RSMessage";
var CSSError = "RSError";

/********************************************************************
The following bit of script checks to see if document.all is present,
but document.getElementById is not. If this is the case it then
assigns a new function to document.getElementById that wraps around
document.all
Taken from http://www.metalusions.com/backstage/articles/8/
********************************************************************/
if (document.all && !document.getElementById)
{
	document.getElementById = function(id)
	{
		return document.all[id];
	}
}

// If a script calls someForm.submit(), the onsubmit event does not
// fire, so we need to redefine the submit method of the
// form class.
document.forms[0]._submit = document.forms[0].submit;
document.forms[0].submit = rsweb_Submit;


