		var geocoder = new GClientGeocoder();
		var places;
		var map;
		var inCache=false;
		var marker;
		var mapInitialized=false;
    var searchString;
    var resultList;
		
		function load() {
	      if (GBrowserIsCompatible()) {
	        map = new GMap2(document.getElementById("map"));
			map.setCenter(new GLatLng(52.515023, 13.473312), 13);
			map.disableDragging();
			map.disableDoubleClickZoom();
			var mapControl = new GMapTypeControl();
			map.addControl(mapControl);
			map.addControl(new GSmallMapControl());
		  }
	    }
		
		
		
		function setCache(toSet){
			var httpSetter;
			if(window.XMLHttpRequest){
				httpSetter = new XMLHttpRequest();
			}else if(window.ActiveXObject){
				httpSetter = new ActiveXObject("Microsoft.XMLHTTP");
			}
			
			try{
				httpSetter.open("POST" , "/acp/lib/gmapscache.php" , true);
				httpSetter.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				httpSetter.send("address="+toSet[1]+"&lat="+toSet[2]+"&lon="+toSet[3]+"&country_code="+toSet[4]+"&search_string="+searchString+"&type=set");
			}catch(e){
				alert(e);
			}
		}
		
		function lookupGmapsCache(){
			var http = null;
			if(window.XMLHttpRequest){
				http = new XMLHttpRequest();
			}else if(window.ActiveXObject){
				http = new ActiveXObject("Microsoft.XMLHTTP");
			}
			
			try{
				http.open("POST" , "/acp/lib/gmapscache.php" , false);
				//alert("open done");
				http.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				//alert("setRequestHeader");
				http.send("val="+searchString+"&type=get");
				//alert("send done");
				if(http.responseText != "nope"){
					var cacheJSON=http.responseText;
					try{
					//alert(cacheJSON);
					cacheJSON=eval("("+cacheJSON+")");
					inCache=true;
					}catch(e2){
						alert(e2);
					}
				}else{
					inCache=false;
				}
			}catch(e){
				alert(e);
			}
			
			if (inCache){
				//alert('in Cache');
				cnt=cacheJSON.length;
				for (var i=0; i<cnt;i++){
					place=new Array();
					place[0]=i;
					place[1]=cacheJSON[i].gmaps_string;
					place[2]=cacheJSON[i].lat;
					place[3]=cacheJSON[i].lon;
					place[4]=cacheJSON[i].country_code;
					places.push(place);
					if(i<cnt-1){
						setList(place, false);
					}else{
						setList(place, true);
					}
				}
			}
		
		}
		
		
		
		
		function getPlaces(resultListId,search){
		
			//alert(document.add.street.value+" , "+document.add.zipcode.value+" , "+document.add.city.value+" , "+document.add.xcountry.options[document.add.xcountry.selectedIndex].text);
			searchString=search;
      resultList=document.getElementById(resultListId);
      places=new Array()
			clearList();
			lookupGmapsCache();
			if(!inCache){
				//alert('not in Cache');
				geocoder.getLocations(
				searchString,
				evalResults
				)
			}
		}
			
		function evalResults(response){
			if (!response || response.Status.code != 200){
				alert("Address not found...");
			}else{
				var cnt=response.Placemark.length;
				for (var i=0 ; i < cnt ; i++){
					place=new Array(6);
					var val;
					place[0]=i;
					place[1]=response.Placemark[i].address;
					place[2]=response.Placemark[i].Point.coordinates[1];
					place[3]=response.Placemark[i].Point.coordinates[0];
					place[4]=response.Placemark[i].AddressDetails.Country.CountryNameCode;
					setCache(place);
			        for (p in response.Placemark[i]){
						place[5]+=p;
					}
					places.push(place);
					if(i<cnt-1){
						setList(place, false);
					}else{
						setList(place, true);
					}
				}
			}
		}
		
		function clearList(){
			var toClear = resultList;
			var ll = toClear.length;
			for (i=0; i < ll; i++){
				toClear.options[toClear.length-1] = null;
			}
		}
		
		function setList(place , show){
      actList=resultList;
			var myOption = document.createElement("option");
			var myText = document.createTextNode(place[0]+"# "+place[1]);
			myOption.appendChild(myText);
			actList.appendChild(myOption);
			if (show){
				actList.style.visibility="visible";
				load();
				submitData();
				
			}
		}
		
		function submitData(){
			var actList=resultList;
			var selection = actList[actList.selectedIndex].text;
			var index=parseInt(selection.substring(0,selection.indexOf('#')));
			//alert(places[index][0]+" / "+places[index][1]+" / "+places[index][2]+" / "+places[index][3]+" / "+places[index][4]);
			LatLngToUTM(parseFloat(places[index][2]),parseFloat(places[index][3]));
			document.add.utme.value=parseInt(utml);
			document.add.utmn.value=parseInt(utmb);
			document.add.utmz.value=utmz;
			document.add.ctr_code.value=places[index][4];
			document.add.lat.value=places[index][2];
			document.add.lon.value=places[index][3];
			document.add.gmaps_string.value=places[index][1];
			document.getElementById('map').style.visibility="visible";
			
			map.setCenter(new GLatLng(places[index][2],places[index][3]), 13);
			
			marker = new GMarker(map.getCenter(), {draggable: true});
			
			map.addOverlay(marker);
			
			GEvent.addListener(marker, "dragstart", function(){
				map.closeInfoWindow();
			});
			
			GEvent.addListener(map, "dragend", function(marker, point) {
				map.clearOverlays();
				map.panTo(point);
				map.addOverlay(marker);
				//var latLngStr = '<br/>Latitude: ' + point.y.toString() + '<br/> Longitude: ' + point.x.toString();
			});
			
			
			mapInitialized=true;
			
		}
		
		
		function get_ctr_code(countryName){
			//alert('Geben Sie mir 2-3 Sekunden Zeit zum überlegen...');
			geocoder.getLocations(
				countryName,
				extract_code
			)
			
		}
		
		function extract_code(response){
			if (!response || response.Status.code != 200){
				alert("Address not found...");
			}else{
				var cnt=response.Placemark.length;
				if (cnt>1){
					alert("Google-Rückgabe mehrdeutig...");
					document.getElementById('shorty').value=response.Placemark[1].AddressDetails.Country.CountryNameCode;
				}else{
					
					document.getElementById('shorty').value=response.Placemark[0].AddressDetails.Country.CountryNameCode;
				}
			}
		}
		
		function createStaticMapsURL(){
			if(mapInitialized==true){
				var staticMapsURL = "http://maps.google.com/staticmap?";
				var center="center="+(map.getCenter()).lat()+","+(map.getCenter()).lng();
				var zoom="zoom="+map.getZoom();
				var size="size="+(map.getSize()).width+"x"+(map.getSize()).height;
				var format="format=jpg";
				var markers="markers="+(marker.getLatLng()).lat()+","+(marker.getLatLng()).lng();
				staticMapsURL= staticMapsURL+center+"&"+zoom+"&"+size+"&"+format+"&"+markers;
				//alert(staticMapsURL);
				//staticMapsURL = staticMapsURL+"&key="+document.getElementById('gmapskey').value;
				document.add.static_maps_url.value=staticMapsURL;
			}
			
		
		}
		
		
		
		
		
		