var curSelection = 0;
var countElements = 0;
var searchStatus = true;


	function resetOnBlur(divOutput)
	{
		curSelection = 0;
		countElements = 0;
		searchStatus = true;
		document.getElementById(divOutput).style.display='none';

	}

	function getAjaxObject()
	{
		var xHttp;
		
		try
		{
			xHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				xHttp = false;
			}
		}
		if (!xHttp)
		{
			xHttp = new XMLHttpRequest();
		}
		
		return xHttp;
	}
	
	function mouseSelectRow(selection)
	{
		if (curSelection != 0)
		{
			document.getElementById('feld' + curSelection).style.backgroundColor = '#FFFFFF';
			document.getElementById('feld' + curSelection).style.color = '#000000';
		}
		curSelection = selection;
		document.getElementById('feld' + curSelection).style.backgroundColor = '#990000';
		document.getElementById('feld' + curSelection).style.color = '#FFFFFF';
	}
	
	function mouseClick(inputText, divOutput, selection)
	{
		curSelection = 0;
		document.getElementById(divOutput).style.display = 'none';
		document.getElementById(inputText).value = document.getElementById('feld' + selection).innerHTML;
	}
	
	function updateDivOutput(inputText, divOutput, dataArr)
	{
		var i = 0;
		var output = "";
		
		for (i=0; i<dataArr.length; i++)
		{
			if (dataArr[i] != '') output += '<div id="feld' + (i+1) + '" style="height:20px; cursor:default;" onmousedown="mouseClick(\'' + inputText + '\',\'' + divOutput + '\',' + (i+1) + ');" onmouseover="mouseSelectRow(' + (i+1) + ');">' + dataArr[i] + '</div>';
		}
		if (output != '')
		{
			document.getElementById(divOutput).innerHTML = output;
			document.getElementById(divOutput).style.display = '';
			curSelection = 0;
		} else {
			resetOnBlur(divOutput);
		}
		
	}
	
	
	function search(xHttp, table, field, inputText, divOutput)
	{
		var searchParameter = document.getElementById(inputText).value;
		var type = document.getElementById('type').value;
		xHttp.open("GET", "/AutoCompleteApi.php?type=" + type + "&field=" + field + "&searchParameter=" + searchParameter, true);
		xHttp.onreadystatechange = function ()
		{
			if(xHttp.readyState == 4)
			{
				var data = xHttp.responseText.split("|||");
				
				countElements = data.length;
				
				updateDivOutput(inputText, divOutput, data);
				
				delete xHttp;
				
			}
		
		}
		xHttp.send(null);		
	}
	
	
	function handleKeys(inputText, divOutput, event)
	{
		if (!event) event = window.event;
		// Nach unten
		if (event.keyCode == 40)
		{
			if (document.getElementById(divOutput).style.display == 'none') searchStatus = true;
			else {
				if (curSelection != 0)
				{
					document.getElementById('feld' + curSelection).style.backgroundColor = '#FFFFFF';
					document.getElementById('feld' + curSelection).style.color = '#000000';
				}
				curSelection++;
				if (curSelection > countElements) curSelection = countElements;
				document.getElementById('feld' + curSelection).style.backgroundColor = '#990000';
				document.getElementById('feld' + curSelection).style.color = '#FFFFFF';
				
				searchStatus = false;
			}
			
		} else if (event.keyCode == 38)
		{
			
			if (curSelection != 0)
			{
				document.getElementById('feld' + curSelection).style.backgroundColor = '#FFFFFF';
				document.getElementById('feld' + curSelection).style.color = '#000000';
			}
			curSelection--;
			if (curSelection < 1)
			{
				curSelection = 0;
			} else {
				document.getElementById('feld' + curSelection).style.backgroundColor = '#990000';
				document.getElementById('feld' + curSelection).style.color = '#FFFFFF';
			}
			
			searchStatus = false;
			
		} else if (event.keyCode == 13)
		{

			if (curSelection != 0) document.getElementById(inputText).value = document.getElementById('feld' + curSelection).innerHTML;
			document.getElementById(divOutput).style.display='none';
			
			searchStatus = false;
			
		} else {
			searchStatus = true;
		
			if (document.getElementById(inputText).value != '')
			{
				resetOnBlur(divOutput);
			}
		
			
		}
		
	}
	
	
	function doSearch(inputText, divOutput)
	{
		
		// Ajax initialisieren
		var xHttp = getAjaxObject();
		
		if (!xHttp) return;
		
		// Suchen
		if (document.getElementById(inputText).value != '')
		{
			if (searchStatus == true) search(xHttp, '', '', inputText, divOutput);
		} else {
			resetOnBlur(divOutput);
		}
		
	}