var M_COUNTRY_ID;
var M_REGION_ID;
var M_CITY_ID;
var NEEDNULL;
var sel_id_country = 'id-country';
var sel_id_region = 'id-region';
var sel_id_city = 'id-city';


function getCountry()
{
  if (NEEDNULL) { nulc = 1; } else { nulc = 0; }
  ld.url = HTTP_FULL_PATH + '/callback/cb_getcountry.php?getcountry&nulc='+nulc;
  ld.method = 'GET';
  ld.onload = getCountry_callback;
  ld.loadXMLDoc();
}
function getCountry_callback()
{
  var nl = null;
  if (this.req.responseXML) { nl = this.req.responseXML.getElementsByTagName('country'); }
  if (nl)
  {
    clearSelect(sel_id_country);
    if (M_COUNTRY_ID) { sel_country_id = M_COUNTRY_ID; } else { sel_country_id = 0; }
    var sel_country = byID(sel_id_country);
    for( var i=0; i<nl.length; i++ )
    {
      var nli = nl.item(i);
      var country_id = parseInt(nli.getAttribute('country_id'));
      var country_rus = nli.getAttribute('country_rus');
      if (country_id == sel_country_id) { selected = true; } else { selected = false; }
      newOption = newopt(country_id,country_rus,selected);
			sel_country.appendChild(newOption);
			newOption = null;
    }
    getRegion();
  }
}

function getRegionForCountry()
{
  disable(sel_id_region);
  disable(sel_id_city);
  sel = byID(sel_id_country);
  var id = sel.options[sel.selectedIndex].value;
  M_COUNTRY_ID = id;
  M_REGION_ID = 0;
  M_CITY_ID = 0;
  if (NEEDNULL) { nulc = 1; } else { nulc = 0; }
  ld.url = HTTP_FULL_PATH + '/callback/cb_getcountry.php?getregion&country_id='+id+'&nulc='+nulc;
  ld.method = 'GET';
  ld.onload = getRegion_callback;
  ld.loadXMLDoc();
}
function getRegion()
{
  disable(sel_id_region);
  disable(sel_id_city);
  if (M_COUNTRY_ID) { id = M_COUNTRY_ID; } else { id = 0; }
  if (NEEDNULL) { nulc = 1; } else { nulc = 0; }
  ld.url = HTTP_FULL_PATH + '/callback/cb_getcountry.php?getregion&country_id='+id+'&nulc='+nulc;
  ld.method = 'GET';
  ld.onload = getRegion_callback;
  ld.loadXMLDoc();
}
function getRegion_callback()
{
  var nl = null;
  if (this.req.responseXML) { nl = this.req.responseXML.getElementsByTagName('region'); }
  if (nl)
  {
    clearSelect(sel_id_region);
    disable(sel_id_region);
    if (M_REGION_ID) { sel_region_id = M_REGION_ID; } else { sel_region_id = 0; }
    var sel_region = byID(sel_id_region);
    for( var i=0; i<nl.length; i++ )
    {
      var nli = nl.item(i);
      var region_id = parseInt(nli.getAttribute('region_id'));
      var region_rus = nli.getAttribute('region_rus');
      if (region_id == sel_region_id) { selected = true; } else { selected = false; }
      newOption = newopt(region_id,region_rus,selected);
			sel_region.appendChild(newOption);
			newOption = null;
    }
    enable(sel_id_region);
    if (M_CITY_ID != 0) { getCity(); } else { getCityForRegion(); }
  }
}

function getCityForRegion()
{
  disable(sel_id_city);
  sel = byID(sel_id_region);
  var id = sel.options[sel.selectedIndex].value;
  M_REGION_ID = id;
  M_CITY_ID = 0;
  if (NEEDNULL) { nulc = 1; } else { nulc = 0; }
  ld.url = HTTP_FULL_PATH + '/callback/cb_getcountry.php?getcity&region_id='+id+'&nulc='+nulc;
  ld.method = 'GET';
  ld.onload = getCity_callback;
  ld.loadXMLDoc();
}
function getCity()
{
  disable(sel_id_city);
  if (M_REGION_ID) { id = M_REGION_ID; } else { id = 0; }
  if (NEEDNULL) { nulc = 1; } else { nulc = 0; }
  ld.url = HTTP_FULL_PATH + '/callback/cb_getcountry.php?getcity&region_id='+id+'&nulc='+nulc;
  ld.method = 'GET';
  ld.onload = getCity_callback;
  ld.loadXMLDoc();
}
function getCity_callback()
{
  var nl = null;
  if (this.req.responseXML) { nl = this.req.responseXML.getElementsByTagName('cities'); }
  if (nl)
  {
    clearSelect(sel_id_city);
    disable(sel_id_city);
    if (M_CITY_ID) { sel_city_id = M_CITY_ID; } else { sel_city_id = 0; }
    var sel_city = byID(sel_id_city);
    for( var i=0; i<nl.length; i++ )
    {
      var nli = nl.item(i);
      var city_id = parseInt(nli.getAttribute('city_id'));
      var city_rus = nli.getAttribute('city_rus');
      if (city_id == sel_city_id) { selected = true; } else { selected = false; }
      newOption = newopt(city_id,city_rus,selected);
			sel_city.appendChild(newOption);
			newOption = null;
    }
    enable(sel_id_city);
    checkIsNulc();
  }
  autostartfunct();
  setTimeout('checkMsg()',60000);
}

function checkIsNulc()
{
  /*
  var sel = byID(sel_id_country);
  var id = sel.options[sel.selectedIndex].value;
  if (id == 0) 
  {
    disable(sel_id_region);
    disable(sel_id_city);
  }
  var sel = byID(sel_id_region);
  var id = sel.options[sel.selectedIndex].value;
  if (id == 0) 
  {
    disable(sel_id_city);
  }
  */
}
