// ***************************************************************************
// *  search.js
// *
// *  Copyright  2007  Larry Moore
// *  larmoor@gmail.com
// ****************************************************************************/
//
//
//   This program 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 2 of the License, or
//   (at your option) any later version.
// 
//   This program 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 this program.  The GPL is available at http://www.gnu.org, 
//   or by writing to the Free Software Foundation, Inc., 59 Temple Place - 
//   Suite 330, Boston, MA 02111-1307, USA.
// 
// This module contains most of the code for search and directions input forms
// USGN coordinates are computed with calls to functions in usng.js


var lat_lines = []
var lng_lines = []
var directions

// zoom to either a street address or USNG string

function showAddress(address) {
  var thispoint;
  var usngstr;
  var geocoder = new GClientGeocoder();

  // case 1 -- usng or mgrs string
  if (usngstr = isUSNG(address)) {
      //alert("flag01");
      // convert USNG string to lat/lng
      thispoint = GUsngtoLL(usngstr) 
      displayCoordMarker(thispoint.lat(),thispoint.lng(),14,"")
  }
  // case 2 -- lat/lng in dd-mm.mmm
  else if (thispoint = isLatLngDM(address)) {
      displayCoordMarker(thispoint.lat(),thispoint.lng(),14,"")
  }
  // default case...let Google try a street address of dd.ddd lat/lng
  else {
     geocoder.getLatLng(
       address,
       function(point) {
         if (!point) {
           alert(address + " - Address cannot be found");
         } 
         else {
           var location = address  + "<br><br>" 
           displayCoordMarker(point.lat(),point.lng(),14, location)
         }
       }
     );
  }
}

function showDirections(start,end) {
   var start_str
   var latlng
   var usngstr = isUSNG(start)
   if (usngstr) {
       // convert USNG string to lat/lng
       latlng = GUsngtoLL(usngstr) 
       start_str = latlng.lat() + "," + latlng.lng()
   }
   else {
       start_str = start
   }

   usngstr = isUSNG(end)
   if (usngstr) {
       // convert USNG string to lat/lng
       latlng = GUsngtoLL(usngstr) 
       end_str = latlng.lat() + "," + latlng.lng()
   }
   else {
       end_str = end
   }

   // be sure the panel for directions is visible
   Info.Show();

   // clear sidebar
   if (directions) {
        clearDirections();
   }

   document.getElementById("info").innerHTML = Info.ClearButton + Info.VisibleText;
   // may need elseif block here to account for different browsers
   directions = new GDirections(usngmap, document.getElementById("info"))
   directions.load("from: "+start_str + " to: " + end_str);
}

function clearDirections() {
   directions.clear()
   document.getElementById("info").innerHTML = Info.VisibleText + Info.DefaultText
}


// show forms to get directions
function switchtoDirections() {
   document.getElementById("search_label").innerHTML = "Directions";
   document.getElementById("search_input").innerHTML = "<input type='text' size='30' name='from' value='' />"
         + " To<input type='text' size='30' name='to' value='' onChange='showDirections(form.from.value,form.to.value);switchtoDirections()'/>"
         + "<input type='button' value='Go' onclick=showDirections(form.from.value,form.to.value) />"
   document.getElementById("search_type").innerHTML = "<input type='button' value='Search'  onclick='switchtoSearch()' />"
         + "<input type='button' value='Directions' disabled='true' onclick='' />"

}


// show form to search for a location
function switchtoSearch() {
   document.getElementById("search_label").innerHTML = "Search";
   document.getElementById("search_input").innerHTML = "<input type='text' size='40' name='address' value='' onChange='showAddress(form.address.value);switchtoSearch()'/>"
         + "<input type='button' value='Go' onclick=showAddress(form.address.value) />"
   document.getElementById("search_type").innerHTML = "<input type='button' value='Search' disabled='true' onclick='' />"
         + "<input type='button' value='Directions'  onclick=switchtoDirections() />"
}
