// ===== convert words to standard versions =====
	    function standardize(a) {
	      for (var i=0; i<standards.length; i++) {
	        if (a == standards[i][0]) a = standards[i][1];
	      }
	      return a;
	    }

// ===== check if two addresses are sufficiently different =====
	    function different(a,b) {
	      // only interested in the bit before the first comma in the reply
	      var c = b.split(",");
	      b = c[0];
	      // convert to lower case
	      a = a.toLowerCase();
	      b = b.toLowerCase();
	      // remove apostrophies
	      a = a.replace(/'/g ,"");
	      b = b.replace(/'/g ,"");
	      // replace all other punctuation with spaces
	      a = a.replace(/W/g," ");
	      b = b.replace(/W/g," ");
	      // replace all multiple spaces with a single space
	      a = a.replace(/s+/g," ");
	      b = b.replace(/s+/g," ");
	      // split into words
	      awords = a.split(" ");
	      bwords = b.split(" ");
	      // perform the comparison
	      var reply = false;
	      for (var i=0; i<bwords.length; i++) {
	        //GLog.write (standardize(awords[i])+"  "+standardize(bwords[i]))
	        if (standardize(awords[i]) != standardize(bwords[i])) {
	        reply = true
	        }
	      }
	      //GLog.write(reply);
	      return (reply);
	    }
	    
	      // ====== Plot a marker after positive reponse to "did you mean" ======
	      function place(lat,lng) {
	        var point = new GLatLng(lat,lng);
                //var marker = drawCircle(point, document.suchform.umkreis.value,"#0036FF",1,"","#F52F2F");
	        map.setCenter(point,10); 
                //map.addOverlay(marker);
	        map.addOverlay(new GMarker(point));
                sendRequest(lat, lng, document.suchform.umkreis.value, classe);
               // cityauswahl('geht', lat, lng, document.suchform.umkreis.value);
	        document.getElementById("gewort").innerHTML = "";
	      }

    // ====== Geocoding ======
	      function showAddress() {
	        var search = document.getElementById("search").value;
                    document.getElementById('inhalt').innerHTML = "<div style='background: #e8eefa; width:245px; height:130px; overflow:auto; border: 1px solid #b5b5b5;'><img src='http://www.immonation.de/template/vertical-menu/images/loadcity.gif' border='0' style='margin-top: 50px; margin-left: 106px;'></div>";
                    document.getElementById('inhalt2').innerHTML = "";
                    search = search+', Germany';
	        // ====== Perform the Geocoding ======        
	        geo.getLocations(search, function (result)
	          
                  {
	            map.clearOverlays(); 
	            if (result.Status.code == G_GEO_SUCCESS) {
	              // ===== If there was more than one result, "ask did you mean" on them all =====
	              if (result.Placemark.length > 1) {

	                //document.getElementById("inhalt").innerHTML = "Did you mean:";
                        document.getElementById('gewort').innerHTML = 'Meinten Sie:';
	                // Loop through the results
					document.getElementById('inhalt').innerHTML = "";
					if (result.Placemark.length > 6) { result.Placemark.length = 6;}
	                for (var i=0; i<result.Placemark.length; i++) {
	                  var p = result.Placemark[i].Point.coordinates;
	                  document.getElementById("inhalt").innerHTML += "<br>"+(i+1)+": <a href='javascript:place(" +p[1]+","+p[0]+")'>"+ result.Placemark[i].address+"</a>";
	                }
	              }
	              // ===== If there was a single marker, is the returned address significantly different =====
	              else {
	                document.getElementById("gewort").innerHTML = "";
	                if (different(search, result.Placemark[0].address)) {
                      var p = result.Placemark[0].Point.coordinates;
	                  place(p[1],p[0]);
	                //  document.getElementById("message").innerHTML = "Did you mean: ";
	                  var p = result.Placemark[0].Point.coordinates;
	                 // document.getElementById("message").innerHTML = "Located: "+result.Placemark[0].address;
	                } else { 
	                  var p = result.Placemark[0].Point.coordinates;
	                  place(p[1],p[0]);
	                 // document.getElementById("message").innerHTML = "Located: "+result.Placemark[0].address;
	                }
	              }
	          }
	            // ====== Decode the error status ======
	            else {
	              var reason="Code "+result.Status.code;
	              if (reasons[result.Status.code]) {
	                reason = reasons[result.Status.code]
	              } 
	              document.getElementById("gewort").innerHTML = "Could not find "+search+" "+reason;
	            }
	          }
	        );
}	     

// Liest gemeinsam mit getURLParam den Ort aus URL

function ortlesen() {
 if (getURLParam("ort") != "") {
     document.getElementById("search").value = getURLParam("ort");
      showAddress();
     }
}

function getURLParam(strParamName){
var strReturn = "";
var strHref = window.location.href;
if ( strHref.indexOf("?") > -1 ){
var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
var aQueryString = strQueryString.split("&");
for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
if (
aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
var aParam = aQueryString[iParam].split("=");
strReturn = aParam[1];
break;

}

}

}

return unescape(strReturn);

}

// Methode zur Auswertung der 

function sendRequest(lat, lon, umkreis, aclass) {
	try {
		req = window.XMLHttpRequest?new XMLHttpRequest(): 
		new ActiveXObject("Microsoft.XMLHTTP"); 
	} catch (e) {
		//Kein AJAX Support
	} 

	req.onreadystatechange = handleResponse;
	req.open('get', 'ziel.php?lat='+lat+'&lon='+lon+'&umkreis='+umkreis+'&pclass='+aclass);
	req.send(null);
}

function handleResponse() {
	if ((req.readyState == 4) && (req.status == 200)) { 
		document.getElementById('inhalt').innerHTML = req.responseText;
                document.getElementById('gewort').innerHTML = 'Kreuze die gewünschten Orte einfach an:';

document.getElementById('weiter').innerHTML = '<input type="button"  style="background-color: transparent; background-image: url(http://www.immonation.de/template/vertical-menu/menue/weiter.gif); width: 121px; height: 33px; border: solid 0px #000000; margin-top: 13px; margin-left:0px;" onclick="submit();">';
                execJS(document.getElementById('inhalt'));
                 
	}
}

// Methode zur Auswertung der Stadtteil/Kreis Anfrage

function sendRequest2(lat, lon, umkreis, zwei, aclass) {
	try {
		req2 = window.XMLHttpRequest?new XMLHttpRequest(): 
		new ActiveXObject("Microsoft.XMLHTTP"); 
	} catch (e) {
		//Kein AJAX Support
	} 

	req2.onreadystatechange = handleResponse2;
	req2.open('get', 'ziel.php?lat='+lat+'&lon='+lon+'&umkreis='+umkreis+'&zwei='+zwei+'&pclass='+aclass);
	req2.send(null); 
}

function handleResponse2() {
	if ((req2.readyState == 4) && (req2.status == 200)) { 
		document.getElementById('inhalt2').innerHTML = req2.responseText;
	}
}



