/* GEOCODING SERVICE */

var addressLocationCache = new Array();

function yahooPrecisionToScore(precision)
{
  switch(precision)
  {
    case "address":
      return 3;
    case "street":
      return 2;
    case "zip+4":
      return 1;
    default:
      return 0;
  }
}

function getLocationFromAddress(address)
{
  if(addressLocationCache[address] != null)
    return addressLocationCache[address];
    
  var url = "geocode20.php?address=" + escape(address);
  
  //alert("getLocationFromAddress: " + url);
  
  var xmlDoc = getXML(url);
  
 // alert("xmlDoc.xml" + xmlDoc.xml);
  
  if(xmlDoc == null)
  {
    alert("Could not access GeoCoder service at this time.");
    return null;
  }
  var xmlBestResult = null;
  var bestScore = 0;
  // alert("DEV Yahoo!GeoCoder XML Response (IE ONLY):\n" + xmlDoc.xml);
  var xmlResults = xmlDoc.getElementsByTagName("Result");
  for(var i=0; i<xmlResults.length; i++)
  {
    var xmlResult = xmlResults[i];
    var score = yahooPrecisionToScore( xmlResult.getAttribute("precision") );
    if(score > bestScore)
    {
      xmlBestResult = xmlResult;
      bestScore = score;
    }
  }
  if(bestScore == 0)
    return null;
  var lon = xmlBestResult.getElementsByTagName("Longitude")[0].firstChild.nodeValue;
  var lat = xmlBestResult.getElementsByTagName("Latitude")[0].firstChild.nodeValue;
  var locAddress = xmlBestResult.getElementsByTagName("Address")[0].firstChild.nodeValue;
//  window.status = locAddress + " (" + address + ") is at " + lon + ", " + lat;
  var loc = new Location(locAddress, new GLatLng(lat, lon));
  addressLocationCache[locAddress] = loc;
  return loc;
}

