/*==============================================================
inc_commonPage.js
----------------------
Client-side javascript functions included in all pages presented in the CONTENT frame.

NOTE: Some of the functions call functions defined in the top (navigation) frame, and
      are included here to relieve you of the task of calling functions across frames.
==============================================================*/


/*==============================================================
Client-side Form Read/Write/Manipulate Functions
==============================================================*/

// ---------------------------------------------- 
// isNonBlank(strIn) -  
//
// Input parms:
// 		strIn - (String) String to be tested.
// Returns:
//		true - 	String contains something other than blank spaces.
//		false - String contains nothing but empty spaces, or has no length
// ---------------------------------------------- 
function isNonBlank(strIn) {
	var strTmp = "";
	for (var i=0; i<strIn.length; i++) {
		if (strIn.charAt(i) != " ") strTmp+=strIn.charAt(i);
	}
	if (strTmp.length > 0) return true;
	else return false;
}

function isEmpty(strIn) {
	var isEmpty = true;
	for (var i=0; i<strIn.length; i++) {
		if (strIn.charAt(i) != " ") {
			isEmpty = false;
			break;
		}
	}
	if (isEmpty || strIn.length == 0) return true;
	else return false;	

}



// ---------------------------------------------- 
// getFormElementValue(objIn) -  
//
// Pass in any valid form element (currently coded for) that returns a value, get the value of the 
// object as a string.  Function handles peculiarities of each input type.
// 
// Input parms:
// 		objIn - (Object) - should be some type of form elemnt object (Text box, radio button, hidden, etc)
// Returns:
//		A string representation of the value of the element.
// ---------------------------------------------- 
function getFormElementValue(objIn) {
	var daForm = objIn.form;
	var currentValue = "";
	//alert(objIn.name + " type:" + objIn.type);
	
	// Eventually need to add button, reset, file, reset, submit, textarea
	switch (objIn.type) {
		case 'radio': 
			for (var i=0; i<daForm.elements[objIn.name].length; i++) {
				if (daForm.elements[objIn.name][i].checked==true) {
					currentValue = daForm.elements[objIn.name][i].value;
					break;
				}
			}
			break;
		
		case 'text':
		case 'hidden':
			return objIn.value;
			break;
		
		case 'select-one':
			for (var i=0; i<daForm.objIn.options.length; i++) {
				if (daForm.objIn.options[i].selected) {
					currentValue = daForm.objIn.options[i].value;
					break;
				}
			}
			break;
	
		case 'select-multiple':
			for (var i=0; i<daForm.objIn.options.length; i++) {
				if (daForm.objIn.options[i].selected) {
					currentValue += daForm.objIn.options[i].value + ",";
					break;
				}
			}
			// snip off last comma
			currentValue = currentValue.substring(0, currentValue.length-1);
			break;
	
	}

	return currentValue.toUpperCase();	
}


// ---------------------------------------------- 
// setFormElementValue(objIn, valueIn) -  
//
// Pass in any valid form element (currently coded for) that returns a value and the value you'd like to 
// which you'd like to set it.  Function handles peculiarities of setting value for each input type.
// 
// Input parms:
// 		objIn (Object) 		- should be some type of form elemnt object (Text box, radio button, hidden, etc)
//		valueIn (String)	- The value you'd like the control to display
// Returns:
//		Nothing - changes control in UI to display specified value
// ---------------------------------------------- 
function setFormElementValue(objIn, valueIn) {
	var daForm;
	var elemType = "";
	var currentValue = "";

	if (objIn.length != void(0) && objIn.length > 1) {
		elemType = objIn[0].type;
		daForm = objIn[0].form;
		objIn = objIn[0];
	} else {
		elemType = objIn.type;
		daForm = objIn.form;
	}
	//alert(objIn.name + " type:" + objIn.type);
	
	// Eventually need to add button, reset, file, reset, submit, textarea
	switch (elemType) {
		case 'radio': 
			for (var i=0; i<daForm.elements[objIn.name].length; i++) {
				if (daForm.elements[objIn.name][i].value.toUpperCase()==valueIn.toUpperCase() ) {
					daForm.elements[objIn.name][i].checked=true;
					break;
				}
			}
			break;
		
		case 'text':
		case 'hidden':
			return objIn.value = valueIn;
			break;
		
		case 'select-one':
			for (var i=0; i<daForm.objIn.options.length; i++) {
				if (daForm.objIn.options[i].value.toUpperCase() == valueIn.toUpperCase() ) {
					daForm.objIn.options[i].value.selected = true;
					break;
				}
			}
			break;
	
		case 'select-multiple':
			var arrTmp = valueIn.split(",");
			
			for (var i=0; i<daForm.objIn.options.length; i++) {
				for (var j=0; j<arrTmp.length; j++) {
					if (daForm.objIn.options[i].value.toUpperCase() == arrTmp[j].value.toUpperCase() ) {
						daForm.objIn.options[i].value.selected = true;
						break;
					}
				}
			}
			break;
	}
}


// ---------------------------------------------- 
// enableFormElement(objIn, boolEnableControl, boolCacheOldValue) -  
//
// Pass in any valid form element (currently coded for) that returns a value, plus a boolean showing whether
// control should be enable or not, and a boolean showing whether you'd like the form to remember old values 
// when disabling the control.  Function will enable/disable control as specified, applying appropriate style
// class to show the control enabled or disabled, and optionally cache the control value(s) visible on the control
// before wiping out the display value(s) and disabling the control.
// 
// Input parms:
// 		objIn (Object) 				- should be some type of form element object (Text box, radio button, hidden, etc)
//		boolEnableControl (boolean)	- True: control is editable, control is styled per system defaults (editable)
//									- False: control is disabled, control is styled as "not editable" using current styles
//		boolCacheOldValue (boolean) - Optional: If no value specified, value defaults to "true"
//									- True: function create a new property called "oldValue" on the Javascript object that 
//											corresponds to the control, and set the current control value there before disabling the control
//											and wiping the display value.  When control is enabled again, oldValue is 
//											restored to the display value.
//									- False: function will not cache old value before wiping display value.
// Returns:
//		Nothing - changes display of control in UI to enabled/disabled, including "disabled" styling of element.
// ---------------------------------------------- 
function enableFormElement(objIn, boolEnableControl, boolCacheOldValue) {
	// set boolCacheOldValue default value to 'true' if not specified.
	if (boolCacheOldValue == void(0)) boolCacheOldValue=true;

	var daForm = objIn.form;
	var thisElement;
	// Eventually need to add button, reset, file, reset, submit, textarea
	switch (objIn.type) {
		case 'radio': 
			for (var i=0; i<daForm.elements[objIn.name].length; i++) {
				thisElement = daForm.elements[objIn.name][i];
				cacheElement = daForm.elements[objIn.name][0];
				
				if (boolEnableControl) {
					// -- Enable control ----
					thisElement.disabled=false;
					// create new propert on client-side object and stuff value there.
					// For radio buttons, old value will be set on 1st button object
					if (boolCacheOldValue) {
						if (!cacheElement.oldValue) cacheElement.oldValue="";
						//alert("cacheElement.oldValue: " + cacheElement.oldValue + " - " + "thisElement.value: " + thisElement.value);
						if ( thisElement.value.toUpperCase() == cacheElement.oldValue.toUpperCase() ) {
							thisElement.checked = true;
						}
					}
				} else {
					// -- Disable control ----
					// create new property on client-side object and stuff value there.
					// For radio buttons, old value will be set on 1st button object
					if (boolCacheOldValue) {
						if (!cacheElement.oldValue) cacheElement.oldValue="";
						if (thisElement.checked) {
							cacheElement.oldValue=thisElement.value;
						}
					}
					// clear out value
					thisElement.checked = false;
					// disable and style
					thisElement.disabled=true;
					// alert("cacheElement.oldValue: " + cacheElement.oldValue);
				}
			}
			break;

		case 'text':
			if (boolEnableControl) {
				// -- Enable control ----
				objIn.disabled=false;
				objIn.className="";
				// create new propert on client-side object and stuff value there.
				if (boolCacheOldValue) {
					if (!objIn.oldValue) objIn.oldValue="";
					if (objIn.oldValue.length>0) objIn.value=objIn.oldValue;
				}
			} else {
				// -- Disable control ----
				// create new propert on client-side object and stuff value there.
				if (boolCacheOldValue) {
					if (!objIn.oldValue) objIn.oldValue="";
					if (objIn.value.length > 0) {
						objIn.oldValue=objIn.value;
					}
				}
				// clear out value
				objIn.value="";
				// disable and style
				objIn.disabled=true;
				objIn.className="value";
			}
			break;
		
		case 'select-one':
			var cacheElement = objIn;
			if (!cacheElement.oldValue) cacheElement.oldValue="";
			// capture existing value and save to oldValue.  Needed to prevent not "selecting" existing value when called from onLoad event
			if(objIn.value != "") cacheElement.oldValue = objIn.value;

			if (boolEnableControl) {
				// -- Enable control ----
				objIn.disabled=false;
				objIn.className="";
				// create new property on client-side object and stuff value there.
				// For select boxes, old value will be set on the SELECT object
				if (boolCacheOldValue) {
					for (var i=0; i<objIn.options.length; i++) {
						// alert("cacheElement.oldValue: " + cacheElement.oldValue + " - " + "thisElement.value: " + objIn.options[i].value);
						if (objIn.options[i].value.toUpperCase() == cacheElement.oldValue.toUpperCase() ) {
							objIn.options[i].value.selected = true;
							objIn.selectedIndex=i;
							break;
						}
					}
				}
			} else {
				// create new propert on client-side object and stuff value there.
				// For select boxes, old value will be set on the SELECT object
				if (boolCacheOldValue) {
					for (var i=0; i<objIn.options.length; i++) {
						//alert("cacheElement.oldValue: " + cacheElement.oldValue + " - " + "thisElement.value: " + objIn.options[i].value);
						if (objIn.options[i].selected ) {
							cacheElement.oldValue = objIn.options[i].value;						
							objIn.options[i].value.selected = false;
							objIn.selectedIndex=0;
							break;
						}
					}
				}
				// -- Disable control ----
				objIn.className="value";
				objIn.disabled=true;
			}
			
			break;
	
		case 'select-multiple':
			var arrTmp;
			var cacheElement = objIn;
			if (!cacheElement.oldValue) cacheElement.oldValue="";

	
			
			if (boolEnableControl) {
				// -- Enable control ----
				objIn.disabled=false;
				objIn.className="";
				// create new propert on client-side object and stuff value there.
				// For select boxes, old value will be set on the SELECT object
				if (boolCacheOldValue) {
					arrTmp = cacheElement.oldValue.split(",");
					
					for (var i=0; i<objIn.options.length; i++) {
						// alert("cacheElement.oldValue: " + cacheElement.oldValue + " - " + "thisElement.value: " + objIn.options[i].value);
						for (var j=0; j<arrTmp.length; j++) {
							if (objIn.options[i].value.toUpperCase() == arrTmp[j].toUpperCase() ) {
								objIn.options[i].selected = true;
							}
						}
					}
				}
			} else {
				// -- Disable control ----
				// create new propert on client-side object and stuff value there.
				// For select boxes, old value will be set on the SELECT object

				// Clear cache value and reload with current.
				cacheElement.oldValue = "";
				if (boolCacheOldValue) {
					for (var i=0; i<objIn.options.length; i++) {
						//alert("cacheElement.oldValue: " + cacheElement.oldValue + " - " + "thisElement.value: " + objIn.options[i].value);
						if (objIn.options[i].selected ) {
							cacheElement.oldValue += objIn.options[i].value + ",";						
							objIn.options[i].selected = false;
						}
					}
					objIn.selectedIndex=-1;

					// trim off extra common on oldValue
					cacheElement.oldValue = cacheElement.oldValue.substring(0,cacheElement.oldValue.length-1);
					//alert("cacheElement.oldValue: " + cacheElement.oldValue);
				}
				// -- Disable control ----
				objIn.className="value";
				objIn.disabled=true;
			}
			
			break;
		
		// No case for hidden - you don't disable it.

	}
}





// ---------------------------------------------- 
// enableDependentControls(objTrigger, strTriggerValue, objDependent1, objDependent2,objDependent3, etc. ) -  
//		NOTE: "Dependent" above is a data dependency, not somebody's kid.
//
// Allows one control to enable/disable a group of other controls based on the value of the trigger control.
// Function will manage 1-to-many dependent controls ... the number of screen controls that can be made 
// dependent on the trigger control is infinite.
// 
// NOTE: This function enableFormElement() with the value caching enabled.
// 
// Input parms:
// 		objTrigger (Object) 		- The form element (control) that will trigger the dependency.  
//									  Should be some type of form element object (Text box, radio button, hidden, etc)
//		strTriggerValue (String)	- The value of the trigger control that will cause the dependent controls to become "enabled"
//									- False: control is disabled, control is styled as "not editable" using current styles
//		dependentObjects (Object) 	- Needs at least one parameter as the dependent control.
//									  Specify as many paramaters as you wish, with each being an Object representing
//									  a form element(control) you wish enabled/disabled based upon the trigger object's value.
//
// Returns:
//		Nothing - changes display of ALL dependent control(s) in UI to enabled/disabled, 
//				  including "disabled" styling of element and caching of old value, based upon 
//				  current value of trigger object.
// ---------------------------------------------- 
function enableDependentControls(objTrigger, strTriggerValue) {
	// validate objTrigger
	if (objTrigger == void(0)) {
		// If trigger object does not exist on page, allow to exit without error.
		return;
	} else if (typeof objTrigger == "string") {
		alert("enableDependentControls(): Config. Error\n\n objTrigger must be a form element object.");
		return;
	} else if ( (typeof objTrigger == 'object') && (objTrigger.type == void(0)) ) {
		alert("enableDependentControls(): Config. Error\n\n If objTrigger is a radio button, Use\n\n'document.theFormName.theElementName[0]'\n\nas param.");
		return;
	}
	
	// alert(typeof objTrigger + " " + objTrigger.type);

	// validate there are some target objects to act upon
	if (arguments.length < 3) {
		alert("enableDependentControls(): Config. Error\n\n Must specify at least one form element object as the\ntarget of this function.\n\nAll arguments passed beyond the first 2 are considered target form elements.");
		return;
	}
	
	// Load args passed beyond the first 2 into an array as target objects.
	// Validate that args are Objects (not strings) ... warn problems.
	var arrTargetObjects = new Array();
	for (var i=2; i<arguments.length; i++) {
		if (typeof arguments[i] == "string") {
			alert("enableDependentControls() Config, error\n\nPass in object references, not string names: '" + arguments[i] + "'");
			return;
		} else {
			arrTargetObjects[arrTargetObjects.length] = arguments[i];
		}
	}
	
	// ------------------------------------------
	// Form elements can be missing.  They might not get written to the page depending upon user type.
	// Allow function to return without error if trigger is undefined
	// ------------------------------------------
	if (objTrigger == void(0)) return;	
	
	// set trigger value to Upper Case ... all element values will also convert to UC.	
	strTriggerValue = strTriggerValue.toUpperCase();
	var daForm = objTrigger.form;
	var currentValue = getFormElementValue(objTrigger);
	
	// check if trigger value matches objectTrigger's value.
	//		If so, enable target fields, else disable target fields.

	// alert('"'+strTriggerValue+' : '+currentValue+'"');
	if (strTriggerValue == currentValue) {
		// Match - enable targetFields
		for (var i=0; i<arrTargetObjects.length; i++) {
			if (arrTargetObjects[i] == void(0) ) break;
			enableFormElement(arrTargetObjects[i], true);
		}
	} else {
		// No Match - disable targetFields
		for (var i=0; i<arrTargetObjects.length; i++) {
			if (arrTargetObjects[i] == void(0) ) break;
			enableFormElement(arrTargetObjects[i], false);
		}
	}
	
	
}





/*--------------------------------------------------------------------------
Open Help window
--------------------------------------------------------------------------*/
function popWin(strIn) {
	var strArgs = 'left=50,height=500,width=800,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes';
	var newWin = window.open('', newWin, strArgs);
	newWin.location = strIn;
	newWin.focus();
}

//alert("page height: " + findPageHeight() + "\npage Width: " + findPageWidth());



/*--------------------------------------------------------------------------
Send email from form using Mailto
--------------------------------------------------------------------------*/
function sendEmail(objButtonIn) {
	var daForm = objButtonIn.form;
	var strEmail = daForm.Email.value;
	var strErr = "";
	
	if ( isEmpty(daForm.Name.value) ) {
		strErr += "- Name cannot be blank.\n";
	}
	if ( isEmpty(strEmail) ) {
		strErr += "- Email Address cannot be blank.\n";
	}
	
	if (strErr.length > 0) { 
		alert("Error:\n"+strErr );
		return;
	}
	
	var isValidEmail = true;
	if (strEmail.indexOf("@") == -1) isValidEmail = false;
	if (strEmail.indexOf(".") == -1) isValidEmail = false;
	if (strEmail.indexOf(".") < strEmail.indexOf("@")) isValidEmail = false;
	if (strEmail.indexOf("@") < 2) isValidEmail = false;
	if (strEmail.lastIndexOf(".") > (strEmail.length-2) ) isValidEmail = false;
	if (strEmail.indexOf(".") - strEmail.indexOf("@") <2 ) isValidEmail = false;
	
	if (!isValidEmail) {
		alert("Error:\nInvalid email address.\nPlease enter as\naccount@hostname.domain" );
		return;
	}
	
	daForm.action="mailto:" + ORG_EMAIL_ADDRESS + "?subject=Email from " + daForm.Name.value + "&body=From: " + daForm.Email.value;
	daForm.submit();	
}

