// These are customised form event functions

// ********************************************************************************************* //

function checkRequired(frm) {
// function to check that required form fields have been completed before submitting form
// Intended for use with 'OnSubmit' Event

	var pass = true;
	var shortFieldName = "";
	var msgStr = "";
	var tempString = "";

	if (document.images) {
		for (i=0; i < frm.length; i++) {
			var tempobj = frm.elements[i];
			if ((tempobj.name.substring(4, 7) == "rqd") || (tempobj.name.substring(0, 3) == "rqd")) {
				tempString = tempobj.type.substring(0,4);
				switch (tempString) {
					case "text":
						if (tempobj.value == "")  {// || trimString(tempobj.value) == ""
							pass = false;
						}
						break;
					case "pass":
						if (tempobj.value == "") {// || trimString(tempobj.value) == ""
							pass = false;
						}
						break;
					case "hidd":
						if (tempobj.value == "") {// || trimString(tempobj.value) == ""
							pass = false;
						}
						break;
					case "radi":
						var tempradio = true;
						for (var j = 0; j < tempobj.length; j++) {
							if (tempobj[j].checked) {
								tempradio = true;
								break;
							} else {
								tempradio = false;
							}
						}
						if (!tempradio) {
							pass = false;
						}
						break;
					case "sele":
						if (tempobj.selectedIndex == 0 || tempobj.options[tempobj.selectedIndex].value == "N/A") {
							pass = false;
						}
						break;
					case "chec":
						if (!tempobj.checked) {
							pass = false;
						}
						break;
					default:
						break;
				} // end switch
				if (!pass) { break; } // break out of for loop if required field is blank
			} // end if rqd
		} // end for
	} // if document

	if (!pass) {
		shortFieldName = tempobj.name.substr(3)
		shortFieldName = shortFieldName.replace("rqd", "");
		shortFieldName = shortFieldName.replace(/_/g, " "); //.replace("_", " ");
		//shortFieldName = replaceAbbrevText(shortFieldName).replace("_", " ").replace("_", " ");
		alert("Please make sure the " + shortFieldName + " field is properly completed.");
		if (tempString != "hidd") {
			tempobj.focus();
		}
		return (false);
	} else {
		return(true);
	}
}

// ********************************************************************************************* //

function checkKey(obj) {
// checks whether the Enter/Return key is pressed

	if (window.event.keyCode == 13) {
		obj.click();
	}
}

// ********************************************************************************************* //

function specifyOtherOption(obj, optionName) {
// This function prompts the user for an alternative option for the 'other' selection in a drop-down list
// and adds this to the choice of options
// Intended for use with the 'OnChange' Event

	if (obj.options[obj.selectedIndex].value.toLowerCase().substring(0, 5) == "other") {
		var vOtherOption = window.prompt("Please input your preferred " + optionName + " here:", "")
		if (vOtherOption != null) {
			var oOption = document.createElement("OPTION");
			oOption.text=vOtherOption;
			oOption.value=vOtherOption;
			obj.add(oOption);
			oOption.selected = true;
		} else {
			obj.selectedIndex = 0;
		}
	}
}

// ********************************************************************************************* //

function showTooltip(obj, txt) {
    obj.title=txt;
    return;
}

// ********************************************************************************************* //

function checkObjectDataChanged(obj) {
	if (!FormDataChanged) {
		switch (obj.type.substring(0, 4)) {
			case "sele":
				if (obj.options[obj.selectedIndex].value != DataOnGotFocus) {
					FormDataChanged = true;
				}
				break;
			case "radi":
				if (obj.checked != DataOnGotFocus) {
					FormDataChanged = true;
				}
				break;
			case "chec":
				if (obj.checked != DataOnGotFocus) {
					FormDataChanged = true;
				}
				break;
			default:
				if (obj.value != DataOnGotFocus) {
					FormDataChanged = true;
				}
				break;
		}
	}
}

// ********************************************************************************************* //

function checkFormDataState(btn) {
// this function checks to see if form data has changed
var parametersSet;
	if (FormDataChanged) {
		if (window.confirm("The data on this form has changed\n\nPlease click on the 'Ok' button if you would like to save your changes\nor click on the 'Cancel' button to cancel any changes made.")) {
			FormDataChanged = false;
			if (btn != undefined) {
				btn.click();
			}
		}
	} else {
		FormDataChanged = false;
	}
}

// ********************************************************************************************* //

function checkFormDataChangedState(obj, btn) {
	if (FormDataChanged) {
		//if (window.confirm("The data on this form has changed!\n\nIf you would like to Save your changes before continuing,\nplease click on the 'Cancel' button to cancel this operation\nand click on the 'Save' button to save your changes.\n\nOtherwise click on the 'Ok' button \nto proceed with the selected operation.")) {
		if (window.confirm("The data on this form has changed\n\nPlease click on the 'Ok' button if you would like to save your changes\nor click on the 'Cancel' button to cancel any changes made.")) {
			return false;
		} else {
			FormDataChanged = false;
			return true
		}
	} else {
		FormDataChanged = false;
		return true;
	}
}

// ********************************************************************************************* //

function checkFormDataState1() {
	btn.click();
}

// ********************************************************************************************* //

function doCheckBox_FldUpdate(chkObj, hfldObj, onVal, offVal) {
// this function is used to update a hidden field, hfldObj, with the selected checkbox object, chkObj, value 
// used to send the selected data to the database as the checkbox 'value' property behaves in a strange way
	
	if (chkObj.checked) {
		hfldObj.value = onVal;
	} else {
		hfldObj.value = offVal;
	}
}

// ********************************************************************************************* //
// DHTML micro API - from http://www.quirksmode.org/js/dhtmloptions.html [accessed: 13-Dec-2006 10:24]
// Two functions: getObj(name) and getObjNN4(obj,name)
// micro-API for writing correct cross-browser DHTML. You can use this function to gain access to the HTML element you want to influence.

function getObj(name)
{
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
	this.obj = getObjNN4(document,name);
	this.style = this.obj;
  }
}

function getObjNN4(obj,name)
{
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id == name)
		 	foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}

// ********************************************************************************************* //