
	$(document).ready(function() {
		$("#kunduppgifter").bind("keypress", function(e) {
		  if (e.keyCode == 13) return false;
		});
		$("#addressSearch").keyup(function(event) {
			if (event.keyCode == '13') {
   			event.preventDefault();
				if($(".ulSearch").find(".li_hiC").length){
					fill($(".ulSearch").find(".li_hiC").html());
					addressLookUp($(".ulSearch").find(".li_hiC").attr("id"));
					$("#addressSearch").blur();					
				}					     			
 			}
 			else if(event.keyCode == '38' || event.keyCode == '40'){
 				var kids = $(".ulSearch").children().length;
 				if(kids){
					if(event.keyCode == '38') var hiC = kids;
					else var hiC = 1;
					if($(".ulSearch").find(".li_hiC").length){
						hiC = $(".ulSearch").find(".li_hiC").attr("rel");
  					$(".ulSearch li:nth-child("+hiC+")").removeClass("li_hiC");
						if(event.keyCode == '38'){
							hiC--;
							if(hiC < 1) hiC = kids;
						}
						else {
							hiC++;
							if(hiC > kids) hiC = 1;
						}
					}
					$(".ulSearch li:nth-child("+hiC+")").addClass("li_hiC");
 				}
 			}
 			else {
 				suggest($("#addressSearch").val(), $("#ajaxInputType").val());
 			}
		});
	});

	function suggest(inputString, inputType){
		if(inputString.length == 0) {
			$('#suggestions').fadeOut();
		} else {
		$('#addressSearch').addClass('load');
	    $.ajax({
	      type: "GET",
	      url: "/ajaxAddressSearch.phtml?searchString=" + inputString + "&addressType=" + inputType,
				dataType: "xml",
				cache: "false",
	      success: function(xml) {
	      	var nbrResults = $(xml).find("address").length;
	      	if(nbrResults > 0){
						// Special case: If only 1 result. Do the update instantly. Disabled atm ( == 0)
	      		if(nbrResults == 0){
							$('#addressSearch').removeClass('load');
							$('#suggestionsList').html("");
							$('#suggestions').fadeOut();
							$('#addressSearch').blur();
			        $(xml).find("address").each(function(){
			          var aid = $(this).find("id").text();
			          var aname = $(this).find("name").text();
								$('#addressSearch').val(aname);
								addressLookUp(aid);
							});
	      		}
	      		else {
							$('#suggestions').fadeIn();
							$('#addressSearch').removeClass('load');
							var newList = "<ul class=\"ulSearch\">";
							var liCount = 1;
			        $(xml).find("address").each(function(){
			          var aid = $(this).find("id").text();
			          var aname = $(this).find("name").text();
			          var areg = $(this).find("RegNumber").text();

								if($("#ajaxInputType").val() == "M"){
									newList = newList + "<li id=\"" + aid + "\" rel=\"" + liCount + "\" onClick=\"fill('" + areg + "'); addressLookUp(" + aid + ");\" onMouseover=\"fixHoverIn(this);\" onMouseout=\"fixHoverOut(this);\">" + areg + ": " + aname + "</li>";
								}
								else {
									newList = newList + "<li id=\"" + aid + "\" rel=\"" + liCount + "\" onClick=\"fill('" + aname + "'); addressLookUp(" + aid + ");\" onMouseover=\"fixHoverIn(this);\" onMouseout=\"fixHoverOut(this);\">" + aname + "</li>";
								}

								liCount++;
							});
							newList = newList + "</ul>";
							$('#suggestionsList').html(newList);
						}
					}
					else {
						$('#addressSearch').removeClass('load');
						$('#suggestionsList').html("");
						$('#suggestions').fadeOut();
					}
				}
			});
		}
	}

	function fill(thisValue) {
		$('#addressSearch').val(thisValue);
		setTimeout("$('#suggestions').fadeOut();", 300);
	}
	
	function fixHoverIn(elem){
		/*
		$("#suggestions").mouseover(function () {
			$(".ulSearch li").each(function() {
				$(this).removeClass("li_hiC");
			});
		});
		*/
		$(".ulSearch li").each(function() {
			$(this).removeClass("li_hiC");
		});
		$(elem).addClass("li_hiC");
			
	}
	
	function fixHoverOut(elem){
		$(elem).removeClass("li_hiC");
	
	}
	
