function prepProductList() {
	if(!document.getElementById) return false;
	if(!document.getElementById("product-select")) return false;
	if(!document.getElementsByTagName) return false;
	if(!document.getElementById("product-forms")) return false;
	
	//hide unnecessary forms
	var productForms = document.getElementById("product-forms");
	productDivs = productForms.getElementsByTagName("DIV");
	
	for (var i = 0; i < productDivs.length; i++)
	{
		if(productDivs[i].className.indexOf("pf-Box") != -1)
		{
			productDivs[i].className += " hidden";
		}
	}
	
	var productList = document.getElementById("product-select");
	var product = productList.getElementsByTagName("LI");
	
	for(var i = 0; i < product.length; i++)
	{
		var checkBox = product[i].getElementsByTagName("INPUT")[0];
		if(checkBox.className)
		{
			checkBox.className += " hidden";
		}
		else {
			checkBox.className = "hidden";
		}
		
		if(checkBox.checked)
		{
			if(product[i].className.indexOf("selected") == -1)
			{
				if(product[i].className)
				{
					product[i].className += " selected";
				}
				else {
					product[i].className = "selected";
				}
			}
			
			var checkBoxId = checkBox.getAttribute("id");
			checkBoxId = "pf-"+checkBoxId;
			if(!document.getElementById(checkBoxId)) return false;
			var box = document.getElementById(checkBoxId);
			
			if(box.className.indexOf("hidden") != -1)
			{
				if(box.className == "hidden")
				{
					box.className = "";
				}
				else {
					box.className = box.className.replace(" hidden", "");
					box.className = box.className.replace("hidden ", "");
				}
			}
		}
		
		var label = product[i].getElementsByTagName("LABEL")[0];
		var linkName = label.getElementsByTagName("SPAN")[0].firstChild.nodeValue;
		while(linkName.indexOf(" ") != -1)
			linkName = linkName.replace(" ","_");
		linkName = "#"+linkName;
		if(label.className)
			label.className += " hidden-label";
		else
			label.className = "hidden-label";
		var labelHTML = label.innerHTML;
		var link = document.createElement("a");
		link.innerHTML = labelHTML;
//		link.setAttribute("href",linkName);
		product[i].appendChild(link);
		
		link.onclick = function() {
			labelOnclick(this);
		}
	}
	
	
}

function labelOnclick(label) {
	
	var parentLi = label.parentNode;
	if(parentLi.nodeName != "LI") return false;
	var checkBox = parentLi.getElementsByTagName("INPUT")[0];
	if(!checkBox && checkBox.getAttribute("type") != "checkbox") return false;
	
	var checkBoxId = checkBox.getAttribute("id");
	checkBoxId = "pf-"+checkBoxId;
	if(!document.getElementById(checkBoxId)) return false;
	var box = document.getElementById(checkBoxId);
	
	if(parentLi.className.indexOf("selected") == -1)
	{
		if(parentLi.className != "")
		{
			parentLi.className += " selected";
		}
		else {
			parentLi.className = "selected";
		}
		if(!checkBox.checked)
			checkBox.checked = true;
		
		if (box.className.indexOf("hidden") != -1)
		{
			if(box.className == "hidden")
			{
				box.className = "";
			}
			else {
				box.className = box.className.replace(" hidden", "");
				box.className = box.className.replace("hidden ", "");
			}
		}
	}
	else {
		if(parentLi.className == "selected")
		{
			parentLi.className = "";
		}
		else {
			parentLi.className = parentLi.className.replace(" selected", "");
			parentLi.className = parentLi.className.replace("selected ", "");
		}
		if (checkBox.checked)
			checkBox.checked = false;
			
		if (box.className.indexOf("hidden") == -1)
		{
			if(box.className)
			{
				box.className += " hidden";
			}
			else {
				box.className = "hidden";
			}
		}
	}
	
}


function addLoadEvent(func) {
	var oldOnLoad = window.onload
	if (typeof window.onload != 'function') 
	{
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldOnLoad();
			func();
		}
	}
}

addLoadEvent(prepProductList);
