Here is a function I've made and works perfect. Hard tested in X-Plane Flight Simulator. At the end there is a division of earth's radius by 1852, so the function returns the distance to nautical miles.
function GC_distance_calc(lat1, lon1, lat2, lon2)
--This function returns great circle distance between 2 points.
--Found here: http://bluemm.blogspot.gr/2007/01/excel-formula-to-calculate-distance.html
--lat1, lon1 = the coords from start position (or aircraft's) / lat2, lon2 coords of the target waypoint.
--6371km is the mean radius of earth in meters. Since X-Plane uses 6378 km as radius, which does not makes a big difference,
--(about 5 NM at 6000 NM), we are going to use the same.
--Other formulas I've tested, seem to break when latitudes are in different hemisphere (west-east).
local distance = math.acos(math.cos(math.rad(90-lat1))*math.cos(math.rad(90-lat2))+
math.sin(math.rad(90-lat1))*math.sin(math.rad(90-lat2))*math.cos(math.rad(lon1-lon2))) * (6378000/1852)
return distance
end