

// setup the request object... 
// this came from rasmus's tutorial on these things....

function createRequestObject()
  {
  var ro;
  var browser = navigator.appName;
  if(browser == 'Microsoft Internet Explorer')
    {
    ro = new ActiveXObject('Microsoft.XMLHTTP');
    }
  else
    {
    ro = new XMLHttpRequest();
    }
  isBusy = true;
  return ro;
  } // end function

// initialize the xml request
var http = createRequestObject();

http.open('GET','weather_locations.php?nothing=true',true);
var isBusy = true;
http.onreadystatechange = catchData;
http.send(null);

function catchData()
  {
  if (http.readyState == 4)
    {
    // alert(http.responseText);
    }
  isBusy = false;
  }

function getCountryList()
  {
  if (isBusy)
    {
   
    http.abort();
    setTimeout('getCountryList()', 1000);
    }
  else
    { 
    isBusy = true;
    http.open('get', 'weather_locations.php?action=getWeatherCountries&noCache=');
    http.onreadystatechange = handleGetCountryList;
    http.send(null);
    }
  }
  
// and this is that engine...
function handleGetCountryList()
  {
  if(http.readyState == 4)
    {
    isBusy = false;
    var response = http.responseText;
    // clear the select box...
    var weatherDropdown = document.getElementById('wms_weatherCountryList');
    weatherDropdown.innerHTML = '';
    
    // add the default
    newOption = document.createElement('option');
    newOption.text = 'Select Country...';
    newOption.innerText = 'Select Country...';
    newOption.value = '';
    weatherDropdown.appendChild(newOption);
    
    // since the object is json, we can process it like an object...
    var countryList = response.parseJSON();
    for (var i=0; i<countryList.length; i++)
      {
      newOption = document.createElement('option');
      newOption.text = countryList[i];
      newOption.value = countryList[i];
      newOption.innerText = countryList[i];
      weatherDropdown.appendChild(newOption);
      } 
    } // end if
  } // end function







function handleWeatherCountryChoice()
  {
 if (document.getElementById('wms_weatherCountryList').value == '')
    {
     document.getElementById('wms_weatherStateSelect').style.display = 'none';
     document.getElementById('wms_weatherStationSelect').style.display = 'none';
     document.getElementById('wms_chooseStation').style.display = 'none';
    }
   else if (document.getElementById('wms_weatherCountryList').value == 'United States')
    {
    document.getElementById('wms_weatherStateSelect').style.display = 'block';
    document.getElementById('wms_weatherStationSelect').style.display = 'none';
    document.getElementById('wms_chooseStation').style.display = 'none';
    }
  else
    {
   getStationList(document.getElementById('wms_weatherCountryList').value);
    document.getElementById('wms_weatherStateSelect').style.display = 'none';
    document.getElementById('wms_weatherStationSelect').style.display = 'block';
    document.getElementById('wms_chooseStation').style.display = 'none';
    }
  } // end function


function selectStation()
  {
  stationSelection = document.getElementById('wms_stationChoiceList').value;
  createCookie('weatherStation', stationSelection, 365);
  document.location = '';
  }

function getStateStations()
  {
  state = document.getElementById('wms_stateList').value;
  document.getElementById('wms_weatherStationSelect').style.display = 'block';
  document.getElementById('wms_chooseStation').style.display = 'block';
  http.open('get', 'weather_locations.php?action=getStateStations&stateName=' + state + '&noCache=');
  http.onreadystatechange = handleStateStations;
  http.send(null);
  }
  
  
function handleStateStations()
  {
  if(http.readyState == 4)
    {
    var response = http.responseText;
    
    // clear the select box...
    var stationSelecter = document.getElementById('wms_weatherStationSelect');
    stationSelecter.innerHTML = '';
    
    newSelect  = document.createElement('select');
    newSelect.id = 'wms_stationChoiceList';
    newSelect.onchange = showWeatherSubmit;
    stationSelecter.appendChild(newSelect);
    
    // add the default
    newOption = document.createElement('option');
    newOption.text = 'Select Station...';
    newOption.innerText = 'Select Station...';
    newOption.value = '';
    newSelect.appendChild(newOption);
    
    
    
    // since the object is json, we can process it like an object...
    var stationList = response.parseJSON();
    for (var i=0; i<stationList.length; i++)
      {
      newOption = document.createElement('option');
      if (stationList[i]['placeName'].indexOf(',') > -1)
        {
        var dataArray = stationList[i]['placeName'].split(',');
        newOption.text = dataArray[0];
        newOption.innerText = dataArray[0];
        }
      else
        {
        newOption.text = stationList[i]['placeName'];
        newOption.innerText = stationList[i]['placeName'];
        }

      newOption.value = stationList[i]['stationID'];
      newSelect.appendChild(newOption);
      } 
    } // end if
  } // end function

function showWeatherSubmit()
  {
    document.getElementById('wms_chooseStation').style.display = 'block';
  }

function getStationList(country)
  {
  document.getElementById('wms_weatherStateSelect').style.display = 'none';
  http.open('get', 'weather_locations.php?action=getCities&countryName=' + country + '&noCache=');
  http.onreadystatechange = handleStationList;
  http.send(null);
  }


function handleStationList()
  {
  if(http.readyState == 4)
    {
    var response = http.responseText;
    
    // clear the select box...
    var stationSelecter = document.getElementById('wms_weatherStationSelect');
    stationSelecter.innerHTML = '';
    
    newSelect  = document.createElement('select');
    newSelect.id = 'wms_stationChoiceList';
    newSelect.onchange = showWeatherSubmit;
    stationSelecter.appendChild(newSelect);
    
    // add the default
    newOption = document.createElement('option');
    newOption.text = 'Select Station...';
    newOption.innerText = 'Select Station...';
    newOption.value = '';
    newSelect.appendChild(newOption);
    
   
    
    // since the object is json, we can process it like an object...
    var stationList = response.parseJSON();
    for (var i=0; i<stationList.length; i++)
      {
      newOption = document.createElement('option');
      newOption.text = stationList[i]['placeName'];
      newOption.innerText = stationList[i]['placeName'];
      newOption.value = stationList[i]['stationID'];
      
      newSelect.appendChild(newOption);
      } 
    } // end if
  } // end function





// international weather pop...


function showInternationalWeather()
  {
    if(document.getElementById('wms_weatherCountryListDiv').style.display == 'block'){
      document.getElementById('wms_weatherCountryListDiv').style.display = 'none';
    }
    else{
      document.getElementById('wms_weatherCountryListDiv').style.display = 'block';
      getCountryList(); 
    }
  }

  
