/* this is the beginning of the new dropdown functionality.
   To encompase all the variations and dependencies of dynamic dropdowns with a depth of 3 or more,
   a hierarchical tree structure is the best method to record this and allow for growth.
   Each node will contain drop down content.  The content will consist of a list of value(s) (array)
   and a named range for the associated dropdown for the dropdown at that level/depth.
   ie. if the node's parent has the value with in the node's value list, then the node uses the
   named range list called optionList. */

// stores an ordered list of the ids of the select field objects.
// Order is important as each element index indicates the object's depth in the hierarchical tree.
// ie. dropDowns[2] object represents the depth of 3 in the hierarchical tree structure.
// ie. the third dependent select field object.
//var dropDownOrder = new Array();

// root variable for the dynamic hierarchical tree structure;
// ie. should point to root node for dynamic dropdown tree.
//var dropDownTree = null;
var dropDownGroups = new Array();

function dropDownGroup() {
	this.dropDownOrder = new Array();
	
	this.dropDownTree = null;
	
	this.addNode = function(parentNode, childNode) {
			if (parentNode != null) {
				// set node as child of parentNode
				return parentNode.node.addChild(childNode);  // return childNode
			} else {
				// set node as root node of tree.
				this.dropDownTree = childNode;
				return this.dropDownTree; // return root node of tree
			}
		}
	
	this.addDropDown = function(myDropDownId) {
			this.dropDownOrder[this.dropDownOrder.length] = myDropDownId;
		}

	this.displayTree = function(currentNode, myDepth) {
			if (myDepth == null)
				myDepth = 0;
			var indent = "";
			for (var i=0; i < myDepth; i++)
				indent += "&nbsp;&nbsp;&nbsp;";
			document.getElementById("treeDiagram").innerHTML += indent + " - node (" + currentNode.getValueList() + ")<br />";
			myDepth++;
			for (var i=0; i < currentNode.node.children.length; i++) {
		//		document.getElementById("treeDiagram").innerHTML += " - " + node + "<br />";
				this.displayTree(currentNode.node.children[i], myDepth);
			}

			return;
		}

	this.showTree = function() {
		this.displayTree(this.dropDownTree);
	}
	
	
	this.checkTree = function(mySelectObj, loadValues) {
	// crawls node tree looking for current mySelectObj
	// if loadValues = true, then will load the value from _dd hidden field into the select object. Typically only to be done onload event.  Onchange event this should be false.
		
			//can be null when the object is hidden.
			if(mySelectObj == null){
				return;
			}
		
			// begin currentNode at the root of the node tree.
			var currentNode = this.dropDownTree;
			// get mySelectObj's dropdown order ie. depth to search to initially in the node tree.
			var dropDownDepth = 1;  // initialised to a depth of 1.
			var i = 0;
			//alert(mySelectObj + "," + (mySelectObj == null));
			// get drop down depth of mySelectObj..
			for (i=0; i < this.dropDownOrder.length; i++) {
				if (this.dropDownOrder[i] == mySelectObj.id) {
					dropDownDepth = i + 1;
					break;
				}
			}

			// search up to and including - to find the current node in the node tree
			var found = false;
			var j = 0;

			// for each depth of up until the mySelectObj's depth, populate value into select object at each depth level beginning from the root level,
			// and then, find the currentNode relative to the mySelectObj.
			// if currentNode does not exist in node tree, disable following select objects after mySelectObj in drop down order.
			for (i=0; i < dropDownDepth; i++) {
				found = false;

				// get select object at current depth
				tempSelectObj = document.getElementById(this.dropDownOrder[i]);
				// load value of _dd hidden field into the temporary select object
				if (loadValues) {
					setDropDownValue(tempSelectObj.id);
				}

				
				if (currentNode.node.children.length != 0) {
				// if the currentNode has any children..
					for (j=0; j < currentNode.node.children.length; j++) {
					// for each child belonging to the currentNode..
						if ((currentNode.node.children[j].hasValue("all")) || (currentNode.node.children[j].hasValue(tempSelectObj.options[tempSelectObj.options.selectedIndex].value))) {
						// if the currentNode child has the value of "all" or the value of the temporary select object, then have the next node to traverse so set child as new currentNode (thus traversing the key).
							currentNode = currentNode.node.children[j];
							found = true;
							break;  // found node at this depth so break to move onto next depth.
						}
					}
					if (!found) {
						//disable all dropdown after tempSelectObj...
					}
				} else {
					//disable all dropdown after tempSelectObj...
				}
			}

			if (found) {
				// currentNode has child node so has dependent.
			
				// get next select object in drop down order, following tempSelectObj.
				nextSelectObj = document.getElementById(this.dropDownOrder[i]);
				// populate options of next drop down (ie. dependent drop down) with the options list from the located child node currentNode.
				setOptions(nextSelectObj, currentNode.getOptionList());

				// recursive call for next select object in drop down order..
				this.checkTree(nextSelectObj, loadValues);

				return;
			} else {
				// currentNode does not have child node so no dependent.
				// reset all dropdowns following the current dropdown.
				for (i = dropDownDepth; i < this.dropDownOrder.length; i++) {
					disableOptions(this.dropDownOrder[i]);
				}
				return;
			}
		}
		
	this.populateDropDowns = function() {		
			this.checkTree(document.getElementById(this.dropDownOrder[0]), true);
		}
}

function dropDownContent(myValueList, myOptionList) {
	this.node = new node(); // for lookup / reference of node properties and methods.
	this.value = myValueList;
	this.optionList = myOptionList;
	this.addOptionList = function (myList) {
			this.optionList = myList;
		}
	this.addValueList = function (myValue) {
			this.value = myValue;
		}
		
	this.hasValue = function (myValue) {
			for (var i=0; i < this.value.length; i++) {
				if (this.value[i] == myValue) {
					return true;
				}
			}
			return false;
		}
	this.getOptionList = function () {
			return this.optionList;
		}
	this.addContent = function (myValue, myList) {
			this.addOptionList(myList);
			this.addValue(myValue);
		}
	this.getFieldObj = function () {
			return dropDowns[this.node.getDepth()];  // might be an issue here since getDepth is in the parent object
		}
	this.getValueList = function() {
			return this.value;
		}
}

function node(parent) {
	this.parent = parent ? parent : false;
	this.children = new Array();
	this.addChild = function(myNode) {
			myNode.parent = this;
			this.children[this.children.length] = myNode;
			return this.children[this.children.length - 1];
		}
	this.removeChild = function(myNode) {
			for (var i=0; i < this.children.length; i++) {
				if (this.children[i] == myNode) {
					this.children.splice(i, 1);
				}
			}
		}
	this.getDepth = function() {
			var myDepth = 0;
			while (myNode = myNode.parent) {
				myDepth++;
			}
			return myDepth;
		}
}

function removeOptions(mySelectObj) {
	mySelectObj.options.length = 0;
}

function disableOptions(mySelectId) {
	var mySelectObj = document.getElementById(mySelectId);
	removeOptions(mySelectObj);
	disableField(mySelectObj);
}

function setOptions(mySelectObj, myOptionList) {
	if (myOptionList.length == 0) {
		disableField(mySelectObj);
	} else {
		enableField(mySelectObj);
	}
	removeOptions(mySelectObj);
	for (var i=0; i < myOptionList.length; i+=2) {
		mySelectObj.options[mySelectObj.options.length] = new Option(myOptionList[i],myOptionList[i+1]);
	}
}

function selectDropDownOption(selectBoxId, myValue) {
// sets a select drop down object by id == selectBoxId to have the value == myValue if it exists in the current option list.
	var selectBox = document.basix_form.elements[selectBoxId];  // get select drop down object for id == selectBoxId.
	for (var i=0; i < selectBox.options.length; i++) {
	// for each option in the option list.
		if (selectBox.options[i].value == myValue) {
		// the value exists in the option list.
			selectBox.options[i].selected = true;
		}
	}
}

function setDropDownValue(selectBoxId) {
	// set a value for the given select box id.
	// Based from the value of the hidden input box with id of selectBoxId + "_dd".
	if (document.getElementById(selectBoxId + "_dd") != "undefined") {
		selectDropDownOption(selectBoxId, document.getElementById(selectBoxId + "_dd").value);
	}
}
