/* 
		These methods support selecting multiple categories and displaying the
   	categories below the select controls.  This code assumes these things:
		
		1.  A select control called categoryList which contains top
				level categories.
				
		2.  A select control called subCategoryList which contains second
				and third level categories.
				
		3.  A global array of categories with the following structure.  Each
				array element represents a category and contains another array with 
				the category detail.  Each category array element has the following 
				elements:
				
					0:  ID
					1:  Top level id (null if this is a top level category)
					2:  Second level id (null if this a first or second level category)
					3:  List text (text that will show in select controls)
					4:  Display text (text that will show below select controls)
*/

var ID = 0;
var PARENT_LEVEL1 = 1;
var PARENT_LEVEL2 = 2;
var LISTTEXT = 3;
var DISPLAYTEXT = 4;

// array of selected categories (size of this array determines max categories allowed)
var selectedCategories = new Array(null, null, null, null, null);

// array of categories selected at last state 
var lastSelected = new Array();

// handle nav4 resize issues
if ( is_nav4 ) {
	origWidth = innerWidth;
	origHeight = innerHeight;
}
	
function reDo() {
	if (innerWidth != origWidth || innerHeight != origHeight)
		location.reload();
}

if ( is_nav4 ) onresize = reDo;

// handle initial loading of page
function init() {
	// add previously selected categories to internal array
	for (i = 0; i < document.mainform.selectedCategories.length; i++) {
		var id = document.mainform.selectedCategories[i].value;
	
		if (id != "") {
			// get array index from id 
			for (j = 0; j < categories.length; j++) {
				if (categories[j][ID] == id) {
					add(j);
					break;
				}
			}
		}
	}
	
	// load subcategories and select any that should be
	for (i = 0; i < document.mainform.categoryList.length; i++) {
		if (document.mainform.categoryList.options[i].selected == true) {
			populateSubCategories(document.mainform.categoryList.options[i].value);
			
			for (j = 0; j < document.mainform.subCategoryList.length; j++) {
				for (k = 0; k < selectedCategories.length; k++) {
					if (selectedCategories[k] != null) {
						if (document.mainform.subCategoryList.options[j].value == selectedCategories[k][ID]) { 
							document.mainform.subCategoryList.options[0].selected = false;
							document.mainform.subCategoryList.options[j].selected = true;

						}	
					}	
				}
			}
			break;
		}
	}
	
	setLastSelected();
	displaySelected();
	
	if (getSelectedCount() == 0)
		document.mainform.categoryList.options[0].selected = true;
}

// method is called whenever a category is selected
function categorySelected(level) {
	var categoryCount = getSelectedCount();
	var selectedId = getLastSelected();
	if (level == 'primary') {
		populateSubCategories(selectedId);
	}

	if (selectCategory(document.mainform.subCategoryList) && selectCategory(document.mainform.categoryList)) {
		setLastSelected();
		displaySelected();
	}	
	else {
		alert('You may only select up to 5 categories and subcategories.');
		if (level == 'primary') {
			for (i = 0; i < document.mainform.categoryList.length; i++) {
				if (document.mainform.categoryList.options[i].value == selectedId) document.mainform.categoryList.options[i].selected = false;
			}
		}
		else {
			for (i = 0; i < document.mainform.subCategoryList.length; i++) {
				if (document.mainform.subCategoryList.options[i].value == selectedId) document.mainform.subCategoryList.options[i].selected = false;
			}
		}
	}	
}

function populateSubCategories(selectedId) {
	// check if functionality is supported 	

	var categoryList = document.mainform.categoryList;
	var subCategoryList = document.mainform.subCategoryList;
	
	// clear child list
	while (subCategoryList.options.length) subCategoryList.options[0] = null;

	// populate child list from category array 
	if (selectedId != "9991" && selectedId != "9992" && selectedId != -1) {
		subCategoryList.options[0] = new Option('------ All Subcategories ------', '9992');
		subCategoryList.options[0].selected = true;

		populateSubCntr = 1;
		for (i = 0; i < categories.length; i++) {
			if (categories[i][PARENT_LEVEL1] == selectedId) {
				if (categories[i][PARENT_LEVEL2] == null) subCategoryList.options[populateSubCntr] = new Option(categories[i][LISTTEXT], categories[i][ID]);
				else subCategoryList.options[populateSubCntr] = new Option('          ' + categories[i][LISTTEXT], categories[i][ID]);
				populateSubCntr++;
			}	
		}
	} 
	else {
		subCategoryList.options[0] = new Option('------ Select a Category ------', '9992');
	}
}

function selectCategory(list) {
	var index = 0;
	
	// remove all unselected
	for (k = 0; k < list.length; k++) {	
		if (!list.options[k].selected && list.options[k].value != "9991" && list.options[k].value != "9992") { 
			// get array index from id 
			for (j = 0; j < categories.length; j++) {
				if (categories[j][ID] == list.options[k].value) {
					index = j;
					break;
				}
			}
			remove(index);
		}
	}

	// add all selected

	for (k = 0; k < list.length; k++) {
		if (list.options[k].selected && list.options[k].value != "9991" && list.options[k].value != "9992") { 
			list.options[0].selected = false;
			// get array index from id 
			for (j = 0; j < categories.length; j++) {
				if (categories[j][ID] == list.options[k].value) {
					index = j;
					break;
				}
			}
			if (!add(index)) return false;
		}
	}
	return true;
}	

function add(index) {
	var replaced = false;

	for (j = 0; j < selectedCategories.length; j++) {
		// if more general item is in list, replace it
		if (selectedCategories[j] != null && (
				selectedCategories[j][ID] == categories[index][ID] ||
			  selectedCategories[j][ID] == categories[index][PARENT_LEVEL1])) {

				selectedCategories[j] = new Array(categories[index][ID], 
																 categories[index][PARENT_LEVEL1],
																 categories[index][PARENT_LEVEL2], 
																 categories[index][LISTTEXT], 
																 categories[index][DISPLAYTEXT]);
				replaced = true;
				break;

		// if more specific item is in list, ignore it
		} else if (selectedCategories[j] != null && (
				selectedCategories[j][PARENT_LEVEL1] == categories[index][ID] ||
				selectedCategories[j][PARENT_LEVEL2] == categories[index][ID])) {
				replaced = true;
				break;
		}
	}
	// if not a replacement, add it to list 	
	if (!replaced) {
		for (j = 0; j < selectedCategories.length; j++) {
			if (selectedCategories[j] == null) {
				selectedCategories[j] = new Array(categories[index][ID], 
																 categories[index][PARENT_LEVEL1],
																 categories[index][PARENT_LEVEL2], 
																 categories[index][LISTTEXT], 
																 categories[index][DISPLAYTEXT]);
				replaced = true;
				break;
			}
		}
	}
	return replaced;
}

function remove(index) {
	// remove item and any more specific dependent selections 
	for (j = 0; j < selectedCategories.length; j++) {
		if (selectedCategories[j] != null && (
			  selectedCategories[j][ID] == categories[index][ID] || 
				selectedCategories[j][PARENT_LEVEL1] == categories[index][ID] || 
				selectedCategories[j][PARENT_LEVEL2] == categories[index][ID]))  
				selectedCategories[j] = null;
	}	
}

function displaySelected() {
	var displayCats = '<ul>';
	
	for (i = 0; i < selectedCategories.length; i++) {
		if (selectedCategories[i] != null) 
			displayCats = displayCats + "<li>" + selectedCategories[i][DISPLAYTEXT] + "</li>";
	}
	displayCats = displayCats + "</ul>";

  if (is_ie5up || is_nav6up) {
			document.getElementById("displayCats").innerHTML = displayCats; 
			
			if (document.getElementById("catText")) {
				if (displayCats == '<ul></ul>') 
					document.getElementById("catText").style.visibility = "hidden";
				else 
					document.getElementById("catText").style.visibility = "visible";
			}
	}
	
	// place categories in hidden form fields
	for (i = 0; i < selectedCategories.length; i++) {
		if (selectedCategories[i] != null)
			document.mainform.selectedCategories[i].value = selectedCategories[i][ID];
		else
			document.mainform.selectedCategories[i].value = "";
	}		
}

// get number of categories currently selected
function getSelectedCount() {
	var selectedCount = 0;
	
	for (i = 0; i < selectedCategories.length; i++) {
		if (selectedCategories[i] != null) selectedCount++
	}
	return selectedCount;
}
		
// determine which category was actually selected in lists 
function getLastSelected() {
	var selectedId = -1;
	
	// loop through major categories
	for (i = 0; i < document.mainform.categoryList.length; i++) {
		if (document.mainform.categoryList.options[i].selected == true) {
			var alreadySelected = false;
			for (j = 0; j < lastSelected.length; j++) {
				if (document.mainform.categoryList.options[i].value == lastSelected[j]) alreadySelected = true;
			}
			if (!alreadySelected) selectedId = document.mainform.categoryList.options[i].value;
		}
	}
	// if last selected was not a major category, loop through subcategories
	if (selectedId == -1) {
		for (i = 0; i < document.mainform.subCategoryList.length; i++) {
			if (document.mainform.subCategoryList.options[i].selected == true) {
				var alreadySelected = false;
				for (j = 0; j < lastSelected.length; j++) {
					if (document.mainform.subCategoryList.options[i].value == lastSelected[j]) alreadySelected = true;
				}
				if (!alreadySelected) selectedId = document.mainform.subCategoryList.options[i].value;
			}
		}
	}
	return selectedId;
}

// build new array of all categories and subcategories selected
function setLastSelected() {
	lastSelected = new Array();
	var lastSelectedCntr = 0;
	for (i = 0; i < document.mainform.categoryList.length; i++) {
		if (document.mainform.categoryList.options[i].selected == true) {
			lastSelected[lastSelectedCntr] = document.mainform.categoryList.options[i].value;
			lastSelectedCntr++;
		}
	}
	for (i = 0; i < document.mainform.subCategoryList.length; i++) {
		if (document.mainform.subCategoryList.options[i].selected == true) {
			lastSelected[lastSelectedCntr] = document.mainform.subCategoryList.options[i].value;
			lastSelectedCntr++;
		}
	}		
}

function displayCatHtml() {
	if (is_ie || is_nav6up) {
		document.write(
		'<tr>' +
			'<td valign="top" class="form_list">' +
				'<div id="catText" style="visibility:hidden;">' +
					'Selected Categories' +
				'</div>' +
			'</td>' +
			'<td colspan="3" class="form_list" valign="top" width="100%">' +
				'<div id="displayCats">' +
				'</div>' +
			'</td>' + 
		'</tr>');
	}
}
