	var lastVal = "";
	var stregexp = "";
	var nrwr_DefaultText = "typ hier om de lijst kleiner te maken";

	function init(namesArray, selectID)
	{
		// set up arrays
		
		tempArray  = new Array();
		remvdArray = new Array();
		
		// get select object
		selObj = document.getElementById(selectID);
		
		if (namesArray.length == 0) {
			// set up temporary array
			for (i=0;i<selObj.options.length;i++)
			{
				namesArray[namesArray.length] = new Array(selObj.options[i].text,  selObj.options[i].value);
			}
		}
		
		// rebuild the list
		buildOptions(namesArray);
		
		// clear the input box
		document.getElementById("nrwr").value = nrwr_DefaultText;
		
		// clear the last typed value
		lastVal = "";
		
		// write the number of matches
		writeMatches();
	}
	
	function narrow(str)
	{
		// if the length of str is 0, bypass everything else
		if (str.length == 0)
		{
			buildOptions(namesArray);
			remvdArray.length = 0;
		}
		else
		{
			// clear tempArray
			tempArray.length = 0;
			
			// set up temporary array
			for (i=0;i<selObj.options.length;i++)
			{
				tempArray[tempArray.length] = new Array(selObj.options[i].text,  selObj.options[i].value);
			}
			
			// make case-insensitive regexp
			stregexp = new RegExp(str,"i");
			
			// remove appropriate item(s)
			if (lastVal.length < str.length)
			{
				for (j=selObj.options.length-1;j>-1;j--)
				{
					if (selObj.options[j].text.match(stregexp) == null)
					{
						// remove the item
						tempArray.splice(j,1);
					}
				}
			}
			
			// add appropriate item(s)
			else
			{
				// if a removed item matches the new pattern, add it to the list of names
				for (k=remvdArray.length-1;k>-1;k--)
				{
					tempName = remvdArray[k][0];
					if (tempName.match(stregexp) != null)
					{
						tempArray[tempArray.length] = new Array(remvdArray[k][0], remvdArray[k][1]);
					}
				}
				
				// sort the names array
				tempArray.sort();
			}
			
			// build the new select list
			buildOptions(tempArray);
		}
		
		// remember the last value on which we narrowed
		lastVal = str;
		
		// write the number of matches
		writeMatches();
	}
	
	function buildOptions(arrayName)
	{
		// clear the select list
		selObj.options.length = 0;
		
		// build the select list
		for (l=0;l<arrayName.length;l++)
		{
			selObj.options[l] = new Option(arrayName[l][0], arrayName[l][1])
		}
		
		// remember which items were removed
		buildRemvd();
	}
	
	function buildRemvd()
	{
		// clear the removed items array
		remvdArray.length = 0;
		
		// build the removed items array
		for (m=namesArray.length-1;m>-1;m--)
		{
			if (namesArray[m][0].match(stregexp) == null)
			{
				// remember which item was removed
				remvdArray[remvdArray.length] = new Array(namesArray[m][0], namesArray[m][1]);
				
			}
		}
	}
	
	function writeMatches()
	{
		if (selObj.options.length == 1)
		{
			document.getElementById("matches").innerHTML = "1 product";
		}
		else
		{
			document.getElementById("matches").innerHTML = selObj.options.length + " producten";
		}
	}
	