var curSelection = 0;
var countElements = 0;
var searchStatus = true;
selectColor = '#99CC00';
var choiceCat = 'allgemein';


	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;
		form = document.getElementById('searchform');
		form.submit();

	}

	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(table, field, inputText, divOutput, lang)
	{
		var searchParameter = document.getElementById(inputText).value;
        if (searchParameter.length < 3) return;

       	new Ajax.Request('/de/cgi-bin/shop/front/autocomplete.cgi',{
           method: 'post',
           parameters: {action: 'show', sbeg: searchParameter, lang: lang},
           onSuccess: function(transport){
               var response = transport.responseText || 0;

               var arr = response.split('|||');
               if (response) updateDivOutput(inputText, divOutput, arr);

           },
           onFailure: function(){ alert('Something went wrong...') }
       });
	}


    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, lang) {

		// Suchen
		if (document.getElementById(inputText).value != '') {
			if (searchStatus == true) search('', '', inputText, divOutput, lang);
		} else {
			resetOnBlur(divOutput);
		}
	}
