
/*---------------------------------------------------------------------------------------------------
attachEventToFormElements
---------------------------------------------------------------------------------------------------*/
function attachEventToFormElements(oForm,eventName,eventHandler) {
	var sType;
	for (var index=0; index < oForm.elements.length; index++) {
		sType = oForm.elements[index].type;
		if (sType != "button" && sType != "hidden") {
			addEvent(oForm.elements[index],eventName,eventHandler);
		}
	}
}
/*---------------------------------------------------------------------------------------------------
getFormDebugStr
---------------------------------------------------------------------------------------------------*/
function getFormDebugStr(oForm) {
	var sTemp = "";
	sTemp = '[FORM DEBUG]\n';
	sTemp += "method=" + oForm.method + "\n";
	sTemp += "action=" + oForm.action + "\n";
	sTemp += "target=" + oForm.target + "\n";
	sTemp += "name=" + oForm.name + "\n";
	sTemp += "id=" + oForm.id + "\n";
	sTemp += "----------------------------------------\n";
	for (var index=0; index < oForm.elements.length; index++) {
		oEl = oForm.elements[index];
		sTemp += oEl.name + "=";
		if (oEl.type == "button") sTemp += "[button object]"; //to prevent all the HTML inside of a button from displaying.
		else sTemp += oEl.value;
		
		sTemp += " (type:" + oEl.type + ")";
		
		if (oEl.type == "radio" || oEl.type=="checkbox") sTemp += " (checked:" + oEl.checked + ")"
		
		sTemp += "\n";

	}
	return sTemp;
}
/*---------------------------------------------------------------------------------------------------
createForm
---------------------------------------------------------------------------------------------------*/
function createForm(sAction) {
	try {
		var args = createForm.arguments;
		var sTarget = (args.length >=2 ? args[1] : "");
		var sMethod = (args.length >=3 ? args[2] : "POST");
		
		var oForm = null
		if (oForm = document.createElement("FORM")) {
			oForm.method = sMethod;
			oForm.action = sAction;
			if (sTarget != "") oForm.target = sTarget;
			
			document.body.appendChild(oForm);
		}
		
		return oForm;
	} catch (err) {
		throwConsoleError("createForm()",err);
	}
}
/*---------------------------------------------------------------------------------------------------
addToForm
---------------------------------------------------------------------------------------------------*/
function addToForm(oForm,fieldName,fieldValue) {
	var oHidden = document.createElement("INPUT");
	if (oHidden) {
		oHidden.type = "hidden";
		oHidden.name = fieldName;
		oHidden.value = fieldValue;
		
		return oForm.appendChild(oHidden);
	}
	return null;
}
/*-----------------------------------------------------------------------------
checkTextareaLength
-----------------------------------------------------------------------------*/
function countTextareaLength(oTextarea) {
	var sAttr = (oTextarea.getAttribute("maxLength") || oTextarea.getAttribute("maxlength"));
	var maxLength = parseInt(sAttr);
	var textLength = trim(oTextarea.value).length
	
	var oEl = document.getElementById(oTextarea.name + ".characterCountText");
	if (!oEl) oEl=document.getElementById(oTextarea.id + ".characterCountText");
	if (oEl) oEl.innerHTML = "(" + (maxLength-textLength) + " characters remaining)";
	
	if (textLength > maxLength) oTextarea.value = (oTextarea.value).slice(0,maxLength);
}
function allowTextareaLength(oTextarea) {	
	var sAttr = (oTextarea.getAttribute("maxLength") || oTextarea.getAttribute("maxlength"));
	var maxLength = parseInt(sAttr);
	var textLength = trim(oTextarea.value).length;

	return (textLength < maxLength);
}

/*---------------------------------------------------------------------------------------------------
 getSelectedRadio
	- pass in the radio button name to return the object that is selected
---------------------------------------------------------------------------------------------------*/
function getSelectedRadio(oRadio) {
	if (oRadio) {
		for (var index=0; index < oRadio.length; index++) {
			if (oRadio[index].checked) return oRadio[index] 
		}
		return null;
	}
}
/*---------------------------------------------------------------------------------------------------
 setSelectedRadio
	- pass in a value that exists in an radio button group list and that option will be set as selected
---------------------------------------------------------------------------------------------------*/
function setSelectedRadio(oRadio,value) {
	if (oRadio) {
		for (var index=0; index < oRadio.length; index++) {
			if (oRadio[index].value==value) { 
				oRadio[index].checked=true;
				break;
			}
		}
	}
}
/*---------------------------------------------------------------------------------------------------
 setSelectedOption 
	- pass in a value that exists in an option list and that option will be set as selected
---------------------------------------------------------------------------------------------------*/
function setSelectedOption(oSel,value) {
	var boolFound = false;
	if ((oSel) && (oSel.options)) {
		for (var index=0; index < oSel.options.length; index++) {
			if (oSel.options[index].value == value) { 
				oSel.options[index].selected=true;
				if (!oSel.multiple) oSel.selectedIndex = index; //setting selectedIndex on multi-select box will clear any other selected items
				boolFound = true;
				break;
			}
		}
		if (!boolFound) oSel.selectedIndex = -1;
	}
}
/*---------------------------------------------------------------------------------------------------
 setSelectedOptions 
	- pass in a comma delimited string containing the selected values.  Used for multi-select boxes
---------------------------------------------------------------------------------------------------*/
function setSelectedOptions(oSel,values) {
	var valueArr = values.split(",");
	for (var i=0; i<valueArr.length; i++) {
		setSelectedOption(oSel, trim(valueArr[i]));
	}
}
/*---------------------------------------------------------------------------------------------------
 appendOption
	-  append an option to a SELECT control
---------------------------------------------------------------------------------------------------*/
function appendOption(oSelect,name,value) {
	var bCheckDuplicates = (appendOption.arguments.length==4 ? appendOption.arguments[3] : false);
	
	if (bCheckDuplicates) {
		var bFound = false;
		var optionsLength = oSelect.options.length;
		for (var index=0; ((!bFound) && (index < optionsLength)); index++) {
			bFound = oSelect.options[index].text==name
		}
		if (bFound) return false;
	}
	var oOption = document.createElement("OPTION");
	oSelect.options.add(oOption);
	oOption.text = name;
	//oOption.innerText = name;
	oOption.value = value;
	
	return true;
}
/*---------------------------------------------------------------------------------------------------
 removeSelectedOption
	-  remove a particular option from a SELECT control based on the selectedIndex property
---------------------------------------------------------------------------------------------------*/
function removeSelectedOption(oSelect) {
	if (oSelect) {
		if (oSelect.selectedIndex != -1) oSelect.options.remove(oSelect.selectedIndex);
	}
}
/*---------------------------------------------------------------------------------------------------
 removeOptionName
	-  remove a particular option name from a SELECT control
---------------------------------------------------------------------------------------------------*/
function removeOptionName(oSelect,name) {
	var bRemoveIfSelected = (arguments.length==3 ? arguments[2] : true); 
	
	var optionsLength = oSelect.options.length;
	var bFound = false;
	for (var index=0; ((!bFound) && (index < optionsLength)); index++) bFound = (oSelect.options[index].text==name);
	if (bFound) index--;  //index advances before bFound criteria short circuits loop
	
	if (bFound && (bRemoveIfSelected || (!bRemoveIfSelected && oSelect.selectedIndex != index)) ) oSelect.options.remove(index); //index advances before bFound criteria short circuits loop
}
/*---------------------------------------------------------------------------------------------------
 removeOptionValue
	-  remove a particular option value from a SELECT control
---------------------------------------------------------------------------------------------------*/
function removeOptionValue(oSelect,value) {
	var bRemoveIfSelected = (arguments.length==3 ? arguments[2] : true); 
	
	var optionsLength = oSelect.options.length;
	var bFound = false;
	for (var index=0; ((!bFound) && (index < optionsLength)); index++) bFound = (oSelect.options[index].value==value);
	if (bFound) index--;  //index advances before bFound criteria short circuits loop
	
	if (bFound && (bRemoveIfSelected || (!bRemoveIfSelected && oSelect.selectedIndex != index)) ) oSelect.options.remove(index);
}
function removeOptionsByValue(oSelect,aValues) {
	var bRemoveIfSelected = (arguments.length==3 ? arguments[2] : true); 
	for (var index=0; index < aValues.length; index++) removeOptionValue(oSelect,aValues[index],bRemoveIfSelected);
}

/*---------------------------------------------------------------------------------------------------
 fillOptions
	- pass in an array of values to populate an options list
---------------------------------------------------------------------------------------------------*/
function fillOptions(oSel,nameValueArr) {
	try {
		var initialSelected = (fillOptions.arguments.length == 3 ? fillOptions.arguments[2] : null);
		oSel.options.length = 0; //clear all;
		if (nameValueArr.length > 0) {
			var boolNameValueObj = (nameValueArr[0].hasOwnProperty("value")); //is nameValueArr an array of objects with name and value properties, or just values (in which case both name and value of OPTION use same value)
			for (var index=0; index < nameValueArr.length; index++) {
				var oOption = document.createElement("OPTION");
				oSel.options.add(oOption);
		
				if (boolNameValueObj) {	
					oOption.text = nameValueArr[index].name;
					oOption.value = nameValueArr[index].value;
				} else {
					oOption.text = nameValueArr[index];
					oOption.value = nameValueArr[index];
				}
			}
		}
	
		if (initialSelected) setSelectedOptions(oSel,initialSelected);
	} catch(e) {
		alert("fillOptions(" + oSel.id + "," + nameValueArr +")\n" + e.description);
	}
}
/*---------------------------------------------------------------------------------------------------
 getSelectedIndexes, getSelectedValue, getSelectedValues
---------------------------------------------------------------------------------------------------*/
function getSelectedIndexes(oSelect) {
	//returns an array of all selected items in a multiple select box
	var returnArr = Array();
	for (var index=0; index < oSelect.options.length; index++)
		if (oSelect.options[index].selected) returnArr[returnArr.length] = index;
		
	return returnArr;
}
function getSelectedValues(oSelect) {
	var valueArr = Array();
	var tempArr = getSelectedIndexes(oSelect);
	
	for (var index=0; index < tempArr.length; index++)
		valueArr[valueArr.length] = oSelect.options[tempArr[index]].value;
				
	return valueArr;
}
function getSelectedValue(oSelect) {
	var value=null;
	if (oSelect) {
		if (oSelect.selectedIndex != -1) value=oSelect.options[oSelect.selectedIndex].value;
	}
	return value;
}
function getSelectedName(oSel) {
	return getOptionNameByValue(oSel,getSelectedValue(oSel));
}
function isOptionValueSelected(oSelect,value) {
	var aSelected = getSelectedValues(oSelect)
	for (var index=0; index < aSelected.length; index++) {
		if (aSelected[index] == value) return true
	}
	return false;
}
/*---------------------------------------------------------------------------------------------------
 getOptionNameByValue
	- pass in a value that exists in an option list and the name associated with the value will be returned
---------------------------------------------------------------------------------------------------*/
function getOptionNameByValue(oSel,value) {
	if (oSel) {
		optionsLength = oSel.options.length;
		for (var index=0;index < optionsLength; index++)
			if (oSel.options[index].value == value) return oSel.options[index].text;
	}
	return "";
}
/*---------------------------------------------------------------------------------------------------
createOptionsSubset
	-copy only the value/descriptions from the source into the target that match the "values" parameter
---------------------------------------------------------------------------------------------------*/
function createOptionsSubset(oSrcSelect, oDestSelect, values) {
	var valueArr = values.split(",");
	var nameValueArr = Array();
	oDestSelect.options.length = 0; 
	for (var index=0; index < valueArr.length; index++) {
		var oNameValue = new nameValueObj(getOptionNameByValue(oSrcSelect,valueArr[index]),valueArr[index]);
		nameValueArr[nameValueArr.length] = oNameValue;
	}
	fillOptions(oDestSelect,nameValueArr);
	return oDestSelect;
}


/*---------------------------------------------------------------------------------------------------
 Option List Movement Functions
---------------------------------------------------------------------------------------------------*/
function moveSelectedOption(oFromSelect, oToSelect, selectedIndex, boolSort) {
	if (selectedIndex != -1) {		
		var itemText = oFromSelect.options[selectedIndex].text;
		var itemValue = oFromSelect.options[selectedIndex].value;
		
		oToSelect.options[oToSelect.length] = new Option(itemText, itemValue);
		oFromSelect.options[selectedIndex]=null;
	}	
	if (boolSort) sortOptionsList(oToSelect);

	return true;
}

function moveOptionsFromTo(fromListName, toListName) {
	var oFromSelect = document.getElementById(fromListName);
	var oToSelect = document.getElementById(toListName);
	var boolSort = ((moveOptionsFromTo.arguments.length==3) && (moveOptionsFromTo.arguments[2]))
	
	for(var index=0; index < oFromSelect.options.length; index++)  {
		if(oFromSelect.options[index].selected) {
			moveSelectedOption(oFromSelect, oToSelect, index, boolSort);       
			index--;
     	}
   }
   
   return true;
}

function moveAllOptions(fromElementID, toElementID) {
	var oSelect = document.getElementById(fromElementID);
	for (var optionIndex=0; optionIndex < oSelect.length; optionIndex++) oSelectList.options[optionIndex].selected = true;
	moveOptionsFromTo(fromElementID, toElementID);
}

function sortOptionsList(oSelect) {
	var temp_opts = new Array()
	var temp = new Object()
	for(var i=0; i < oSelect.options.length; i++)  {
    	temp_opts[i] = oSelect.options[i]
	}
	for(var x=0; x < temp_opts.length-1; x++)  {
		for(var y=(x+1); y < temp_opts.length; y++)  {
			if(temp_opts[x].text > temp_opts[y].text)  {
        		temp = temp_opts[x].text
				temp_opts[x].text = temp_opts[y].text
				temp_opts[y].text = temp
				temp = temp_opts[x].value
				temp_opts[x].value = temp_opts[y].value
				temp_opts[y].value = temp        
			}
		}
	}
}

/*-------------------------------------------------------------------------------
check/uncheck all
-------------------------------------------------------------------------------*/
function toggleCheckAll(oCheckbox,sMultiboxID) {
	if (oCheckbox) {
		var bCheckAll = oCheckbox.checked;
		
		var oMultibox;
		var oForm = oCheckbox.form;
		if (oForm) 	oMultibox = oForm[sMultiboxID];
		else oMultibox = document.getElementsByName(sMultiboxID); //for floating window check all

		if (oMultibox) {	
			if (bCheckAll) checkAll(oMultibox); 
			else uncheckAll(oMultibox);
		}
	}
			
}
function checkAll(oMultibox) {
	if (checkAll.arguments.length == 2) boolChecked = checkAll.arguments[1]
	else boolChecked = true;
	
	if (oMultibox) {
		if (!oMultibox.type) {
			//mutliple checkboxes on the page with the same name will come in here
			for (var index=0; index < oMultibox.length; index++) oMultibox[index].checked=boolChecked;
		} else oMultibox.checked = boolChecked;
	}

}
function uncheckAll(oMultibox) {
	if (oMultibox)	for (var index=0; index<oMultibox.length; index++) oMultibox[index].checked=false;
}
function getCheckedValues(oMultibox) {
	var sValues = "";
	if (oMultibox)	{
		if (!oMultibox.type) {
			//mutliple checkboxes on the page with the same name will come in here
			for (var index=0; index<oMultibox.length; index++) 
				if (oMultibox[index].checked) {
					sValues += (sValues=="" ? "" : ",") + oMultibox[index].value;
				}
			
		
		} else sValues = (oMultibox.checked ? oMultibox.value : "");
	}
	return(sValues);
}








/*-------------------------------------------------------------------------------------------------------------------------
formObjectsArray
	- Used by ckError, ajaxForms, ckCommonEdit
	- Useful for ajax related forms where HTML is brought in via an ajax call and displayed as a form. Instead of building
	  the form using innerHTML (problems in IE), give each form field the same "name" attribute. Everything with
	  that name will be found by this function and added to the form object returned. The "fieldname" attribute will be used
	  as the form field name. 
---------------------------------------------------------------------------------------------------------------------------*/
function formObjectsArray(variant) {	
	var aFormObjects = Array();
	var oEl,sFieldName, sType;

	var aEl = Array();	
	switch (typeof(variant)) {
		case "object" :
			//variant is FORM object	
			aEl = variant.elements; 
			break;
		case "string" :
			//variant is name attribute value
			//Get all elements with specified name, but radio buttons and checkboxes special. They will not be retrieved using 
			//  getElementsByName. Find them and add to array of elements to process
			//  <input type="radio" name="contact.field.gender" fieldname="gender" value="F">Female
			var aByName = document.getElementsByName(variant); //fixed assoc array with a length attribute
			for (var index=0;index<aByName.length;index++) aEl[aEl.length] = aByName[index]; //need to convert to normal arrays so we can iterate by indexed element in loop below
			
			var aInputEl = document.getElementsByTagName("input"); 
			for (var index=0; index < aInputEl.length; index++) {
				oEl = aInputEl[index]; 
				if (typeof(oEl)=="object") { //array as built comes with a "length" key we want to avoid
					sType = oEl.getAttribute("type");
					if ((sType=="radio" || sType=="checkbox") && (sFieldName = oEl.getAttribute("fieldname")) && (oEl.getAttribute("name")==variant + "." + sFieldName)) {
						aEl[aEl.length] = oEl;
					}	
				}
			}
			break;
	}

	//this loop must use an indexed element instead of (for var key in aEl) becaue IE will iterate through all properties of FORM
	for (var index=0; index < aEl.length; index++) {
		if ((oEl = aEl[index]) && (typeof(oEl)=="object") && (oEl.type != "button")) {
			sFieldName = ((oEl.getAttribute("fieldname")) || (oEl.name)); //fieldname is most unique, so try first. Those are forms without a FORM tag

			var oObj = aFormObjects[sFieldName];
			if (!oObj) {
				oObj = ({"aEl":[],"value":""}); //****form object format. Array of elements (checkboxes and radio have multiple) and a value
				aFormObjects[sFieldName] = oObj;
			}
				
			oObj.aEl[oObj.aEl.length] = oEl; 

			sValue = getElementValue(oEl); //advancedForms.js
			if (sValue != "") oObj.value += (oObj.value=="" ? "" : ",") + sValue;
		}
	}


	return aFormObjects;
}



/*-------------------------------------------------------------------------------
getElementValue and buildFormByName 18 Jan 2010
-------------------------------------------------------------------------------*/
function getElementValue(oEl) {
	//When dynamically building forms (ajax) just asking for the value attribute of an element doesn't always suffice. This
	//  looks at the element's tag and returns the appropriate value for submitting via a form. Called via
	//  getFormByName() but placed outside of that function for other scripts to use it.
	var sValue="";
	if (oEl) {
		//alert( oEl.tagName);
		
		switch (oEl.type) {
			case "checkbox":
			case "radio" : //individual radio elements coming in, so only need to return value if checked. No need to check all in group
				sValue = (oEl.checked ? oEl.value : "");
				break;
			case "text":
			case "textarea":
			case "hidden" :
			case "password" :
			case "select-one" :
			case "file" :
				sValue = oEl.value;
				break;
			case "select-multiple":
				for (var index=0; index < oEl.options.length; index++) { //must use indexed instead of key since IE iterates all properties
					if (oEl.options[index].selected) sValue += (sValue=="" ?  "" : ",") + oEl.options[index].value;
				}
				break;
			case "button" :
			case "fieldset" :
			case "object" : break; //do nothing
			
			default :
				if ((oEl.tagName.toLowerCase() != "fieldset") && (typeof(oEl) != "object"))
					throwConsoleError("getElementValue()","Unhandled element type:" + oEl.type + "\nid:" + oEl.id + "\nname:" + oEl.name + "\ntypeof:" + typeof(oEl) + "\ntagName:" + oEl.tagName);
		}
	}
	return(trim(sValue));
}







