From 65b1fd20e723323be7273ca98d0c201bcdb1ab07 Mon Sep 17 00:00:00 2001
From: Torsten Dreyer <Torsten@t3r.de>
Date: Mon, 22 Aug 2011 20:01:55 +0200
Subject: [PATCH] Location-in-Air: pick nearest VOR/NDB (fix#228)

When entering a duplicate VOR or NDB id into the Location-in-Air
dialog, the first entry in the database was picked. This patch
assumes that the station with the given ID nearest to the current
position was requested.
A better fix would probably be to present the user a list of found
stations - that's something for the future.
---
 gui/dialogs/location-in-air.xml | 29 +++++++++++++++++++++++------
 1 file changed, 23 insertions(+), 6 deletions(-)

diff --git a/gui/dialogs/location-in-air.xml b/gui/dialogs/location-in-air.xml
index b57a0b80c..179dff37f 100644
--- a/gui/dialogs/location-in-air.xml
+++ b/gui/dialogs/location-in-air.xml
@@ -6,8 +6,8 @@
 
   <nasal>
     <open>
-      p = props.globals.getNode("/sim/gui/dialogs/location-in-air/", 1);
-      mode = {
+      var p = props.globals.getNode("/sim/gui/dialogs/location-in-air/", 1);
+      var mode = {
         airport: p.getNode("airport", 1),
         lonlat:  p.getNode("lonlat", 1),
         vor:     p.getNode("vor", 1),
@@ -15,14 +15,14 @@
         fix:     p.getNode("fix", 1),
       };
 
-      set_radio = func(m) {
-        foreach (k; keys(mode)) {
+      var set_radio = func(m) {
+        foreach (var k; keys(mode)) {
           mode[k].setBoolValue(m == k);
         }
       }
 
-      initialized = 0;
-      foreach (k; keys(mode)) {
+      var initialized = 0;
+      foreach (var k; keys(mode)) {
         if (mode[k].getType() == "NONE") {
           mode[k].setBoolValue(0);
         }
@@ -31,6 +31,19 @@
       if (!initialized) {
         set_radio("airport");
       }
+      var pickNearest = func(type,propname,freqpropname) {
+
+        var found = navinfo(type,getprop(propname));
+        if( found == nil or size(found) == 0 ) {
+          print(type, " ", getprop(propname), " NOT found");
+          setprop(propname, "");
+          setprop(freqpropname, "");
+          return;
+        }
+        setprop(propname, found[0].id);
+        setprop(freqpropname, found[0].frequency / 100.0);
+       
+      }
     </open>
 
     <close># just kept for educational purposes :-)</close>
@@ -300,10 +313,14 @@
 
           if (!mode.vor.getBoolValue()) {
             setprop("/sim/presets/vor-id", "");
+          } else {
+            pickNearest("vor","/sim/presets/vor-id","/sim/presets/vor-freq");
           }
 
           if (!mode.ndb.getBoolValue()) {
             setprop("/sim/presets/ndb-id", "");
+          } else {
+            pickNearest("ndb","/sim/presets/ndb-id","/sim/presets/ndb-freq");
           }
 
           if (!mode.fix.getBoolValue()) {