/* --------------------------------------------------------------------

	Javascript associated with the opdstruts-html.tld tags
	and opd-other.tld tags
	
--------------------------------------------------------------------- */
//--------------------------Browser Detection-------------------------
var NS4 = (document.layers) ? 1 : 0;
var IE = (document.all) ? 1 : 0;
var DOM = (document.getElementById) ? 1 : 0;

 

/* --------------------------------------------------------------------
FUNCTION:  checkDirtyFlag
PARAMS  :  
RETURNS :  none
PURPOSE :  Check the dirty flag and warn if necessary
--------------------------------------------------------------------- */
var dirtyFlag = false;
var dirtyCheckEnabled = true;

function checkDirtyFlag()
{
	if (dirtyCheckEnabled && isDirty())
	{
		event.returnValue = "You haven't saved your changes to this page yet.\n\n" + 
		                    "Click OK to discard all changes and leave the page.\n" + 
		                    "Click Cancel to return to the page. When you\n" + 
							"return to the page, click Save to save your changes.";	
	}
}

/* --------------------------------------------------------------------
FUNCTION:  setDirtyFlag
PARAMS  :  
RETURNS :  none
PURPOSE :  Set the dirty flag that marks input fields' change.
           Note: This will attempt to set the value in a hidden input 
           element named 'dirty' in the first form in the document to 'true'
           If the element is not found, a variable will be used.
--------------------------------------------------------------------- */
function setDirtyFlag()
{
	try
	{
		document.forms[0].dirty.value = "true";
	}
	catch (e)
	{
		dirtyFlag = true;
	}
}

/* --------------------------------------------------------------------
FUNCTION:  clearDirtyFlag
PARAMS  :  
RETURNS :  none
PURPOSE :  clear the dirty flag that marks input fields' change
           Note: This will attempt to set the value in a hidden input 
           element named 'dirty' in the first form in the document to 'false'.
           If the element is not found, a variable will be used.
--------------------------------------------------------------------- */
function clearDirtyFlag()
{
	try
	{
		document.forms[0].dirty.value = "false";
	}
	catch (e)
	{
		dirtyFlag = false;
	}
}

/* --------------------------------------------------------------------
FUNCTION:  isDirty
PARAMS  :  
RETURNS :  true if dirty
PURPOSE :  test dirty flag
--------------------------------------------------------------------- */
function isDirty()
{
	try
	{
		return (document.forms[0].dirty.value == "true");
	}
	catch (e)
	{
		return dirtyFlag;
	}
}

/* --------------------------------------------------------------------
FUNCTION:  enableDirtyCheck
PARAMS  :  
RETURNS :  none
PURPOSE :  turn on checking for dirty flag by the checkDirtyFlag function.
--------------------------------------------------------------------- */
function enableDirtycheck()
{
	dirtyCheckEnabled = true;
}

/* --------------------------------------------------------------------
FUNCTION:  disableDirtyCheck
PARAMS  :  
RETURNS :  none
PURPOSE :  turn off checking for dirty flag by the checkDirtyFlag function.
           This can be used when performing a 'save' to prevent a 
           warning that data has been changed.
--------------------------------------------------------------------- */
function disableDirtyCheck()
{
	dirtyCheckEnabled = false;
}

/* --------------------------------------------------------------------
FUNCTION:  isDirtyCheckEnabled
PARAMS  :  
RETURNS :  true if dirty check is enabled
PURPOSE :  test dirty check status
--------------------------------------------------------------------- */
function isDirtyCheckEnabled()
{
	return dirtyCheckEnabled;
}

/* --------------------------------------------------------------------
FUNCTION:  saveFocus(field)
PARAMS  :  
RETURNS :  
PURPOSE :  Saves the current page focus and location
--------------------------------------------------------------------- */
function saveFocus(field)
{
	try
	{
		var focusField = document.forms[0].focusField;
		if (focusField != null)
		{
			document.forms[0].scrollLeft.value = document.body.scrollLeft;
			document.forms[0].scrollTop.value = document.body.scrollTop;
		
			try
			{
				focusField.value = (field instanceof String) ? field : field.name;
			}
			catch (ex)
			{
				focusField.value = "";
			}
		}
	}
	catch (ex)
	{
	}
}

/* --------------------------------------------------------------------
FUNCTION:  saveFocus(field)
PARAMS  :  
RETURNS :  
PURPOSE :  Saves the current page focus and location
--------------------------------------------------------------------- */
function setHiddenField(field)
{
	try
	{
		var fieldname = field.name;
		var fieldvalue = field.value;
		//alert(fieldname + "=" + fieldvalue);
		//hiddenfield has same name without the underscore temp appended
		var split = fieldname.split("_temp");
		//alert("name of hidden field" + split[0]);
		var hiddenfieldname = split[0];
		
		//alert("field checked = " + field.checked); 
		
		if(field.checked){
			fieldvalue = "true"
		}else{
			fieldvalue = "false"
		}
		
		//alert("setting hidden field to: " + fieldvalue);
		document.forms[0].elements[hiddenfieldname].value = fieldvalue;
	}
	catch (ex)
	{
	}
}

/* --------------------------------------------------------------------
FUNCTION:  restoreFocus()
PARAMS  :  
RETURNS :  
PURPOSE :  Restores the previous page focus and location
--------------------------------------------------------------------- */
function restoreFocus()
{
	document.body.scrollLeft = document.forms[0].scrollLeft.value;
	document.body.scrollTop = document.forms[0].scrollTop.value;

	var focusField = document.forms[0].focusField;
	if (focusField != null)
	{
		if (focusField.value.length > 0)
		{
			var element = document.forms[0].elements[focusField.value];
			if (element != null)
			{
				try
				{
					element.focus();
				}
				catch (ex)
				{
					// Element is probably disabled so do nothing
				}
			}
	
			focusField.value = "";
		}
	}
}

/* --------------------------------------------------------------------
 * FUNCTION:  autoTab()
 * PARAMS  :  input document, maximum length, event.
 * RETURNS :  boolean
 * PURPOSE :  Auto tab between the fields- helpful for SSN, EIN and Telephone numbers
 * -------------------------------------------------------------------*/
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
		function autoTab(input,len, e) {
			var keyCode = (isNN) ? e.which : e.keyCode; 
			var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
			if(input.value.length >= len && !containsElement(filter,keyCode)) {
				input.value = input.value.slice(0, len);
				input.form[(getIndex(input)+1) % input.form.length].focus();
			}
			return true;
		}
		
		function containsElement(arr, ele) {
			var found = false, index = 0;
			while(!found && index < arr.length)
				if(arr[index] == ele)
					found = true;
				else
					index++;
			return found;
		}
		function getIndex(input) {
			var index = -1, i = 0, found = false;
			while (i < input.form.length && index == -1)
				if (input.form[i] == input)index = i;
				else i++;
			return index;
		}
