function UniqueAppend(ary, val)
{
	var i;
	
	for (i = 0; i < ary.length; i++)
	{
		if (ary[i] == val)
		{
			return ary;
		}
	}
	
	ary[ary.length] = val;

	return ary;
}

function RecreateMenus(ctl_cat, ctl_subcat, ctl_vendor)
{
	cat = ctl_cat.value;
	subcat = ctl_subcat.value;
	vendor = ctl_vendor.value;

	catlist = GenerateOptionList("", subcat, vendor, 1);
	subcatlist = GenerateOptionList(cat, "", vendor, 2);
	vendorlist = GenerateOptionList(cat, subcat, "", 3);
	
	SetOptionList(ctl_cat, catlist, cat, "Categories");
	SetOptionList(ctl_subcat, subcatlist, subcat, "Subcategories");
	SetOptionList(ctl_vendor, vendorlist, vendor, "Vendors");
}

function GenerateOptionList(first, second, third, idx)
{
	list = new Array();
	list.push("");
	if (first == "")
	{
		first = ".*";
	}
	
	if (second == "")
	{
		second = ".*";
	}
	
	if (third == "")
	{
		third = ".*";
	}

	re = "^(" + first + "):(" + second + "):(" + third + ")$";
	finder = new RegExp(re);
	finder.compile(re);

	var i;
	for (i = 0; i < categories.length; i++)
	{
		res = categories[i].match(finder);

		if (res != null)
		{
			list = UniqueAppend(list, res[idx]);
		}
	}

	list.sort();

	return list;
}	

function SetOptionList(selectbox, options, dflt, type)
{
	var i;
	for (i = 0; i < options.length; i++)
	{
		var value = options[i];
		var name = value;

		if (name == "")
		{
			name = "All " + type;
		}
		
		selectbox.options[i] = new Option(name, value);
		if (dflt != "")
		{
			selectbox.value = dflt;
		}
	}
	
	var j;
	// Clean out any extra options that might still be hanging around
	for (j = selectbox.options.length - 1; j >= i; j--)
	{
		selectbox.options[j] = null;
	}
}

function CheckSubmitValues(a, b, c)
{
	if (a.value == "" && b.value == "" && c.value == "")
	{
		alert("Please select at least one category, subcategory, or vendor");
		return false;
	}
	else
	{
		return true;
	}
}

