

////////////////////////////////////////////////////////////////////////////////
// Name:	ToggleOther
////////////////////////////////////////////////////////////////////////////////
// Purpose:	To toggle the "other" textbox (enabled/disabled) based on
//			the value of the select list.
// Params:	varSelectedValue - The value of the selected option in the select list.
//			varOtherValue - The value of the select option for "other"
//			objOtherBox - a handle on the "other" textbox within the page.
// Returns:	None
// Depends:	None
//
function ToggleOther(varSelectedValue, varOtherValue, objOtherBox) {
	var objOther;

	// Now, handle the event based on the what happened.
	if (varSelectedValue != varOtherValue || varSelectedValue == "") {
	
		if (objOtherBox.disabled == false) {
		
			// The "other" textbox was enabled, but the "Other" option is no longer selected,
			// so we need to clear and disable the "other" textbox.
			objOtherBox.value = "";
			objOtherBox.disabled = true;
			objOtherBox.className = "disabled";
		}
		
	} else {
	
		objOtherBox.disabled = false;
		objOtherBox.className = "formTextField";
		objOtherBox.focus();
	
	}
}



////////////////////////////////////////////////////////////////////////////////
// Name:	fixDisableWhenUserComesBack
////////////////////////////////////////////////////////////////////////////////
// Purpose:	To enable the "Other" box when based on the selected value of the
//			select list.
// Params:	strSelectID - The document ID of the select list.
//			strOtherBox - The name of the "Other" input box
//			varOtherValue - The value of the select option for "other"
// Returns:	None
// Depends:	None
//
function fixDisableWhenUserComesBack( strSelectID, strOtherBoxID, varOtherValue ) {
	var objOther;
	var objCompanyList;
	var strSelectedValue;
	
	objCompanyList = document.getElementById( strSelectID );
	strSelectedValue = objCompanyList.options[objCompanyList.selectedIndex].value;
	
	if ( strSelectedValue == varOtherValue) {
		objOther = document.getElementById( strOtherBoxID );
		objOther.disabled = false;
		objOther.className = "formTextField";
	}
}

function prefillRange(objSelect, objFrom, objTo)
{
	var strSelected = objSelect[objSelect.selectedIndex].value;
	
	if (strSelected != '') {
		//strictly type diff value
		var intDiff = new Number(strSelected);
		
		//set "from" date by adding diff to current timestamp and fixing to first day
		var datFrom = new Date();
		datFrom.setMonth((datFrom.getMonth() + intDiff));
		datFrom.setDate(1);
		
		//set "to" date by adding one month to "from" date then subtracting a day
		var datTo = new Date(datFrom.toString());
		datTo.setMonth((datTo.getMonth() + 1));
		datTo.setDate(0);
		
		//update form element values
		objFrom.value = formatShortDate(datFrom);
		objTo.value = formatShortDate(datTo);
	} else {
		//reset form element values
		objFrom.value = '';
		objTo.value = '';
	}
}

function formatShortDate(varDate)
{
	var strDate = new String();
	
	strDate += (varDate.getMonth() + 1) + '/';
	strDate += varDate.getDate() + '/';
	strDate += varDate.getFullYear();
	
	return strDate;
}

//window functions
var blnRefreshWindow = false;

function windowIsOpen(objWindow)
{
	var blnIsOpen = false;
	
	if (typeof(objWindow)=='object') {
		if (!objWindow.closed) {
			blnIsOpen = true;
		}
	}
	return blnIsOpen;
}

function setOpenerRefresh()
{
	if (windowIsOpen(opener)) {
		opener.blnRefreshWindow = true;
	}
}

function refreshOpener(strDefaultURL,blnCloseMe)
{
	var blnExists = false;
	
	if (windowIsOpen(opener)) {
	 	if (opener.blnRefreshWindow==true) {
			opener.location.reload();
			opener.focus();
		}
		blnExists = true;
	}
	
	if (blnExists==false) {
		window.open(strDefaultURL);
		blnCloseMe = true;
	}
	
	if (blnCloseMe==true) window.close(self);
}

function openerLocation(strLocation)
{
	if (windowIsOpen(opener)) {
		opener.location.href=strLocation;
		opener.focus();
	} else {
		window.open(strLocation);
		window.close(self);
	}
}

function focusPopup(objPopup,theURL,winName,features,width,height)
{
	if (!windowIsOpen(objPopup)) {
		objPopup = returnPopup(theURL,winName,features,width,height);
	}
	 objPopup.focus();
	 return objPopup;
}

function openPopup(theURL,winName,features,width,height)
{
	var objPopup = returnPopup(theURL,winName,features,width,height);
	objPopup.focus();
}

function returnPopup(theURL,winName,features,width,height)
{
	var winWidth	= width;
	var winHeight	= height;
	var strWinSize	= ",width=" + winWidth + ",height=" + winHeight;

	if (window.screen) {
		var winPosL = (screen.availWidth - winWidth) / 2;
		var winPosT = (screen.availHeight - winHeight) / 2;
		strWinSize += ",left=" + winPosL + ",screenX=" + winPosL + ",top=" + winPosT + ",screenY=" + winPosT;
	}	
	
	return window.open(theURL,winName,features + strWinSize);
}

function closePopup()
{
	window.close(self);
}

//form functions
function getkey(e)
{
	if (window.event)
	   return window.event.keyCode;
	else if (e)
	   return e.which;
	else
	   return null;
}






//////////////////////////////
// MirrorElement
//////////////////////////////
// Purpose: If the determinant object is null or a checked checkbox, then the dest element
//			will be made the same as the source element.
// Params:	chkDeterminant - checkbox that determins if the two elements should be the same.
//			objSource - Source Element
//			objDest - Destination Element
// Constraints: This method does not work for radio buttons as is.
// Depends: SetSelectedOptions
// Returns: Boolean - true : mirrored; false : didn't mirror for some reason.
function MirrorElement(chkDeterminant, objSource, objDest) {
	var blnMirrored = false;
	
	
	// the determinate element either doesn't exist or must be a valid checkbox and checked
	if (!chkDeterminant || (chkDeterminant && chkDeterminant.type == 'checkbox' && chkDeterminant.checked)) {
			
		// the source and destination elements must be valid, and of the same type.
		if (objSource && objDest && objSource.type == objDest.type) {

			// mirror source element based on the type of element
			switch (objSource.type) {
			
				//TEXTBOX
				case 'text' : 
					objDest.value = objSource.value;
					blnMirrored = true;
					break;
					
				//CHECKBOX
				case 'checkbox' :
					objDest.checked = objSource.checked;
					blnMirrored = true;
					break;
					
				//SELECT LIST
				case 'select-one' :
				case 'select-multiple' :
					SetSelectedOptions(objSource, objDest);
					blnMirrored = true;
					break;
			} //switch
		} // objSource same type as objDest
	} // checked
	
	return blnMirrored;
	
}



//////////////////////////////
// SetSelectedOptions
//////////////////////////////
// Purpose: Sets all option indexes for the dest element to be the same as those for the source element.
// Params:	objSource - Source Element
//			objDest - Destination Element
// Constraints: This method matches by option index, not by option value.
// Depends: None
// Returns: None
function SetSelectedOptions(lstSource, lstDest) {
	var x;
	
	for (x=0; x<lstSource.length && x<lstDest.length; x++) {
		lstDest.options[x].selected = lstSource.options[x].selected
	}

}


//////////////////////////////
// EnforceIntegrity
//////////////////////////////
// Purpose: If the two element values do not match, then the determinant checkbox is made unchecked.
// Params:	chkDeterminant - checkbox that determins if the two elements should be the same.
//			objSource - element1
//			objDest - element2
// Depends: ElementsMatch
//			GetSelectedOptions
// Returns: none
function EnforceIntegrity(chkDeterminant, objSource, objDest) {

	// the determinate element either doesn't exist or must be a valid checkbox and checked
	if (chkDeterminant && chkDeterminant.type == 'checkbox' && chkDeterminant.checked) {
		if (! ElementsMatch(objSource, objDest)) {
			chkDeterminant.checked = false;
		}
	}
}


//////////////////////////////
// ElementsMatch
//////////////////////////////
// Purpose: This method returns a boolean flag as to whether the two form elements have equal values.
//			For two select boxes, the lists must be identical in value for the comparison to work.
// Params:	objSource - element1
//			objDest - element2
// Depends: GetSelectedOptions
// Returns: Boolean - true - they are equal; false - they are not equal

function ElementsMatch(objSource, objDest) {
	var blnElementsMatch;
	
	// the source and destination elements must be valid, and of the same type.
	if (objSource && objDest && objSource.type == objDest.type) {
	
		// mirror source element based on the type of element
		switch (objSource.type) {
		
			//TEXTBOX
			case 'text' : 
				blnElementsMatch = (objDest.value == objSource.value);
				break;
				
			//CHECKBOX
			case 'checkbox' :
				blnElementsMatch = (objDest.checked == objSource.checked);
				break;
				
			//SELECT LIST
			case 'select-one' :
			case 'select-multiple' :
				blnElementsMatch = (GetSelectedOptions(objSource) == GetSelectedOptions(objDest));
				break;
			default :
				blnElementsMatch = false;
				break;
		} //switch
	} // objSource same type as objDest
	else {
		blnElementsMatch = false;
	}
	
	return blnElementsMatch;
}



//////////////////////////////
// GetSelectedOptions
//////////////////////////////
// Purpose: Gets a comma delimited list of values for the options that are selected in the 
//			specified list.  If the list is a single select, then only one value is returned.
// Params:	lstSource - The list element for which to get the selected options.
// Depends: None
// Returns: String - comma delimited if list allows multiple select

function GetSelectedOptions(lstSource) {
	var x;
	var strSelectedOptions = '';
	var strSelectedOptions = '';
	
	
	for (x=0; x<lstSource.length; x++) {
		if (lstSource.options[x].selected) {
			if (lstSource.multiple) {
				strSelectedOptions += strDelimiter + lstSource.options[x].value;
				strDelimiter = ',';
			} else {
				strSelectedOptions = lstSource.options[x].value;
			}
		}
	}
	
	return strSelectedOptions;
}


