/*
 * Toggles the button CSS class, ad toggles which category is displayed.
 * Called when a parent category is clicked. It toggles the category button to frmButtonSelected, and 
 * the last clicked on button to frmButton.  It changes the CSS class of subcategory DIV with class 
 * list-open to list-closed.  It then changes the CSS class of the subcategory DIV with ID of the
 * parent categories key to list-open.
 *
 * Used by the category selection submodal.
 *
 * @param a two element array.  
 *       first element:  the key for the clicked on category
 *       second element: the name of the clicked on category
 */
function sectionClick(category) {
    var section = YAHOO.util.Dom.get(category[1]);
    var button = YAHOO.util.Dom.get(category[0]) + section;
    var open_section = YAHOO.util.Dom.getElementsByClassName('list-open');
    var last_button = YAHOO.util.Dom.getElementsByClassName('frmButtonSelected');

    if (open_section[0] !== section) {
      YAHOO.util.Dom.replaceClass(open_section[0], 'list-open', 'list-closed');    
      YAHOO.util.Dom.replaceClass(section, 'list-closed', 'list-open');    
      YAHOO.util.Dom.replaceClass(last_button[0], 'frmButtonSelected', 'frmButton');    
      YAHOO.util.Dom.replaceClass(button, 'frmButton', 'frmButtonSelected');    
    }
}

/*
 * Removes the selected category from the selected_subcategories select element.
 * Used by the category selection submodal.
 */
function removeSelectedSubcategory() {
    var selection = document.getElementById('selected_subcategories');
    var i;
    for (i = selection.length - 1; i>=0; i--) {
      if (selection.options[i].selected) {
        var button = YAHOO.util.Dom.get(selection.options[i].value);
        YAHOO.util.Dom.replaceClass(button, 'subcategoryButtonSelected', 'subcategoryButton');    
        selection.remove(i);
      }
    }
}

/*
 * Removes the given category from the selected_subcategories select element.
 * Used by the category selection submodal.
 *
 * @param the value of the option to be removed from the select element.  
 */
function removeSubcategoryFromSelection(category) {
    var selection = document.getElementById('selected_subcategories');
    var i;
    for (i = selection.length - 1; i>=0; i--) {
      if (selection.options[i].value === category) {
        selection.remove(i);
        var button = YAHOO.util.Dom.get(category);
        YAHOO.util.Dom.replaceClass(button, 'subcategoryButtonSelected', 'subcategoryButton');    
      }
    }
}

/*
 * Determines if a given categroy is in the select element.
 * Used by the category selection submodal.
 * @param the value of the option to be checked for.
 * @return true if the category is in the select element, false if it is not.
 */
function isSubcategorySelected(category) {
    var selection = document.getElementById('selected_subcategories');
    var i;
    for (i = selection.length - 1; i>=0; i--) {
      if (selection.options[i].value === category) {
        return true;
      }
    }
    return false;
}

/*
 * Adds a category to the selected_subcategories select element.
 * Used by the category selection submodal.
 * @param the category to be added.
 */
function appendOption(subcategory) {
    var element = document.createElement('option');
    element.text = subcategory;
    element.value = subcategory;
    var subcategories = document.getElementById('selected_subcategories');

    try {
      subcategories.add(element, null); // standards compliant; doesn't work in IE
    } catch(ex) {
      subcategories.add(element); // IE only
    }
}

/*
 * Selects all option elements in the selected_subcategories select element.
 * This insures that all of the categories the user has selected for inclusion get POSTed.
 * Used by the category selection submodal.
 */
function selectAllOptionsAndSubmit() {
    var subcategories = document.getElementById('selected_subcategories');
    for (var i=0; i<subcategories.options.length; i++) {
      subcategories.options[i].selected = true;
    }
    document.fs_select_subcategories_form.submit();
}


/*
 * Toggles if a categroy is selected for inclusion.
 * Switches the CSS class of the clicked on category from subcategoryButton to subcategoryButtonSelected,
 * or vice-versa.  Removes the category from the selected_subcategories select element, if present, and
 * adds it if it is not present.
 * 
 * Used by the category selection submodal.
 * @param a two element array.  
 *       first element:  the key for the clicked on category
 *       second element: the name of the clicked on category
 */
function toggleOption(subcategory) {
    if (isSubcategorySelected(subcategory[0])) {
      removeSubcategoryFromSelection(subcategory[0]);
    } else {
      var element = document.createElement('option');
      element.text = subcategory[1];
      element.value = subcategory[0];
      var subcategories = document.getElementById('selected_subcategories');

      try {
        subcategories.add(element, null); // standards compliant; doesn't work in IE
      } catch(ex) {
        subcategories.add(element); // IE only
      }
  
      var button = YAHOO.util.Dom.get(subcategory[0]);
      YAHOO.util.Dom.replaceClass(button, 'subcategoryButton', 'subcategoryButtonSelected');    
    }
}

function hidediv(div_id) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(div_id).style.display = 'none';
	} else {
		if (document.layers) { // Netscape 4
			document.div_id.display = 'none';
		} else { // IE 4
			document.all.div_id.style.display = 'none';
		}
	}
}

function showdiv(div_id) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(div_id).style.display = 'inherit';
	} else {
		if (document.layers) { // Netscape 4
			document.div_id.display = 'inherit';
		} else { // IE 4
			document.all.div_id.style.display = 'inherit';
		}
	}
}

