/*

Common JavaScript for showing/hiding areas.

Author:  Brian Bailey (The Seva Group)
Date:    March 5, 2007

Revisions:

		Author			Date		Description of Changes
		===================================================
		B.Bailey		03/05/2007	Original creation.

Functions Available:
--------------------
ShowHideArea
		Shows or hides an area based on click of a radio button, checkbox, or select list value
		
		Inputs:
		
				strFieldType			- Type of field.  Acceptable values are:  "radiobutton", "selectlist", "checkbox", "force".
										  Force is to allow showing/hiding of an area based on some action that isn't form field driven such
										  as a click of a link to toggle something on/off.
										  Required field.
				strFormName				- Form name.  Required for radiobutton, selectlist, checkbox or if using status field.
				strAreaName				- HTML id of area that should be shown/hidden.  Required field.
				strDrivingField			- HTML form field name for the radio button, checkbox or select list.
										  Required for radiobutton, selectlist, checkbox.
				blnShowArea				- 0/1 to show (1) or hide (0), status field driven (2) - will show/hide based on current status of field.
										  Required field.  Field driven is only relevant for a strFieldtype of force.
				intValueToFind			- Value of the checkbox, radiobutton, or select list item to find.  Required for checkbox, selectlist, radio button.
				strStatusField			- HTML form field name into which you wish to put the status of whether the hidden
										  area is currently displayed or not.  Will put 0 for No and 1 for yes.
										  Optional field.
				blnSwapImage			- 0/1 - 0 is default.  If 1, will swap images based on strImageID, strOpenImageURL, strCloseImageURL
										- Only relevant for field type of "force"
				strImageID				- ID of the image tag that should be swapped on/off to show state of area
										- Only relevant for field type of "force"
				strOpenImageURL			- Resolvable image reference for "open" up the area.  
										- Only relevant for field type of "force"
				strCloseImageURL		- Resolvable image reference for "close" of the area.  
										- Only relevant for field type of "force"
				
		Example call:
				<TR><TD>
					<input type="radio" name="flShowHide" value="1" onClick="return ShowHideArea('radiobutton', 'myformname', 'ShowHideMainArea', 'flShowHide', 0)" >Yes
					<input type="radio" name="flShowHide" value="0" onClick="return ShowHideArea('radiobutton', 'myformname', 'ShowHideMainArea', 'flShowHide', 1)" >No
				</TD></TR>		
				<tr id="ShowHideMainArea" style="display: none;">
					<td>This will show or hide</td>
				</tr>
								
*/
function ShowHideArea(strFieldType, strFormName, strAreaName, strDrivingField, blnShowArea, intValueToFind, strStatusField, blnSwapImage, strImageID, strOpenImageURL, strCloseImageURL) {

	//alert("in showhidearea with strfieldtype = " + strFieldType + ", strFormName = " + strFormName + ", strAreaName = " + strAreaName + ", strDrivingField = " + strDrivingField + ", blnShowArea = " + blnShowArea + ", intValueToFind = " + intValueToFind)
	blnError = false;
	
	if (strFieldType == 'radiobutton') {
		blnShowAreaDerived = DetermineShowHideRadio(strFormName, strDrivingField, blnShowArea, intValueToFind)
	}
	else if (strFieldType == 'selectlist') {
		blnShowAreaDerived = DetermineShowHideSelect(strFormName, strDrivingField, intValueToFind, blnShowArea)
	}
	else if (strFieldType == 'checkbox') {
		blnShowAreaDerived = DetermineShowHideCheckbox(strFormName, strDrivingField, blnShowArea, intValueToFind)
	}
	else if (strFieldType == 'force') {
		blnShowAreaDerived = DetermineShowHideForce(strFormName, blnShowArea, strStatusField)
	}
	else {
		alert("Incorrect field type passed to ShowHideArea function in ShowHideDHTML.js: " + strFieldType)
		blnError = true
	}
	
	if (blnError == false) {
	
		blnCurrentlyVisible = PerformShowHide(strAreaName, blnShowAreaDerived)
		SetShowHideStatusField(strFormName, strStatusField, blnCurrentlyVisible)
		if (blnSwapImage) {
			ChangeImageBasedOnStatus(strFormName, blnCurrentlyVisible, blnSwapImage, strImageID, strOpenImageURL, strCloseImageURL)
		}
	}
	
}

function DetermineShowHideRadio(strFormName, strDrivingRadioField, blnShowArea, intValueToFind) {

	blnShowTheArea = 0
	
	objForm = eval("document." + strFormName);

	if (objForm) {
		objRadioField = eval("objForm." + strDrivingRadioField)

		if (objRadioField) {

			rdoValue = ReturnRadioValue(objRadioField);
		
			if (rdoValue == intValueToFind) {
				blnShowTheArea = 1
			}
			else {
				blnShowTheArea = 0
			}
		}
	}
	return blnShowTheArea
}

function ReturnRadioValue(objRadioField) {

	if (objRadioField) {
		if (objRadioField.length) {
			for (i=0; i < objRadioField.length; i++) {
				if (objRadioField[i].checked == true) {
					return objRadioField[i].value;
				}
			}
		}
		else {
			if (objRadioField.checked == true) {
				return objRadioField.value;
			}
		}
	}

	return false;
}

function DetermineShowHideCheckbox(strFormName, strDrivingField, blnShowArea, intValueToFind) {

	blnShowTheArea = 0
	
	objForm = eval("document." + strFormName);

	if (objForm) {
	
		objCheckbox = eval("objForm." + strDrivingField)
	
		if (objCheckbox) {
		
			rdoValue = CheckboxFieldStatus(objCheckbox, intValueToFind);
		
			if (rdoValue == false) {
				// not clicked
				if (blnShowArea == 1) {
					// hide the area
					blnShowTheArea = 0
				}
				else {
					// show the area
					blnShowTheArea = 1
				}	
			}
			else {
				// was clicked
				if (blnShowArea == 1) {
					// show the area
					blnShowTheArea = 1
				}
				else {
					// hide the area
					blnShowTheArea = 0
				}	
			}
		}
	}
	
	return blnShowTheArea;
}

function CheckboxFieldStatus(objCheckbox, intValueToFind) {

	if (objCheckbox) {
		if (objCheckbox.length) {
			for (i=0; i < objCheckbox.length; i++) {
				if (objCheckbox[i].checked == true && objCheckbox[i].value == intValueToFind) {
					return true
				}
			}
		}
		else {
			if (objCheckbox.checked == true && objCheckbox.value == intValueToFind) {
				return true;
			}
		}
	}

	return false;
}

function DetermineShowHideSelect(strFormName, strDrivingSelectField, intValueToFind, blnShowIfValueFound) {

	blnShowTheArea = 0
	
	objForm = eval("document." + strFormName);
	
	if (objForm) {
	
		objSelectField = eval("objForm." + strDrivingSelectField)
	
		if (objSelectField) {
			selValue = ReturnSelectValue(objSelectField);
		
			if (selValue == intValueToFind) {
				if (blnShowIfValueFound == 1) {
					blnShowTheArea = 1
				}
				else {
					blnShowTheArea = 0
				}
			}
			else {
				if (blnShowIfValueFound == 1) {
					blnShowTheArea = 0
				}
				else {
					blnShowTheArea = 1
				}
			}
		}
	}
	
	return blnShowTheArea
}

function ReturnSelectValue(objSelectField) {

	if (objSelectField) {
		if (objSelectField.length) {
			for (i=0; i < objSelectField.length; i++) {
				if (objSelectField.options[i].selected == true) {
					return objSelectField.options[i].value;
				}
			}
		}
	}

	return false;
}

function DetermineShowHideForce(strFormName, blnShowArea, strStatusField) {

	blnShowTheArea = 0;
	
	if (blnShowArea == 2) {
		// set based on a status field
		blnCurrentlyVisible = GetStatusFieldValue(strFormName, strStatusField)
		
		if (blnCurrentlyVisible == 1) {
			// hide it
			blnShowTheArea = 0
		}
		else {
			// show it
			blnShowTheArea = 1	
		}
	}
	else if (blnShowArea == 1) {
		// show the area
		blnShowTheArea = 1
	}
	else {
		// hide the area
		blnShowTheArea = 0
	}	

	return blnShowTheArea
}

function PerformShowHide(strAreaName, blnShowArea) {

	if (blnShowArea == 1) {
		// show the area
		eval("document.getElementById('" + strAreaName + "').style.display = ''")
		blnCurrentlyVisible = 1
	}
	else {
		// hide the area
		eval("document.getElementById('" + strAreaName + "').style.display = 'none'")
		blnCurrentlyVisible = 0
	}	
	
	return blnCurrentlyVisible

}

function SetShowHideStatusField(strFormName, strStatusField, blnCurrentlyVisible) {

	if (strFormName != '') {
	
		objForm = eval("document." + strFormName);
	}

	if (objForm && strStatusField != '') {
		objStatusField = eval("objForm." + strStatusField)
		
		if (objStatusField) {
			objStatusField.value = blnCurrentlyVisible
		}
	}

	return true
}

function GetStatusFieldValue(strFormName, strStatusField) {

	if (strFormName != '') {
	
		objForm = eval("document." + strFormName);
	}

	if (objForm && strStatusField != '') {
		objStatusField = eval("objForm." + strStatusField)
		
		if (objStatusField) {
			return objStatusField.value
		}
	}

	return true
}

function ChangeImageBasedOnStatus(strFormName, blnCurrentlyVisible, blnSwapImage, strImageID, strOpenImageURL, strCloseImageURL) {
	blnSwapImageWithCheck = 0;
	
	if (blnSwapImage == 1) {
		if (strImageID != '') {
			if (strFormName != '') {
				objForm = eval("document." + strFormName);
				
				if (objForm) {
					objImage = eval("document.getElementById('" + strImageID + "')")
					
					if (objImage) {
						if (strOpenImageURL && strOpenImageURL != '' && strCloseImageURL && strCloseImageURL != '') {
							blnSwapImageWithCheck = 1
						}
					}
				}
			}
		}
	}
			
	if (blnSwapImage == 1 && blnSwapImageWithCheck == 0) {
		blnSwapImage = 0
	}
			
	if (blnSwapImage == 1) {
		if (blnCurrentlyVisible == 1) {
			objImage.src = strCloseImageURL;
		}
		else {
			objImage.src = strOpenImageURL;
		}
	
	}
}
