disabled_cb = new Array('BB','FB','ALL','NB','SUP','APRT','FAM','STUD','SUIT','BGW','VILL');


DOMAIN_XML = document.domain+"/xml";
 
function init(form,country_id,city_id,hotel_id,foros_only){
  var req = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('Microsoft.XMLHTTP');
  form.foros_only = foros_only;
  req.onreadystatechange = function(){
    if(req.readyState==4 && req.status==200){
      var xml = req.responseXML.XMLDocument ? req.responseXML.XMLDocument : req.responseXML;        
      set_geo(form,country_id,city_id,hotel_id,xml);
    }
  }
  _getURL = 'http://'+DOMAIN_XML+'/xml_selects.php?country_id='+country_id+'&city_id='+city_id+'&hotel_id='+hotel_id+'&r='+Math.random();      
  req.open('GET',_getURL,true);
  req.send('');
  
}


function set_geo(form,country_id,city_id,hotel_id,xml){
    
    var countries = xml.getElementsByTagName('country');
    var cities = xml.getElementsByTagName('city');
    var hotels = xml.getElementsByTagName('hotel');    
    if(countries.length>0 && form.countryid.options.length==0) 
	setOptions(form.countryid,countries,country_id,'Выберите страну');
    if(cities.length>0) setOptions(form.cityid,cities,city_id,'Выберите город')
    else disableOptions(form.cityid,'Выберите город');
    if(hotels.length>0 && form.hotelid) setOptions(form.hotelid,hotels,hotel_id,'Выберите отель');      
    else disableOptions(form.hotelid,'Выберите отель');    
     change_hotel(form.hotelid);
}  
  
function change_country(sel ){  
 
  var country_id = sel.options[sel.selectedIndex].value;  
  var req = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('Microsoft.XMLHTTP');
  req.onreadystatechange = function(){
    if(req.readyState==4 && req.status==200){
      var xml = req.responseXML.XMLDocument ? req.responseXML.XMLDocument : req.responseXML;
      set_geo(sel.form,country_id,-1,-1,xml);
    }
  }
  _getURL = 'http://'+DOMAIN_XML+'/xml_selects.php?foros_only='+sel.form.foros_only+'&country_id='+country_id+'&city_id=-1&hotel_id=-1&r='+Math.random();        
  req.open('GET',_getURL,true);
  req.send('');
}

function change_city(sel){
  var city_id = sel.options[sel.selectedIndex].value;
  var country_id = sel.form.countryid.options[sel.form.countryid.selectedIndex].value;
  var req = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('Microsoft.XMLHTTP');
  req.onreadystatechange = function(){
    if(req.readyState==4 && req.status==200){
      var xml = req.responseXML.XMLDocument ? req.responseXML.XMLDocument : req.responseXML;
      set_geo(sel.form,country_id,city_id,-1,xml);
    }
  }
  _getURL = 'http://'+DOMAIN_XML+'/xml_selects.php?foros_only='+sel.form.foros_only+'&country_id='+country_id+'&city_id='+city_id+'&hotel_id=-1&r='+Math.random();        
  req.open('GET',_getURL,true);
  req.send('');
}

function change_hotel(sel){
  var hotel_id = sel.options[sel.selectedIndex].value;
  var city_id = sel.form.cityid.options[sel.form.cityid.selectedIndex].value;
  var country_id = sel.form.countryid.options[sel.form.countryid.selectedIndex].value;
  if ((hotel_id > 0 ) && (hotel_id != "undefined" ) )  {  
    if (document.getElementById('vote_name') != null) {
     document.getElementById('id_votes').style.display = '';   
     document.getElementById('vote_name').innerHTML = sel.options[sel.selectedIndex].text;      
     document.getElementById('head_name').innerHTML = sel.options[sel.selectedIndex].text;
    }
  } else {
    if (document.getElementById('id_votes') != null)
            document.getElementById('id_votes').style.display = 'none';   
  
  } 
} 
function setOptions(sel,opts,activeId,str) {
  var j=0;
  sel.disabled = false;
  var o = document.createElement('OPTION');
  o.value = -1;
  o.text = str;
  sel.innerHTML = '';
  sel.options.add(o);
  for(var i=0; i<opts.length; i++){
    var id = opts[i].getAttribute('id');
    var tc = opts[i].getAttribute('tcnt');
    var text = opts[i].firstChild.data;    
    o = document.createElement('OPTION');
    o.tc = tc;
    o.value = opts[i].getAttribute('id');
    o.text = text;
    sel.options.add(o);
    if(id==activeId) o.selected = true;
  }
   
  
}

function disableOptions(sel,txt) {
  if(!sel) return;  
  var opt = document.createElement('option');
  opt.text = txt;
  sel.innerHTML = '';
  sel.options.add(opt);
  sel.disabled = true;  
}

function form_submit(sbm) {
  var form = sbm.form;

  var countrysel = form.countryid;
  var citysel = form.cityid;
  var hotelsel = form.hotelid;
  var inp = document.createElement('INPUT');
      inp.type = 'hidden';
   
  var tcnt = 0;
if (hotelsel.options.length > 0) {  
  if(hotelsel.options[hotelsel.selectedIndex].value>0){
    tcnt = hotelsel.options[hotelsel.selectedIndex].tc;   
    inp.name = 'hotelValue';    
    inp.value = hotelsel.options[hotelsel.selectedIndex].text;    
    form.appendChild(inp); 
   } 
  }
  if (citysel.options.length>0) {
   if(citysel.options[citysel.selectedIndex].value>0){
    tcnt = citysel.options[citysel.selectedIndex].tc;
    inp.name = 'cityValue';
    inp.value = citysel.options[citysel.selectedIndex].text;    
     form.appendChild(inp); 
   }  
  }
 
 if (countrysel.options.length>0) { 
  if(countrysel.options[countrysel.selectedIndex].value>0){
    tcnt = countrysel.options[countrysel.selectedIndex].tc;
    inp.name = 'countryValue';
    inp.value = countrysel.options[countrysel.selectedIndex].text;    
    form.appendChild(inp);     
  } 
  }
    
  if(tcnt==0){ 
    var inp = document.createElement('INPUT');
    inp.type = 'hidden';
    inp.name = 'notours';
    inp.value = 1;
    form.appendChild(inp);
    for(var i=0; i<disabled_cb.length; i++)
      if(form.elements[disabled_cb[i]].type == 'checkbox')
        form.elements[disabled_cb[i]].checked = false;
      else if(form.elements[disabled_cb[i]].type == 'hidden')
        form.elements[disabled_cb[i]].value = 0;
    // form.mod.value = 'custom_search';
  } 
  form.submit();
}

function getFormByElement(el) {
  while(el.tagName!='FORM') el = el.parentNode;
  return el.tagName == 'FORM' ? el : null;
}

function change_inputs(el) {
  var form =  getFormByElement(el);  
  if(form.inp_mode.value == 'inp'){
    form.countryid.style.display = 'block';
    form.countryidtxt.style.display = 'none';
    form.countryidtxt.value = '';
    form.cityid.style.display = 'block';
    form.cityidtxt.style.display = 'none';
    form.cityidtxt.value = '';
    form.hotelid.style.display = 'block';
    form.hotelidtxt.style.display = 'none';
    form.hotelidtxt.value = '';
    document.getElementById('sel_text').style.display = 'block';
    document.getElementById('inp_text').style.display = 'none';
    form.inp_mode.value = 'sel';
  }else{
    form.countryid.style.display = 'none';
    if(form.countryid.value>0) form.countryidtxt.value = form.countryid.options[form.countryid.selectedIndex].text;
    form.countryidtxt.style.display = 'block';
    form.cityid.style.display = 'none';
    if(form.cityid.value>0) form.cityidtxt.value = form.cityid.options[form.cityid.selectedIndex].text;
    form.cityidtxt.style.display = 'block';
    form.hotelid.style.display = 'none';
    if(form.hotelid.value>0) form.hotelidtxt.value = form.hotelid.options[form.hotelid.selectedIndex].text;
    form.hotelidtxt.style.display = 'block';
    document.getElementById('sel_text').style.display = 'none';
    document.getElementById('inp_text').style.display = 'block';
    form.inp_mode.value = 'inp';    
  }
  return false;
}

