//Ajax
var xmlhttp = false;
// If IE7, Mozilla, Safari, etc: Use native object
if (window.XMLHttpRequest) {
        //Intiate the object
        xmlhttp = new XMLHttpRequest();
        //Set the mime type
        xmlhttp.overrideMimeType('text/xml');
// ...otherwise, use the ActiveX control for IE5.x and IE6
} else if (window.ActiveXObject) {
        //Intiate the object
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else{
	//IE7 Fix
}

// Maximaal aantal tekens in textarea
function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
	var over = mlength - obj.value.length;
	document.getElementById('over').innerHTML = "Je mag nog " + over +" tekens gebruiken.";
}


// Get LNG van complete waarde
function getlng(waarde) {
   		//Put the form data into a variable
		var waarde = waarde + "!";
   		var waarde = String(waarde);

		var start1 = waarde.search(/,/)-1;
		var start2 = waarde.search(/, /)+2;
		var einde = waarde.search(/!/)-start2-1;
		
		lat = waarde.substr(1,start1);
		lng = waarde.substr(start2,einde);
		
		return lng;	
}
// Get LAT van complete waarde
function getlat(waarde) {
   		//Put the form data into a variable
		var waarde = waarde + "!";
   		var waarde = String(waarde);

		var start1 = waarde.search(/,/)-1;
		var start2 = waarde.search(/, /)+2;
		var einde = waarde.search(/!/)-start2-1;
		
		lat = waarde.substr(1,start1);
		lng = waarde.substr(start2,einde);
		
		return lat;	
}

// Deze functie haalt de postcode op adhv de Lattitude en Longitude
function reverse(waarde,www) {
   		//Put the form data into a variable
		var waarde = waarde + "!";
   		var waarde = String(waarde);

		var start1 = waarde.search(/,/)-1;
		var start2 = waarde.search(/, /)+2;
		var einde = waarde.search(/!/)-start2-1;
		
		lat = waarde.substr(1,start1);
		lng = waarde.substr(start2,einde);
		
		//Change the content of the "result" DIV to "Searching..."
		document.getElementById('result').innerHTML = "Zoeken...";
		
        //This sets a variable with the URL (and query strings) to our PHP script
		var pst="?lat="+lat+"&lng="+lng;
		//Open the URL above "asynchronously" (that's what the "true" is for) using the GET method
		if(www=="www"){
			xmlhttp.open('POST', 'http://www.rcdrift.nl/map/contactothersite.php?lat='+lat+'&lng='+lng, true);
		}
		else{
			xmlhttp.open('POST', 'http://rcdrift.nl/map/contactothersite.php?lat='+lat+'&lng='+lng, true);
		}
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        
		//Check that the PHP script has finished sending us the result
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                		//Replace the content of the "result" DIV with the result returned by the PHP script
				document.getElementById('result').innerHTML = xmlhttp.responseText + ' ';
				document.formulier.postcode.value = xmlhttp.responseText;
			} else {
                //If the PHP script fails to send a response, or sends back an error, display a simple user-friendly notification
				//document.getElementById('result').innerHTML = 'Error: preSearch Failed!';
			}
		};
		xmlhttp.send(pst);  
		
}


//Stap 2, ajax ga naar stap 2
function stap2() {
    	//Haal de LAT en de LNG op
       	var lat = document.formulier.lat.value;
       	var lng = document.formulier.lng.value;
       	// Haal de postcode op
       	var postcode = document.formulier.postcode.value;
		
        //Geef even wat weer, we zijn bezig
		document.getElementById('left').innerHTML = "Verwerken...";
     
        //This sets a variable with the URL (and query strings) to our PHP script
		var pst="lat="+escape(lat)+"&lng="+escape(lng)+"&postcode="+escape(postcode)+"&stap=2";
        	
		//Open the URL above "asynchronously" (that's what the "true" is for) using the GET method
		xmlhttp.open('POST', 'stappen.php', true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

        //Check that the PHP script has finished sending us the result
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                //Replace the content of the "result" DIV with the result returned by the PHP script
				document.getElementById('left').innerHTML = xmlhttp.responseText + ' ';
			} else {
                //If the PHP script fails to send a response, or sends back an error, display a simple user-friendly notification
				document.getElementById('left').innerHTML = 'Error: ' + xmlhttp.statusText;
			}
		};
		xmlhttp.send(pst);
}



//Stap 3, ajax ga naar stap 3
function stap3() {
    	//Haal de LAT en de LNG op
       	var lat = document.formulier.lat.value;
       	var lng = document.formulier.lng.value;
       	var postcode = document.formulier.postcode.value;
       	var tekst = document.formulier.tekst.value;
		
		//Geef even wat weer, we zijn bezig
		document.getElementById('left').innerHTML = "Verwerken...";
		
        //This sets a variable with the URL (and query strings) to our PHP script
		var pst="lat="+escape(lat)+"&lng="+escape(lng)+"&postcode="+escape(postcode)+"&tekst="+escape(tekst)+"&stap=3";
        	
		//Open the URL above "asynchronously" (that's what the "true" is for) using the GET method
		xmlhttp.open('POST', 'stappen.php', true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

        //Check that the PHP script has finished sending us the result
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                		//Replace the content of the "result" DIV with the result returned by the PHP script
				document.getElementById('left').innerHTML = xmlhttp.responseText;
			} else {
                //If the PHP script fails to send a response, or sends back an error, display a simple user-friendly notification
				//document.getElementById('ws1_result').innerHTML = 'Error: preSearch Failed!';
			}
		};
		xmlhttp.send(pst);  
}
