var req;
try {
    req = window.XMLHttpRequest?new XMLHttpRequest():
new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
    //Kein AJAX Support
}

function ajaxload() {
	document.getElementById('ajaxload').innerHTML = document.getElementById('ajaxloadvorlage').innerHTML;
}
		
function ajaxunload() {
	document.getElementById('ajaxload').innerHTML = null;
}


function getgooglekoordinaten(toid,name,strasse,hausnummer,plz,ort,action,typ) {
    ajaxload();
    var req;
    try {
        req = window.XMLHttpRequest?new XMLHttpRequest():
        new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
        //Kein AJAX Support
    }

	// delay between geocode requests - at the time of writing, 100 miliseconds seems to work well
	var delay = 100;
    var search = strasse + " " + hausnummer + " " + plz + " " + ort;
	if (GBrowserIsCompatible()) { 
		
      		// ====== Create a Client Geocoder ======
		var geo = new GClientGeocoder(); 

		// ====== Array for decoding the failure codes ======
		var reasons=[];
		reasons[G_GEO_SUCCESS]           	= "Success";
		reasons[G_GEO_MISSING_ADDRESS]    	= "Missing Address: The address was either missing or had no value.";
		reasons[G_GEO_UNKNOWN_ADDRESS]    	= "Unknown Address:  No corresponding geographic location could be found for the specified address.";
		reasons[G_GEO_UNAVAILABLE_ADDRESS]	= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
		reasons[G_GEO_BAD_KEY]            	= "Bad Key: The API key is either invalid or does not match the domain for which it was given";
		reasons[G_GEO_TOO_MANY_QUERIES]   	= "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
		reasons[G_GEO_SERVER_ERROR]       	= "Server error: The geocoding request could not be successfully processed.";
		reasons[403]                      	= "Error 403: Probably an incorrect error caused by a bug in the handling of invalid JSON.";

	      
		geo.getLocations(search,
			function (result) { 
				// If that was successful
				if (result.Status.code == G_GEO_SUCCESS) {
					// Lets assume that the first marker is the one we want
					var p = result.Placemark[0].Point.coordinates;
					var lat=p[1];
					var lng=p[0];
                    if(action == "ortedit"){
                        req.onreadystatechange = function() {
                            if ((req.readyState == 4) && (req.status == 200)) {
                                // Ausgabe
                                res = req.responseText;
                                if(res == 1) {
                                    ajaxunload()
                                    location.href="?seite=trainingsorte";
                                }
                                else {
                                    alert(res);
                                }
                            }
                        }
                        req.open('post', 'trainingsorte/trainingsort_bearbeiten_action.php');
                        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                        req.send('toid=' + toid + '&name=' + escape(name) + '&strasse='+ escape(strasse) +'&hausnummer=' +hausnummer +'&plz=' + plz + '&ort=' + escape(ort) + '&lat=' + lat+ '&lng=' + lng);
                    }
                    else if(action == "ortneu"){
                        req.onreadystatechange = function() {
                            if ((req.readyState == 4) && (req.status == 200)) {
                                // Ausgabe
                                res = req.responseText;
                                if(res == 1) {
                                    location.href="?seite=trainingsorte";
                                }
                                else {
                                    alert(res);
                                }
                            }
                        }
                        req.open('post', 'trainingsorte/trainingsort_neu_action.php');
                        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                        req.send('typ='+ typ + '&name=' + escape(name) + '&strasse='+ escape(strasse) +'&hausnummer=' +hausnummer +'&plz=' + plz + '&ort=' + escape(ort) + '&lat=' + lat+ '&lng=' + lng);
                    }
				}
                else {
                    alert(result.Status.code);
                    alert(search);
                }
			}	
		);
	}
}

function googleadresseeintragen(lat,lng) {
	var req;
        try {
                req = window.XMLHttpRequest?new XMLHttpRequest():
                        new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
                //Kein AJAX Support
        }
        req.onreadystatechange = function() {
                if ((req.readyState == 4) && (req.status == 200)) {
                      // document.getElementById('ausgabe').innerHTML = req.responseText;
                }
        }
        req.open('post', 'ajax_trainingsort.php');
        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        req.send('lat=' +lat + '&lng=' + lng + '&mysqlid=' + mysqlid);
	
}

function in_array(item,arr) {
	for(p=0;p<arr.length;p++) if (item == arr[p]) return true;
	return false;
}
		
function zeichen_pruefen(text) {
	zeichen = new Array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
						"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
						"0","1","2","3","4","5","6","7","8","9");
	for(i = 0; i < text.length;i++) {
		if(in_array(text.charAt(i),zeichen)) {
		}
	else {
		alert("Bitte nur gültige Zeichen verwenden (A-Z,a-z,0-9)");
		return false;
		}
	}
	return true;
}

function mein_profil_bearbeiten(benutzer_id,vorname,nachname,strasse,hausnummer,plz,ort,telefon,handy,email,teams) {
    req.onreadystatechange = function() {
                if ((req.readyState == 4) && (req.status == 200)) {
                      res = req.responseText;
                      if(res == "1"){
                        location.href='?seite=meinprofil';
                      }
                      else {
                        alert(res);
                      }
                      // document.getElementById('ausgabe').innerHTML = req.responseText;
                }
        }
        req.open('post', 'benutzer/benutzer_profil_bearbeiten_action.php');
        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        req.send(   'benutzer_id=' + escape(benutzer_id) + '&vorname=' + escape(vorname) + '&nachname=' + escape(nachname) +
                    '&strasse=' + escape(strasse) + '&hausnummer=' + escape(hausnummer) + '&plz=' + escape(plz) +
                    '&ort=' + escape(ort) + '&telefon=' + escape(telefon) + '&handy=' + escape(handy) + '&email=' + escape(email) + '&teams=' + teams );
}


