/*
	category functions
*/

if (!this.Categories) 
{
    var Categories = new function () {
		// public variable
		this.catData = '';

		// private function getCategories
		function getCategories(id, selectid, leavefirst)
		{
			removeAllSubCats(selectid, leavefirst);
			var myData = JSON.parse(Categories.catData);	
			for (i=0;i<myData.length;i++)
			{
				if (myData[i].cat_parent_id==id)
				{
					appendSubCat(myData[i].cat_id, myData[i].cat_title,selectid);
				}
			}
		}

		// private function removeAllCubCats
		function removeAllSubCats(selectid, leavefirst)
		{
			var elSel = this.document.getElementById(selectid);
			if (elSel)
			{
				if (leavefirst)
				{
					while (elSel.length > 1) { elSel.remove(elSel.length - 1); }
				}
				else
				{
					while (elSel.length > 0) { elSel.remove(elSel.length - 1); }
				}
			}
		}

		// private function appendSubCat
		function appendSubCat(value, text, selectid)
		{
			// get the container
			var elSel = document.getElementById(selectid);
		  
			if (elSel)
			{
				// create new element
				var elOptNew = document.createElement('option');
				text = text.replace("&#39;","'");

				elOptNew.text = text;
				elOptNew.value = value;

				try 
				{
					// add it to subcat select
					elSel.add(elOptNew); // IE only
				}
				catch(ex) 
				{
					elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
				}
			}
		}		
         
		this.getSubCat = function (id, selectid, leavefirst) {
			return getCategories(id, selectid, leavefirst);
		};
    };
}

function zoekBalkCategoryChanged(obj)
{
	var selectedCat = obj.options[obj.selectedIndex].value;
	Categories.getSubCat(selectedCat,'sc', true);
	setCookie("vw:" + obj.id, selectedCat, null);
}

function zoekBalkSubCategoryChanged(obj)
{
	var selectedSubCat = obj.options[obj.selectedIndex].value;
	setCookie("vw:" + obj.id, selectedSubCat, null);
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays)
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate)
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=")
		if (c_start!=-1)
		{ 
			c_start=c_start + c_name.length+1 
			c_end=document.cookie.indexOf(";",c_start)
			if (c_end==-1) c_end=document.cookie.length
			return unescape(document.cookie.substring(c_start,c_end))
		} 
	}
	return null
}