var curSelection = 0;
var countElements = 0;
var searchStatus = true;
selectColor = '#D61803';
var Color = new Array();
Color['AKTIV'] = '#C80084';
Color['EINRICHTUNG'] = '#00AE8D';
Color['HYGIENE'] = '#009CB8';
Color['KREATIV'] = '#ED550B';
Color['allgemein'] = '#D61803';
var choiceCat = 'allgemein';


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

	function choicePop(category) {
		colorElement = 'container_suche_zertifikate_' + category;
	 	selectColor = Color[category];
		//choiceCat  = category;
	}

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

		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 regex = new RegExp('https:(.*)');
		var match = regex.exec(window.location.href);
		var sslurl = (match != null) ? 'https://ssl.webpack.de/www.frankengmbh.de' : '';
		var searchParameter = document.getElementById(inputText).value;
		xHttp.open("GET",sslurl + "/AutoCompleteApi.php?cat=" + choiceCat + "&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) {
		   			 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) {

		// Ajax initialisieren
		var xHttp = getAjaxObject();

		if (!xHttp) return;

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

	function outPrice(inputText,element, event) {
		if (!event) event = window.event;

		// Nur die Zahlen unnötige Aufrufe vermeiden
		if ((event.keyCode >= 96 && event.keyCode <= 105) || (event.keyCode >= 48 && event.keyCode <= 57) || event.keyCode == 8) {

			// Ajax initialisieren
			var xHttp = getAjaxObject();

			if (!xHttp) return;

			// Suchen
			count = element.value;
			if (inputText != '' && count !='' && count !='0') {
				var regex = new RegExp('[0-9]+');
		    	var match = regex.exec(element.value);
		    	if (match != null) {
					xHttp.open("GET", "/AutoCompletePrice.php?" + inputText + "&count=" + count, true);
					xHttp.onreadystatechange = function () {
						if(xHttp.readyState == 4) {
							var data = xHttp.responseText;
							document.getElementById('price').innerHTML =  data + "</span>&nbsp;€</span>";
							delete xHttp;
						}
					}
					xHttp.send(null);
		    	}
			}
		}
	}
