//////



function forms_DisableInputByObjectAndClear(oInput, sClassName)
{
	oInput.value = "";
	forms_DisableInputByObject(oInput, sClassName);
}



function forms_EnableInputByObjectAndClear(oInput, sClassName)
{
	forms_EnableInputByObject(oInput, sClassName);
	oInput.value = "";
}



function forms_DisableInputByObject(oInput, sClassName)
{
	if (sClassName != false)
	{
		oInput.className = sClassName;
	}
	else
	{
		oInput.style.backgroundColor = "#CCCCCC";
		oInput.style.color = "#333333";
	}

	oInput.disabled = true;
}



function forms_EnableInputByObject(oInput, sClassName)
{
	oInput.disabled = false;

	if (sClassName != false)
	{
		oInput.className = sClassName;
	}
	else
	{
		oInput.style.backgroundColor = "#FFFFFF";
		oInput.style.color = "#000000";
	}
}



function forms_getInputObjectByNameAndFormObject(sInputName, oFormObject)
{
	var oInput = false;
	var i = 0;

	for (i=0; i<oFormObject.elements.length; i++)
	{
		if (oFormObject.elements[i].name == sInputName)
		{
			oInput = oFormObject.elements[i];
			break;
		}
	}

	return oInput;
}



function forms_getFormObjectByName(sFormName)
{
	var oForm = false;
	var i = 0;

	for (i=0; i<document.forms.length; i++)
	{
		if (document.forms[i].name == sFormName)
		{
			oForm = document.forms[i];
			break;
		}
	}

	return oForm;
}



function forms_focusInputObject(oInput)
{
	// The form elements that will be tested. Anything with a dot indicates the "type" attribute of the element
	var formElements = ["input.text", "input.checkbox", "input.radio", "select", "textarea"];
	var selectedNode = null;

	// IE's selection method
	if (typeof document.selection != "undefined" && document.selection != null && typeof window.opera == "undefined")
	{
		var theSelection = document.selection;
		var textRange = document.selection.createRange();

		selectedNode = textRange.parentElement();
	}
	// W3 selection method. Currently only Mozilla & Safari support it. However, neither of them support ranges inside form objects, so this part is redundant. Merely included in case they decide to include support in the future
	else if (typeof window.getSelection != "undefined")
	{
		var theSelection = window.getSelection();

		// The Safari way to get the node that a selection starts in
		if (typeof theSelection.baseNode != "undefined")
		{
			selectedNode = theSelection.baseNode;
		}
		// The Mozilla way to get the node that a selection starts in
		else if (typeof theSelection.getRangeAt != "undefined" && theSelection.rangeCount > 0)
		{
			selectedNode = theSelection.getRangeAt(0).startContainer;
		}
	}

	// If a selected node was found above, check whether it's a selection inside one of the specified form element types
	if (selectedNode != null)
	{
		for (var i = 0; i < formElements.length; i++)
		{
			if (selectedNode.nodeName.toLowerCase() == formElements[i].replace(/([^.]*)\..*/, "$1"))
			{
				return false;
			}
		}
	}

	var forms = document.forms;

	// Do a check of each form element on the page. If one of them has a value, do not focus
	for (var i = 0; i < forms.length; i++)
	{
		var formElements = forms[i];

		for (var j = 0; j < formElements.length; j++)
		{
			if (formElements[j].getAttribute("type") == "checkbox" || formElements[j].getAttribute("type") == "radio")
			{
				if (formElements[j].checked != formElements[j].defaultChecked)
				{
					return false;
				}
			}
			else
			{
				if (typeof formElements[j].defaultValue != "undefined" && formElements[j].value != formElements[j].defaultValue)
				{
					return false;
				}
			}
		}
	}

	// If no form elements were found to be focused -- or with values -- go ahead and focus
	oInput.focus();

	return false;
}



//////

