/**
 * 	Handles the filtering of sailmakers by their coordinates and the coordinates of the user.
 */

dojo.require("dijit.form.ValidationTextBox");
dojo.require("dijit.form.Select");
dojo.require("dojo.NodeList-traverse"); 

if (typeof(Number.prototype.toRad) === "undefined") {
  Number.prototype.toRad = function() {
    return this * Math.PI / 180;
  }
}

function getCoords(postcode, callback) {
	var geocoder = new google.maps.Geocoder();
	
	if (geocoder) {
		geocoder.geocode({ 'address': postcode }, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				callback(results);
			} else {
				alert("Geocoding failed: " + status + " (Postcode: )" + postcode);
			}
		});
	}
}	

function distance(lat1, lng1, lat2, lng2) {
	var R = 6371; // km
	var dLat = (lat2-lat1).toRad();
	var dLon = (lng2-lng1).toRad(); 
	var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
			Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
			Math.sin(dLon/2) * Math.sin(dLon/2); 
	var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
	var d = R * c;
	return d;
}
