// JavaScript Document

var selectedCategory = '';
var selectedSubcategory = '';
var selectedItems = '';

var listCategories = new Array();
var listSubcategories = new Array();
var listItems = new Array();
var sectionTitlesArray = new Array();

sectionTitlesArray[0] = 'section';
sectionTitlesArray[1] = 'subsection';
sectionTitlesArray[2] = 'item';

// populate 'searching in ...' text

function buildList(arrayName) {

	var returnString = '';
	var tempArray = new Array();
	
	var i = 0;
	for (var keyVal in arrayName) {
		if (arrayName[keyVal] != '') {
			tempArray[i] = arrayName[keyVal];
			i++;
		}
		
	} 
	
	var lastItem = tempArray.pop();
	
	if (getLength(tempArray)>1) {
		returnString = tempArray.join(", ") + " and " + lastItem;
	} else if(getLength(tempArray) == 1) {
		returnString = tempArray[0]  + " and " + lastItem;
	} else {
		returnString = lastItem;
	}
	
	return ' <strong>&ldquo;' + returnString + '&rdquo;</strong>';
}

function buildSearchString() {

	searchStringContent = '';
	
	var tempArray = new Array(); 
	var availableItems = new Array(); 

	selectOrder1List = document.getElementById('selectOrder1');
	selectOrder2List = document.getElementById('selectOrder2');
	selectOrder3List = document.getElementById('selectOrder3');
	
	// populate list from selectOrder1
	
	for (i = 0; i < selectOrder1List.length; i++) {
		if (selectOrder1List.options[i].selected == true) {
			tempArray[i] = selectOrder1List.options[i].innerHTML;
		} 
	}
	
	if (getLength(tempArray) > 0) {
		searchStringContent += 'Search in ' + sectionTitlesArray[0] + ((getLength(tempArray) != 1) ? 's': '') + buildList(tempArray);
	}
	
	
	// populate list from selectOrder2
	
	
	tempArray.length = 0;
	
	
	for (i = 0; i < selectOrder2List.length; i++) {
		if (selectOrder2List.options[i].selected == true) {
			tempArray[i] = selectOrder2List.options[i].innerHTML;
		} 
	}
	
	if (getLength(tempArray) > 0) {
		searchStringContent += '; in ' + sectionTitlesArray[1] + ((getLength(tempArray) != 1) ? 's': '') + buildList(tempArray);
	}
	
	// populate list from selectOrder3
	
	tempArray.length = 0;
	
	for (i = 0; i < selectOrder3List.length; i++) {
		if (selectOrder3List.options[i].selected == true) {
			tempArray[i] = selectOrder3List.options[i].innerHTML;
		} 
	}
	
	if (getLength(tempArray) > 0) {
		searchStringContent += ' and in ' + sectionTitlesArray[2] + ((getLength(tempArray) != 1) ? 's': '')  + buildList(tempArray);
	}

	statusText(searchStringContent);

}

// list population functions

function clearOptions(OptionList) {

   for (x = OptionList.length; x >= 0; x--) {
      OptionList[x] = null;
   }
   
}

function addToOptionList(OptionList, OptionValue, OptionText) {
   OptionList[OptionList.length] = new Option(OptionText, OptionValue);
}

// associative array functions

function getLength(input1) {
	
		var length = 0;
		
		for (keyVal in input1) { 
			length++; 	
		}
		
		return length;
}


function arrayAdd(arrayOld, arrayNew) {

	var tempArray = new Array();
	
	for (var keyVal in arrayOld) {
		tempArray[keyVal] = arrayOld[keyVal];
	}

	for (var keyVal in arrayNew) {
		tempArray[keyVal] = arrayNew[keyVal];
	}
	
	return tempArray;
	
}


function sortAssoc(arrayName) {


	var tempArray = new Array();
	var outputArray = new Array();
	var temp;
	
	i=0;
	
	for (var keyVal in arrayName) {
		tempArray[i] = String(arrayName[keyVal]).toUpperCase() + '//--string_divider--//' + keyVal;
		i++;
	}
	
	tempArray = tempArray.sort();
	
	for (i=0; i < tempArray.length; i++) {
		if (tempArray[i]!=tempArray[i-1]) {
			temp = tempArray[i].split('//--string_divider--//');
			tempIndex = temp[1];
			outputArray[tempIndex] = arrayName[tempIndex];
		}
	}
	
	return outputArray;
}

// statusText function


function statusText(value) {
	document.getElementById('statusText').innerHTML = value;
}



function populateList(currentItem) { // populateList start

var j = 0;

var availableSubcatogories = new Array();
var availableItems = new Array();



clearOptions(document.getElementById('selectOrder3'));

selectOrder1List = document.getElementById('selectOrder1');
selectOrder2List = document.getElementById('selectOrder2');
selectOrder3List = document.getElementById('selectOrder3');

// populate first list - always

if (selectOrder1List.length == 0) {
	for (keyValue in listCategories) {
		addToOptionList(selectOrder1List, keyValue, listCategories[keyValue]);
	}
}

// determine which items in the category list are selected
	availableSubcatogories.length = 0; // flush current subcategory selection array

for (i = 0; i < selectOrder1List.length; i++) {
	if (selectOrder1List.options[i].selected == true) {
		var selectedValue = selectOrder1List.options[i].value;
		availableSubcatogories = arrayAdd(availableSubcatogories, listSubcategories[selectedValue]);
	} 
}



if (currentItem == 'cat') {	 // populate subcategories if required

	clearOptions(document.getElementById('selectOrder2'));
   
	if (getLength(availableSubcatogories) > 0) {
	
		availableSubcatogories = sortAssoc(availableSubcatogories);
	
		for (keyValue in availableSubcatogories) {
			addToOptionList(selectOrder2List, keyValue, availableSubcatogories[keyValue]);
		}
	
	}

}

// determine which items in the subcategory list are selected

availableItems.length = 0; // flush current item selection array

for (i = 0; i < selectOrder1List.length; i++) {
	if (selectOrder1List.options[i].selected == true) {
		categoryValue = selectOrder1List.options[i].value;
		for (j = 0; j < selectOrder2List.length; j++) {
			if (selectOrder2List.options[j].selected == true) {
				subcategoryValue = selectOrder2List.options[j].value;
				availableItems = arrayAdd(availableItems, listItems[categoryValue][subcategoryValue]);
			}   
		}
	}   
}


// populate item if required

   
if (getLength(availableItems) > 0) {

	availableItems = sortAssoc(availableItems);

	clearOptions('selectOrder3');

	for (keyVal in availableItems) {
		addToOptionList(selectOrder3List, keyVal, availableItems[keyVal]);
	}

}

buildSearchString();

} // populateList end


