/*
var xmlDoc;
var cityInfo;
var cities = [];
*/

/**
 * Provides suggestions for city names (USA).
 */
function CitySuggestions() {
   try {
      var xmlDoc = null;
      //state = document.getElementById("state").value;
      beginCity = document.getElementById("city").value;
      if (window.XMLHttpRequest) {
         xmlDoc = new window.XMLHttpRequest();
      }
      else {
         if (window.ActiveXObject) {
            // IE 5.x and IE 6.x compliant.
            xmlDoc = new ActiveXObject('MSXML2.XMLHTTP.3.0');
         }
      }
      //xmlDoc.open("GET","citysuggest.php?state="+state,false);
      xmlDoc.open("GET","citysuggest.php?str="+beginCity,false);
      xmlDoc.onreadystatechange = function(){
         if(xmlDoc.readyState == 4) {
            if(xmlDoc.status == 200) {
               xmlDoc=xmlDoc.responseXML;
               cityInfo=xmlDoc.getElementsByTagName("location");
               for (i=0; i<cityInfo.length; i++) {
                 cities[i] = cityInfo[i].getElementsByTagName("city")[0].childNodes[0].nodeValue + ", " + cityInfo[i].getElementsByTagName("region")[0].childNodes[0].nodeValue + ", " + cityInfo[i].getElementsByTagName("countrylong")[0].childNodes[0].nodeValue;
               }
            }
         }
      };
      xmlDoc.send(null);
   }
   catch(Error) {
      //alert("Description:"+Error.description);
   }
}

/**
 * Request suggestions for the given autosuggest control. 
 * @scope protected
 * @param oAutoSuggestControl The autosuggest control to provide suggestions for.
 */
CitySuggestions.prototype.requestSuggestions = function (oAutoSuggestControl /*:AutoSuggestControl*/,
                                                          bTypeAhead /*:boolean*/) {
   var aSuggestions = [];
   var sTextboxValue = oAutoSuggestControl.textbox.value;
   var maxSuggestions = 10;

   getCitySuggestions();
   if (sTextboxValue.length > 0){
      //search for matching states
      for (var i=0; i < cities.length; i++) {
//alert(cities[i]);
         var lc_city = cities[i].toLowerCase();
         var cmpStr = sTextboxValue.toLowerCase();
         if (lc_city.indexOf(cmpStr) == 0) {
            aSuggestions.push(cities[i]);
            if (aSuggestions.length == maxSuggestions) {
               break;
            }
         } 
      }
   }

   //provide suggestions to the control
   oAutoSuggestControl.autosuggest(aSuggestions, bTypeAhead);
};

function updateLonLat(obj) {
   var cityFound = false;
   for (var i=0; i < cities.length; i++) {
      var lc_city = cities[i].toLowerCase();
      var cmpStr = document.getElementById("city").value.toLowerCase();
      if (lc_city.indexOf(cmpStr) == 0) {
         document.getElementById("lon").value = cityInfo[i].getElementsByTagName("lon")[0].childNodes[0].nodeValue;
         document.getElementById("lat").value = cityInfo[i].getElementsByTagName("lat")[0].childNodes[0].nodeValue;
         cityFound = true;
      }
   }
   if (cityFound) {
      document.changelocation.action = "changelocation.php";
      document.changelocation.submit();
   } else {
      alert("City not found.  Please try again.");
      document.getElementById("lon").value = "";
      document.getElementById("lat").value = "";
   }
}
