function zmienKrajeLotniska()
{
  loadAjaxSelector('country',getSearchValues());
  loadAjaxSelector('departure_city',getSearchValues());
  czyscRegionHotel();
}

function getSearchValues()
{
    if (document.getElementById('lastminute').checked) sLastminute = '1';
    else sLastminute = '0';
    return( 'country='+gEl('country').value+'&region='+gEl('region').value+'&transport='+gEl('transport').value+'&kontynent='+gEl('kontynent').value+'&lastminute='+sLastminute );
}

function getSearchValuesHidden() {
	if (document.getElementById('lastminute').checked) sLastminute = '1';
	else sLastminute = '0';
	return( 'country='+gEl('country_hid').value+'&region='+gEl('region_hid').value+'&transport='+gEl('transport_hid').value+'&kontynent='+gEl('kontynent').value+'&lastminute='+sLastminute );
}
  
function loadAjaxSelector( a_Name, a_Warunki ) {
//    urlAjax = engineDir + "ajax/selector.php?id_client="+ idClient +"&typ=" + a_Name + "&" + a_Warunki;
    urlAjax = "ajax_selector.php?id_client="+ idClient +"&typ=" + a_Name + "&" + a_Warunki;
    spanThrobber = document.getElementById("thr_" + a_Name);
    if (spanThrobber) spanThrobber.innerHTML = '<IMG SRC="images/schemat/throbber.gif" width="16" height="16">';
    wyczyscCombo(gEl(a_Name),'dowolny');
    ajaxFillCombo();
}

var urlAjax = "";

function ajaxFillCombo() {
	ajaxGETCombo(urlAjax);
}

function ajaxGETCombo(url) {
          advAJAX.get({
                       url: url,
                       onSuccess : function(obj) {obsluzDane(obj.responseXML)},
                       onFinalization : function(obj) {finalizationAjax(obj.responseXML)}
          });
}

function obsluzDane (xmldoc) {
	var id = xmldoc.getElementsByTagName("RECORDSET")[0];
	var typ = xmldoc.getElementsByTagName("TYP")[0];
//	alert(typ);
	if (!typ) {
	  alert("Błąd AJAX-a"); 
	  return;
	}
	
	if (typ.childNodes[0].nodeValue){
		wypelnijCombo(xmldoc.getElementsByTagName("RECORDSET")[0], typ.childNodes[0].nodeValue);
	}
}

function finalizationAjax(xmldoc) {
    var typ = xmldoc.getElementsByTagName("TYP")[0];
    if ( !typ || !typ.childNodes[0].nodeValue ) return false;
    document.getElementById("thr_"+typ.childNodes[0].nodeValue).innerHTML = '';
}

function czyscRegionHotel()
{
    wyczyscCombo(gEl('region'),'dowolny'); // wyczysc regiony jesli zmieniono kraj
    wyczyscCombo(gEl('hotels_list'),'dowolny'); // wyczysc regiony jesli zmieniono kraj
}

function wypelnijCombo( xml, typ ) {
    
    var oCombo = document.getElementById(typ);
    if (!oCombo) return false;
    
    wyczyscCombo(oCombo,'dowolny');
	if ( xml.childNodes.length<1) {
	    return true;
	} else {
	    oCombo.disabled=false;
	}
	for (i=0; i < xml.childNodes.length; i++) {
		var wiersz = xml.childNodes[i];
		if (wiersz) {
		    if ( wiersz.getElementsByTagName("VALUE")[0].childNodes[0] && wiersz.getElementsByTagName("TEXT")[0].childNodes[0] ) {
        		var selValue = wiersz.getElementsByTagName("VALUE")[0].childNodes[0].nodeValue;
        		var selText = wiersz.getElementsByTagName("TEXT")[0].childNodes[0].nodeValue;
        		
        		oCombo.options[oCombo.options.length] = new Option(selText, selValue);
        		if (gEl(typ+'_hid').value == selValue) {
        		    oCombo.options[oCombo.options.length-1].selected=true;
        		}
		    }
		}
	}
}

function wyczyscCombo(oCombo,dowolne) {
    if (!oCombo) return false;
	for (i = oCombo.options.length - 1; i >= 0; i--) {
		oCombo.remove(i);    
	}
	oCombo.options[0] = new Option(dowolne, '0');
	oCombo.disabled=true;
}

function gEl(nazwa)
{
    var el = document.getElementById(nazwa);
    if (el) return el;
}