/* Javascript file for ordersamples.php */
// bevat de code voor de ajax functionaliteit voor het vullen van de dealer selectielijst

function selectDealers(){
	var postcode = window.document.getElementById('ZIPcode');	
	var stad = window.document.getElementById('city');	
	var country = window.document.getElementById('country');
	
	var serverPage = "ordersamples_dealerlijst.php";
	if(postcode.value.length >= 3){
		serverPage += "?pc="+postcode.value;
	}
	serverPage += "&city="+stad.value;
	serverPage += "&country="+country.value;
	
	// target object
	obj = window.document.getElementById('dealer_selectie');
	
//	$("#dealer_selectie").load(serverPage);
	
	//obj.innerHTML = toonLoader();
	
	//activeXobject aanmaken
	xmlhttp = getxmlhttp ();
	
	// ajax request uitvoeren
	processajax (serverPage, obj, "GET", "", xmlhttp);
}

function initSampleForm(){
	$("#ZIPcode, #city, #adress, #country").change(function(){
		calculateDistance();
	});	
}

function calculateDistance(){
	var postcode = $("#ZIPcode").val();
	var city = $("#city").val();
	var adress = $("#adress").val();
	var country = $("#country").val();
	
	var adres = "";
	if(adress != ""){
		adres = adres + adress;
	}
	if(postcode != ""){
		postcode.replace(" ", "");
		adres = adres + " " + postcode;
	}else if(city != ""){
		adres = adres + " " + city;
	}
	
	if(country != "" && country != "none"){
		adres = adres + " " + country;
	}

	var geocoder;

	if (GBrowserIsCompatible()) {
		// Create new geocoding object
		geocoder = new GClientGeocoder();
		
		geocoder.getLocations(adres, function(response){
			if (!response || response.Status.code != 200) {
				// kan niet bepalen
				return false;
			}
			// coordinaten opslaan
			place = response.Placemark[0];
	
			// Retrieve the latitude and longitude
			var latitude = place.Point.coordinates[1];
			var longitude = place.Point.coordinates[0];
			var a = new Date()
			$("#dealer_selectie").load("ordersamples_dealerlijst.php?lat="+latitude+"&long="+longitude+"&city="+city+"&a="+a.getTime());
			
		 });
	}
}
