	var theselect;
	var includeLocation = false;
	var locationlastValue = "Område, kommune, by";
	var roadlastValue ="Vej";
	
	function findLocation(evt,input,select,isroad,nomap) {
		
		var boligFaneId = document.getElementById('faneblad_bolig_id');
		
		selectid=select;
		var val = input.value;
		var nVer = navigator.appVersion;
		var nAgt = navigator.userAgent;
				
		theselect = document.getElementById(select);
		if (evt.keyCode==9) {
		}else if (evt.keyCode==13) { //enter
			var hidden = isroad ? 'road' : 'location';
			var lastVal = isroad ? roadlastValue : locationlastValue;
			handleAutomaticSelection(theselect, hidden,input.id,nomap, lastVal,true);
		} else if (evt.keyCode==40) { // arrowdown {
			theselect.focus();
			if (theselect.options.length>0) {
				theselect.options[0].selected = true;
			}
		} else {
			if (val.length > 1) {
				var poststring = "";
				removeAllOptions(theselect);
				if (isroad) {
					postString = 'road=';
					postString += encodeIt(val);
					var where = document.getElementById('location').value;
										
					if (where) {						
						postString += "&where=";	
						postString += encodeIt(where);
					}
				} else {
					postString = 'where=';
					postString += encodeIt(val);					
				}
				
				closeLocationPicker();
				
	  			xmlPost("/locationsearch",postString,gotLocationAnswer);
	  			
			}
		}
		
	setFanebladLayer();
		
	}	
	
	function decodeIt(text) {
		
		var nVer = navigator.appVersion;
		var nAgt = navigator.userAgent;

		if (nAgt.indexOf('MSIE')>-1) {
			 return text;
		 } else if (nAgt.indexOf('Firefox')>-1) {			 
			 return unescape(text);
		 } else if (nAgt.indexOf('Safari')>-1) {
			 return text;
		 }	
	}
	
	function encodeIt(text) {
		var nVer = navigator.appVersion;
		var nAgt = navigator.userAgent;

		if (nAgt.indexOf('MSIE')>-1) {
			return escape(text);
		 } else if (nAgt.indexOf('Firefox')>-1) {		 			 
			 return encodeURI(text);	
		 } else if (nAgt.indexOf('Safari')>-1) {
			return escape(text);
		 }				 
	}
	
	function gotLocationAnswer() {
		
		if (req.readyState == 4) {
			if (req.status == 200) {	
				var result = req.responseText;	
				
				if (result != '') {					
					parseLocationXML(result);
				}	
			} else {
				alert("There was a problem retrieving the XML data:\n" + req.statusText);
			}
		}
    }
	
	
	function parseLocationXML(xml) {
		
		try { //Internet Explorer
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async="false";
			xmlDoc.loadXML(xml);
		}
		catch(e) {
			try { //Firefox, Mozilla, Opera, etc.
				parser=new DOMParser();
				xmlDoc=parser.parseFromString(xml,"text/xml");
		  	}
		  	catch(e) {
		  		alert('3:'+e.message);
		  		return;
		  	}
		}
		var items;  
		var	root = xmlDoc.getElementsByTagName('locations')[0];
		if (root) {
			items = parseLocation(root);
		} else {
			root = xmlDoc.getElementsByTagName('roads')[0];
			if (root) {
				items = parseRoad(root);
			}
		}
		if (items && items.length > 0) {			
			openLocationPicker();
		} else {
			closeLocationPicker();
		}
	}
	
	function parseLocation(root) {	
		var items = root.getElementsByTagName('location');
				
		var rawpNr = document.getElementById('hvor').value;
		var newPnr = rawpNr.substring(rawpNr.indexOf(",")+2,rawpNr.length);
		
		for (i=0  ; i < items.length ; i++) {
			var item = items[i];
			var areatype = item.getElementsByTagName('areatype')[0].firstChild.nodeValue;
			var areaid = item.getElementsByTagName('areaid')[0].firstChild.nodeValue;
			var areaname = item.getElementsByTagName("areaname")[0].firstChild.nodeValue;
			var east = item.getElementsByTagName("areaeast")[0].firstChild.nodeValue;
			var west = item.getElementsByTagName("areawest")[0].firstChild.nodeValue;
			var north = item.getElementsByTagName("areanorth")[0].firstChild.nodeValue;
			var south = item.getElementsByTagName("areasouth")[0].firstChild.nodeValue;
			areaname = urldecodeing(areaname);
			var caseSearch;
			
			if (theselect!=null) {
			addOption(theselect,areaname,areatype+";"+areaid+";"+east+";"+west+";"+north+";"+south);
			}
			
			if (item.getElementsByTagName("fromcasesearch")!=null &&
					item.getElementsByTagName("fromcasesearch")[0]!=null) {
				
				if (newPnr!=null) {					
					var firstTwo = areaid.substring(0,2);					
					if (newPnr.indexOf(firstTwo)!=-1) {						
						var parameters = 'POSTNUMMER;'+areaid+';'+east+';'+west+';'+north+';'+south;						
						selectLocationSagsnummer(parameters, 'hvor', 'location', true);
					}
				}				
			} 
			/* why are we setting these ?? It will never be the right values, since it is just the last node in xml-doc*/ 
			if (false && document.getElementById('x_min')) {
				document.getElementById('x_min').value = east;
				document.getElementById('x_max').value = west;
				document.getElementById('y_min').value = south;
				document.getElementById('y_max').value = north;
				
			}
		}
		return items;
	}
	
	function parseRoad(root) {
		var items = root.getElementsByTagName('road');
		for (i=0  ; i < items.length ; i++) {
			var item = items[i];
			var road = item.getElementsByTagName('roadname')[0].firstChild.nodeValue;
			var city = item.getElementsByTagName('cityname')[0].firstChild.nodeValue;
			var zip = item.getElementsByTagName('zipcode')[0].firstChild.nodeValue;
			var east = item.getElementsByTagName("areaeast")[0].firstChild.nodeValue;
			var west = item.getElementsByTagName("areawest")[0].firstChild.nodeValue;
			var north = item.getElementsByTagName("areanorth")[0].firstChild.nodeValue;
			var south = item.getElementsByTagName("areasouth")[0].firstChild.nodeValue;
			var kommune = item.getElementsByTagName("kommunenavn")[0].firstChild.nodeValue;
			kommune=urldecodeing(kommune);
			city = urldecodeing(city);
			addOption(theselect,urldecodeing(road)+", "+zip+" "+city+" ("+kommune+")",road+";"+zip+";"+east+";"+west+";"+north+";"+south);
			//addOption(theselect,urldecodeing("SKOD")+", "+zip+" "+city,road+";"+zip+";"+east+";"+west+";"+north+";"+south);
			//I use them in the listsearch 
			if (document.getElementById('x_min')) {
					document.getElementById('x_min').value = east;
					document.getElementById('x_max').value = west;
					document.getElementById('y_min').value = south;
					document.getElementById('y_max').value = north;
			}
		}
		
		return items;
	}


	function openLocationPicker() {		
		var select = document.getElementById(selectid);
		select.parentNode.style.display = 'block';
	}
	
	function closeLocationPicker() {
			
		var select = document.getElementById(selectid);		
		select.parentNode.style.display = 'none';		
				
				
	}	
	
	function selectLocationSagsnummer(sel,inputtext,inputvalue, nomap) {  
	
		setFanebladLayer();
				
	    var selValue = sel;
		var thething = selValue.split(";");			
		
		// this is a firefox hack
		// we need to decode the encoded name for firefox
		// else no street names containing æ, ø or å will reach the ASPFormBuilder
		var decodedStreet = decodeIt(thething[0]);
		
		for (i = 1;i<thething.length;i++) {
			decodedStreet+=";"+thething[i];
		}			
		
		document.getElementById(inputvalue).value = decodedStreet;						
		closeLocationPicker();
		includeLocation = true;
		if(inputvalue == 'location'){
			locationlastValue = sel.value;
			wherelastValue = sel.text;
		}else{
			roadlastValue = sel.value;
		}
		
		adjustMap(inputvalue, nomap);
	
		resetFanebladLayer();
	
	}



	function doSelectLocation(selValue,selText,inputtext,inputvalue,nomap) {
		
		//alert('#260# '+selValue+':'+selText+':'+inputtext+''+inputvalue+''+nomap);
		document.getElementById(inputtext).value = selText;		
		
		var thething = selValue.split(";");			
		
		// this is a firefox hack
		// we need to decode the encoded name for firefox
		// else no street names containing æ, ø or å will reach the ASPFormBuilder
		var decodedStreet = decodeIt(thething[0]);
		
		for (i = 1;i<thething.length;i++) {
			decodedStreet+=";"+thething[i];
		}			
		
		document.getElementById(inputvalue).value = decodedStreet;				
		
		if (inputvalue == 'location' && document.getElementById("location_text")) {
			document.getElementById("location_text").value = selText;			
		}	
		
		closeLocationPicker();
		includeLocation = true;
		if(inputvalue == 'location'){
			locationlastValue = selValue;
			wherelastValue = selText;
			clearRoad('road','vej');
		}else{
			roadlastValue = selValue;
		}
		
		adjustMap(inputvalue, nomap);
		resetFanebladLayer();
	}
	
	function selectLocation(evt,sel,inputtext,inputvalue, nomap) {  //alert('#167#');
		setFanebladLayer();
		
		if (evt.type == 'click' || evt.keyCode==13) { // enter or click
			doSelectLocation(sel.value, sel.options[sel.selectedIndex].text, inputtext, inputvalue, nomap);
		} else if (evt.keyCode==27) { // escape
			includeLocation = false;
			closeLocationPicker();
			var where = document.getElementById(inputtext);
			where.focus();
			where.value = where.defaultValue;
			document.getElementById(inputvalue).value = '';
		} 
	}
	
	function setFanebladLayer() {
		
		var boligFaneId = document.getElementById('faneblad_bolig_id');
		
		if (boligFaneId!=null) {
 			boligFaneId.style.position = "relative";
  			boligFaneId.style.zIndex = -1;
			}		
	}
	
	function resetFanebladLayer() {
		
		var browserV = navigator.appVersion;
				
		var boligFaneId = document.getElementById('faneblad_bolig_id');
		
		if (boligFaneId!=null) {
 			boligFaneId.style.position = "";
  			boligFaneId.style.zIndex = 1;
			}
		
		if (browserV.indexOf("MSIE")!=-1) {
			if (!browserV.indexOf("MSIE 6")!=-1) {
				closeLocationPicker();
			}			
		}	
	}
	
	function doSimpleLocationValidate(input) {
		var s=input.value;
		var l = s.length;
		if (l==0 || document.getElementById('location').value == '') {
			s = input.defaultValue;
		}
		input.value = s;
	}

	function doLocationValidate(input,valueid,divid,nomap) {
		closetime(divid);
		var valinput = document.getElementById(valueid);
		var def1 = (valueid == 'road') ? 'Vej' : 'Kortudsnit';
		var def2 = (valueid == 'road') ? 'Bogus' : 'By, postnr, kommune';
		var s = input.value;
		var lastVal = (valueid == 'road') ? roadlastValue : locationlastValue;		
		var value = document.getElementById(valueid).value;	
		var locationTextInput = document.getElementById("location_text");
		var locationText = "";
		
		if (input.id == 'hvor' && locationTextInput) {
			locationText = locationTextInput.value;					
		}		
						
		if (locationText == s && s != "") {
			return;
		}
				
		var select = document.getElementById(divid).children[0];
				
		if ((s != def1 && s != def2) || s == "") {
			document.getElementById(valueid).value = "";
			
			if (input.id == 'hvor' && locationTextInput) {
				locationTextInput.value = "";
			}
			
			input.value = document.getElementById(valueid+'default').value;		
		}
		if(nomap && input.value == document.getElementById(valueid+'default').value){
			startSearch(nomap);
		}
		
	}

	function handleAutomaticSelection(select,valueid,textid,nomap,lastvalue,force) {
		if (select.children.length > 0 && select.selectedIndex < 0 && select.options.length > 0) {
			if (select.options[0].value != lastvalue || force) {
				doSelectLocation(select.options[0].value, select.options[0].text, textid, valueid, nomap);
			}
			closeLocationPicker();
			return select.options[0].text;
		} else {
//			if (lastvalue == document.getElementById(valueid).value) {
//				return document.getElementById(textid).value;
//			}	
		}
		return '';
	}
	
	function keepItOpen(input) {
		cancelclosetime(input);	
	}

	function adjustMap(inputtext,nomap) {
		//alert('#393# '+inputtext+':'+nomap)
		var value = document.getElementById(inputtext).value;			
		var thething = value.split(";");					
		document.homesearchform.x_min.value = thething[2];
		document.homesearchform.x_max.value = thething[3];		
		document.homesearchform.y_min.value = thething[5];		
		document.homesearchform.y_max.value = thething[4];
		if(nomap){ 
			startSearch(nomap);
		} else {			
			var tempZoom = lastZoomLevel;
			var extent = new KrakMap.Extent(thething[4], thething[5], thething[2], thething[3]);		
			tileMap.setMapViewExtent(extent);
			if(tempZoom == tileMap.getZoomLevel()){
				startSearch(nomap); //alert('#247# startSearchFromlocation'); 
			}
		}		
	}
	
	function adjustMapByCoords(value) {
		var thething = value.split(";");		
		var extent = new KrakMap.Extent(thething[2], thething[3], thething[0], thething[1]);		
		tileMap.setMapViewExtent(extent);
	}

	
	function clearRoad(valueid,textid) {
		document.getElementById(valueid).value = '';
		document.getElementById(textid).value = document.getElementById(valueid+'default').defaultValue;
	}