	var curSelection = 0;
	var countElements = 0;
	var searchStatus = true;
	selectColor = '#B53703';


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

	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 = selectColor;
		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 = "";


		if (dataArr[0] == 'no') {
			dataArr[0] = 'Kein Ergebnis. Volltextsuche starten.';
			selectColor = '#FF6666';
			//nhn output += '<div id="feld' + (i+1) + '" style="height:20px; cursor:default;" onmousedown="largeSeach();" onmouseover="mouseSelectRow(' + (i+1) + ');">' + dataArr[i] + '</div>';
			output += '<div id="feld' + (i+1) + '" style="height:20px; cursor:default;" onmousedown="largeSeach();" onmouseover="mouseSelectRow(' + (i+1) + ');">' + dataArr[i] + '</div>';
		} else {
			selectColor = '#B53703';
			for (i=0; i<dataArr.length; i++) {
				if (dataArr[i] != '') output += '<div id="feld' + (i+1) + '" style="padding:2px 0;border-bottom:1px solid #ccc; 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 largeSeach() {
		document.location.href  = 'shop_main.cgi?func=searchdo&wkid='+wkid+'&sbeg='+encodeURIComponent($('sbeg').value);
	}


	function search(table, field, inputText, divOutput)
	{
		var search = document.getElementById(inputText).value;
		var posting = ("https:" == document.location.protocol) ? "/linsen-meile.de/shop/AutoCompleteApi.php?searchParameter=" : "/shop/AutoCompleteApi.php?searchParameter=";
		ob = new Ajax.Request(posting, {
			method: 'post',
           	parameters: {searchParameter: search},
			onSuccess: function(transport) {
				var response = transport.responseText || 'no'
	           	var data = response.split('|||');
	           	// need for handle Keys
	           	countElements = data.length;
				updateDivOutput(inputText, divOutput, data);
			},

			onFailure: function(){
				alert('Suchfunktion Error');
			}
		});
		console.log(ob);

	}

    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) {
		   			 if (document.getElementById('feld' + curSelection)) {
						document.getElementById('feld' + curSelection).style.backgroundColor = '#FFFFFF';
						document.getElementById('feld' + curSelection).style.color = '#000000';
					 } else {
                        curSelection = 1;
                       	document.getElementById('feld' + curSelection).style.backgroundColor = '#FFFFFF';
						document.getElementById('feld' + curSelection).style.color = '#000000';
					 }
				}
				curSelection++;
				if (curSelection > countElements) curSelection = countElements;

                if (document.getElementById('feld' + curSelection)) {
					document.getElementById('feld' + curSelection).style.backgroundColor = selectColor;
					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 = selectColor;
	        	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, event) {

		if (!event) event = window.event;

		if (event.keyCode != 38 && event.keyCode != 40) {
			if (document.getElementById(inputText).value != '') {
				search('', '', inputText, divOutput);
			} else {
				resetOnBlur(divOutput);
			}
		}
	}
