var lastSearchVal = "";
var sIndex = 0;
jQuery(document).ready(function() {
	$("#searchField").keydown(function(e) {
		var unicode = GetKeyCode(e);
		if (unicode == 13) {
			var ref2 = document.getElementById("a"+sIndex);
    			if(ref2){
    			var ref_field = document.getElementById("searchField");
    			if(ref_field){
    			  ref_field.value = ref2.innerHTML;
    			}
			}
		}
	});

	$("#searchField").keyup(function(e) {
		var ref_field = document.getElementById("searchField");
		var val = ref_field.value;
		if (val != lastSearchVal && val != '' && val != 'undefined') {
			var url = "srv.do?op=show_search_completions&q="+val;
			$.ajax({
			  url: url,
			  cache: false,
			  success: function(html){
				$("#ajaxSearchResult").css("display","block");
				$("#ajaxSearchResult").html(html);
			  }
			});		
		}
		lastSearchVal = val;

		var unicode = GetKeyCode(e);
		if (unicode == 40) {			
			if (sIndex < maxIndex) {
				sIndex = sIndex + 1;
				if (sIndex > 1) {
					$("#a"+(sIndex-1)).css("color","#000");
				}
				$("#a"+sIndex).css("color","#D9077E");
			}
		}
		if (unicode == 38) {
			sIndex = sIndex - 1;
			if (sIndex > -1) {
				$("#a"+(sIndex+1)).css("color","#000");
			}
			$("#a"+sIndex).css("color","#D9077E");
		}
		if (sIndex < 0) {
			sIndex = 0;
		}		
		if (unicode != 40 && unicode != 38) {
			sIndex = 0;
		}
		
		return;
	});
});

function submitAjaxSearchValue(val) {
	var ref = document.getElementById("searchForm");
	ref.search.value=val;
	ref.submit();
}

function GetKeyCode(e) {
	if (e) {
		return e.charCode ? e.charCode : e.keyCode;
	}
	else {
		return window.event.charCode ? window.event.charCode : window.event.keyCode;
	}
}

document.onkeydown = Tastendruck;
var keyCode = 0;
function Tastendruck (Ereignis) {
	keyCode = GetKeyCode(Ereignis);
}

function checkIfUpArrow(){
	if (keyCode == 38) {
		return false;
	}
}
