	
var groupSet = new Array();

function initGroupSetData()
{
	var theItems = new Array();
	theItems[theItems.length] = { code:"bb",name:"black" };
	theItems[theItems.length] = { code:"pt",name:"pinto" };
	theItems[theItems.length] = { code:"kd",name:"kidney" };
	theItems[theItems.length] = { code:"yl",name:"yellow" };
	theItems[theItems.length] = { code:"zc",name:"zucchini" };
	theItems[theItems.length] = { code:"ac",name:"acorn" };
	
	var theGroups = new Array();
	theGroups[theGroups.length] = { code:"beans", name:"some beans", list:["bb","pt","kd"] };
	theGroups[theGroups.length] = { code:"squash",name:"some squash",list:["yl","zc","ac"] };
	theGroups[theGroups.length] = { code:"mysal" ,name:"my salad",   list:["pt","zc"] };

	groupSet[0] = { itemList:theItems, groupDef:theGroups };
	groupSet[0].itemBox = document.test.veggieList;
	groupSet[0].antiItemBox = document.test.antiVeggieList;
	groupSet[0].groupBox = document.test.groups;
}


function initGroupSets(displaySettings)
{
	initGroupSetData();

	for (var i = 0; i < groupSet.length; i++)
	{
		var itemList = groupSet[i].itemList;
		
		for (var j = 0; j < itemList.length; j++)
		{
			itemList[codeAsIndex(itemList[j].code)] = itemList[j];
		}

		var groupDef = groupSet[i].groupDef;
		
		for (var j = 0; j < groupDef.length; j++)
		{
			groupDef[codeAsIndex(groupDef[j].code)] = groupDef[j]; 
		}

		fillIt(i,displaySettings);
	}	
}

// IMPORTANT: can't use just code cuz it might be a number (or empty?)
function codeAsIndex(code)
{
	return "_" + code;
}

function listIt(groupSetIndex)
{
	var theItems = groupSet[groupSetIndex].itemList;
	var theGroups = groupSet[groupSetIndex].groupDef;

	var list = "GroupSet=" + groupSetIndex + " groupCount=" + theGroups.length + " itemCount=" + theItems.length +":\n";
	
//	alert (list);
	for (var i = 0; i < theGroups.length; i++)
	{
		list += "Name: " + theGroups[i].name + " ";
//	alert (list);
		
		var group = theGroups[i].list;
		var groupText = "List: ";
//alert("list length=" + group.length);		
		for (var j = 0; j < group.length; j++)
		{
//		alert ("group " + j + "=" + group[j]);
			groupText += theItems[codeAsIndex(group[j])].name + " ";
		}
		
		list += groupText + "\n";
	}
	
	alert (list);
}

function negateIt(groupSetIndex)
{
	var itemBox = groupSet[groupSetIndex].itemBox;
	var antiItemBox = groupSet[groupSetIndex].antiItemBox;
	
	for (var i=0; i < itemBox.options.length; i++)
	{
		antiItemBox.options[i].selected = !itemBox.options[i].selected;
	}
}

function resetGroupList(groupSelectBox,itemSelectBox,groupIndex)
{
	if ((groupSelectBox == null) && (itemSelectBox == null))
		return; // short-circuit
	
	var selectedGroupIndex = (groupSelectBox != null) ? groupSelectBox.selectedIndex : itemSelectBox.selectedIndex;
	var selectedItemIndex  = (itemSelectBox != null)  ? itemSelectBox.selectedIndex  : groupSelectBox.selectedIndex;
		
	var kActive = 0;
	var kInactive = 1;
	var kAll = 2;
	var settings = { showActiveGroups:true, showInactiveGroups:true, showActiveItems:true, showInactiveItems:true };

	switch (selectedGroupIndex)
	{
		case kActive:   settings.showInactiveGroups = false; break;
		case kInactive: settings.showActiveGroups   = false; break;
	}

	switch (selectedItemIndex)
	{
		case kActive:   settings.showInactiveItems = false; break;
		case kInactive: settings.showActiveItems   = false; break;
	}
	
	fillIt(groupIndex,settings);
}

var gAllOnDisplaySettings   = { showActiveGroups:true, showInactiveGroups:true, showActiveItems:true, showInactiveItems:true };
var gDefaultDisplaySettings = { showActiveGroups:true, showInactiveGroups:false, showActiveItems:true, showInactiveItems:false };

function fillIt(groupSetIndex,display)
{
	var activeSavvy = true;
//	var activeSavvy = groupSet[groupSetIndex].activeSavvy;
	
	if (display == null)
	{
//		activeSavvy = false;
		display = gDefaultDisplaySettings;
//		display = gAllOnDisplaySettings;
	}
	
	var theItems = groupSet[groupSetIndex].itemList;
	var theGroups = groupSet[groupSetIndex].groupDef;
	var itemBox = groupSet[groupSetIndex].itemBox;
	var antiItemBox = groupSet[groupSetIndex].antiItemBox;
	var groupBox = groupSet[groupSetIndex].groupBox;

	groupBox.options.length = 0;
	
	for (var i=0; i < theGroups.length; i++)
	{
		var skip = false;
		var mark = false;
		
		if (activeSavvy)
		{
			var active = theGroups[i].active;
			var showAll = display.showActiveGroups && display.showInactiveGroups;
		
			skip = (!showAll && ((display.showActiveGroups && !active) || (display.showInactiveGroups && active)));
			mark = showAll && !active;
		}
		
		if (skip)
			continue;
			
		var groupName = theGroups[i].name;
		
		if (mark)
		{
			groupName += "*";
		}
		
		groupBox.options[groupBox.options.length] = new Option(groupName,theGroups[i].code);
	}
	

	if (itemBox == null)
		return; // we're done

	itemBox.options.length = 0;
	
	if (antiItemBox != null)
	{
		antiItemBox.options.length = 0;
	}
	
	for (var i=0; i < theItems.length; i++)
	{
		var skip = false;
		var mark = false;
		
		if (activeSavvy)
		{
			var active = theItems[i].active;
			var showAll = display.showActiveItems && display.showInactiveItems;
		
			skip = (!showAll && ((display.showActiveItems && !active) || (display.showInactiveItems && active)));
			mark = showAll && !active;
		}
		
		if (skip)
			continue;

		var itemName = theItems[i].name;
		
		if (mark)
		{
			itemName += "*";
		}
		
		itemBox.options[itemBox.options.length] = new Option(itemName,theItems[i].code,false);
		
		if (antiItemBox != null)
		{
			antiItemBox[antiItemBox.options.length] = new Option(itemName,theItems[i].code,true);
		}
	}
}

function selectAllInBox(box,selectedState)
{
	for (var i = 0; i < box.options.length; i++)
	{
		box.options[i].selected = selectedState;
	}
	
	if (box.onchange != null)
	{
		box.onchange();
	}
}

function findInBox(box,valueToFind,selectedState)
{
	var matchedIndex = -1;
	
	for (var i = 0; i < box.options.length; i++)
	{
		var option = box.options[i];
		
		if (option.value == valueToFind)
		{
			matchedIndex = i;
			
			if (selectedState != null)
			{
				option.selected = selectedState;
			}
			
			break;
		}
	}
	
	return matchedIndex;
}

function fillGroupList(groupSetIndex)
{
	var theItems = groupSet[groupSetIndex].itemList;
	var theGroups = groupSet[groupSetIndex].groupDef;
	var groupItemsBox = groupSet[groupSetIndex].itemBox;
	var groupAntiItemsBox = groupSet[groupSetIndex].antiItemBox;
	var groupBox = groupSet[groupSetIndex].groupBox;
	var resultBox = groupSet[groupSetIndex].resultBox;
	
	if (resultBox != null)
	{
		resultBox.options.length = 0;
	}
	
	if (groupItemsBox != null)
	{
		for (var i = 0; i < groupItemsBox.options.length; i++)
		{
			groupItemsBox.options[i].selected = false;
			
			if (groupAntiItemsBox != null)
			{
				groupAntiItemsBox.options[i].selected = true;
			}
		}
	}	

	for (var i = 0; i < groupBox.options.length; i++)
	{
		if (!groupBox.options[i].selected)
			continue;

		var group = theGroups["_" + groupBox.options[i].value].list;
//		var alertStr = "";
//		for (var k = 0; k < group.length; k++)
//		{
//			alertStr += "," + group[k];
//		}
//		alert(groupBox.options[i].value + ' matched: ' + alertStr);
		
		// Option 1: Fill the Result box with all the groupItems	
		if (resultBox != null)
		{
			for (var j = 0; j < group.length; j++)
			{
				var groupItem = theItems[codeAsIndex(group[j])];
				
				if (groupItem != null)
				{
					var alreadyThere = (findInBox(resultBox,groupItem.code) >= 0);
			
					if (!alreadyThere)
					{
						resultBox.options[resultBox.options.length] = new Option(groupItem.name,groupItem.code);
					}
				}
				else
				{
					alert('no item for ' + group[j]);
				}
			}
		}	

		// Option 2: Just select the proper items in the groupItems box
		if (groupItemsBox != null)
		{
			for (var j = 0; j < groupItemsBox.options.length; j++)
			{
				var codeToMatch = groupItemsBox.options[j].value;
				var matched = false;
				
				for (var k = 0; k < group.length; k++)
				{
					if (group[k] == codeToMatch)
					{
						matched = true;
						break;
					}
				}
				
				groupItemsBox.options[j].selected |= matched;
				
				if (groupAntiItemsBox != null)
				{
					groupAntiItemsBox.options[j].selected &= !matched;
				}
			}
		
			if (groupItemsBox.onchange != null)
			{
				groupItemsBox.onchange();
			}	
		}
	}
}
