//	var searchstring = null;
	var http = new XMLHttpRequest();
	function do_search(){
	var search = document.getElementById('search').value;
/*	var keyCode = event.which || event.keyCode;
		if(keyCode == 40){
		document.getElementById('searchresults').focus();
		}    */
		if(http != null) {
				http.open("POST", "search_city.php", true);
				http.onreadystatechange = ausgeben;
				http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                           //application/x-www-form-urlencoded
				http.send("search="+search); 
			}
		
	}
	
	function ausgeben() {
		if(http.readyState == 4) { 
			window.document.getElementById('Ausgabe').innerHTML = http.responseText;
		}
	}

	function clicked(value){
		document.getElementById('search').value=value;
		document.getElementById('search').focus();
		window.document.getElementById('Ausgabe').innerHTML = "";
	}
	
	
	

	function do_search_head(){
	var search = document.getElementById('head').value;
/*	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if(keyCode == 40){
		document.getElementById('searchresults').focus();
		}
*/		if(http != null) {
				http.open("POST", "search_head.php", true);
				http.onreadystatechange = show_results_head;
				http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				http.send("search="+search);
			}

	}
	
	function show_results_head() {
		if(http.readyState == 4) {
			window.document.getElementById('show_head').innerHTML = http.responseText;
		}
	}

	function clicked_head(value){
		document.getElementById('head').value=value;
		document.getElementById('head').focus();
		window.document.getElementById('show_head').innerHTML = "";
	}
	
	
	function url_from_form(){
	var maxdist = document.getElementById('maxdist').value;
	var head = document.getElementById('head').value;
	var city = document.getElementById('search').value;
	if(http != null) {
				http.open("POST", "generate_url_from_form.php", true);
				http.onreadystatechange = url_from_form_response;
				http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				http.send("maxdist="+maxdist+"&head="+head+"&city="+city);
			}
        return false;
	}
	
		function url_from_form_response() {
		if(http.readyState == 4) {
			location.href = http.responseText;
		}
		
	}
	
	
function showTT(id) {
  tt = document.getElementById(id);
  tt.style.display = "block";
  tt.style.top = "-100000px";
  tt.style.left = "-100000px";
  set_opacity(id, 100);
}

document.onmousemove = updateTT;
function updateTT(event) {
  if (!event)
    event = window.event;
  if (typeof tt !='undefined' && tt != null) {
    x = event.clientX;
    y = event.clientY;
    tt.style.left = (x + 10) + "px";
    tt.style.top   = (y - 20) + "px";
    set_opacity(tt.id, 100);
    if(typeof timeout != 'undefined') clearTimeout(timeout);
    timeout = setTimeout("set_opacity('" + tt.id + "', 1)", 2000);
  }
}

function hideTT() {
  tt.style.display = "none";
}

function set_opacity(id, grade){
    var element = document.getElementById(id);
    element.style.opacity = grade/100;
    element.style.filter = 'alpha(opacity =' + grade + ')';  
} 





// Gmaps 

var map = null;
var geocoder = null; 

function initialize(lon, lat, maxdist) {
  var zoomlevel = null;
  if(maxdist=='all'){
    lat = 51.04;
    lon = 10.41;
    zoomlevel = 5;
  }
  if(maxdist=='50'){
    zoomlevel = 8;
  }
  if(maxdist=='25'){
    zoomlevel = 9;
  }
  if(maxdist=='10'){
    zoomlevel = 10;
  }
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));
    map.disableDoubleClickZoom();
    geocoder = new GClientGeocoder();
    map.setCenter(new GLatLng(lat, lon), zoomlevel);
  }
}

function showAddress(address, maxdist) {
var blur = null;
if(maxdist=='all'){
    blur = 0.1;
  }
  if(maxdist=='50'){
    blur = 0.05;
  }
  if(maxdist=='25'){
    blur = 0.02;
  }
  if(maxdist=='10'){
    blur = 0.008;
  }
var  addresses = address.split(";");
  if (geocoder) {
    for (var i = 0; i < addresses.length; ++i){
        addresses[i] = addresses[i].split("|");
        var myHtml = "<b><a href=\"article.php?art=" + addresses[i][3] + "\"><img src=\"thumb/" + addresses[i][3] +".jpg\"><br/>" +  addresses[i][2] + "</a></b><br/><br/><a href=\"index.php?maxdist=25&city=" + addresses[i][0] + "\">" + addresses[i][0] + "</a><br/>";
        setplacemark(addresses[i][0] + " " + addresses[i][1], myHtml, blur);
      }
    }
}

function setplacemark(address, myHtml, blur){
    var yellowIcon = new GIcon(G_DEFAULT_ICON);
    var yIconSize = new GSize(16, 16);
    var yIconShadowSize = new GSize(0, 0);
    var yIconPoint = new GPoint(8, 8);
    yellowIcon.image = "img/yellowmarker.png";
    yellowIcon.transparent = "img/yellowmarker_transparent.png";
    yellowIcon.iconSize = yIconSize;
    yellowIcon.iconAnchor = yIconPoint;
    yellowIcon.infoWindowAnchor = yIconPoint;
    yellowIcon.shadowSize = yIconShadowSize;
    markerOptions = { icon:yellowIcon };
    geocoder.getLatLng(address,
        function(point) {
            if (!point) {
              //alert( addresses[number] + " not found");
            } else {
              var newpoint = new GLatLng(point.lat() + blur * rand_pos_neg(), point.lng() + blur * rand_pos_neg());
              var marker = new GMarker(newpoint, yellowIcon);
              GEvent.addListener(marker,"click", function() {
                map.openInfoWindowHtml(newpoint, myHtml);
                });
              map.addOverlay(marker);
            }
          }
        );
}

function showsinglearticleonmap(address){
    if (GBrowserIsCompatible()) {
    var yellowIcon = new GIcon(G_DEFAULT_ICON);
    var yIconSize = new GSize(16, 16);
    var yIconShadowSize = new GSize(0, 0);
    var yIconPoint = new GPoint(8, 8);
    yellowIcon.image = "img/yellowmarker.png ";
    yellowIcon.transparent = "img/yellowmarker_transparent.png";
    yellowIcon.iconSize = yIconSize;
    yellowIcon.iconAnchor = yIconPoint;
    yellowIcon.infoWindowAnchor = yIconPoint;
    yellowIcon.shadowSize = yIconShadowSize;
    map = new GMap2(document.getElementById("map_canvas"));
    map.disableDoubleClickZoom();
    geocoder = new GClientGeocoder();
    geocoder.getLatLng(address, function(point){
        map.setCenter(point, 11); 
        var marker = new GMarker(point, yellowIcon);
        /*GEvent.addListener(marker,"click", function() {
            map.openInfoWindowHtml(point, myHtml);
        });    */
        map.addOverlay(marker);   
        });
    }
}

function rand_pos_neg(){
    if(Math.random()>0.5){
       var prefix = 1;
    }
    else{
       var prefix = -1;
    }
   return Math.random() * prefix;
}   


function verify_address() {
  if (GBrowserIsCompatible()) {
  var address = "null";
  address = "de "
  address +=  window.document.getElementsByName('plz')[0].value + " ";
  address +=  window.document.getElementsByName('ort')[0].value + " ";
  address +=  window.document.getElementsByName('strasse')[0].value + " ";
//  address +=  window.document.getElementsByName('hausnummer')[0].value;
    geocoder = new GClientGeocoder();
    geocoder.getLocations(address, get_location_info);
  }
}

function get_location_info(response){
    if(response.Status.code==200 && response.Placemark[0].AddressDetails.Accuracy >= 6){
        var text = null;
            if(window.document.getElementsByName('plz')[0].value != response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber){
                window.document.getElementsByName('plz')[0].style.color = "#FF0000";
                window.document.getElementsByName('plz')[0].value = response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;    
            }
            else{
                window.document.getElementsByName('plz')[0].style.color = "#000000";    
            }
            if(window.document.getElementsByName('ort')[0].value != response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName){
                window.document.getElementsByName('ort')[0].style.color = "#FF0000";
                window.document.getElementsByName('ort')[0].value = response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
            }
            else{
                window.document.getElementsByName('ort')[0].style.color = "#000000";
            }
            if(window.document.getElementsByName('strasse')[0].value != response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName){
                window.document.getElementsByName('strasse')[0].style.color = "#FF0000";
                window.document.getElementsByName('strasse')[0].value = response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
            }
            else{
                window.document.getElementsByName('strasse')[0].style.color = "#000000";
            }
            
            text="";
            window.document.getElementById('address_verification').style.backgroundColor = "#FFFFFF";
    }    
    else{
        text = "die adresse konnte nicht gefunden werden";
        window.document.getElementsByName('plz')[0].focus();
        window.document.getElementById('address_verification').style.backgroundColor = "#FFCC03";   
    }
    window.document.getElementById('address_verification').innerHTML = text;
    
}


