//<script>

/*
 * Sets the focus on the first element that should "reasonably" receive it
 */
function Fev_FocusOnFirstFocusableFormElement()
{
	for (i = 0; i < document.forms.length; i++)
	{
		if (Fev_FocusOnDefaultElement(document.forms[i])) return;
		//if (Fev_FocusOnFirstFocusableElement(document.forms[i])) return;
	}
}

/*
 * Sets the focus on the first element that is able to receive it
 */
function Fev_FocusOnFirstFocusableElement(objForm)
{
	if (objForm && (objForm != null))
	{
		for (i = 0; i < objForm.length; i++)
		{
			var objElement = objForm.elements[i];
			if (Fev_IsFocusableElement(objElement))
			{
				objElement.focus();
				return true;
			}
		}
	}
	return false;
}

/*
 * Sets the focus on the first element that should "reasonably" receive it
 */
function Fev_FocusOnDefaultElement(objForm)
{
	if (objForm && (objForm != null))
	{
		for (i = 0; i < objForm.length; i++)
		{
			var objElement = objForm.elements[i];
			if (Fev_IsFocusableElement(objElement))
			{
				var strType = Fev_GetElementType(objElement);
				//we know (strType != null) because it was checked within Fev_IsFocusableElement().
				if (strType.toLowerCase().indexOf("select") == 0)
				{
					//NOTE: SELECT tags are ignored (they interfere with mousewheel scrolling when they have focus)
				}
				else
				{
					objElement.focus();
					return true;
				}
			}
		}
	}
	return false;
}

/*
 * returns true if the element can receive focus
 */
function Fev_IsFocusableElement(objElement)
{
	if (objElement && 
		(objElement != null) && 
		Fev_IsElementEnabled(objElement) && 
		Fev_IsElementVisible(objElement) && 
		Fev_IsElementEditable(objElement) )
	{
		var strType = Fev_GetElementType(objElement);
		if (strType != null)
		{
			if ((strType == "text") || (strType == "textarea") || (strType.toString().charAt(0) == "s"))
			{
				return true;
			}
		}
	}
	return false;
}

/*
 * returns true if the element is enabled
 */
function Fev_IsElementEnabled(objElement)
{
	if (objElement && (objElement != null))
	{
		if (!(objElement.disabled == false))
		{
			return false;
		}
		return true;
	}
	return false;
}

/*
 * returns true if the element's content is editable by the user
 */
function Fev_IsElementEditable(objElement)
{
	if (objElement && (objElement != null))
	{
		if (objElement.readOnly)
		{
			return false;
		}
		if ((!objElement.isContentEditable) && (typeof(objElement.isContentEditable) != 'undefined'))
		{
			return false;
		}
		return true;
	}
	return false;
}

/*
 * returns true if the element is visible to the user
 */
function Fev_IsElementVisible(objElement)
{
	if (objElement && (objElement != null))
	{
		if (objElement.style && (objElement.style != null))
		{
			if (objElement.style.display && (objElement.style.display.toLowerCase() == 'none'))
			{
				return false;
			}
			if (objElement.style.visibility && (objElement.style.visibility.toLowerCase() == 'hidden'))
			{
				return false;
			}
			if (objElement.style.visibility && (objElement.style.visibility.toLowerCase() == 'inherit'))
			{
				var objParentElement = Fev_GetParentElement(objElement);
				if (objParentElement && (objParentElement != null) && (!Fev_IsElementVisible(objParentElement)))
				{
					return false;
				}
			}
		}
		return true;
	}
	return false;
}

/*
 * returns true if the element responds directly to Enter key presses
 * return true for:
 *     Textarea, Select/Dropdown, Input Buttons (Submit/Button/Image/Reset),
 *     A tags
 * return false for everything else, including:
 *     Input type=[Radio/Checkbox/Text/Password/File]
 *     IMG tags
 */
function Fev_IsElementUsesEnterKey(objElement)
{
	if (objElement && (objElement != null))
	{
		var strType = Fev_GetElementType(objElement);
		if (strType != null) strType = strType.toLowerCase();
        switch (strType)
        {
            case "textarea":
            case "select":
            case "submit":
            case "button":
            case "image":
            case "reset":
                return true;
                break;
            case "radio":
            case "checkbox":
            case "text":
            case "password":
            case "file":
                return false;
                break;
            default: 
                break;
        }

		var strTagName = Fev_GetElementTagName(objElement);
		if (strTagName != null) strTagName = strTagName.toLowerCase();
		switch (strTagName)
		{
			case "textarea":
			case "select":
			case "a":
				return true;
				break;
			case "img":
			case "input":
			default:
				break;
		}
	}
	return false;
}

function Fev_GetParentElement(objElement)
{
	if (objElement && (objElement != null))
	{
		if (objElement.parentNode && (objElement.parentNode != null))
		{
			return objElement.parentNode;
		}
		if (objElement.parentElement && (objElement.parentElement != null))
		{
			return objElement.parentElement;
		}
	}
	return null;
}

function Fev_GetElementType(objElement)
{
	if (objElement && (objElement != null))
	{
		if (objElement.type)
		{
			return objElement.type;
		}
	}
	return null;
}

function Fev_GetElementTagName(objElement)
{
	if (objElement && (objElement != null))
	{
		if (objElement.tagName)
		{
			return objElement.tagName;
		}
	}
	return null;
}

function Fev_GetEventSourceElement(objEvent)
{
	if (objEvent && (objEvent != null))
	{
		if (objEvent.srcElement)
		{
			return objEvent.srcElement;
		}
	}
	return null;
}

function Fev_IsEnterKeyPressed(bIgnoreTextAreaEvents)
{
	if (window.event)
	{
		var e = window.event;
		var bIsEnterKeyPress = ((e.keyCode == 13) && (e.type == 'keypress'));
		if (bIsEnterKeyPress)
		{
			if (bIgnoreTextAreaEvents && (bIgnoreTextAreaEvents == true))
			{
				var strType = Fev_GetElementType(Fev_GetEventSourceElement(e));
				if (strType != null) strType = strType.toLowerCase();
				if (strType == "textarea")
				{
					return false;
				}
			}
			return true;
		}
	}
	return false;
}

function Fev_IsFormSubmitKeyPress()
{
	if (window.event)
	{
		var e = window.event;
		var bIsEnterKeyPress = ((e.keyCode == 13) && (e.type == 'keypress'));
		if (bIsEnterKeyPress)
		{
			var eventSrc = Fev_GetEventSourceElement(e);
			if (!Fev_IsElementUsesEnterKey(eventSrc))
			{
				return true;
			}
		}
	}
	return false;
}

function Fev_ClickButton(buttonId)
{
	var button = document.all(buttonId);
	if (button == null) button = document.all(buttonId + '_Button');
	if (button && (typeof(button.click) == 'object'))
	{
		button.click();
		return true;
	}
	return false;
}

//Sets the value or selection of the form element, independent of the element's type.
function Fev_SetFormControlValue(objElement, strValue)
{
	var strTagName = Fev_GetElementTagName(objElement);
	if (strTagName != null) strTagName = strTagName.toLowerCase();
	switch (strTagName)
	{
		case "textarea":
			objElement.value = strValue;
			return true;
			break;
		case "select":
			var currentIndex = objElement.selectedIndex;
			objElement.value = strValue;
			if (objElement.selectedIndex < 0)
			{
				objElement.selectedIndex = currentIndex;
				return false;
			}
			return true;
			break;
		case "input":
			switch (objElement.type.toLowerCase())
			{
				case "text":
				case "password":
				case "hidden":
					objElement.value = strValue;
					return true;
					break;
				case "file":
					//can't programatically set the value of file controls
					return false;
				case "checkbox":
					if ((strValue == null) || (strValue == ''))
					{
						objElement.checked = false;
						return true;
						break;
					}
					else if (strValue == objElement.value)
					{
						objElement.checked = true;
						return true;
						break;
					}
					else
					{
						//the specified value matches niether the checked nor unchecked state
						//objElement.checked = true;
						//objElement.value = strValue;
						//return true;
						break;
					}
				case "radio":
					if (strValue == null)
					{
						//uncheck all radio buttons in the group
						objElement.checked = true;
						objElement.checked = false;
						return true;
						break;
					}
					else if (strValue == objElement.value)
					{
						objElement.checked = true;
						return true;
						break;
					}
					else
					{
						var f = objElement.form;
						var allRadioButtonsInGroup = f.elements(objElement.name)
						for (i = 0; i < allRadioButtonsInGroup.length; i++)
						{
							var rb = allRadioButtonsInGroup[i];
							if (strValue == rb.value)
							{
								rb.checked = true;
								return true;
							}
						}
						//the specified value matches the checked state of none of the radio buttons
						//objElement.checked = true;
						//objElement.checked = false;
						//return true;
						break;
					}
				default:
					break;
			}
		default:
			break;
	}
	return false;
}

//Inserts the value into a list element, independent of the element's type.
function Fev_ReplaceLastListControlOption(objListElement, strValue, strText)
{
	var strTagName = Fev_GetElementTagName(objListElement);
	if (strTagName != null) strTagName = strTagName.toLowerCase();
	switch (strTagName)
	{
		case "select":
			var objOption = objListElement.options[objListElement.options.length-1];
			objOption.value = strValue;
			objOption.text = strText;
			//objOption.innerText = strText;
			return true;
			break;
		default:
			break;
	}
	return false;
}

function Fev_HandleFormSubmitKeyPress(buttonId)
{
	//if (window.event && (window.event.returnValue == false))
	//	return false;

	if (Fev_IsFormSubmitKeyPress()) //if (Fev_IsEnterKeyPressed(true))
	{
		if (Fev_ClickButton(buttonId))
		{
			if (window.event)
			{
				//window.event.returnValue = false;
				window.event.cancelBubble = true;
			}
			return true;
		}
	}
	return false;
}


/**************************************************************************************
 *  Function    : toggleExpandCollapse()                                              *
 *  Description : Toggles the expanding and collapsing of the content region of       *
 *                    record and table panels; also swaps the "expand/collapse" icon, *
 *                    and "total records" count based upon the current                *
 *                    expand/collapse state.                                          *
 *  Parameters  : anchorNode, <a> tag node which is clicked upon to initiate toggling *
 *                    of expand/collapse                                              *
 *  Assumptions : The region which is expanded/collapsed is the table (with HTML      *
 *                    id, "CollapsibleRegion") within the sibling (row) of the table  *
 *                    row which contains the anchorNode.                              *
 *  Author      : Samson Wong                                                         *
 **************************************************************************************/
function toggleExpandCollapse(anchorNode)
{
	var collapsibleNode;
			
	// find the node to be expanded/collapsed based on browser type
	if (navigator.appName == "Netscape")
	{
		collapsibleNode = anchorNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.childNodes.item(2).childNodes.item(1).childNodes.item(1);
	}
	else // navigator.appName == "Microsoft Internet Explorer"
	{
		collapsibleNode = anchorNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.childNodes.item(1).childNodes.item(0).childNodes.item(0);
	}
	
	
    // make sure this node is a collapsible region before collapsing
    if ((collapsibleNode.id == "CollapsibleRegion") && (collapsibleNode.tagName == "TABLE"))
    {
		collapsibleNode.style.display = (collapsibleNode.style.display == "block") ? "none" : "block";
	}
	
	// traverse to image node (note that for both Netscape and IE, this is the first child of anchor tag)
	var imageNode = anchorNode.childNodes.item(0);

	// make sure this node contains the expand/collapse image before swapping icon
	if ((imageNode.id == "ExpandCollapseIcon") && (imageNode.tagName == "IMG"))
	{
		// show appropriate icon for current expand/collapse state
		imageNode.src = (collapsibleNode.style.display == "block") ? "../Images/DialogHeaderIconCollapse.gif" : "../Images/DialogHeaderIconExpand.gif";
	}
	
	
	var totalRecordsNode;
	
	// traverse to the total records node
	if (navigator.appName == "Netscape")
	{
		totalRecordsNode = anchorNode.parentNode.parentNode.childNodes.item(9).childNodes.item(1);
	}
	else // navigator.appName == "Microsoft Internet Explorer"
	{
		totalRecordsNode = anchorNode.parentNode.parentNode.childNodes.item(4).childNodes.item(0);
	}
	
	// make sure this node contains the total records count before toggling
    if ((totalRecordsNode.id == "CollapsibleRegionTotalRecords") && (totalRecordsNode.tagName == "TABLE"))
    {
    	// show total records count if panel in collapsed state
		totalRecordsNode.style.display = (totalRecordsNode.style.display == "block") ? "none" : "block";
	}	
	
	return false;
}


/**************************************************************************************
 *  Description : Global variables used by the following functions:                   * 
 *                    moveToNextTableRow()                                            *
 *                    moveToPreviousTableRow()                                        *
 *                    moveToThisTableRow()                                            *
 *                    updateCurrentTableAndIndex()                                    *
 *                    highlightTableRow()                                             *
 *                    clickEditButtonOfTableRowInFocus()                              *
 *  ISD Feature : "Table row selection via up/down arrow keypress"                    *
 *  Authors     : Samson Wong & Cocosoft B.V.                                         *
 **************************************************************************************/
var currentTable = null;
var currentRowIndex = 1;

/**************************************************************************************
 *  Function    : captureUpDownKey()                                                  *
 *  Description : Captures an "up/down arrow" and "enter" keyboard event, and calls   *
 *                    the respective function to process the event.                   *
 *  Parameters  : pTableInFocus, html table receiving the keyboard event              *
 *                event, browser-generated event object                               *
 *  Assumptions : Only table panels will call this function.                          *
 *  ISD Feature : "Table row selection via up/down arrow keypress"                    *
 *  Authors     : Samson Wong & Cocosoft B.V.                                         *
 **************************************************************************************/
function captureUpDownKey(pTableInFocus, event) {

	// for IE...
	if (event.keyCode) {
		// if key down...
		if (event.keyCode == 40) {
			event.returnValue = false;
			event.cancel = true;
			moveToNextTableRow(pTableInFocus);
		}
		// if key up...
		else if (event.keyCode == 38) {
			event.returnValue = false;
			event.cancel = true;
			moveToPreviousTableRow(pTableInFocus);
		}
		// if enter key...
		else if (event.keyCode == 13) {
			event.returnValue = false;
			event.cancel = true;
		    clickEditButtonOfTableRowInFocus();
		}
	}
	// if Netscape/Firefox...
	else if (event.which) {
		// if key down...
		if (event.which == 40) {
			event.returnValue=false;
			event.cancel = true;
			moveToNextTableRow(pTableInFocus);
		}
		// if key up...
		else if (event.which == 38) {
			event.returnValue = false;
			event.cancel = true;
			moveToPreviousTableRow(pTableInFocus);
		}  
		// if enter key...
		else if (event.which == 13) {
			event.returnValue = false;
			event.cancel = true;
		    clickEditButtonOfTableRowInFocus();
		}
	}
}

/**************************************************************************************
 *  Function    : moveToNextTableRow()                                                *
 *  Description : Upon "down arrow" keypress, unhighlights the current table row, and *
 *                    highlights the next table row.                                  *
 *  Parameters  : pTableInFocus, html table receiving the "down arrow" keyboard event *
 *  Assumptions : None.                                                               *
 *  ISD Feature : "Table row selection via up/down arrow keypress"                    *
 *  Authors     : Samson Wong & Cocosoft B.V.                                         *
 **************************************************************************************/
function moveToNextTableRow(pTableInFocus) {
    var tableInFocus;
    var tableRows;
    var maxRowIndex;
    var tableCells;
    
	if (pTableInFocus != null) {
	
		// if page newly loaded...
		if (currentTable == null) {
			currentTable = pTableInFocus;
		}
		
		// retrieve all rows of the current table
		tableRows = currentTable.getElementsByTagName("tr");
		
		if (tableRows != null) {
		    
		    // determine the number of rows (including "header row") in this table
		    maxRowIndex = tableRows.length;
		     	
			// if current row is not the last row of this table...
		    if ((currentRowIndex >= 1) & (currentRowIndex < maxRowIndex-1)) {
		    
		    	// unhighlight the current row
				unhighlightTableRow(tableRows.item(currentRowIndex));
			
				// if focus still in this table...
				if (currentTable != pTableInFocus) {
				
					// retrieve all rows of the new table in focus
					tableRows = pTableInFocus.getElementsByTagName("tr");
					
					// determine the number of rows (including "header row") in this new table in focus
					maxRowIndex = tableRows.length;
					
					// make this new table in focus the current table
					currentTable = pTableInFocus;
			    }
					
				// make next row of this table the current row
				currentRowIndex++;
		    }
		    
			// if current row is not the last row of this table...
		    if ((currentRowIndex >= 1) & (currentRowIndex < maxRowIndex)) {

				// highlight the (new) current row
				highlightTableRow(tableRows.item(currentRowIndex));		            
		    }
		}
	}
}

/**************************************************************************************
 *  Function    : moveToPreviousTableRow()                                            *
 *  Description : Upon "up arrow" keypress, unhighlights the current table row, and   *
 *                    highlights the previous table row.                              *
 *  Parameters  : pTableInFocus, html table receiving the "down arrow" keyboard event *
 *  Assumptions : None.                                                               *
 *  ISD Feature : "Table row selection via up/down arrow keypress"                    *
 *  Authors     : Samson Wong & Cocosoft B.V.                                         *
 **************************************************************************************/
function moveToPreviousTableRow(pTableInFocus) {
    var tableInFocus;
    var tableRows;
    var maxRowIndex;
    var tableCells;
    
	if (pTableInFocus != null) {
	
		// if page newly loaded...
		if (currentTable == null) {
			currentTable = pTableInFocus;
		}
		
		// retrieve all rows of the current table
		tableRows = currentTable.getElementsByTagName("tr");
		
		if (tableRows != null) {
		    
		    // determine the number of rows (including "header row") in this table
		    maxRowIndex = tableRows.length;
		     	
			// if current row is not the first row of this table...
		    if ((currentRowIndex > 1) & (currentRowIndex < maxRowIndex)) {
		    
		    	// unhighlight the current row
				unhighlightTableRow(tableRows.item(currentRowIndex));
			
				// if focus still in this table...
				if (currentTable != pTableInFocus) {
				
					// retrieve all rows of the new table in focus
					tableRows = pTableInFocus.getElementsByTagName("tr");
					
					// determine the number of rows (including "header row") in this new table in focus
					maxRowIndex = tableRows.length;
					
					// make this new table in focus the current table
					currentTable = pTableInFocus;
				}
					
				// make previous row of this table the current row
				currentRowIndex--;
		    }
		    
			// if current row is not the last row of this table...
		    if ((currentRowIndex >= 1) & (currentRowIndex < maxRowIndex)) {

				// highlight the (new) current row
				highlightTableRow(tableRows.item(currentRowIndex));		            
		    }
		}
	}
}

/**************************************************************************************
 *  Function    : moveToThisTableRow()                                                *
 *  Description : Upon "radio button select", unhighlights the current table row, and *
 *                    highlights the newly selected table row.                        *
 *  Parameters  : pTableCell, "radio button" table cell selected                      *
 *  Assumptions : Only "radio button" table cells will call this function.            *
 *  ISD Feature : "Table row selection via up/down arrow keypress"                    *
 *  Authors     : Samson Wong & Cocosoft B.V.                                         *
 **************************************************************************************/
function moveToThisTableRow(pTableCell) {
	var tableRow;

	if (pTableCell != null) {
	
		pTableCell.parentNode.getElementsByTagName("input").item(2).focus();
		
		tableRow = pTableCell.parentNode;

		// if current row highlighted...
		if (currentTable != null) {
		
			// retrieve all rows of the current table
			tableRows = currentTable.getElementsByTagName("tr");
			
			unhighlightTableRow(tableRows.item(currentRowIndex));
		}

		// determine current table and index resulting from focus change
		updateCurrentTableAndIndex(tableRow);
		
		// highlight (new) current table row
		highlightTableRow(tableRow);
	}
}

/**************************************************************************************
 *  Function    : updateCurrentTableAndIndex()                                        *
 *  Description : Updates the global variables, currentTable and currentRowIndex,     *
 *                    indicating the new table and row in focus.                      *
 *  Parameters  : pTableRow, new table row in focus                                   *
 *  Assumptions : None.                                                               *
 *  ISD Feature : "Table row selection via up/down arrow keypress"                    *
 *  Authors     : Samson Wong & Cocosoft B.V.                                         *
 **************************************************************************************/
function updateCurrentTableAndIndex(pTableRow) {
	var tableRows;
	var maxTableIndex;
	
	currentTable = pTableRow.parentNode;
	
	tableRows = currentTable.getElementsByTagName("tr");
	
	if (tableRows != null) {
		maxTableIndex = tableRows.length;
		
		for (var i=1; i<maxTableIndex; i++) {
			if (tableRows.item(i) == pTableRow) {
				currentRowIndex = i;
				break;
			}
		}
	}
}

/**************************************************************************************
 *  Function    : unhighlightTableRow()                                               *
 *  Description : For the specified table row, deselects the "radio button", and      *
 *                    unhighlights its table cells.                                   *
 *  Parameters  : pTableRow, table row to be unhighlighted                            *
 *  Assumptions : The second table cell in row contains "radio button"; all           *
 *                    subsequent cells are data cells.                                *
 *  ISD Feature : "Table row selection via up/down arrow keypress"                    *
 *  Authors     : Samson Wong & Cocosoft B.V.                                         *
 **************************************************************************************/
function unhighlightTableRow(pTableRow) {

	if (pTableRow != null) {
	
		// deselect table row's radio button
		iconCells = pTableRow.getElementsByTagName("input");
		if ((iconCells != null) & (iconCells.item(2) != null)) {
		    iconCells.item(2).checked = false;
		        
			// unhighlight row
			tableCells = pTableRow.getElementsByTagName("td");
			for (var i=0; i<tableCells.length; i++) {
				if (i<=2) {
				 	tableCells.item(i).className = "icon_cell";
			 	}
			 	else {
				 	tableCells.item(i).className = "table_cell";
			 	}
			}
		}
	}
}

/**************************************************************************************
 *  Function    : highlightTableRow()                                                 *
 *  Description : For the specified table row, selects the "radio button", and        *
 *                    highlights its table cells.                                     *
 *  Parameters  : pTableRow, table row to be highlighted                              *
 *  Assumptions : The second table cell in row contains "radio button"; all           *
 *                    subsequent cells are data cells.                                *
 *  ISD Feature : "Table row selection via up/down arrow keypress"                    *
 *  Authors     : Samson Wong & Cocosoft B.V.                                         *
 **************************************************************************************/
function highlightTableRow(pTableRow) {

	if (pTableRow != null) {
	
		// select table row's radio button
		iconCells = pTableRow.getElementsByTagName("input");
		if ((iconCells != null) & (iconCells.item(2) != null)) {
			iconCells.item(2).focus();
		    iconCells.item(2).checked = true;
		            
			// highlight row
			tableCells = pTableRow.getElementsByTagName("td");
			for (var i=0; i<tableCells.length; i++) {
				if (i<=2) {
				 	tableCells.item(i).className = "icon_cell_highlighted";
			 	}
			 	else {
				 	tableCells.item(i).className = "table_cell_highlighted";
			 	}
			}
		}
		// page total row
		else {
			// re-highlight previously unhighlighted row (i.e., page totals row should
			//    not be highlighted; just stay on last data row)
			currentRowIndex--;
			tableRows = currentTable.getElementsByTagName("tr");
			highlightTableRow(tableRows.item(currentRowIndex));
		}
	}
}

/**************************************************************************************
 *  Function    : clickEditButtonOfTableRowInFocus()                                  *
 *  Description : Invokes the edit record page of the selected record (in focus table *
 *                    row) by programmatically clicking the record's edit button.     *
 *  Parameters  : None.                                                               *
 *  Assumptions : The edit button is contained in the first cell of the table row in  *
 *                    focus.                                                          *                 
 *  ISD Feature : "Table row selection via up/down arrow keypress"                    *
 *  Authors     : Samson Wong & Cocosoft B.V.                                         *
 **************************************************************************************/
function clickEditButtonOfTableRowInFocus() {
	var tableRows = null;
	var tableRowInFocus = null;
	var iconCells = null;
	
	if (currentTable != null) {
	
		tableRows = currentTable.getElementsByTagName("tr");
		if (tableRows != null) {
		
			tableRowInFocus = tableRows.item(currentRowIndex); 
			if (tableRowInFocus != null) {
			
				iconCells = tableRowInFocus.getElementsByTagName("input");
				if (iconCells != null) {
					iconCells.item(0).click();
				}	
			}	
		}
	}
}
