function getUniStr(str){
    var arrFound = str.match(/&#x(.{4});/gi);
    if(arrFound){
        for(var i=0;i<arrFound.length;i++){
            var charCode = arrFound[i].replace(/&#x(.{4});/,"$1");
            var uniChar  = String.fromCharCode(parseInt(charCode,16));
            var regexp   = new RegExp(arrFound[i],'ig');
            str = str.replace(regexp,uniChar);
        }
    }
    return str;
}

var uthema='';

function updateRegListe(TTREGS){
    g_nZiel=-1;    g_nRegion=-1;
    g_strZiel="";  g_strRegion="";
    UpdateRegionen(TTREGS);
}

function UpdateRegionen(rec)
{
        //alert('in fkt UpdateRegionen(rec)')
        if(rec){
                TTREGS = rec;
        } else {
                return false;
        }
        TTREGS=rec;
        uthema=(document.bengine.uthema.options[document.bengine.uthema.selectedIndex].value);
        HBit=uthema.split("_");

        /*
        alert('UpdateRegionen - uthema:'+uthema);
        alert('Welches HotelBit?: '+HBit[0]);
        alert('HotelBit: '+HBit[1]);
        alert('Bit: '+(1<<HBit[1]-1));
        */

        opt=document.bengine.elements["region"].options;
        opt[0].value=-1;opt[0].text="<Wszystkie regiony>";

        opt=document.bengine.elements["ziel"].options;
        opt[0].value=-1;opt[0].text="<Wszystkie cele>";

        if(HBit[0]==1){
            tmp_val=3;
            tmp_HBit=1;

        }
        else{
            tmp_val=4;
            tmp_HBit=2;
        }
        found=1;
        for(i=0;i<TTREGS.length;i++){
                sr=TTREGS[i].split("|");
                if(sr[1]>=10000){
                    if( (sr[tmp_val]&(1<<HBit[1]-1)&& HBit[0]==tmp_HBit)  ||  uthema==''){
                        if(found>=opt.length){
                                opt[found++]=new Option(getUniStr(sr[0]),sr[1]);
                        } else {
                                opt[found].text=getUniStr(sr[0]);
                                opt[found++].value=sr[1];
                        }
                    }
                }
        }
        while(opt.length > found){
                opt[opt.length-1]=null;
        }
        FixRSel();
}

function RZC(ds)
{

        //alert('in fkt RZC(ds)')
        opt=document.bengine.elements["ziel"].options;
        kenner=opt[opt.selectedIndex].value;
        g_nZiel=kenner;

        //HBit=uthema.split("_");

        //alert('RZC-uthema: '+uthema);

        if(opt.selectedIndex>0){
                g_strZiel=getUniStr(opt[opt.selectedIndex].text);
        } else g_strZiel="";
        gfilt=kenner!=-1;
        opt=document.bengine.elements["region"].options;
        found=1;

        for(i=0;i<TTREGS.length;i++){
                sr=TTREGS[i].split("|");
                if(sr[1]<10000 && (kenner<0 || sr[2]==kenner)){
                    if( (sr[tmp_val]&(1<<HBit[1]-1)&& HBit[0]==tmp_HBit) || uthema==''){
                        txt=sr[0];
                        if(found>=opt.length){
                                opt[found++]=new Option(getUniStr(txt),sr[1]+"|"+sr[2]);
                        } else {
                                opt[found].text=getUniStr(txt);
                                opt[found++].value=sr[1]+"|"+sr[2];
                        }
                    }
                }
        }

        while(opt.length > found){
                opt[opt.length-1]=null;
        }

        if(ds){
                opt.selectedIndex=0;
                g_strRegion="";
                g_nRegion=-1;
        }

}

function REGC()
{
        //alert('in fkt REGC()')
        ropt=document.bengine.elements["region"].options;
        kenner=ropt[ropt.selectedIndex].value.split("|");
        g_nRegion=kenner[0];
        if(ropt.selectedIndex>0){
                sr=getUniStr(ropt[ropt.selectedIndex].text);
                g_strRegion=sr.substr(0,sr.lastIndexOf("(")-1);
        } else g_strRegion="";

        if(!gfilt && kenner[0]>=0){
                opt=document.bengine.elements["ziel"].options;
                for(i=1;i<opt.length;i++){
                        if(opt[i].value==kenner[1]){
                                opt.selectedIndex=i;
                                g_nZiel=kenner[1];
                                g_strZiel=getUniStr(opt[i].text);
                                break;
                        }
                }
        }
}
function FixRSel()
{
        //alert('in fkt FixRSel()')
        opt=document.bengine.elements["ziel"].options;
        if(opt.selectedIndex<0 || opt[opt.selectedIndex].value!=g_nZiel){
                for(i=0;i<opt.length;i++)
                if(opt[i].value==g_nZiel){
                        opt.selectedIndex=i;break;
                }
                if(i==opt.length){
                        //alert(g_strZiel);
                        if(g_strZiel!=""){
                                opt[opt.length]=new Option(g_strZiel,g_nZiel,true,true);
                        } else {
                                opt.selectedIndex=0;
                                RZC();return;
                        }
                }
        }
        RZC();

        opt=document.bengine.elements["region"].options;
        if(opt.selectedIndex<0 || opt[opt.selectedIndex].value.split("|")[0]!=g_nRegion){
                for(i=0;i<opt.length;i++)
                if(opt[i].value.split("|")[0]==g_nRegion){
                        opt.selectedIndex=i;break;
                }
                if(i==opt.length){
                        if(g_strRegion!=""){
                                opt[opt.length]=new Option(g_strRegion+" (brak ofert)",g_nRegion+"|"+g_nZiel,true,true);
                        } else {
                                opt.selectedIndex=0;
                                g_nRegion=-1;
                                g_strRegion="";
                        }
                }
        }
}

