/* Criteria Category Behaviors - version 1.0
 *    by Jacob Hume, Warren County, http://www.co.warren.ny.us
 *    LastMod: 2008-05-06, JH
 *
 * Note: Requires the addEvent() code by Dustin Diaz
 *         (http://www.dustindiaz.com/rock-solid-addevent/) to add the onLoad event politely, and
 *         for the behavior for the parent criteria
 *
 * Copyright (c) 2008 Warren County, New York
 */

var criteria = {																				// All functions encapsulated in "criteria"

	Init : function()																			// Initialization function
	{
		// Get the form element
		var criteriaForm = document.getElementsByTagName('form').item(0);

		// Get all of the input tags in the first fieldset
		var inputs = document.getElementsByTagName('fieldset').item(0).getElementsByTagName('input');

		var thisInput = inputs.item(0);											// Get the first input tag
		var inputCounter = 0;																// Start an input counter

		while(thisInput)
		{
			// Add the event to the input field
			addEvent(thisInput, 'click', criteria.Toggle);

			inputCounter++;																		// Increment the input counter
			thisInput = inputs.item(inputCounter);						// Get the next input
		}
	},

	Toggle : function()																		// Toggle and disable all the stuff
	{
		var list = this.parentNode;													// Get the parent node of the input

		while(list && list.tagName.toLowerCase() != 'ul')		// If we're out of nodes, or we found a list
		{
			list = list.nextSibling;													// Check the next sibling

			while(list && list.nodeType != 1)									// Until we find a real node
			{
				list = list.nextSibling;												// Check the next sibling
			}
		}

		if(list)																						// If we found a list of sub-criteria
		{
			var subs = list.getElementsByTagName('input');		// Get all the sub-criteria checkboxes
			var disable = this.checked;												// Only disable if the input is checked

			for(i = 0; i < subs.length; i++)									// For each sub-criteria checkbox
			{
				subs.item(i).disabled = disable;								// Toggle!
			}
		}
	}
}

addEvent(window, 'load', criteria.Init);
