// When page loads
function changeAllOnLoad(arrOfObj,arrOfArr) {
	// Unpack objects
	for (var iObj = 0; iObj < arrOfObj.length; iObj++) {
		// onChange them all
		changeChildren(arrOfObj[iObj],arrOfArr[iObj]);
	}
}

// When each <select> loads
function addObjToList(parentObj, childPropNames) {
	// If we're the first, create our parcel
	if (typeof document.hierarchyObjs == "undefined") {
		document.hierarchyObjs = new Array();
	}
	if (typeof document.hierarchyProps == "undefined") {
		document.hierarchyProps = new Array();
	}
	// Add ourselves to it
	document.hierarchyObjs.push(parentObj);
	document.hierarchyProps.push(childPropNames);

}

// An onchange for the parent
function changeChildren(parentObj,childPropNames) {

	// Determine my selected properties
	var parentSelected = optionsSelected(parentObj);
	var parentConfig = document.getElementById(parentObj.id + '_parent_config');

	// For each child
	for (var iChild = 0; iChild < childPropNames.length; iChild++) {
		var thisChildName = childPropNames[iChild];
		var thisChild = document.getElementById('hierarchy_' + thisChildName);
		
		var thisSelectOne = false; 

		// Child has to be present in the form
		if (thisChild != null) {

			// Is it a select-one?
			thisSelectOne = (thisChild.type == 'select-one');
			var childConfig = document.getElementById(thisChild.id + '_child_config');

			var permittedOptions = new Object();
			// Find unique list of options switched on by each parent-selected property
			for (var iParentSelect = 0; iParentSelect < parentSelected.length; iParentSelect++) {
				var thisParentSelected = parentSelected[iParentSelect];
				var configID = parentConfig.id + '_' + thisChildName + '_' + thisParentSelected;

				// Obtain comma-delimited list of IDs permitted in child
				if (thisParentSelected != "") {
					permittedList = document.getElementById(configID);
					if (permittedList == null) {
						alert(configID);
						return;
					} else {
						permittedList = permittedList.className.replace(/^permitted_/,'').split(',');
					}
				} else {
					permittedList = new Array();
				}

				// Loop over list, setting permittedOptions[ID] to true
				for (var iPermitted = 0; iPermitted < permittedList.length; iPermitted++) {
					permittedOptions[permittedList[iPermitted]] = true;
				}

			}
			// Store existing options
			var debug = false;
			if (thisSelectOne && (thisChild.selectedIndex > -1) ) {
				for (var iStore = 0; iStore < thisChild.options.length > 0; iStore++) {
					var thisOption = thisChild.options[iStore];
					setFakeSelect(thisChildName,thisOption.value,false,debug);
				}
				setFakeSelect(thisChildName, thisChild.options[thisChild.selectedIndex].value,true,debug);
			} else {
				for (var iStore = 0; iStore < thisChild.options.length > 0; iStore++) {
					// Store value somewhere?
					var thisOption = thisChild.options[iStore];
					setFakeSelect(thisChildName,thisOption.value,thisOption.selected,debug);
				}
			}
			// Remove
			for (;thisChild.options.length > 0;) {
				thisChild.remove(0);
			}

			// Replace existing child's select options, looping over config rather than this array
			childConfigNodes = childConfig.getElementsByTagName("div");
			addedAlready = ",";
			index_to_zoom_to_if_single = -1;
			for (var iConfigNode = 0; iConfigNode < childConfigNodes.length; iConfigNode++) {
				// Get config node and extract the <select> ID it references
				thisConfigNode = childConfigNodes[iConfigNode];
				idFromConfig = thisConfigNode.id.replace(/.*_/,'');
				// Only add if not added already
				if ((typeof permittedOptions[idFromConfig] != "undefined") &&
					(addedAlready.indexOf(idFromConfig) == -1)
					) {
					if (isIE) {
						thisChild.add(new Option(thisConfigNode.firstChild.nodeValue,idFromConfig,false,getFakeSelect(thisChildName,idFromConfig)));
					} else {
						thisChild.add(new Option(thisConfigNode.firstChild.nodeValue,idFromConfig,false,getFakeSelect(thisChildName,idFromConfig)),null);
					}
					// If this node is selected, then set the
					// non-multiple zoom-to index (see below) to
					// the index of the last in the node array
					// i.e. this node
					if (getFakeSelect(thisChildName,idFromConfig)) { // && (index_to_zoom_to_if_single == -1)) {
						//index_to_zoom_to_if_single = thisChild.length - 1;
						//alert(thisChild.options[thisChild.options.length-1].text);
					}
					addedAlready = addedAlready + idFromConfig + ',';
				}

			}
			// If a single select, zoom to a particular index (it won't
			// do this automatically)
			// alert(index_to_zoom_to_if_single);
		}
	}
}



// Determine selected options
function optionsSelected(selectObj) {
	var selected = new Array();
	if (typeof selectObj.options == "undefined") {
		return selected;
	}
	for (var iOpt = 0; iOpt < selectObj.options.length; iOpt++) {
		var thisOption = selectObj.options[iOpt];
		if (thisOption.selected) {
			selected.push(thisOption.value);
		}
	}
	return selected;
}

function getFakeSelect(opName,opValue) {
	var fake = document.getElementById("hierarchy_" + opName + "_child_config_" + opValue);
	if (fake != null) {
		return (fake.className.indexOf("selected") != -1);
	}
	return false;
}

function setFakeSelect(opName,opValue,isOpSelected,debug) {
	var fake = document.getElementById("hierarchy_" + opName + "_child_config_" + opValue);
	if (fake != null) {
		// Get rid of any existing settings
		fake.className = fake.className.replace(/selected/g, "");
		if (isOpSelected) {
			// Append "selected" to className"
			fake.className = fake.className + " selected";
		}
		//if (debug) { alert(fake.className); }

		// Tidy up whitespace
		fake.className = fake.className.replace(/\s+/g, " ");
	}
}
