var htmlentities_interpreter = document.createElement('div');
htmlentities_interpreter.style.display = 'none';

function refreshRegions()
{
  var form = document.MainForm;
  var country = form.country.options[form.country.selectedIndex].value;

  if(country == '')
  {
    form.region.options.length = 1;
    form.city.options.length = 1;
  }
  else
  {
    ajax.open('get', '/location_ajax.php?ajax=regions&country='+escape(country));
    ajax.onreadystatechange = showRegions;
    ajax.send(null);
  }
}

function showRegions()
{
  var form = document.MainForm;

  if( ajax.readyState == 4 )
  {
    var regions = ajax.responseText;

    if(regions == '')
    {
      form.region.options.length = 1;
      refreshCities();
    }
    else
    {
      displayRegions( regions, '\n', '' );
      form.city.options.length = 1;
    }
  }
}

function displayRegions( regionString, splitChar, selectedRegion )
{
  var form = document.MainForm;
  var sIndex = 0;
  regions = regionString.split(splitChar);
  form.region.options.length = 1;

  for( var i = 0; i < regions.length; i++ )
  {
    region = regions[i].split(',');
    htmlentities_interpreter.innerHTML = region[1];
    form.region.options[i+1] = new Option( htmlentities_interpreter.innerHTML, region[0] );
    if( region[0] == selectedRegion )
    {
      sIndex = i + 1;
    }
  }
  
  form.region.options[sIndex].selected = true;
  
}

function refreshCities()
{
  var form = document.MainForm;
  var country = form.country.options[form.country.selectedIndex].value;
  var region = form.region.options[form.region.selectedIndex].value;

  if(region == '' && form.region.options.length > 1)
  {
    form.city.options.length = 1;
  }
  else
  {
    ajax.open('get', '/location_ajax.php?ajax=cities&country='+escape(country)+'&region='+escape(region));
    ajax.onreadystatechange = showCities;
    ajax.send(null);
  }
}

function showCities()
{
  var form = document.MainForm;

  if( ajax.readyState == 4 )
  {
    displayCities( ajax.responseText, '\n', '' );
  }
}

function displayCities( cityString, splitChar, selectedCity )
{
  var form = document.MainForm;
  var sIndex = 0;
  cities = cityString.split(splitChar);
  form.city.options.length = 1;

  for( var i = 0; i < cities.length; i++ )
  {
    city = cities[i].split(',');
    htmlentities_interpreter.innerHTML = city[1];
    form.city.options[i+1] = new Option( htmlentities_interpreter.innerHTML, city[0] );
    if( city[0] == selectedCity )
    {
      sIndex = i + 1;
    }
  }
  
  form.city.options[sIndex].selected = true;
}

function refreshLocation()
{
  var form = document.MainForm;
  var country = form.country.options[form.country.selectedIndex].value;
  var region = form.region.options[form.region.selectedIndex].value;
  var city = form.city.options[form.city.selectedIndex].value;

  if(city == '' && form.city.options.length > 1)
  {
    form.longitude.value = '';
    form.latitude.value = '';
  }
  else
  {
    ajax.open('get', '/location_ajax.php?ajax=location&country='+escape(country)+'&region='+escape(region)+'&city='+escape(city));
    ajax.onreadystatechange = showLocation;
    ajax.send(null);
  }
}

function showLocation()
{
  var form = document.MainForm;

  if( ajax.readyState == 4 )
  {
    var location = ajax.responseText;

    if(location == '')
    {
      form.longitude.value = '';
      form.latitude.value = '';
    }
    else
    {
      displayLocation(location);
    }
  }
}

function displayLocation( locationString )
{
  var form = document.MainForm;
  var location = locationString.split(',');
  form.longitude.value = location[0];
  form.latitude.value = location[1];
}

var postalCodeTest = /^\d{5}$/;

function refreshPCode()
{
  var form = document.MainForm;
  var postal_code = form.postal_code.value;

  if(postal_code.match(postalCodeTest))
  {
    ajax.open('get', '/location_ajax.php?ajax=postalcode&code='+escape(postal_code));
    ajax.onreadystatechange = showPCode;
    ajax.send(null);
  }
}

function showPCode()
{
  var form = document.MainForm;

  if( ajax.readyState == 4 )
  {
    var location = ajax.responseText;
    
    if(location == 'notfound')
    {
      return;
    }
    
    location = location.split('\n');
    
    displayLocation( location[0] );
    selectCountry( location[1] );
    displayRegions( location[3], ';', location[2] );
    if( location[4] )
    {
      displayCities( location[5], ';', location[4] );
    }
  }
}

function selectCountry( selectedCountry )
{
  var form = document.MainForm;
  var country = form.country;
  
  for( var i = 0; i < country.options.length; i++ )
  {
    if(country.options[i].value == selectedCountry)
    {
      country.options[i].selected = true;
      return;
    }
  }
}
