
	var chosen_city = '';
	var city_array = new Array();
	var zip_array = new Array();

	function searchBox(e, elem)
	{

		var charCode = e.keyCode;

		if(elem.value != "")
		{
			if(getNavi(charCode))
				return false;

			$('searchResults').style.display = 'block';
			new Ajax.Request('search.php',{

				method: 'post',
				parameters: 'q=' + elem.value,

				onComplete: function(transport) {
					updateSearchResults(transport.responseText);
				}

			})
		}
		else
		{
			chosen_city = '';
			$('searchResultLines').innerHTML = '';
			$('searchResults').style.display = 'none';
		}


	}

	function updateSearchResults(result)
	{
		var searchDiv = $('searchResultLines');

		if(result == 'FALSE')
		{
			chosen_city = '';
			$('searchResultLines').innerHTML = '';
			$('searchResults').style.display = 'none';
			return false;
		}

		city_array = result.split(";");
		zip_array = new Array();



		var html = '';
		chosen_city = '';

		if(city_array.length > 5)
		{
			searchDiv.style.overflow = 'auto';
			searchDiv.style.height = '125px';
			searchDiv.scrollTop = 0;

		}
		else
		{
			searchDiv.style.overflow = '';
			searchDiv.style.height = '';
		}


		var part_num=0;
		while (part_num < city_array.length)
		{
			var tmp = city_array[part_num].split(" ");
			zip_array[part_num] = tmp[0];

			html = html + '<div class="searchResult" onmouseover="mouseOverResult(this);" id="city_'+ part_num +'" onclick="mouseClickResult(this);">' + city_array[part_num] + '</div>';
		 	part_num+=1;
  		}


		searchDiv.innerHTML = html;




	}

	function getNavi(pressedKey)
	{


		// Down
		if(pressedKey == 40)
		{


			if(chosen_city === '' || chosen_city == (city_array.length - 1))
			{
				if(chosen_city === (city_array.length - 1))
					$('city_' + chosen_city).className='searchResult';

				chosen_city = 0;
				$('searchResultLines').scrollTop = 0;
			}
			else
			{
				$('city_' + chosen_city).className='searchResult';
				chosen_city = chosen_city + 1;
			}

			$('city_' + chosen_city).className='searchResult active';

			if(chosen_city > 2 && chosen_city != 0)
				$('searchResultLines').scrollTop = ((chosen_city - 2) * 24);


			return true;
		}

		// Up
		if(pressedKey == 38)
		{


			if(chosen_city === '' || chosen_city == 0)
			{

				if(chosen_city === 0)
					$('city_' + chosen_city).className='searchResult';

				chosen_city = (city_array.length - 1);
				$('searchResultLines').scrollTop = $('searchResultLines').scrollHeight;
			}
			else
			{
				if(chosen_city > 0)
				{
					$('city_' + chosen_city).className='searchResult';
					chosen_city = chosen_city - 1;
				}

			}

			$('city_' + chosen_city).className='searchResult active';

			if(chosen_city < (city_array.length - 2))
			$('searchResultLines').scrollTop = ((chosen_city - 2) * 24);


			return true;
		}

		// Escape
		if(pressedKey == 13)
		{

			if(chosen_city !== '')
			{
				window.location.href='by.php?postnr='+zip_array[chosen_city];
			}
			return true;
		}

		if(pressedKey == 27)
		{
			closeResults();
			return true;
		}

		return false;

	}

	function mouseOverResult(elem)
	{

		if(chosen_city !== '')
		{
			$('city_' + chosen_city).className='searchResult';
			chosen_city = '';
		}

		chosen_city = Number(elem.id.substr(5));
		elem.className='searchResult active';

	}

	function mouseClickResult(elem)
	{
		window.location.href='by.php?postnr='+zip_array[chosen_city];
	}

	function closeResults()
	{
		chosen_city = '';
		$('searchResultLines').innerHTML = '';
		$('searchResults').style.display = 'none';
	}