				var map;
			    var gdir;
			    var geocoder = null;
			    var addressMarker;
				var to = 'kiel';
				var loadStatus;
				var reset = false;

			  	function getLocations() {
			  		var uri = "/LocationDel.htm?tok="+tk + "&lcids=" + dojo.byId('lcids').innerHTML;
	                dojo.xhrGet( {
	                    url: uri,
	                    handleAs: "json",
	                    timeout: 5000,
	                    load: function(data, ioArgs) {
	                        for(var i=0; i < data.length; i++) {
	                            to = parseFloat(data[i].lat) + ' ' + parseFloat(data[i].long);
	                            dojo.byId('btnDirection').enabled = false;
	                            break;
	                        }
	                    },
	                    error: function(response, ioArgs) {
	                      console.error("HTTP status code: ", ioArgs.xhr.status);
	                      return response;
	                      }
	                    });
			  	}

			  	function resetInput() {
			  		if(!reset) {
			  			document.getElementById("fromAddress_city").value = "";
			  			reset = true;
			  		}
			  	}

			    function setDirections(zip,city,street,country) {
			       var addr = getStartAddress(zip.value,city.value,street.value,country.value);
			       if(addr && addr.length >= 2) {
				       gdir.loadFromWaypoints(Array (addr, to),  { "locale": locales });
			       } else {
			       		alert('Bitte den Start Ort angeben!');
			       		dojo.byId('fromAddress_city').focus();
			       }
			    }

			    function getStartAddress(zip,city,street,country) {
			    	var addr = "";
			    	if(street)
			    		addr += street + ",";
			    	if(zip && city)
			    		addr += zip + " " + city + ",";
			    	else if(city)
			    		addr += city + ",";
			    	if(country)
			    		addr += country;
			    	if(addr.lastIndexOf(",")==addr.length-1)
			    		return addr.slice(0, addr.length-1);
			    	return addr;
			    }

				function onGDirectionsLoad(){
				  loadStatus = gdir.getStatus().code;
				  window.setTimeout("directionLoaded()", 300);
				}

			    function handleErrors(){
				   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
				     alert("Die Startadresse konnte nicht ermittelt werden.\nÜberprüfen Sie die Schreibweise oder wählen Sie einen\nanderen (eventuell größeren Ort).");
				   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
				     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);

				   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
				     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

				//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
				//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);

				   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
				     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

				   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
				     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);

				   else alert("An unknown error occurred.");

				}

				function directionLoaded() {
					if(loadStatus == '200') {
						if(dojo.byId('directions').innerHTML.length >= 5) {
							var div = dojo.byId('directions');
							var node = div.lastChild.lastChild.childNodes[1];
							var td = node.firstChild.lastChild.rows[0].lastChild;
							td.innerHTML = dojo.byId('toAddress').value;
						} else {
							window.setTimeout("directionLoaded()", 500);
						}
						map.addControl(new GLargeMapControl());
				        map.addControl(new GOverviewMapControl());
					}
				}

				function init()
				{
					dojo.byId('btnDirection').enabled = false;
			      if (GBrowserIsCompatible()) {
			        getLocations();
			        map = new GMap2(document.getElementById("map"));
			        gdir = new GDirections(map, document.getElementById("directions"));
			        GEvent.addListener(gdir, "load", onGDirectionsLoad);
			        GEvent.addListener(gdir, "error", handleErrors);
			      } else {
			      	alert('Entschuldigung aber Dein Browser unterstützt unseren Routenplanner nicht!');
			      }
				}

				dojo.addOnLoad(init);
				dojo.addOnUnload(GUnload);
