var elementname = "";
var httpObject = CreateHTTPObject();
var pagename = "";

function CreateHTTPObject()
{
	if (window.ActiveXObject) 
	{
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) 
	{
		return new XMLHttpRequest();
	}
	else 
	{
		alert("Your browser does not support AJAX.");
	}
	return null;
}

function MakeAjaxRequestAjaxSearch(url, element, page)
{
	elementname = element;
	pagename = page;
	
	if (httpObject != null)
	{
		httpObject.open("GET", url+"&params="+document.forms[0].elements[elementname].value, true);
		httpObject.send(null);

		httpObject.onreadystatechange = handleStateChangedAjaxSearch;
	}
}

function handleStateChangedAjaxSearch()
{
	if (httpObject.readyState == 4)
	{
		//alert(httpObject.responseText);
		//alert(document.forms[0].elements[elementname].value);
		showBox(document.forms[0].elements[elementname], httpObject.responseText);
	}
}

function showBox(element, values)
{
	//alert(values);
	removeSuggestBox();
	var result = eval(values);
	var hasResults = false;
	
	var SuggestBox = document.createElement("ul");
	SuggestBox.id="suggest";
	
	var total = 0;
	
	for (var j = 0; j < result.length; j++)
	{
		if (result[j] != null && result[j] != undefined)
		{
			if (result[j].length > 0)
			{
				hasResults = true;
				for (var i=0; i < result[j].length; i++)
				{
					if (total != 8)
					{
						var node = document.createElement("li");
						SuggestBox.appendChild(node);
						node.className="child";
						node.innerHTML = result[j][i]['total'] + " - " + result[j][i]['omschrijving'] ;
						node.id=result[j][i]['id'];
						node.onclick=function(){handleClick(this.id);};
						node.onmouseover=function(){this.className='childmouseon';};
						node.onmouseout=function(){this.className='childmouseout';};
						
						total++;
					}
				}
			}
		}
	}
	if (!hasResults)
	{
		var node = document.createElement("li");
		node.className="child";
		node.innerHTML = "Uw zoekactie leverde geen resultaten op."
		
		SuggestBox.appendChild(node);
	}
	
	element.parentNode.appendChild(SuggestBox);
}

function handleClick(id)
{
	window.top.location.href = '?action=show&page='+pagename+'&id='+id;
}

function removeSuggestBox()
{
	var SuggestBox = document.getElementById("suggest");
	if (SuggestBox != null && SuggestBox != 'undefined')
	{
		SuggestBox.parentNode.removeChild(SuggestBox);
	}
}

