// Client-side globals for form/function interactivity
var CSblnIsDirty = 0;

// Client-side globals for tables
var CSfnHighlightColor = "#FF0000";
var CSfnlngSelRowIndex = -1;

// prototypes for the string object
function strltrim() {
    //Match spaces at beginning of text and replace with a null string
    return this.replace(/^\s+/,'');
}
//**************************************************************************
function strrtrim() {
    //Match spaces at end of text and replace with a null string
    return this.replace(/\s+$/,'');
}
//**************************************************************************
function strtrim() {
    //Match spaces at beginning and end of text and replace
    //with null strings
    return this.replace(/^\s+/,'').replace(/\s+$/,'');
}
String.prototype.ltrim = strltrim;
String.prototype.rtrim = strrtrim;
String.prototype.trim = strtrim;
//**************************************************************************
function GetElemIndex(objElem){
// Returns index number of control 
// or -1 of control is not a collection
	var intIndex;
	var blnMatch = 0;
	if (objElem.name)
	{
		var objCollection = document.getElementsByName(objElem.name);
		if (objCollection.length > 1)
		{
			var objUniqueElem;
			for (intIndex=0; intIndex <= objCollection.length && objCollection(intIndex).uniqueID != objElem.uniqueID; intIndex++);
			if (intIndex <= objCollection.length) 
				{blnMatch = true;};
		};
		
	};
	if (!blnMatch) {intIndex = -1};
	
	return(intIndex);
}
//**************************************************************************
function cmdCalendar_onclick(strLoc){
// Opens calendar control and sets selected value in corresponding text box
	var strDate;
	var objButton = window.event.srcElement;
	var intIndex = GetElemIndex(window.event.srcElement);
	var objInput = document.all("txt" + objButton.id.substring(3, objButton.id.length))
	
	// Get correct input if more than one (collection)
	if (intIndex > -1)
	{
		objInput = objInput(intIndex);
	};
	
	if (strLoc == undefined){
		strLoc = "utility/CalendarDialog.htm"
	};
	
	strDate = window.showModalDialog(strLoc, objInput.value, "center: no; dialogHeight:195px; dialogWidth:300px; dialogTop:" + (window.event.screenY - 215) + "px; dialogLeft:" + (window.event.screenX - 300) + "px; help:no; status:no; scroll:no; resizable:no;");
	
	if (strDate.trim() != ""){
		objInput.value = strDate;
		if (strDate != objInput.value) 
			{CSblnIsDirty = true};
	};

}
//**************************************************************************
function cmdCalendarAppend_onclick(strLoc){
// Opens calendar control and appends selected value in corresponding text box
	var strDate;
	var objButton = window.event.srcElement;
	var intIndex = GetElemIndex(window.event.srcElement);
	var objInput = document.all("txt" + objButton.id.substring(3, objButton.id.length))
	
	// Get correct input if more than one (collection)
	if (intIndex > -1)
	{
		objInput = objInput(intIndex);
	};
	
	if (strLoc == undefined){
		strLoc = "utility/CalendarDialog.htm"
	};
	
	strDate = window.showModalDialog(strLoc, objInput.value, "center: no; dialogHeight:195px; dialogWidth:300px; dialogTop:" + (window.event.screenY - 215) + "px; dialogLeft:" + (window.event.screenX - 300) + "px; help:no; status:no; scroll:no; resizable:no;");
	
	if (strDate.trim() != ""){
		if (objInput.value.trim() == "")
			objInput.value = strDate;
		else
			objInput.value = objInput.value + ", " + strDate;
		
		if (strDate != objInput.value) 
			{CSblnIsDirty = true};
	};

}
//**************************************************************************
function IgnoreError(msg, url, lno){
// Generic, resume on error handler
	return true;
}

//**************************************************************************
function SelectRow() {
// Toggle the selection of a table row and 
// set the global CSlngSelRowIndex to the selected row's index
	
	if (this.event == undefined)
		var objRow = this;
	else
		var objRow = this.event.srcElement;
	
	var i = 0;
	
	// find the row object
	if (objRow.tagName.toUpperCase() != "TR") {
		for (i = 0; i < 4; i++) {
			objRow = objRow.parentElement;
			if (objRow.tagName.toUpperCase() == "TR")
				break;
		};
		if (objRow.tagName.toUpperCase() != "TR") 
			return false;
	};


	if (objRow.style.backgroundColor.toUpperCase() == CSfnHighlightColor.toUpperCase()) {
		// Unhighlight the row by restoring the background color
		objRow.style.backgroundColor = objRow.getAttribute("LastBgColor");
		
		// Set the currently selected row to nothing
		CSfnlngSelRowIndex = -1;
	};
	else {
		// Highlight the row but retain the background color
		objRow.setAttribute("LastBgColor", objRow.style.backgroundColor);
		objRow.setAttribute("LastColor", objRow.style.color);
		objRow.style.backgroundColor = CSfnHighlightColor;
		// Unhighlight the last selected row
		if (CSfnlngSelRowIndex != -1) {
			var objLastRow = objRow.parentElement.parentElement.rows(CSfnlngSelRowIndex);
			objLastRow.style.backgroundColor = objLastRow.getAttribute("LastBgColor");
		};
		// Record the currently selected row
		CSfnlngSelRowIndex = objRow.rowIndex;
	};
	
}