var countrySelect = null,
    regionSelect = null;

var request = false;
    try {
        request = new XMLHttpRequest();
    } catch (trymicrosoft) {
    try {
        request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
       try {
         request = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (failed) {
         request = false;
       }
     }
}

function getajaxzapros(url) {
    request.open("GET", url);
    request.onreadystatechange = update;
    request.send(null);
}

function update() {
    loadingBoxClose();
    document.getElementById('city_container').innerHTML = request.responseText;
}
function countryChanged() {
  countrySelect = $('country');
  countryCode = (countrySelect.options[countrySelect.selectedIndex]).value;
  regionSelect = $('region');
  regionLocation = $('region_location');
  cityLocation = $('city_container');

  if (countryCode != '351_7' && countryCode != '') {
    regionLocation.style.display="none";
    regionSelect.selectedIndex=0;
    loadingBoxOpen();
    getajaxzapros('add.new.httpRequest.phtml?country_id='+countryCode+'&region_id=0');
  } else {
    regionLocation.style.display="block";
    cityLocation.style.display="none";
  }
  countryPhocodeAutocomplete(countryCode,'phone_country');
}

function regionChanged() {
  countrySelect = $('country');
  countryCode = (countrySelect.options[countrySelect.selectedIndex]).value;
  regionSelect = $('region');
  regionCode = (regionSelect.options[regionSelect.selectedIndex]).value;
  regionLocation = $('region_location');
  cityLocation = $('city_container');

  if (regionSelect.value != '') {
    loadingBoxOpen();
    getajaxzapros('add.new.httpRequest.phtml?country_id='+countryCode+'&region_id='+regionCode);
//    cityLocation.style.display="block";
  }
}

function cityChanged() {
  citySelect = $('city');
  cityInput = $('city_name');
  citySpan = $('city_conteiner');
  cityCode = (citySelect.options[citySelect.selectedIndex]).value;
  if (cityCode==-1) {
    //citySelect.style.display='none';
    citySpan.style.display='block';
    cityInput.focus();
  } else {
    citySpan.style.display='none';
    cityInput.value='';
    countryPhocodeAutocomplete(cityCode,'phone_city');
  }
}

function getLocationInput(cityId,cityName){
  countrySelect = $('country');
  countryCode = (countrySelect.options[countrySelect.selectedIndex]).value;
  regionSelect = $('region');
  regionCode = (regionSelect.options[regionSelect.selectedIndex]).value;
  regionLocation = $('region_location');
  cityLocation = $('city_container');
  if (regionSelect.value != '' || countryCode!=0 && countryCode!='351_7') {
    loadingBoxOpen();
    getajaxzapros('add.new.httpRequest.phtml?country_id='+countryCode+'&region_id='+regionCode+'&city='+cityId+'&city_name='+cityName);
  }
  window.onload += function(){countryPhocodeAutocomplete(countryCode,'phone_country');};
}

function countryPhocodeAutocomplete(idCode, container){
  var reg=/\d+_(\d+)/
  var arr=reg.exec(idCode);
  if(arr!=null) {
      $(container).value=arr[1];
  }
}


function loadingBoxOpen(){
  cityLocation = $('city_container');
  $('city_container').style.display = 'none';
  $('loading_box').style.display = 'block';
}


function loadingBoxClose(){
  $('loading_box').style.display = 'none';
  $('city_container').style.display = 'block';
  return false;
}
