//-----------------------------------------------------------------------------
function selectStateByValue(sIDOfForm, sIDOfSelectBox, sValueOfOption)
{
	// get a reference to the form object, test for validity
	var frmToUse = document.forms[sIDOfForm];
	if(!frmToUse)
		return;
	
	// get a reference to the select object, test for validity
	var objSelect = frmToUse[sIDOfSelectBox];
	if(!objSelect)
		return;

	// loop through all of the options on the select box
	for(var niCurrentOption = 0; niCurrentOption < objSelect.options.length; niCurrentOption++)
	{
		var sCurrentValue = objSelect.options[niCurrentOption].value;
	
		// if we've found the correct option, then we set the selected index and return (nothing more to do)
		if(sCurrentValue == sValueOfOption)
		{
			objSelect.selectedIndex = niCurrentOption;
			return;
		}
	}
}
//-----------------------------------------------------------------------------
function selectOptionByValue(sIDOfForm, sIDOfSelectBox, sValueOfOption)
{
	// get a reference to the form object, test for validity
	var frmToUse = document.forms[sIDOfForm];
	if(!frmToUse)
		return;
	
	// get a reference to the select object, test for validity
	var objSelect = frmToUse[sIDOfSelectBox];
	if(!objSelect)
		return;

	// loop through all of the options on the select box
	for(var niCurrentOption = 0; niCurrentOption < objSelect.options.length; niCurrentOption++)
	{
		var sCurrentValue = objSelect.options[niCurrentOption].value;
	
		// if we've found the correct option, then we set the selected index and return (nothing more to do)
		if(sCurrentValue == sValueOfOption)
		{
			objSelect.selectedIndex = niCurrentOption;
			return;
		}
	}
}
//-----------------------------------------------------------------------------
function setTextBoxValue(sIDOfForm, sIDOfTextBox, sValueToSet)
{
	// get a reference to the form object, test for validity
	var frmToUse = document.forms[sIDOfForm];
	if(!frmToUse)
		return;
	
	// get a reference to the select object, test for validity
	var objTextBox = frmToUse[sIDOfTextBox];
	if(!objTextBox)
		return;

	objTextBox.value = sValueToSet;
}
//-----------------------------------------------------------------------------
function setRadioButtonValue(sIDOfForm, sIDOfRadioButton, sValueToSet)
{
	// get a reference to the form object, test for validity
	var frmToUse = document.forms[sIDOfForm];
	if(!frmToUse)
		return;	
	
	// get a reference to the select object, test for validity
	var objRadioButton = frmToUse[sIDOfRadioButton];
	if(!objRadioButton)
		return;

	// loop through the radio button values
	for (var niCurrentOption = 0; niCurrentOption < objRadioButton.length; niCurrentOption++)
	{
		if (objRadioButton[niCurrentOption].value == sValueToSet)
		{
			objRadioButton[niCurrentOption].checked = true;
			return;
		}
	}
}
//-----------------------------------------------------------------------------
function setCheckBoxValue(sIDOfForm, sIDOfCheckBox, bMarkAsChecked)
{
	// get a reference to the form object, test for validity
	var frmToUse = document.forms[sIDOfForm];
	if(!frmToUse)
		return;	
	
	// get a reference to the select object, test for validity
	var objCheckBox = frmToUse[sIDOfCheckBox];
	if(!objCheckBox)
		return;

	objCheckBox.checked = bMarkAsChecked;
}
//-----------------------------------------------------------------------------
function setCheckBoxValueMultiple(sIDOfForm, sIDOfCheckBox, sValues, sDelim)
{
	// get a reference to the form object, test for validity
	var frmToUse = document.forms[sIDOfForm];
	if(!frmToUse)
		return;	
	
	// get a reference to the checkbox object, test for validity
	var objCheckBox = frmToUse[sIDOfCheckBox];
	if(!objCheckBox)
		return;

	// validate the delimited string of values
	if (!sValues || sValues.length <= 0)
		return;
	
	var niCurrentValue = 0;
	var sValueArray = sValues.split(sDelim);
	
	// loop through the checkbox options and the array of values
	for (var niCurrentOption = 0; niCurrentOption < objCheckBox.length && niCurrentValue < sValueArray.length; niCurrentOption++)
	{
		var sCurrentValue = sValueArray[niCurrentValue];
		
		if (objCheckBox[niCurrentOption].value == sCurrentValue)
		{
			objCheckBox[niCurrentOption].checked = true;
			niCurrentValue++;
		}
	}
}
//-----------------------------------------------------------------------------
function setCheckBoxValueMultipleArray(sIDOfForm, sIDOfCheckBox, sValueArray)
{
	// get a reference to the form object, test for validity
	var frmToUse = document.forms[sIDOfForm];
	if(!frmToUse)
		return;	
	
	// get a reference to the checkbox object, test for validity
	var objCheckBox = frmToUse[sIDOfCheckBox];
	if(!objCheckBox)
		return;

	// validate the delimited string of values
	if (!sValueArray || sValueArray.length <= 0)
		return;
	
	var niCurrentValue = 0;
	
	// loop through the checkbox options and the array of values
	for (var niCurrentOption = 0; niCurrentOption < objCheckBox.length && niCurrentValue < sValueArray.length; niCurrentOption++)
	{
		var sCurrentValue = sValueArray[niCurrentValue];
		
		if (objCheckBox[niCurrentOption].value == sCurrentValue)
		{
			objCheckBox[niCurrentOption].checked = true;
			niCurrentValue++;
		}
	}
}
//-----------------------------------------------------------------------------
function formatPhoneNumber(objTextBox)
{
	if(!objTextBox)
		return;
	
	var sValue = objTextBox.value;
	var sDigitNumber = stripNonDigits(sValue);	
	
	
	var sFormattedPhone = "";
	switch(sDigitNumber.length)
	{
	case 0:
		break;
	case 7:
		// it's a local phone number
		sFormattedPhone = sDigitNumber.substr(0, 3) + "-" + sDigitNumber.substr(3, 4);
		break;
	case 10:
		// it's a US phone number w/ no "1" @ the beginning
		sFormattedPhone = "(" + sDigitNumber.substr(0, 3) + ") " + sDigitNumber.substr(3, 3) + "-" + sDigitNumber.substr(6, 4);
		break;
	case 11:
		// check if the first char is a 1
		var nFirstDigit = parseInt(sDigitNumber.substr(0, 1));
		if(nFirstDigit == 1)
		{
			// it's a US phone number w/ "1" @ the beginning
			sFormattedPhone = "(" + sDigitNumber.substr(1, 3) + ") " + sDigitNumber.substr(4, 3) + "-" + sDigitNumber.substr(7, 4);
		}
		break;
	}
	if( (sFormattedPhone.length > 0) && (sFormattedPhone != sValue))
		objTextBox.value = sFormattedPhone;
}

//-----------------------------------------------------------------------------
function stripNonDigits(sValueToStrip)
{
	var sStripped = "";
	for(var niCurrentChar = 0; niCurrentChar < sValueToStrip.length; niCurrentChar++)
	{
		var sCurrentChar = sValueToStrip.charAt(niCurrentChar);
		if(!isNaN(sCurrentChar) && (sCurrentChar != " "))
		{
			var nCurrentNumber = parseInt(sCurrentChar, 10);
			sStripped += nCurrentNumber;
		}
	}
	return sStripped;
}