/******************************************************************************
	This file contains various functions that are generally useful for GUI purposes.
	Project- or application-specific functions should not go here.
******************************************************************************/

/******************************************************************************
Format:		IterateRadioButtons(strGroupName, funcCallback, ...)

Parameters:
	strGroupName	The name of the Radio Group.

	funcCallback	The function that will be called for each radio
					button in the group.  The first argument to this 
					function will be the RadioButton itself.

	...			An open-ended argument list containing additional arguments 
				that are to be passed to the Callback function.  These arguments 
				will follow the first argument, which is the RadioButton itself.

Returns:
	The RadioButton that is selected, or null if none are selected.

This method iterates the RadioButton Group, and calls the Callback function for 
each radio button in the group.  If the Callback function returns a false, the 
iteration will stop.

Modification History
	Chris Protopapas	02/18/2004		Creation
	Chris Protopapas	05/20/2004		Short circuit some of the work if the 
		caller has not supplied a Callback function.
******************************************************************************/
function IterateRadioButtons(strGroupName, funcCallback)
{
	// If there is no groupname, we're done.
	if (null == strGroupName)
		{return (null);}

	// Build the array of arguments for the Handler.
	// The RadioButton will be first, followed by any 
	// optional arguments.  Here, we are loading up the array with 
	// the optional arguments only, leaving element zero for the 
	// RadioButton later.
	//alert("arguments.length = " + arguments.length);
	var argArray = new Array();
	if (funcCallback != null)
	{
		for (var idx = 2; idx < arguments.length; idx++)
		{
			argArray[idx - 1] = arguments[idx];
		}
	}

	// Find one of the buttons
	var objButton0 = document.getElementById(strGroupName);
	if (objButton0 == null)
		{return (null);}

	// Find the radio button group
	var objForm = objButton0.form;
	var objRadioGroup = objForm.elements[objButton0.name];

	// Iterate through the radio buttons in the group
	// Call the OnClick handler.
	var objButton = null;
	var objCheckedButton = null;
	for (var buttonIdx = 0; buttonIdx < objRadioGroup.length; buttonIdx++)
	{
		// Get the next RadioButton
		objButton = objRadioGroup[buttonIdx];

		// If this button is the checked one, we'll save it so we can 
		// return it to the caller when we're finished.
		if (objButton.checked)
			{objCheckedButton = objButton;}

		// If the caller supplied a Callback function, then
		// insert the current RadioButton into the Argument list array, 
		// and call the Handler.
		if (funcCallback != null)
		{
			argArray[0] = objButton;
			var bKeepGoing = funcCallback.apply(null, argArray);
			if (bKeepGoing == false)
				{break;}
		}
		else
		{
			// The caller did not supply a Callback function.
			// If have found the checked button, then we don't need to 
			// continue iterating through the radio group.
			if (null != objCheckedButton)
				{break;}
		}
	}

	// Return the checked button, or null if none were checked.
	return (objCheckedButton);
}

/******************************************************************************
Set the Select control using its "value=" attribute
Parameters:
	objSelect		The Select object
	strDesiredValue	The value attribute of the Select Option to select

Modification History
	Chris Protopapas	10/31/2003		Creation
	Chris Protopapas	04/06/2004		Change the desired value to a string.
	Chris Protopapas	12/30/2004		Do nothing if the Select object is null.
******************************************************************************/
function SetSelectByValue(objSelect, strDesiredValue)
{
	if (null == objSelect)
		{return;}

	for (var idx = 0; idx < objSelect.options.length; idx++)
	{
		if (objSelect.options[idx].value == strDesiredValue)
		{
			objSelect.options[idx].selected = true;
			break;
		}
	}
}

/******************************************************************************
Set the Target Object's value attribute to the Checked or Unchecked value
according to the state of the controlling checkbox
Parameters:
	objCheckbox		The Checkbox
	checkedValue	The value to be written to the Destination Object if the 
					checkbox is checked.
	uncheckedValue	The value to be written to the Destination Object if the 
					checkbox is NOT checked.
	objTarget		The Target Object

Modification History
	Chris Protopapas	12/12/2003		Creation
	Chris Protopapas	12/30/2004		Do nothing if args are null.
******************************************************************************/
function SetValuesFromCheckbox(objCheckbox, checkedValue, uncheckedValue, objTarget)
{
	if ((null == objCheckbox) || (null == objTarget))
		{return;}

	objTarget.value = ((objCheckbox.checked)? checkedValue : uncheckedValue);
}

/******************************************************************************
If the Control Object's value attribute evaluates to true, set the checkbox.  
Otherwise, clear it
Parameters:
	objControl		The Control Object
	objCheckbox		The Checkbox

Modification History
	Chris Protopapas	12/12/2003		Creation
	Chris Protopapas	12/30/2004		Do nothing if args are null
******************************************************************************/
function SetOrClearCheckbox(objControl, objCheckbox)
{
	if ((null == objControl) || (null == objCheckbox))
		{return;}

	objCheckbox.checked = ((objControl.value) ? true : false);
}

/******************************************************************************
Format:		DisplaySelectedObjectByValue(objControl, ...)
	
Parameters:
	objControl	The object that controls what is to be displayed.  This 
				can be a Select, RadioButton, or Text object.

	...			An open-ended argument list containing pairs that describe 
				the objects to be displayed or hidden.  The pairs are:
					1. A value that could be selected in the Control Object
					2. The object to be manipulated.

This method will show the object that matches the value of the Control 
Object's current selection.  There can be any number of objects listed for 
a given value, and all of them are shown.
All objects in the list that are associated with values that are not currently 
selected are hidden.

This is useful for switching dislay components in response to a selection 
in a dropdown list or radio button.

Modification History
	Chris Protopapas	12/20/2003		Creation
	Chris Protopapas	02/10/2004		For radio buttons, get the value only 
		if the radio button is checked.
	Chris Protopapas	04/13/2004		Allow Text objects to be the controlling 
		object.
	Chris Protopapas	12/15/2004		Use the new GetElementValue method
******************************************************************************/
function DisplaySelectedObjectByValue(objControl)
{
	// Get the selected value from the controlling object
	var strSelectValue = GetElementValue(objControl, "");

	// Iterate the rest of the argument list
	var bDisplayOn;
	for (var idx = 1; idx < arguments.length; idx+= 2)
	{
		bDisplayOn = ((arguments[idx] == strSelectValue) ? true : false);
		if (arguments[idx + 1] != null)
			DisplayObject(arguments[idx + 1], bDisplayOn);
	}
}

/******************************************************************************
Format:		DisplayObject(theObject, bDisplayOn)

Parameters:
	theObject	The object that should be shown or hidden
	bDisplayOn	If true, the object will be shown, otherwise it will be 
				hidden

Modification History
	Chris Protopapas	12/20/2003		Creation
	Chris Protopapas	02/10/2004		Change function name to avoid name 
		collisions with other functions.
	Chris Protopapas	06/03/2004		Do nothing if specified object is null
******************************************************************************/
function DisplayObject(objElement, bDisplayOn)
{
	if (null == objElement)
		{return;}

	objElement.style.display = ((bDisplayOn == true) ? '' : 'none')
}

/******************************************************************************
Format:		DisplayObjectFromCheckbox(objCheckbox, objTarget)

Parameters:
	objCheckbox			The controlling Checkbox
	objCheckedTarget	The checked object that should be shown or hidden.  
		This argument may be null.
	objUnCheckedTarget	The unchecked object that should be shown or hidden.  
		This argument may be null.

If the checkbox is checked, the CheckedTarget is displayed, otherwise it is hidden.
If the checkbox is unchecked, the UnCheckedTarget is displayed, otherwise it is hidden.

Modification History
	Chris Protopapas	02/12/2004		Creation
	Chris Protopapas	12/30/2004		Do nothing if the Checkbox is null
	Chris Protopapas	01/07/2005		Add support for an Unchecked target.
******************************************************************************/
function DisplayObjectFromCheckbox(objCheckbox, objCheckedTarget, objUncheckedTarget)
{
	if (null == objCheckbox)
		{return;}

	DisplayObject(objCheckedTarget, objCheckbox.checked);
	DisplayObject(objUncheckedTarget, !objCheckbox.checked);
}

/******************************************************************************
Format:		SetMandatory(objTarget, bMandatory)

Parameters:
	objTarget	The object that should be made mandatory or not
	bMandatory	If true, the object will be made mandatory, otherwise it will 
		be made optional.

Modification History
	Chris Protopapas	02/12/2004		Creation
	Chris Protopapas	12/30/2004		Do nothing if the Target is null
******************************************************************************/
function SetMandatory(objTarget, bMandatory)
{
	if (null == objTarget)
		{return;}

	if (bMandatory)
		objTarget.mandatory = "yes";
	else
		objTarget.mandatory = "no";
}

/******************************************************************************
Format:		SetMandatoryFromCheckbox(objCheckbox, objTarget)

Parameters:
	objTarget		The object that should be mandatory or not
	objCheckbox		The controlling Checkbox

If the checkbox is checked, the Target is made mandatory.  Otherwise it is 
made optional.

Modification History
	Chris Protopapas	02/12/2004		Creation
	Chris Protopapas	12/30/2004		Do nothing if the Checkbox is null
******************************************************************************/
function SetMandatoryFromCheckbox(objCheckbox, objTarget)
{
	if (null == objCheckbox)
		{return;}

	SetMandatory(objTarget, objCheckbox.checked);
}

/******************************************************************************
Format:		SetVisible(objTarget, bVisible)

Parameters:
	objTarget	The object that should be shown or hidden
	bVisible	If true, the object will be shown, otherwise it will be 
				hidden

This function manipulates the "visibility" style property.  Doing this does not
cause the layout manager to rebuild the display.  Rather, space is reserved for 
the Target object even if it is not visible.  If you do not want space to be 
reserved for invisible objects, use the DisplayObject function instead.

Modification History
	Chris Protopapas	02/19/2004		Creation
	Chris Protopapas	12/24/2004		Return if the Target is null
******************************************************************************/
function SetVisible(objTarget, bVisible)
{
	if (null == objTarget)
		{return;}

	objTarget.style.visibility = ((bVisible) ? "visible" : "hidden")
}

/******************************************************************************
Format:		FocusOnElement(objElement)

Parameters:
	objElement	The element to receive the input focus

Sets focus on the specified element.  If the element contains text that can 
be selected, it is selected as well.

Modification History
	Chris Protopapas	03/09/2004		Creation
	Chris Protopapas	12/30/2004		Do nothing if the Element is null
******************************************************************************/
function FocusOnElement(objElement)
{
	if (null == objElement)
		{return;}

	// Focus on the element
	objElement.focus();

	// Certain types can be selected as well
	var type = objElement.type;
	if (type == "text" || type == "textarea" || type == "password" || type == "fileupload")
		{objElement.select();}
}

/******************************************************************************
Format:		GetElementName(objElement, strNameAttribute)

Parameters:
	objElement			The element whose name you want to obtain.
	strNameAttribute	The name of an attribute whose value is to be returned 
		as the name of this element.  This argument is optional.

Returns the Name for this element.  Attributes are searched in 
this order:  strNameAttribute, name, id

Modification History
	Chris Protopapas	02/01/2005		Creation
******************************************************************************/
function GetElementName(objElement, strNameAttribute)
{
	if ((strNameAttribute != null) && (strNameAttribute.length > 0))
	{
		var strName = objElement.getAttribute(strNameAttribute);
		if ((strName != null) && (strName.length > 0))
			{return (strName);}
	}

	var strName = objElement.getAttribute("name");
	if ((strName != null) && (strName.length > 0))
		{return (strName);}

	var strName = objElement.getAttribute("id");

	return (strName);
}

/******************************************************************************
Format:		GetElementValue(objElement)
			GetElementValue(objElement, default)

Returns the value of the specified element.  Under certain circumstances, 
the default value, if specified, is returned.  These are described below. 

The following types are supported:
	button, submit, reset
	hidden
	text, textarea, password
	checkbox, radio
	select (select-one returns a single value)
	select (select-multiple returns an array of selected values)

For select-multiple, if none are selected, and a default value is not defined, 
then an empty array is returned.

If the specified Element is a radio group (that is, Element[0].type is "radio"), 
then the value of the selected radio button within the group is returned.  
If none are selected, the default value is returned.

If the specified Element is a checkbox group (that is, Element[0].type is "checkbox"), 
then the values of all the checked checkboxes are returned in an array.  If 
none are selected, and a default value is not defined, then an empty array is 
returned.

Parameters:
	objElement	The object whose value is to be returned.

	default		The value that should be returned if the Element is not 
				one of the supported types, or does not exist, or is a 
				selection object, radio button, or checkbox in which 
				no selection has been made.

Returns the value of the object, or the specified default if the Element is not 
one of the types listed.

Modification History
	Chris Protopapas	12/15/2004		Creation
	Chris Protopapas	12/30/2004		Return the default value if the Element 
		is null.
	Chris Protopapas	02/24/2005		Add support for a CheckBox element.
	Chris Protopapas	03/16/2005		Numerous enhancements to support 
		more types and multiple selections.
******************************************************************************/
function GetElementValue(objElement, strDefault)
{
	if (null == objElement)
		{return (strDefault);}

	// Get the selected value from the controlling object
	switch (objElement.type)
	{
	case "button":
	case "hidden":
	case "password":
	case "reset":
	case "submit":
	case "text":
	case "textarea":
		return (objElement.value);

	case "checkbox":
	case "radio":
		return ((objElement.checked) ? objElement.value : defaultReturn());

	case "select-one":
		var idx = objElement.selectedIndex;
		if (idx >= 0)
			{return (objElement.options[idx].value);}

		return (defaultReturn());

	case "select-multiple":
		var arSelected = new Array();

		for (var idx = 0; idx < objElement.options.length; idx++)
		{
			if (objElement.options[idx].selected)
				{arSelected[arSelected.length] = objElement.options[idx].value;}
		}

		// If there is at least one selected item, return the array
		if (arSelected.length > 0)
			{return (arSelected);}

		// None are selected.
		// If there is a default value specified, return it.  Otherwise, 
		// return the empty array.
		return ((typeof strDefault != "undefined") ? strDefault : arSelected);
	}

	// Support various groups
	switch (objElement[0].type)
	{
	case "radio":
		// We have a radio group.  Find the selected one.
		for (var idx = 0; idx < objElement.length; idx++)
		{
			if (objElement[idx].checked)
				{return (objElement[idx].value);}
		}

		return (defaultReturn());

	case "checkbox":
		// We have a checkbox group.  Find all the selected ones.
		var arChecked = new Array();

		for (var idx = 0; idx < objElement.length; idx++)
		{
			if (objElement[idx].checked)
				{arChecked[arChecked.length] = objElement[idx].value};
		}

		// If there is at least one selected item, return the array
		if (arChecked.length > 0)
			{return (arChecked);}

		// None are selected.
		// If there is a default value specified, return it.  Otherwise, 
		// return the empty array.
		return ((typeof strDefault != "undefined") ? strDefault : arChecked);
	}

	return (defaultReturn());
	
	// Private function to return the default value.
	// If it is undefined, then a null is returned.
	function defaultReturn()
	{
		return ((typeof strDefault != "undefined") ? strDefault : null);
	}
}

/******************************************************************************
Format:		IsElementAttributeTrue(objElement, strAttributeName)

Parameters:
	objElement	The element containing the attribute to be checked.
	strAttributeName	The name of the attribute to be checked.

Returns true if:
	1. The attribute is present by itself, with no value.
	2. The attribute has a value of "yes", "true", or "1"

Modification History
	Chris Protopapas	03/09/2004		Creation
	Chris Protopapas	12/30/2004		Return false if the Element is null
******************************************************************************/
function IsElementAttributeTrue(objElement, strAttributeName)
{
	if (null == objElement)
		{return (false);}

	var strValue = objElement.getAttribute(strAttributeName);
	if (strValue == null)
		{return (false);}

	strValue = strValue.toLowerCase();
	if ((strValue == "yes" || strValue == "true" || strValue == "1" || strValue == ""))
		{return (true);}

	return (false);
}

/******************************************************************************
Format:		LoadPropertiesFromChildInputObjects(objProperties, objParent)

Finds Input elements that are considered as Properties, and adds a name-value 
pair to the Properties object for that Input element.  The name of the Input 
element becomes the property name, and the value of the Input element is the 
property value.  Note: only currently visible Input objects are included.

Parameters:
	objProperties	The Properties object to be loaded with properties from 
		the inputs on the form.
	objParent		The object that contains the Input objects.  This could 
		be the entire form, or a smaller object such as a row in a table that
		contains inputs marked as properties.

HTML attributes:
	IsProperty			Indicates that an input is a Property.  If it appears by 
		itself, or is set equal to "yes", "true", or "1", then the input is 
		a property.  If it is absent, or is set to "no", "false", or "0", then 
		the input is not a property.

	propertyName		The name that the property should have.  If this 
		attribute is not present, then the Input element's "name" attribute 
		is used.  If that is not present, then the Input element's "id" 
		attribute is used.  If none of these are present, then the property 
		will be anonymous.

Returns nothing

Modification History
	Chris Protopapas	09/21/2004		Creation
	Chris Protopapas	02/01/2005		Use the new generic function to get the 
		element name to be used as the property name.
	Jim Gumm			03/17/2005		Use the GetElementValue function.
******************************************************************************/
function LoadPropertiesFromChildInputObjects(objProperties, objParent)
{
	// Iterate all currently visible Child Objects
	IterateChildInputObjects(objParent, MyCallback);
	return;

	// Private function to process each child Input object
	function MyCallback(objElement)
	{
		// If it is a "Property", add it to the Properties object
		var bIsProperty = IsElementAttributeTrue(objElement, "IsProperty");
		if (bIsProperty)
		{
			var strName = GetElementName(objElement, "propertyName");
			objProperties[strName] = GetElementValue(objElement, "");
		}

		return (true);		// Keep going
	}
}

/******************************************************************************
Format:		IterateChildInputObjects(objParent, funcCallback, ...)

Parameters:
	objParent		The name of the Object containing the Child objects.

	funcCallback	The function that will be called for each child Input object.  
					The first argument to this function will be the child 
					object itself.

	...			An open-ended argument list containing additional arguments 
				that are to be passed to the Callback function.  These arguments 
				will follow the first argument, which is the Child object itself.

Returns:
	true if the Callback function returned true for all Child objects.  As soon 
	as the Callback function returns false, iteration stops and a false is 
	returned to the caller of this function.

This method iterates the Parent object, and calls the Callback function for 
each Child Input object.  If the Callback function returns a false, the 
iteration will stop.  The iteration is deep, meaning that the Input objects 
need not be immediate children of the parent.  They can be as deeply nested 
as necessary.

Modification History
	Chris Protopapas	03/09/2004		Creation
	Chris Protopapas	09/23/2004		Return immediately if the Parent is null.
******************************************************************************/
function IterateChildInputObjects(objParent, funcCallback)
{
	// If either the Parent object or the Callback function is null, we're done.
	if ((null == objParent) || (null == funcCallback))
		{return (true);}

	// Build the array of arguments for the Handler.
	// The Child will be first, followed by any 
	// optional arguments.  Here, we are loading up the array with 
	// the optional arguments only, leaving element zero for the 
	// Child object later.
	//alert("arguments.length = " + arguments.length);
	var argArray = new Array();
	for (var idx = 2; idx < arguments.length; idx++)
	{
		argArray[idx - 1] = arguments[idx];
	}

	// Get an array of our immediate children and iterate through them
	var childArray = objParent.children;
	//alert("Parent has " + childArray.length + " children");
	for (var idx = 0; idx < childArray.length; idx++)
	{
		// If the child element Iteration returns false, we're done
		var objElement = childArray[idx];
		if (IterateChild(objElement) == false)
			{return (false);}
	}

	// All the Iteration returned true.  We're done.
	return (true);

	// ---------------------------------------------
	// Private function to Iterate a child element
	function IterateChild(objElement)
	{
		//alert("Starting IterateChild");

		// If this element is null, keep going.
		if (null == objElement)
			{return (true);}

		// If this element is not visible, skip it and its children
		if (objElement.style.display == "none")
			{return (true);}
		if (objElement.style.visibility == "hidden")
			{return (true);}

		// First Iterate this object's children
		var childArray = objElement.children;
		//alert("Element has " + childArray.length + " children");
		for (var idx = 0; idx < childArray.length; idx++)
		{
			// If the child element Iteration returns false, we're done
			if (IterateChild(childArray[idx], funcCallback) == false)
				{return (false);}
		}

		// All our children have been iterated
		// Call the Callback for this Child object

		// Only Input objects matter here.
		// If there is a "type" value, then we have an input object.
		var strType = objElement.getAttribute("type");
		if ((strType == null) || (strType.length < 1))
			{return (true);}

		// Call the Callback
		argArray[0] = objElement;
		var bKeepGoing = funcCallback.apply(null, argArray);
		return (bKeepGoing);
	}
}

/******************************************************************************
Format:		ScrubPhoneNumber(strPhoneNo)

Parameters:
	strPhoneNo		A string containing the phone number to be scrubbed.

Removes all inappropriate characters from a string containing a phone number 
and returns that edited string.  The only characters allowed are the digits 
0 through 9.
The original String is not modified.

Returns:
	The scrubbed phone number, or null if strPhoneNo is null.

Modification History
	Chris Protopapas	06/15/2005		Creation
	Venkata Gangisetty	12/02/2005		used replace function.
******************************************************************************/
function ScrubPhoneNumber(strPhoneNo)
{
	if (null == strPhoneNo)	
		{return (null);} 
	return (strPhoneNo.replace(/[^0-9]*/g,''));
}

/******************************************************************************
Format:		FormatPhoneNumber_old(strPhoneNo)

Parameters:
	strPhoneNo		A string containing the scrubbed phone number.  This 
		phone number must contain only digits, and no punctuation.
	strCountry		A string containing the coutry code.

Returns:
	A string containing the formatted phone number, which contains dashes 
	between the phone number segments.

This method formats the supplied Phone Number as follows
	Prefix-AreaCode-Exchange-Number

Modification History
	Chris Protopapas	01/12/2005		Creation
	Venkata Gangisetty 	11/28/2005		modified to add country and format as 
										per the predefined coutry specific mask
******************************************************************************/
function FormatPhoneNumber(strPhoneNo,strCountry)
{
	var cnstUnitedStates = "USA"; //"<%=cnstUnitedStates%>";		
	var cnstCanada = "CAN"; //"<%=cnstCanada%>";		
	var cnstIndia	 = "IND"; //"<%=cnstIndia%>";
	
	var cnstUnitedStatesFormat = "XXX-XXX-XXXX"; //"<%=cnstUnitedStatesFormat%>";		
	var cnstCanadaFormat		 = "XXX-XXX-XXXX"; //"<%=cnstCanadaFormat%>";		
	
	// If we have no supplied phone number, we're done
	if (null == strPhoneNo)
		{return ("");}
	
	switch (strCountry.toUpperCase())
	{
		case cnstUnitedStates:
			return MakeFormattedPhoneNumber(strPhoneNo,cnstUnitedStatesFormat);
		case cnstCanada:
			return MakeFormattedPhoneNumber(strPhoneNo,cnstCanadaFormat);
		default:
			return ScrubPhoneNumber(strPhoneNo);
	}
}


/********************************************************************************
	Format	:	MakeFormattedPhoneNumber(strPhoneNumber,strFormat)

	strPhoneNo		A string containing the phone number.  This 
					phone number must contain only digits, and no punctuation.
	strFormat		    A string containing the format/mask.

	Returns:
		A string containing the formatted phone number, which contains dashes 
		between the phone number segments, as per the couuntry format defined.

	Modification History	: Venkata Gangisetty 	11/28/2005		Creation	
********************************************************************************/
function MakeFormattedPhoneNumber(strPhoneNumber, strFormat)
{
	//Removing any special charecgters from string.
	strPhoneNumber = ScrubPhoneNumber(strPhoneNumber);
	
	// If we have no supplied phone number, we're done
	if ((null == strPhoneNumber) || ("" == strPhoneNumber) || (null == strFormat) || ("" == strFormat))
		{return (strPhoneNumber);}

	var strFormatedPhoneNumber,strTemp;
	strFormatedPhoneNumber = "";
	var strFormatArray = strFormat.split("-");
	
	for (var iCounter = strFormatArray.length-1;iCounter >= 0; iCounter--)
	{
		strTemp = Right(strPhoneNumber,Len(strFormatArray[iCounter]));
		strPhoneNumber = Left(strPhoneNumber,Len(strPhoneNumber) - Len(strTemp));
		if (Len(strTemp) > 0)
		{
			strFormatedPhoneNumber = "-" + strTemp + strFormatedPhoneNumber;
		}
	}
	
	if (Len(strPhoneNumber) > 0) 
	{
		strFormatedPhoneNumber = strPhoneNumber + strFormatedPhoneNumber;
	}
	else
	{
		strFormatedPhoneNumber = Right(strFormatedPhoneNumber,Len(strFormatedPhoneNumber)-1);
	}
	
	
	return strFormatedPhoneNumber;
}

/******************************************************************************
Format:		ExecuteParentCallback(objWindow, strCallbackName, objProperties)

Parameters:
	objWindow		The Window containing the Callback that should be executed.
	strCallbackName	A string containing the name of the Callback to be executed.
	objProperties	A properties object that will be passed to the Callback.

Returns:
	The return code from the Callback

This method finds and executes the specified callback, passing the properties 
object.  If the window or callback can't be found, then an error message is 
displayed.

Modification History
	Chris Protopapas	01/14/2005		Creation
******************************************************************************/
function ExecuteParentCallback(objWindow, strCallbackName, objProperties)
{
	// If there is no Window specified, we're done
	if (null == objWindow)
	{
		alert(	"A Window has not been specified.\n" +
				"No action will be taken.");
		return;
	}

	// If there is no Function Name specified, we're done.
	if ((null == strCallbackName) || (strCallbackName.length < 1))
	{
		alert(	"A Callback Function has not been specified.\n" +
				"No action will be taken.");
		return;
	}

	// If the Function is not present in the Window then we're done
	var fnFunction = objWindow[strCallbackName];
	if (null == fnFunction)
	{
		alert(	"The \"" + strCallbackName + "\" Callback \n" + 
				"does not exist in the Window.\n" + 
				"No action will be taken.");
		return;
	}

/*
	// Build the array of arguments for the Callback.
	// Load the array with the optional arguments.
	//alert("arguments.length = " + arguments.length);

	var argArray = new Array();
	for (var idx = 2; idx < arguments.length; idx++)
	{
		argArray[idx - 2] = arguments[idx];
	}

	// Execute the function
	// I don't know why the "apply" isn't working here, so for now, we're stuck 
	// with one argument only.
	//fnFunction.apply(null, argArray);
*/

	// Execute the Callback
	return (fnFunction(objProperties));
}

/******************************************************************************
Format:		SetChildAttribute(objParent, strChildName, 
								strAttributeName, objAttributeValue)

Parameters:
	objParent			The parent object that contains the child to be modified.
	strChildName		A string containing the name of the child.
	strAttributeName	A string containing the name of the child's attribute 
		to be modified.
	objAttributeValue	The new value for the attribute.  The data type of this 
		argument must be appropriate for the attribute or an error may occur.

Returns:
	void

This method modifies the value of the specified attribute of the specified 
child object.

Modification History
	Chris Protopapas	06/10/2005		Creation
******************************************************************************/
function SetChildAttribute(objParent, strChildName, strAttributeName, objAttributeValue)
{
	var objChild = FindChildByName(objParent, strChildName);
	if (null == objChild)
		{return;}

	// Get the child's attribute object.  If it's null, do nothing.
	var objAttribute = objChild.attributes[strAttributeName];
	if (null == objAttribute)
		{return;}

	// Set the value of the attribute.
	objAttribute.value = objAttributeValue;
}

/******************************************************************************
Format:		SetChildStyle(objParent, strChildName, strStyleName, objStyleValue)

Parameters:
	objParent		The parent object that contains the child to be modified.
	strChildName	A string containing the name of the child.
	strStyleName	A string containing the name of the child's style to be modified.
	objStyleValue	The new value for the style.  The data type of this 
		argument must be appropriate for the style or an error may occur.

Returns:
	void

This method modifies the value of the specified style of the specified 
child object.

Modification History
	Chris Protopapas	06/13/2005		Creation
******************************************************************************/
function SetChildStyle(objParent, strChildName, strStyleName, objStyleValue)
{
	var objChild = FindChildByName(objParent, strChildName);
	if (null == objChild)
		{return;}

	// See if the style exists.  If it doesn't, do nothing.
	var strStyle = objChild.style[strStyleName];
	if (null == strStyle)
		{return;}

	// Set the value of the Style.
	objChild.runtimeStyle[strStyleName] = objStyleValue;
}

/******************************************************************************
Format:		FindChildByName(objParent, strChildName)

Parameters:
	objParent		The parent object that contains the child to be found.
	strChildName	A string containing the name of the child.

Returns:
	The child object, or null if no such child exists

This method returns the specified child object.  Only immediate children are 
searched.  This method does not recurse into any depth.

Modification History
	Chris Protopapas	06/13/2005		Creation
******************************************************************************/
function FindChildByName(objParent, strChildName)
{
	// If the parent object or the Child Name, do nothing.
	if ((null == objParent) || (null == strChildName))
		{return;}

	// Get the child object.
	return (objParent.children[strChildName]);
}

/******************************************************************************
Format:		SetChildAttribute(objParent, strChildName, 
								strAttributeName, objAttributeValue)

Parameters:
	objParent			The parent object that contains the child to be modified.
	strChildName		A string containing the name of the child.
	strAttributeName	A string containing the name of the child's attribute 
		to be modified.
	objAttributeValue	The new value for the attribute.  The data type of this 
		argument must be appropriate for the attribute or an error may occur.

Returns:
	void

This method modifies the value of the specified attribute of the specified 
child object.

Modification History
	Chris Protopapas	06/10/2005		Creation
******************************************************************************/
function SetChildAttribute(objParent, strChildName, strAttributeName, objAttributeValue)
{
	var objChild = FindChildByName(objParent, strChildName);
	if (null == objChild)
		{return;}

	// Get the child's attribute object.  If it's null, do nothing.
	var objAttribute = objChild.attributes[strAttributeName];
	if (null == objAttribute)
		{return;}

	// Set the value of the attribute.
	objAttribute.value = objAttributeValue;
}

/******************************************************************************
Format:		SetChildStyle(objParent, strChildName, strStyleName, objStyleValue)

Parameters:
	objParent		The parent object that contains the child to be modified.
	strChildName	A string containing the name of the child.
	strStyleName	A string containing the name of the child's style to be modified.
	objStyleValue	The new value for the style.  The data type of this 
		argument must be appropriate for the style or an error may occur.

Returns:
	void

This method modifies the value of the specified style of the specified 
child object.

Modification History
	Chris Protopapas	06/13/2005		Creation
******************************************************************************/
function SetChildStyle(objParent, strChildName, strStyleName, objStyleValue)
{
	var objChild = FindChildByName(objParent, strChildName);
	if (null == objChild)
		{return;}

	// See if the style exists.  If it doesn't, do nothing.
	var strStyle = objChild.style[strStyleName];
	if (null == strStyle)
		{return;}

	// Set the value of the Style.
	objChild.runtimeStyle[strStyleName] = objStyleValue;
}

/******************************************************************************
Format:		FindChildByName(objParent, strChildName)

Parameters:
	objParent		The parent object that contains the child to be found.
	strChildName	A string containing the name of the child.

Returns:
	The child object, or null if no such child exists

This method returns the specified child object.  Only immediate children are 
searched.  This method does not recurse into any depth.

Modification History
	Chris Protopapas	06/13/2005		Creation
******************************************************************************/
function FindChildByName(objParent, strChildName)
{
	// If the parent object or the Child Name, do nothing.
	if ((null == objParent) || (null == strChildName))
		{return;}

	// Get the child object.
	return (objParent.children[strChildName]);
}

/******************************************************************************
Format:		SimulateModalDialog(objPopupWindow)

Parameters:
	objPopupWindow	The popup window.

Returns:	void

This method is used to make a modeless popup window behave like a modal 
dialog.  This is necessary when the popup cannot be modal because it must 
transact with the server via submit.

Modification History
	Chris Protopapas	12/12/2005		Creation
******************************************************************************/
function SimulateModalDialog(objPopupWindow)
{
	// If the caller did not supply a Window, we're done.
	if (null == objPopupWindow)
		{return;}

	// Save the Popup Window
	window["SimulateModalDialog_PopupWindow"] = objPopupWindow;

	// Save the parent window's current OnFocus function, if it exists.
	window["SimulateModalDialog_PreviousOnFocusHandler"] = window.onfocus;

	// Activate our OnFocus handler.
	window.onfocus = SimulationOnFocusHandler;
	return;

	// This is called whenever the parent window receives focus
	function SimulationOnFocusHandler()
	{
		// If the popup window still exists, then give it focus
		var objPopupWindow = window["SimulateModalDialog_PopupWindow"];
        if(objPopupWindow != null)
        {
		    if (! objPopupWindow.closed)
		    {
			    objPopupWindow.focus();
			    return;
		    }
        }
		// The popup window no longer exists.
		window["SimulateModalDialog_PopupWindow"] = null;

		// Restore the parent window's original OnFocus handler
		// If it exists, call it.
		window.onfocus = window["SimulateModalDialog_PreviousOnFocusHandler"];
		window["SimulateModalDialog_PreviousOnFocusHandler"] = null;
		if (null != window.onfocus)
		{
			window.onfocus();
		}
	}
}

/******************************************************************************
Format:		BuildArgumentListArray(arSrcArgList, intSrcStartIdx, intDstBlanks)

Parameters:
	arSrcArgList	The argument list to be transferred.

	intSrcStartIdx	The index into the argument list to begin the transfer.

	intDstBlanks	The number of blank entries that should be left at the 
					start of the resulting argument array.

Returns:
	An array containing items from the source argument list.

Builds an array containing items from the specified argument list.  This is 
used to build up an argument list array for passing to a function using the 
apply method.  

Modification History
	Chris Protopapas	12/12/2005		Creation
******************************************************************************/
function BuildArgumentListArray(arSrcArgList, intSrcStartIdx, intDstBlanks)
{
	// array indeces must be at least zero
	intSrcStartIdx = Math.max(intSrcStartIdx, 0);
	intDstBlanks = Math.max(intDstBlanks, 0);

	// Create the array object
	var arDstArgList = new Array();
	// Calculate the destination offset that will leave the blank entries 
	// in the destination array.
	var intDstOffset = intSrcStartIdx - intDstBlanks;

	// Transfer items from the source argument list to the array
	for (var idx = intSrcStartIdx; idx < arSrcArgList.length; idx++)
	{
		arDstArgList[idx - intDstOffset] = arSrcArgList[idx];
	}

	return (arDstArgList);
}

/******************************************************************************
Format:		BuildWindowOpenSizeLocationFeatures
				(nWidth, nHeight, strPosition, nLeftPos, nTopPos)

Parameters:
	nWidth			The desired width of the new window, in pixels.
	nHeight			The desired height of the new window, in pixels.
	strPosition		The desired position of the new window.  The following 
					values are supported:
						center		The new window is to be centered.  The 
									Left and Top values will be calculated 
									based upon the actual screen size so that 
									the window appears in the center of the 
									screen.  The nLeftPos and nTopPos arguments 
									are ignored and may be omitted.
						absolute	The nLeftPos and nTopPos arguments are 
									used to define the left and top edges 
									of the new window.
	nLeftPos		The position of the left edge of the new window, in pixels.
	nTopPos			The position of the top edge of the new window, in pixels.

Returns:
	A string

Builds a string containing the size and location parameters to be used in the 
	"features" string of the window.open function.

Modification History
	Chris Protopapas	04/07/2006		Creation
******************************************************************************/
function BuildWindowOpenSizeLocationFeatures(nWidth, nHeight, strPosition, nLeftPos, nTopPos)
{
	var strFeatures = "width=" + nWidth + ", height=" + nHeight;

	var nXPosition = -1;
	var nYPosition = -1;

	if (strPosition == "center")
	{
		if (null != screen.width)
			{nXPosition = (screen.width - nWidth) / 2;}
		if (null != screen.height)
			{nYPosition = (screen.height - nHeight) / 2;}
	}
	else if (strPosition == "absolute")
	{
		if (null != nLeftPos)
			{nXPosition = nLeftPos;}
		if (null != nTopPos)
			{nYPosition = nTopPos;}
	}

	if (nXPosition >= 0)
		{strFeatures += ", left=" + nXPosition;}
	if (nYPosition >= 0)
		{strFeatures += ", top=" + nYPosition;}

	return (strFeatures);
}

/******************************************************************************************************
			********	VB Script Functions in JavaScript		***************************************
*******************************************************************************************************/			


// Left(string, length): Returns a specified number of characters from the
//                      left side of a string

        function Left(str, n)
        /***
                IN: str - the string we are LEFTing
                    n - the number of characters we want to return

                RETVAL: n characters from the left side of the string
        ***/
        {
                if (n <= 0)     // Invalid bound, return blank string
                        return "";
                else if (n > String(str).length)   // Invalid bound, return
                        return str;                // entire string
                else // Valid bound, return appropriate substring
                        return String(str).substring(0,n);
        }
//*******************************************************************************************************/			

//Right(string, length): Returns a specified number of characters from the
//                       right side of a string

        function Right(str, n)
        /***
                IN: str - the string we are RIGHTing
                    n - the number of characters we want to return

                RETVAL: n characters from the right side of the string
        ***/
        {
                if (n <= 0)     // Invalid bound, return blank string
                   return "";
                else if (n > String(str).length)   // Invalid bound, return
                   return str;                     // entire string
                else { // Valid bound, return appropriate substring
                   var iLen = String(str).length;
                   return String(str).substring(iLen, iLen - n);
                }
        }



/****************************************************************************************************************/

//Mid(string, start, length): Returns a specified number of characters from a
//                            string


        function Mid(str, start, len)
        /***
                IN: str - the string we are LEFTing
                    start - our string's starting position (0 based!!)
                    len - how many characters from start we want to get

                RETVAL: The substring from start to start+len
        ***/
        {
                // Make sure start and len are within proper bounds
                if (start < 0 || len < 0) return "";

                var iEnd, iLen = String(str).length;
                if (start + len > iLen)
                        iEnd = iLen;
                else
                        iEnd = start + len;

                return String(str).substring(start,iEnd);
        }

			
/****************************************************************************************************************/




//Len(String) : Returns the number of characters in a string

        function Len(str)
        /***
                IN: str - the string whose length we are interested in

                RETVAL: The number of characters in the string
        ***/
        {  return String(str).length;  }


/****************************************************************************************************************/

//InStr(str, SearchForStr) : Returns the location a character (charSearchFor)
//                           was found in the string str


function InStr(strSearch, charSearchFor)
/*
InStr(strSearch, charSearchFor) : Returns the first location a substring (SearchForStr)
                           was found in the string str.  (If the character is not
                           found, -1 is returned.)
                           
Requires use of:
	Mid function
	Len function
*/
{
	for (i=0; i < Len(strSearch); i++)
	{
	    if (charSearchFor == Mid(strSearch, i, 1))
	    {
			return i;
	    }
	}
	return -1;
}


/****************************************************************************************************************/

/******************************************************************************
Format:		IterateChildObjects(objParent, funcCallback, ...)

Parameters:
    objParent		The name of the Object containing the Child objects.

    funcCallback	The function that will be called for each child Input object.  
				    The first argument to this function will be the child 
				    object itself.

    ...			An open-ended argument list containing additional arguments 
			    that are to be passed to the Callback function.  These arguments 
			    will follow the first argument, which is the Child object itself.

Returns:
    true if the Callback function returned true for all Child objects.  As soon 
    as the Callback function returns false, iteration stops and a false is 
    returned to the caller of this function.

This method iterates the Parent object, and calls the Callback function for 
each Child Input object.  If the Callback function returns a false, the 
iteration will stop.  The iteration is deep, meaning that the Input objects 
need not be immediate children of the parent.  They can be as deeply nested 
as necessary.

Modification History
    Chris Protopapas	03/09/2004		Creation
    Chris Protopapas	09/23/2004		Return immediately if the Parent is null.
    Jim McClure         01/11/2007      Renamed the method to IterateChildObjects and
                                        made the function generic to take and array
                                        of tag names to find.
******************************************************************************/
function IterateChildObjects(objParent, tags, funcCallback)
{
    // debugger;
    // If either the Parent object or the Callback function is null, we're done.
    if ((null == objParent) || (null == funcCallback))
	    {return (true);}

    if (IterateChildObjects.arguments.length < 2)
    {
        return false;
    }
  
    var objParentType = typeof(objParent);
    var funcCallbackType = typeof(funcCallback);
    var tagsType = typeof(tags);

    // Build the array of arguments for the Handler.
    // The Child will be first, followed by any 
    // optional arguments.  Here, we are loading up the array with 
    // the optional arguments only, leaving element zero for the 
    // Child object later.
    //alert("arguments.length = " + arguments.length);
    var argArray = new Array();
    for (var idx = 3; idx < arguments.length; idx++)
    {
	    argArray[idx - 2] = arguments[idx];
    }

    // Get an array of our immediate children and iterate through them
    var childArray = objParent.children;
    //alert("Parent has " + childArray.length + " children");
    for (var idx = 0; idx < childArray.length; idx++)
    {
	    // If the child element Iteration returns false, we're done
	    var objElement = childArray[idx];
	    if (IterateChild(objElement) == false)
		    {return (false);}
    }

    // All the Iteration returned true.  We're done.
    return (true);

    // ---------------------------------------------
    // Private function to Iterate a child element
    function IterateChild(objElement)
    {
	    //alert("Starting IterateChild");

	    // If this element is null, keep going.
	    if (null == objElement)
		    {return (true);}

	    // If this element is not visible, skip it and its children
	    if (objElement.style.display == "none")
		    {return (true);}
	    if (objElement.style.visibility == "hidden")
		    {return (true);}

	    // First Iterate this object's children
	    var childArray = objElement.children;
	    //alert("Element has " + childArray.length + " children");
	    for (var idx = 0; idx < childArray.length; idx++)
	    {
		    // If the child element Iteration returns false, we're done
		    if (IterateChild(childArray[idx], funcCallback) == false)
			    {return (false);}
	    }

	    // All our children have been iterated
	    // Call the Callback for this Child object

	    // Call the Callback
	    argArray[0] = objElement;
	    var bKeepGoing = true;
	    if (tags != null && tags.length > 0)
	    {
            for (var idx = 0; idx < tags.length; idx++)
            {
                if (objElement.tagName == tags[idx])
                {
                    // alert(tags[idx] + ' tag found = ' + objElement.innerText);
                    bKeepGoing = funcCallback.apply(null, argArray);
                }
            }
        }
        else
        {
	        bKeepGoing = funcCallback.apply(null, argArray);
        }
	    return (bKeepGoing);
    }
}

/****************************************************************************************************************/

