function getChild(node, childName) {
  var kids = node.childNodes;
  for (var i = 0; i < kids.length; i++) {
    if (kids.item(i).tagName==childName) {
      return kids.item(i);
    }
  }
  return null;
}

function contextRequest(iv) {
  var xmlHttpReq = false;
  var ie = false;
  if (window.XMLHttpRequest) {
    var xmlHttpReq = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    ie = true;
  } else {
    alert("Your browser does not support this functionality.");
    return;
  }
  xmlHttpReq.open('POST', '/sectional/loc?lo=' + iv.cfg.params.lastContextLongitude + 
                                        '&la=' + iv.cfg.params.lastContextLatitude, true);
  xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlHttpReq.onreadystatechange = function() {
    if (xmlHttpReq.readyState == 4) {
        var doc;
        if (! ie) {
          var parser = new DOMParser();
          doc = parser.parseFromString(xmlHttpReq.responseText, "text/xml");
        } else {
          var parser = new ActiveXObject("MSXML.DomDocument");
          parser.loadXML(xmlHttpReq.responseText);
          doc = parser;
        }
        //... time passes
        var lalo = doc.getElementsByTagName("lalo");
        if (lalo == null || lalo.length == 0) {
          //the server is not responding
          return;
        }
        //if the imageViewer params no longer match our local variables,
        if (lalo.item(0).firstChild.data != 
            '' + iv.cfg.params.lastContextLatitude + iv.cfg.params.lastContextLongitude) {
          //throw away the answer (we have another right-click in progress)
          return;
        }
        
        //google maps zoom level
        var zl = "&spn=0.068949,0.117073";
        if (iv.dimensions.zoomLevel == 2) {
          zl = "&spn=0.275297,0.468292";
        } else if (iv.dimensions.zoomLevel == 3) {
          zl = "&spn=0.550659,0.936584";
        }

        iv.ona_contextMenu.style.visibility = "hidden";
        iv.offa_contextMenu.style.visibility = "hidden";
        
        // decide which context menu to show
        if (doc.getElementsByTagName("loc").length > 0) {
          //
          // near an airport
          //
          var rwys = doc.getElementsByTagName("rwy");
          var rwytxt = '';
          for (var i = 0; i < rwys.length; i++) {
            rwytxt += getChild(rwys.item(i), "n").firstChild.data + " " +
                      getChild(rwys.item(i), "l").firstChild.data + " x " +
                      getChild(rwys.item(i), "w").firstChild.data + "<br>";
          }
          document.getElementById('ona_contextTitle').innerHTML = 
            doc.getElementsByTagName("cn").item(0).firstChild.data + "<br>" + rwytxt;
            
          document.getElementById('ona_googleLink').innerHTML = 
            "<a href='http://maps.google.com/maps?ll=" +
              iv.cfg.params.lastContextLatitude + "," +
              iv.cfg.params.lastContextLongitude + zl +
            "' target='GMap' id='ona_item1'>Google Maps ...</a>";
                        
          document.getElementById('ona_airnavLink').innerHTML = 
            "<a href='" +
              doc.getElementsByTagName("link").item(0).firstChild.data + 
            "' target='AirNav' id='ona_item2'>AirNav " +
              doc.getElementsByTagName("faa").item(0).firstChild.data +
            " ...</a>";
            
          var mtidNode = doc.getElementsByTagName("mtid").item(0).firstChild;
          if (mtidNode == null) {
            document.getElementById('ona_mtLink').innerHTML = "<div id=ona_item3 />"; 
          } else {
            document.getElementById('ona_mtLink').innerHTML = "<a href='/text?ids=" +
              mtidNode.data + "' id='ona_item3'>" +
              doc.getElementsByTagName("mttxt").item(0).firstChild.data + " ...</a>";
          }
          iv.ona_contextMenu.style.left = iv.cfg.params.lastContextLeft;
          iv.ona_contextMenu.style.top = iv.cfg.params.lastContextTop;
          iv.ona_contextMenu.style.visibility = "visible";
          iv.ona_contextMenu.style.display = "block";
        } else {
          document.getElementById('offa_googleLink').innerHTML = 
            "<a href='http://maps.google.com/maps?ll=" +
              iv.cfg.params.lastContextLatitude + "," +
              iv.cfg.params.lastContextLongitude + zl +
            "' target='GMap' id='offa_item1'>Google Maps ...</a>";
            
          var mtidNode = doc.getElementsByTagName("mtid").item(0).firstChild;
          if (mtidNode == null) {
            document.getElementById('offa_mtLink').innerHTML = "<div id=offa_item2 />";
          } else {
            document.getElementById('offa_mtLink').innerHTML = "<a href='/text?ids=" +
              mtidNode.data + "' id='offa_item2'>" +
              doc.getElementsByTagName("mttxt").item(0).firstChild.data + " ...</a>";
          }
            
          iv.offa_contextMenu.style.left = iv.cfg.params.lastContextLeft;
          iv.offa_contextMenu.style.top = iv.cfg.params.lastContextTop;
          iv.offa_contextMenu.style.visibility = "visible";  
          iv.offa_contextMenu.style.display = "block";
        }
    }
  }
  xmlHttpReq.send("");  
}

