//<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;
}



