

var  gs_LastSelectedCountry = '';

function zemeChange(typ,idRegion, idSel) {
    var cmreg1 = document.getElementById('cmreg1ID');
    var oContext = new Object();
        oContext.id = idRegion;
        oContext.idSel = idSel;
        oContext.onload = false;
    if (idRegion.length == 0)
    {
        disable_adv_entry('cmreg1');    
    }
    else
    {
        var button   = document.getElementById('cmreg1A');
        if ( button.disabled ) {
            button.disabled = false;
            button.className = 'adv_ns';
            button.onclick = function(){ fceEnableAdv(this, 'cmreg1','lokalita'); };
        }
    }

    if ( cmreg1 ) {
        if (!send_xmlhttprequest(adv_obsluha, 'GET', 'getOblasti.php?id='+idRegion, oContext)) {
            return false;
        }
    }
        
    return true;
}

function disable_adv_entry(id)
{
    var button   = document.getElementById(id+'A');
    var div   = document.getElementById(id+'_div_ID');
    if ( div ) {
        p = div.parentNode;
        p.removeChild( div );
    }
    button.disabled = true;
    button.className = 'adv_s';
    button.onclick = function(){ javascript:void(0); };
}

function regionChange(typ,idRegion, idSel) {
    // odeslání požadavku na aktualizaci dat
    var oContext = new Object();
    oContext.id = idRegion;
    oContext.idSel = idSel;

    if (!send_xmlhttprequest(zeme_obsluha, 'GET', 'getOblasti.php?id='+idRegion, oContext)) {
        return false;
    }
    
    return true;
}


    
function remove_selectbox( id)
{
    var label     = document.getElementById(id+'l');
    var sbn       = document.getElementById(id);
    sbn.innerHTML = '';
    label.innerHTML = '';    
}


function fill_selectbox_from_elements( id, name, elementArray, idSel)
{
    var label     = document.getElementById(id+'l');
    var sbn       = document.getElementById(id);
    var inner_select ="";
    
    var se;

    if ( elementArray.length > 0)
    {        
        var attr;
        label.innerHTML = name;

        inner_select = '<select name="cm'+id+'" class="oblast" onchange="return !regionChange(null,this.value,null);" >';
        inner_select = inner_select + '<option value=""> (nerozhoduje) </option>'; 
        
        for (var m=0; m < elementArray.length; m++)
        {
            inner_select = inner_select + '<option value="'+elementArray[m].attributes[0].value+'" ';
            if(elementArray[m].attributes[0].value==idSel) {
                inner_select = inner_select + 'selected="selected" ';
            }
            inner_select = inner_select +'>'+elementArray[m].firstChild.data+'</option>';
        }
        inner_select = inner_select + '</select>';
        sbn.innerHTML = inner_select;
    }
    else if ( sbn.hasChildNodes() )
    {
        sbn.innerHTML = '';
        label.innerHTML = '';
    }
}

function zeme_obsluha(xmlhttp, content) {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var id = content.id;
      
        if (id.length == 2 ) /* Country selected */
        {
          var oblasti = xmlhttp.responseXML.getElementsByTagName('oblast');
          var lokality = new Array();
          
          fill_selectbox_from_elements('reg1', 'letovisko', lokality, content.idSel);
          if ( oblasti.length == 0)
          {
              lokality = xmlhttp.responseXML.getElementsByTagName('lokalita');
              fill_selectbox_from_elements('reg1', 'Letovisko', lokality, content.idSel);
              remove_selectbox('reg2');
          }
          else
          {
            fill_selectbox_from_elements('reg1', 'Destinace', oblasti, content.idSel);
          }
        }
        else
        if (id.length == 4) /* Region selected */
        {
          var lokality = xmlhttp.responseXML.getElementsByTagName('lokalita');
          fill_selectbox_from_elements('reg2', 'Letovisko', lokality, content.idSel);
        }         
    }
}

function fadeOut(id, time) {
	target = document.getElementById(id);
	alpha = 100;
	timer = (time*1000)/50;
	var i = setInterval(
			function() {
				if (alpha <= 0)
					clearInterval(i);
				setAlpha(target, alpha);
				alpha -= 2;
			}, timer);
}

function fadeIn(id, time) {
	target = document.getElementById(id);
	alpha = 0;
	timer = (time*1000)/50;
	var i = setInterval(
			function() {
				if (alpha >= 100)
					clearInterval(i);
				setAlpha(target, alpha);
				alpha += 2;
			}, timer);
}

function setAlpha(target, alpha) {
	target.style.filter = "alpha(opacity="+ alpha +")";
	target.style.opacity = alpha/100;
}

function adv_add_input(name, data, text)
{
    element = document.getElementById(name+'ID');
    if ( !element ) {
        div = document.createElement('div');
        div.setAttribute('id', name+'_div_ID');
        div.setAttribute('class', 'adv_div ');
        div.style.display = 'block';
        
        a = document.createElement('a');
        a.setAttribute('href', 'javascript:void(0);');
        a.onclick = function() { fceDisableAdv( name+'_div_ID'); }
        a.appendChild(document.createTextNode('Zruš'));
        
        input = document.createElement('input');
        input.setAttribute('name', name);
        input.setAttribute('id', name+'ID');
        input.type = 'text';
        input.value  = data[0].firstChild.data;
        
        label = document.createElement('label');
        label.setAttribute('for', name+'ID');
        label.appendChild(document.createTextNode(text));
        
        div.appendChild(label);
        div.appendChild(input);
        div.appendChild(document.createTextNode(' '));
        div.appendChild(a);

        element = document.getElementById('adv_filtr');
        element.insertBefore(div);
    }
}

function clear_lokalita_div(name)
{
    var div = document.getElementById(name+'_div_ID');
    if ( div && div.hasChildNodes() )
    {
        while ( div.childNodes.length >= 1 )
        {
            div.removeChild( div.firstChild );
        }
    }
}

function adv_add_lokalita_drop_down(name, data, text, postText)
{
    var deactivate = document.getElementById(name+'A');
    deactivate.className = 'adv_s';
    
    element = document.getElementById(name+'ID');
    
    if ( !element ) {
        div = document.createElement('div');
        div.setAttribute('id', name+'_div_ID');
        div.style.display = 'block'
        
        a = document.createElement('a');
        a.setAttribute('href', 'javascript:void(0);');
        a.onclick = function() { fceDisableAdv( name+'_div_ID'); }
        a.appendChild(document.createTextNode('Zruš'));
        
        dropDown = document.createElement('select');
        dropDown.setAttribute('name', name);
        dropDown.setAttribute('id', name+'ID');
        option = document.createElement('option');
        option.value = '';
        option.appendChild(document.createTextNode(' (nerozhoduje) '));
        dropDown.appendChild(option);
        for (var m=0; m < data.length; m++)
        {
            option = document.createElement('option');
            option.value = data[m].attributes[0].value;
            option.appendChild(document.createTextNode(data[m].firstChild.data));
            if ( data[m].attributes.length > 1 )
                option.setAttribute('selected', 'selected');
            dropDown.appendChild(option);
        }
        
        label = document.createElement('label');
        label.setAttribute('for', name+'ID');
        label.appendChild(document.createTextNode(text));
        div.appendChild(label);
        div.appendChild(dropDown);
        div.appendChild(document.createTextNode(' '+postText+' '));
        div.appendChild(a);
        
        element = document.getElementById('adv_filtr');
        element.insertBefore(div, element.firstChild);
    }
    else
    {
        if ( element.hasChildNodes() ){
            while ( element.childNodes.length >= 1 )
                element.removeChild( element.firstChild );
        }
        option = document.createElement('option');
        option.value = '';
        option.appendChild(document.createTextNode(' (nerozhoduje) '));
        element.appendChild(option);
        for (var m=0; m < data.length; m++)
        {
            option = document.createElement('option');
            option.value = data[m].attributes[0].value;
            option.appendChild(document.createTextNode(data[m].firstChild.data));
            if ( data[m].attributes.length > 1 )
                option.setAttribute('selected', 'selected');         
            element.appendChild(option);
        }
    }
}

function adv_add_drop_down(name, data, text, postText)
{
    var deactivate = document.getElementById(name+'A');
    deactivate.className = 'adv_s';
    
    element = document.getElementById(name+'ID');
    
    if ( !element ) {
        div = document.createElement('div');
        div.setAttribute('id', name+'_div_ID');
        div.style.display = 'block'
        
        a = document.createElement('a');
        a.setAttribute('href', 'javascript:void(0);');
        a.onclick = function() { fceDisableAdv( name+'_div_ID'); }
        a.appendChild(document.createTextNode('Zruš'));
        
        dropDown = document.createElement('select');
        dropDown.setAttribute('name', name);
        dropDown.setAttribute('id', name+'ID');
        option = document.createElement('option');
        option.value = '';
        option.appendChild(document.createTextNode(' (nerozhoduje) '));
        dropDown.appendChild(option);
        for (var m=0; m < data.length; m++)
        {
            option = document.createElement('option');
            option.value = data[m].attributes[0].value;
            option.appendChild(document.createTextNode(data[m].firstChild.data));
            if ( data[m].attributes.length > 1 )
                option.setAttribute('selected', 'selected');         
            dropDown.appendChild(option);
        }
        
        label = document.createElement('label');
        label.setAttribute('for', name+'ID');
        label.appendChild(document.createTextNode(text));
        
        div.appendChild(label);
        div.appendChild(dropDown);
        div.appendChild(document.createTextNode(' '+postText+' '));
        div.appendChild(a);
        
        element = document.getElementById('adv_filtr');
        element.insertBefore(div, element.firstChild);
    }
}

function adv_obsluha(xmlhttp, content) {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var cena = xmlhttp.responseXML.getElementsByTagName('cena');
        var letiste = xmlhttp.responseXML.getElementsByTagName('letiste');
        var dp = xmlhttp.responseXML.getElementsByTagName('delkapobytu');
        var strava = xmlhttp.responseXML.getElementsByTagName('strava');
        var typ = xmlhttp.responseXML.getElementsByTagName('zajezd');
        var hotel = xmlhttp.responseXML.getElementsByTagName('hotel');
        var text = xmlhttp.responseXML.getElementsByTagName('text');
        var oblast = xmlhttp.responseXML.getElementsByTagName('oblast');
        var lokalita = xmlhttp.responseXML.getElementsByTagName('lokalita');
        
        if (cena.length) adv_add_drop_down('cmCena', cena, 'Cena za osobu: ', 'bez příplatků');
        if (letiste.length) adv_add_drop_down('cmLetiste', letiste, 'Místo odletu: ', '');
        if (strava.length) adv_add_drop_down('cmStrava', strava, 'Strava: ', '');
        if (typ.length) adv_add_drop_down('cmTyp', typ, 'Typ zájezdu: ', '');
        if (hotel.length) adv_add_drop_down('cmHotel', hotel, 'Kategorie hotelu: ', '');
        if (text.length) adv_add_input('cmText', text, 'Zadejte požadovaný text: ', '');
        if (dp.length) adv_add_drop_down('cmDp', dp, 'Délka pobytu: ', '');
        
        if ( content )
        {
            if ( content.id != gs_LastSelectedCountry )
                clear_lokalita_div('cmreg1');
            gs_LastSelectedCountry = content.id; 
        }
        if (oblast.length) adv_add_lokalita_drop_down('cmreg1', oblast, 'Upřesněte destinaci: ', '');
        if (lokalita.length) adv_add_lokalita_drop_down('cmreg1', lokalita, 'Upřesněte lokalitu: ', '');
        fill_std_form(xmlhttp, content);
    }   
}

  function fceEnableAdv( obj, id, filterSelect)
  {
    if ( filterSelect.length > 0 )
    {
        var cs = document.getElementById('cmZeme');
        var idRegion = cs.value;
        if ( obj.className == 'adv_ns' ) {
            obj.className = 'adv_s';
            if (filterSelect == 'lokalita')
            {
                if (!send_xmlhttprequest(adv_obsluha, 'GET', 'getOblasti.php?id='+idRegion, null)) {
                        return false;
                }
            } else {
                if (!send_xmlhttprequest(adv_obsluha, 'GET', 'getFilter.php?id='+idRegion+'&fs='+filterSelect, null)) {
                        return false;
                }
            }
        }
        else
        {
            obj.className = 'adv_ns';
            fceDisableAdv( id+'_div_ID');
        }
        
    }
    return true;
  }
  
  
  function fceDisableAdv( div)
  {
  var deactivate = document.getElementById(div.replace(/_div_ID/,'A'));
  deactivate.className = 'adv_ns';
    /*fadeOut(div, 0.1); */
    /* setTimeout( function() { */ 
        element = document.getElementById(div);
        if ( element ) {
        pn = element.parentNode; 
        pn.removeChild(element);
        }
    /* }, 1000); */
    return false;
  }
  
  function fceOnLoad()
  {
    var oContext = new Object();
        oContext.onload = true;
    if (!send_xmlhttprequest(adv_obsluha, 'GET', 'getFilter.php?si=true', oContext)) {
        return false;
    }
    return true;
  }
  
  function fill_std_form(xmlhttp, content) {
/*
    var delka = xmlhttp.responseXML.getElementsByTagName('delka');
    if( delka.length > 0 )
    {
        var sdp  = document.getElementById("cmDp");
        sdp.value = delka[0].firstChild.data;
    }
*/
    var tp = xmlhttp.responseXML.getElementsByTagName('termin_priletu');
    if( tp.length > 0 )
    {
        var itp  = document.getElementById("cmDo");
        itp.value = tp[0].firstChild.data;
        mCal2.setFormatedDate('%e.%c.%Y',tp[0].firstChild.data);
    }
    
    var tt = xmlhttp.responseXML.getElementsByTagName('typ_terminu');
    if( tt.length > 0 )
    {
        var v = tt[0].firstChild.data;
        if ( v.length > 0 )
        {
            var tta  = document.getElementById("typ_"+v);
            tta.checked = true;
        }
    }
    
    var tn = xmlhttp.responseXML.getElementsByTagName('termin_odletu');
    if( tn.length > 0 )
    {
        var itn  = document.getElementById("cmOd");
        itn.value = tn[0].firstChild.data;
        mCal1.setDate(tn[0].firstChild.data);
        
    }
    
    var region = xmlhttp.responseXML.getElementsByTagName('region'); 
    if( region.length > 0 )
    {
        var rid = region[0].firstChild.data;
        found = 0;
        if (rid.length >=4 ) found = SelectZeme(rid.substring(0,4));    
        if ( !found) SelectZeme(rid.substring(0,2));
    }
    else if ( content && content.onload == true)
    {
        disable_adv_entry('cmreg1');
    }
}

    function SelectZeme(id)
    {
        var zeme = document.getElementById('cmZeme');
    
        for(index = 0;  index < zeme.length; index++) {
            if(zeme[index].value == id)
            {
                zeme.selectedIndex = index;
                return 1;
            }
        }
        return 0;
    }
  
  function index(object, value)
  {
     var sl  = document.getElementById(object);
            for (var m=0; m < sl.length; m++) {
               
                if (sl.options[m].value == value)
                {
                    return m;
                }
            }
            return 0;
  }
