function toggleCategoryPaletteVisibility(link) {
	doShowCategories = readCookie("muShowCategories");
	if (doShowCategories == null) doShowCategories = 1; // if no value, assume was visible
	thePalette = document.getElementById("productCategories");
	if (thePalette) {
		newShowFlag = (parseInt(doShowCategories) + 1) % 2;
		conditionalShow(newShowFlag, thePalette, link);
		createCookie("muShowCategories", newShowFlag, 30);

/*		if (doShowCategories == 1) {
			// hide
			thePalette.style.display = "none";
			toggleText(link, "show");
			createCookie("muShowCategories", ((parseInt(doShowCategories) + 1) % 2), 30);
		} else {
			// show
			thePalette.style.display = "block";
			toggleText(link, "hide");
			createCookie("muShowCategories", ((parseInt(doShowCategories) + 1) % 2), 30);
		}
*/
	}
}

function conditionalShow(status, catPalette, catLink) {
	if (status == 0) {
		catPalette.style.display = "none";
		toggleText(catLink, "show filter");
	} else {
		catPalette.style.display = "block";
		toggleText(catLink, "hide filter");
	}
}

function toggleText(link, newText) {	
	if (link.childNodes.length > 1) {
		link.normalize(); // ensure no intervening whitespace nodes
	}
		// this assumes anchor contains only text nodes (otherwise there would be intervening elements)
	if (link.firstChild.nodeType == Node.TEXT_NODE) {
//		link.replaceChild(document.createTextNode(((link.firstChild.nodeValue == "hide") ? "show" : "hide")), link.firstChild);
		link.replaceChild(document.createTextNode(newText), link.firstChild);
	} else {
		alert("firstChild.nodeType: " + link.firstChild.nodeType);
	}

/* 	while (link.hasChildNodes()) { */
/* 		link.removeChild(link.lastChild); */
/* 	} */
	
}

function init(linkName) {
	theLink = document.getElementById(linkName);
	thePalette = document.getElementById("productCategories");
	if (theLink && thePalette) {
		initShow = readCookie("muShowCategories");
		if (initShow == null) initShow = 1; // by default, show (note, this _doesn't_ set cookie!)
		conditionalShow(initShow, thePalette, theLink);
//		toggleCategoryPaletteVisibility(theLink);
		
	}
}

function changeAllChecklist(ls, onoff) {
	for (var i=0; i<ls.length; i++) {
		ls[i].checked = onoff;
	}
}	


function changeAllCats(onoff) {
// 	var theCats = getCategories();
// 	for (var i=0; i<theCats.length; i++) {
// 		theCats[i].checked = onoff;
// 	}
	changeAllChecklist(getCategories(), onoff);
}

function changeAllLics(onoff) {
	changeAllChecklist(getLicenses(), onoff);
}

function getCategories() {
	return document.getElementsByName("checkedCategories[]");
}

function getLicenses() {
	return document.getElementsByName("checkedLicenses[]");
}

function selectAllCats() {
	changeAllCats(true);	
}

function selectNoCats() {
	changeAllCats(false);
}

function selectAllLics() {
	changeAllLics(true);
}

function selectNoLics() {
	changeAllLics(false);
}