/*
**	 Hiearchical select boxes
*/

var hierarchicalControlsBuilt = false;

function buildSelectHierarchyControls() {
	forEach(getSelectBoxesWithHierarchy(), function(selectBox) {
		buildHiearchySelectBoxes(selectBox);
	});
}

function buildHiearchySelectBoxes(hierarchySelectBox) {

	var containerDiv = getFirstParentByTagAndClassName(hierarchySelectBox, "div", null);

	// Build level one select box
	var levelOneSelect = document.createElement("SELECT");
	levelOneSelect.className = "levelOneSelect";
	forEach(getElementsByTagAndClassName("optgroup", null, hierarchySelectBox), function(optGroup){
		if(optGroup.label == ""){
			optGroup.label = getFirstElementByTagAndClassName("option", null, optGroup).text;
		}
		levelOneSelect.options.add(createOption(optGroup.label, optGroup.id));
	});
	
	connect(levelOneSelect, "onchange", populateRelatedChildSelectBox);
	containerDiv.appendChild(levelOneSelect);

	// Build level two select box
	var levelTwoSelect = document.createElement("SELECT");
	levelTwoSelect.className = "levelTwoSelect";
//	levelTwoSelect.options.add(getBlankOption());

	// Assigning the id of the hierarchy select to 2nd/lastlevel select box to post back selected field value.
	levelTwoSelect.id = hierarchySelectBox.id;
	levelTwoSelect.name = hierarchySelectBox.name;
	hierarchySelectBox.id = "";
	hierarchySelectBox.name = "";

	containerDiv.appendChild(document.createElement("br"));
	containerDiv.appendChild(document.createElement("br"));
	containerDiv.appendChild(levelTwoSelect);
	makeInvisible(hierarchySelectBox);
	
	populateHieararchySelectBoxesValues(hierarchySelectBox);

//  add a hidden field with the select name so that the persisted select values can be reset/blank.
	createHiddenField(levelTwoSelect.name,"");
	
}

function createHiddenField(fieldName, value) {
	var form =  $$('form')[0];
	var hiddenField = document.createElement("input");
	hiddenField.type = "hidden";
	hiddenField.id = "hdn_" + fieldName;
	hiddenField.name = fieldName;
	hiddenField.value = value;
	form.appendChild(hiddenField);
}

function hasBlankOption(selectBox) {

	return /null/.test(selectBox.options[0].value);
}

function populateHieararchySelectBoxesValues(e) {
// Assigning the selected values from the composite hiearchy select box to the individual hiearchy select boxes	
	var hierarchySelectBox = (typeof e.tagName != "undefined"?e:this);

	var containerDiv = getFirstParentByTagAndClassName(hierarchySelectBox, "div", null);
	var levelOneSelect = getElementsByTagAndClassName("select", "levelOneSelect", containerDiv)[0];
	var levelTwoSelect = getElementsByTagAndClassName("select", "levelTwoSelect", containerDiv)[0];

	var selectedOption = hierarchySelectBox[hierarchySelectBox.selectedIndex];  		
	var optionGroup = getFirstParentByTagAndClassName(selectedOption, "optgroup", null);
	var groupOptions = getElementsByTagAndClassName("option", null, optionGroup);

	setSelectOptionByValue(levelOneSelect, optionGroup.id);
	populateRelatedChildSelectBox(levelOneSelect);
	setSelectOptionByValue(levelTwoSelect, selectedOption.value);
}

function setSelectOptionByValue(selectBox, optionValue) {
	for(i=0; i<selectBox.length; i++) {
		if(selectBox[i].value == optionValue) {
			selectBox.selectedIndex = i;
			return;
		}
	}
}

function createOption(text,value) {
	var option = document.createElement('OPTION');
	option.text = text;
	option.value = value;
	return option;
}

function populateRelatedChildSelectBox(e) {

	var parentSelect = (typeof e.tagName != "undefined"?e:this);
	var containerDiv = getFirstParentByTagAndClassName(parentSelect, "div", null);
	var levelTwoSelect = getElementsByTagAndClassName("select", "levelTwoSelect", containerDiv)[0];

	removeAll(getElementsByTagAndClassName("option", null, levelTwoSelect));

	if(hasBlankOption(parentSelect)) {
		levelTwoSelect.options.add(createOption(parentSelect.options[0].text,""));	
		levelTwoSelect.disabled = (parentSelect.selectedIndex==0);
	}

	var optionGroup = $$('optgroup[id=' + parentSelect.value + ']')[0];
	if(optionGroup) {
		var groupOptions = getElementsByTagAndClassName("option", null, optionGroup);
		for(i=0;i<groupOptions.length;i++) {
			levelTwoSelect.options.add(createOption(groupOptions[i].text, groupOptions[i].value));
		}

		// select the first option in level two select if there is only one option
		if(groupOptions.length == 1) {
			levelTwoSelect.selectedIndex = 1;		
		}
	}

	try {
		updateSubmitMessage();
	}catch(error){}
}

function getSelectBoxesWithHierarchy() {

	var uniqueSelectBoxesWithHierarchy = new Object();
	var selectBoxesWithHierarchy = new Array();
	var optGroups = $$('optgroup');
	
	for(i=0;i<optGroups.length;i++) {
		var selectBox = getFirstParentByTagAndClassName(optGroups[i], "select", null);
		if(!uniqueSelectBoxesWithHierarchy[selectBox.id]){
			uniqueSelectBoxesWithHierarchy[selectBox.id]= selectBox.id;
			selectBoxesWithHierarchy.push(selectBox);
		}
	}
	return selectBoxesWithHierarchy;
}

function initHierarchicalDemographics() {
	if(!hierarchicalControlsBuilt) {
		buildSelectHierarchyControls();
	}
	hierarchicalControlsBuilt = true;
}