
function getXMLHttpRequest()
{
  var ret = null;
  // Try to get the right object for different browser
  try
  {
    // Firefox, Opera 8.0+, Safari, IE7+
    ret = new XMLHttpRequest();
  }
  catch (e)
  {
    // Internet Explorer
    try
    {
      ret = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      ret = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  
  return ret;
}

var whenReadyCallback;
var ajaxObj = null;

function getRegions(countryId, callback)
{
  if (ajaxObj != null)  return;
  
  var url = "/location/regions/?country_id=" + countryId;
  ajaxObj = getXMLHttpRequest();
  ajaxObj.open("get", url);
  whenReadyCallback = callback;
  ajaxObj.onreadystatechange = whenReady;
  ajaxObj.send(null);
}

function getCities(countryId, regionId, callback)
{
  if (ajaxObj != null)  return;
  
  var url = "/location/cities/?country_id=" + countryId + "&region_id=" + regionId;
  ajaxObj = getXMLHttpRequest();
  ajaxObj.open("get", url);
  whenReadyCallback = callback;
  ajaxObj.onreadystatechange = whenReady;
  ajaxObj.send(null);
}

function whenReady()
{
  if (ajaxObj.readyState == 4)
  {
    if (ajaxObj.status == 200)
    {
      whenReadyCallback(ajaxObj);
    }
  }
}