var CITYSTARTPOS = 0;
var CITYINCREMENT = 22;

function getCountrySex()
{
  url = HTTP_FULL_PATH + '/callback/cb_getcountry.php?getcountry&nulc=0';
  ajaxChainLoaderGet(url, getCountrySex_callback);
}
function getCountrySex_callback()
{
  var nl = null;
  if (this.req.responseXML) { nl = this.req.responseXML.getElementsByTagName('country'); }
  if (nl)
  {
    clearSelect('sex-id-country');
    if (M_COUNTRY_ID) { sel_country_id = M_COUNTRY_ID; } else { sel_country_id = 0; }
    var sel_country = byID('sex-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;
    }
    getRegionSex();
  }
}

function getRegionSexForCountry()
{
  CITYSTARTPOS = 0;
  disable('sex-id-region');
  sel = byID('sex-id-country');
  var id = sel.options[sel.selectedIndex].value;
  M_COUNTRY_ID = id;
  M_REGION_ID = 0;
  M_CITY_ID = 0;
  url = HTTP_FULL_PATH + '/callback/cb_getcountry.php?getregion&country_id='+id+'&nulc=0';
  ajaxChainLoaderGet(url, getRegionSex_callback);
}
function getRegionSex()
{
  disable('sex-id-region');
  if (M_COUNTRY_ID) { id = M_COUNTRY_ID; } else { id = 0; }
  url = HTTP_FULL_PATH + '/callback/cb_getcountry.php?getregion&country_id='+id+'&nulc=0';
  ajaxChainLoaderGet(url, getRegionSex_callback);
}
function getRegionSex_callback()
{
  var nl = null;
  if (this.req.responseXML) { nl = this.req.responseXML.getElementsByTagName('region'); }
  if (nl)
  {
    clearSelect('sex-id-region');
    disable('sex-id-region');
    if (M_REGION_ID) { sel_region_id = M_REGION_ID; } else { sel_region_id = 0; }
    var sel_region = byID('sex-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('sex-id-region');
    if (M_CITY_ID != 0) { getCitySex(); } else { getCitySexForRegion(); }
  }
}

function getCitySexForRegion()
{
  sel = byID('sex-id-region');
  var id = sel.options[sel.selectedIndex].value;
  M_REGION_ID = id;
  M_CITY_ID = 0;
  url = HTTP_FULL_PATH + '/callback/cb_getcountry.php?getcity&region_id='+id+'&nulc=0';
  ajaxChainLoaderGet(url, getCitySex_callback);
}
function getCitySex()
{
  if (M_REGION_ID) { id = M_REGION_ID; } else { id = 0; }
  url = HTTP_FULL_PATH + '/callback/cb_getcountry.php?getcity&region_id='+id+'&nulc=0';
  ajaxChainLoaderGet(url, getCitySex_callback);
}
function getCitySex_callback()
{
  var nl = null;
  if (this.req.responseXML) { nl = this.req.responseXML.getElementsByTagName('cities'); }
  if (nl)
  {
    if (M_CITY_ID) { sel_city_id = M_CITY_ID; } else { sel_city_id = 0; }
    var sel_city = byID('sex-id-city');
    sel_city.innerHTML = '';
    elLI = byID('sel-city-example').innerHTML;
    var content = '';
    //M_CITY_ID
    /*
    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 (sel_city_id == city_id)
      {
        content += elLI.replace('[$city_rus]',city_rus).replace('[$class]','feat2');

        break;
      }
    }
    */
    len = nl.length;
    var STARTPOS = CITYSTARTPOS;
    var ENDPOS = STARTPOS+CITYINCREMENT
    if (ENDPOS>len) { ENDPOS = len; }
    if (STARTPOS>0) { content += elLI.replace('[$city_rus]','&lt;&lt;&lt;').replace('[$class]','feat2').replace('[$onclick]','prev_sex_city(); return false;'); }
    for( var i=STARTPOS; i<ENDPOS; i++ )
    {
      var nli = nl.item(i);
      var city_id = parseInt(nli.getAttribute('city_id'));
      var city_rus = nli.getAttribute('city_rus');
      if (sel_city_id != city_id)
      {
        content += elLI.replace('[$city_rus]',city_rus).replace('[$class]','feat').replace('[$onclick]','');
      }
    }
    if (ENDPOS<len) { content += elLI.replace('[$city_rus]','&gt;&gt;&gt;').replace('[$class]','feat2').replace('[$onclick]','next_sex_city(); return false;'); }

    sel_city.innerHTML = content;
  }
  if (byID('id-country-2') && byID('id-region-2') && byID('id-city-2'))
  {
    ch_2();
    getCountry();
  }
}

function clearCITYPOS()
{
  CITYSTARTPOS = 0;
}

function prev_sex_city()
{
  CITYSTARTPOS -= CITYINCREMENT;
  getCitySexForRegion();
}

function next_sex_city()
{
  CITYSTARTPOS += CITYINCREMENT;
  getCitySexForRegion();
}

function checkMsg()
{
  return false;
}
