///   <title>ClientUtils.js</title>
///   <description>User Manager Description</description>
///   <createdby>Epionet Framework Team</createdby>
///   <created>28 March 2002</created>


/// <function>
///   <title>ChangeSecText</title>
///   <description>
///			used to display a message/description (different to the option text), 
///			beside an item in a select box, depending on the item you select
///			it gets the message description from another select box, (lookupField), which is hidden.  
///			The function is called by the onchange event of the first select box
///   </description>
///   <param type="int" name="index">
///       the index of the Selected OPTION
///   </param>
///   <param type="document.form.element" name="lookupField">
///       the SELECT field which contains the description OPTIONS
///   </param>
///   <param type="string" name="displayID">
///       the ID of the SPAN to be used to display the description
///   </param>
///   <return></return>
/// </function>
	function ChangeSecText(index, lookupField, displayID) {	
		eval(displayID + ".innerHTML = lookupField.options[index].text;");
	}


/// <function>
///   <title>AddSingleIDtoList</title>
///   <description>
///			Add or Remove an id to a hidden list, the list is of the form (1,2,3) 
///			the function is used in multi-select forms e.g. Add Users to Group, Delete Users 
///			the list is passed to a DB stored proc
///   </description>
///   <param type="document.form.element" name="thisCBField">
///       the checkbox field used to select a record
///   </param>
///   <param type="document.form.element" name="thisListField">
///       the hidden field which contains the list
///   </param>
///   <param type="string" name="thisUser">
///       the value of the id to be added
///   </param>
///   <return></return>
/// </function>
	function AddSingleIDtoList(thisCBField, thisListField, thisUser) { 
		var thisList;
		// if an id is being added to the list
		if (thisCBField.checked) {
			if (thisListField.value == '')
				thisListField.value = thisUser;
			else
				thisListField.value += ',' + thisUser;
		}
		else {	// an id is being removed from the list
			if ( thisListField.value == thisUser )
				thisListField.value = '';
			else {
				thisList = thisListField.value;
				thisList = thisList.replace("," + thisUser, "");
				thisList = thisList.replace(thisUser + ",", "");
				thisListField.value = thisList;
			}
		}
		// alert(thisListField.name + " = " + thisListField.value);  // for debug
	}

/// <function>
///   <title>AddEachIDtoList</title>
///   <description>
///			Add or Remove all ids to a hidden list, the list is of the form (1,2,3) 
///			the function is used in multi-select forms e.g. Add Users to Group, Delete Users
///         When the select all checkbox has been checked this function is called
///			the list is passed to a DB stored proc
///   </description>
///   <param type="document.form" name="objForm">
///       the FORM which is to be actioned
///   </param>
///   <param type="boolean" name="blnChecked">
///       if true add to the list else remove all from list
///   </param>
///   <param type="document.form.element" name="thisListField">
///       the hidden field which contains the list
///   </param>
///   <return></return>
/// </function>
	function AddEachIDtoList(objForm, blnChecked, thisListField) {
		//reset the hidden form field
		thisListField.value = '';
		
		//if the allcheckbox has been checked then loop through the other checkboxes adding members to the list 
		if (blnChecked) {
			for (var i=0; i<objForm.elements.length; i++) {
				if (objForm.elements[i].type == 'checkbox') {
					var cbName = objForm.elements[i].name;
					if (cbName.substring(0,2) == 'cb')
						if (thisListField.value == '')
							thisListField.value = cbName.substring(2);
						else
							thisListField.value += ',' + cbName.substring(2);
				}
			}
		}		
		//alert(thisListField.value);	// for debug	
	}

/// <function>
///   <title>BuildXML</title>
///   <description>
///		loop through the form's element collection. Add all elements
///		except those which are submits/buttons or the field 'formXML'
///		to a well formed XML string and return this string
///   </description>
///   <param type="document.form" name="objForm">
///       the FORM which is to be actioned
///   </param>
///   <return>string : well formed XML</return>
/// </function>
	function BuildXML(objForm) {
		
		var strXML;
		var lStrOpenTag;
		var lStrCloseTag;
		var thisType;
		var thisName;
		//parent node of XML string is got from the form's name
		lStrOpenTag =  "<" + objForm.formName.value + ">";
		lStrCloseTag = "</" + objForm.formName.value + ">";

		//start XML string:
		strXML = lStrOpenTag;

		//loop through the form's element collection. Add all elements
		//except those which are submits/buttons or the field 'formXML'
		//which is used to hold the finished XML string:
		for (var i=0; i<objForm.elements.length; i++) {
			thisType = objForm.elements[i].type;
			thisName = objForm.elements[i].name;
			if (((thisType != "submit") && (thisType != "button")) && (thisName != "formXML")) {
				strXML += ProcessElement(objForm.elements[i]);
				//alert(strXML);
			}
		}
		strXML += lStrCloseTag;
		
		//alert('STRXML ' + strXML);
		
		return(strXML);
	}

/// <function>
///   <title>ProcessElement</title>
///   <description>
///		for a given form field element create an XML string to represent its name and value
///   </description>
///   <param type="document.form.element" name="objElement">
///       the FIELD which is to be actioned
///   </param>
///   <return>string : well formed XML</return>
/// </function>
	function ProcessElement(objElement) {
		var strTmpXML;
		strTmpXML = "";
		var strEscapedText;
		var strEscapedVal;
		var strEscapedVal = replaceStr(objElement.value, '&', '&amp;');
		//strEscapedVal = replaceStr(strEscapedVal, '"', '&#x22;');

		switch (objElement.type) {

			case "radio": {
				if (objElement.checked) {
					strTmpXML =  "<" + objElement.name + ">" + strEscapedVal
					+ "</" + objElement.name + ">";                  
				}
			}
			break;         
		    
			case "checkbox": {
				if (objElement.checked) {
					strTmpXML =  "<" + objElement.name + ">True</" + objElement.name + ">";  
				}               
				else {                
					strTmpXML =  "<" + objElement.name + ">False</" + objElement.name + ">";                
				} 
			}
			break;

			case "select-one": {
				// AC 15/10/2002
				if (objElement.selectedIndex!=-1)
					strEscapedText = replaceStr(objElement.options[objElement.selectedIndex].text, '&', '&amp;');
				else
					strEscapedText ='';
				// add the hidden value of select item and its tag to the XML string
				strTmpXML =  "<" + objElement.name + ">" + strEscapedVal + "</" + objElement.name + ">";
				// add the displayTagName value and its tags to the XML string
				strTmpXML +=  "<" + objElement.displayTagName + ">" + strEscapedText + "</" + objElement.displayTagName + ">";
			}
			break;

			case "select-multiple": {		
				strEscapedText = replaceStr(objElement.options[j].text, '&', '&amp;');
				// add the hidden value of select item and its tag to the XML string
				strTmpXML =  "<" + objElement.name + ">" + "(";
				// add an xml node for each of the selected options to the XML string
				for (j=0; j<objElement.options.length; j++) {
					strTmpXML +=  " '" + strEscapedText + "'";
					if (j+1 < objElement.options.length)
						strTmpXML +=  ", ";
				}
				strTmpXML +=  ")" + "</" + objElement.name + ">";
			}
			break;

		    
			default: {		
				// catch all types not handled above
				strTmpXML =  "<" + objElement.name + ">" + strEscapedVal + "</" + objElement.name + ">";
			}
			break;
		} // end switch
		return strTmpXML;
	}

/// <function>
///   <title>processParams</title>
///   <description>
///      loop through the form elements to find fields of the form paramLabel1..paramLabel9, etc
///      and then find the associated fields of the form paramValue1..paramValue9 and build a string
///      of type paramLabel1=paramValue1 eg. a=b,c=d
///   </description>
///   <param type="document.form" name="objForm">
///       the form to loop through
///   </param>
///   <return>string</return>
/// </function>
	function processParams (objForm) {
		var strTmpVal;
		var paramNum;
	    
		strTmpVal = "";

		for (var i=0; i<objForm.elements.length; i++) {
			objElement = objForm.elements[i];
			thisName = objElement.name;
			// for paramLabel fields find the associated paramValue
			if (thisName.substring(0,10) == "paramLabel") {
				paramNum = thisName.substr(10);
				
				if (objElement.value.length > 0 ) {
					if (strTmpVal.length > 0)
						strTmpVal += ",";
						
					strTmpVal += replaceStr(objElement.value+"", '&', '&amp;') + '=';
					for (var j=0; j<objForm.elements.length; j++) {
						if (objForm.elements[j].name == "paramValue" + paramNum) {
							strTmpVal += replaceStr(objForm.elements[j].value+"", '&', '&amp;');
							// exit the loop when the value field is found
							j=objForm.elements.length;
						}
					}
				}
			}
		}
		if (paramNum.length > 0)
			return strTmpVal;
		else
			return "";
	}

/// <function>
///   <title>replaceStr</title>
///   <description>
///      replace all occurences of a set of characters in a string with a 
///      new set of characters. the search is global and case insensitive
///   </description>
///   <param type="string" name="strSource">
///       the string containing the values to be replaced
///   </param>
///   <param type="string" name="strOld">
///       the characters to be replaced
///   </param>
///   <param type="string" name="strNew">
///       the characters to be inserted in place of those replaced
///   </param>
///   <return>string</return>
/// </function>
	function replaceStr(strSource, strOld, strNew) {
		var strNewString;	
		var re = new RegExp(strOld,"gi")
		strNewString = strSource.replace(re, strNew);
		return strNewString;
	}
 
