/******************************************************************************
 *
 *  Copyright 2008-2009 David D. Emory 
 *  [additional contributors append names above]
 *  
 *  This file is part of Five Points. See <http://www.fpdev.org> for
 *  additional project information and documentation.
 *  
 *  Five Points is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Five Points is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 *  
 ******************************************************************************/  

/**
 * fp_proxreq.js
 *
 * Implements functionality for creating and processing a proximity request,
 * i.e. a query of the 5P database for known locations within a set distance of
 * a user-specified point.
 */

// FIELD DECLARATIONS:

// the following fields store a snapshot of the last successful proximity request:
var fp_proxLocArr_; // (Array of Strings); the locations, in a user-readable format
var fp_proxLocEncArr_; // (Array of Strings); the locations, in encoded format for internal use
var fp_proxLocXArr_, fp_proxLocYArr_; // (Arrays of Numbers); x,y coordinates of locations
var fp_proxLocCount_; // (Number); the number of locations returned


// FUNCTION DECLARATIONS:

/**
 * fp_doProximityRequest(x,y)
 *
 * Initiates an ajax-based proximity request, given the search location in
 * decimal degrees.
 */
function fp_doProximityRequest(x,y) {
  var results = fp_outputDivs_[FP_OUTPUT_LOCATIONS];
  results.innerHTML = "Resolving...";
  
  var fields = "x="+x+"&y="+y+"&r=250";
  
  fp_ajaxLoadAsXML("fp_trpact_nearbylocs.php", fp_processProximityResponseXML, fields);
}

/**
 * fp_processProximityResponseXML(xmlDoc)
 *
 * Processes the XML-based response to a proximity request, given a reference
 * to the XML document.
 *
 * See <dtd/fp_proxreq.dtd> for more on the structure of the XML response.
 */
function fp_processProximityResponseXML(xmlDoc) {

  var node = xmlDoc.documentElement;
  if(node.nodeName != "locations") {
    alert('There was a problem with the nearby locs request.');
    return;
  }
  fp_proxLocArr_ = new Array();
  fp_proxLocEncArr_ = new Array();
  fp_proxLocXArr_ = new Array();
  fp_proxLocYArr_ = new Array();
  var iLoc = 0;
  for (var i = 0; i < node.childNodes.length; i++) {
    var cnode = node.childNodes.item(i);
    if(cnode.nodeName=="loc") {
      fp_proxLocArr_[iLoc] = cnode.getAttribute("disp");
      fp_proxLocEncArr_[iLoc] = cnode.getAttribute("enc");
      fp_proxLocXArr_[iLoc] = parseFloat(cnode.getAttribute("x"));
      fp_proxLocYArr_[iLoc] = parseFloat(cnode.getAttribute("y"));

      iLoc++;
    }
  }
  fp_proxLocCount_ = iLoc;

  switch(fp_currentApp_) {
    case FP_APP_TRIP_PLANNER: fp_oShowLocResults(); break;	
  }
}
