function GetBit(a_str, a_pos)
{
    bitNo = a_pos % 4;
    charNo = (a_pos - bitNo) / 4;
    str = "0x" + a_str.charAt(charNo);
    return (str & (1 << bitNo)) != 0;
}

function JSElem(a_fid, a_name)
{
    this.m_fid = a_fid;
    this.m_name = a_name;
}
function NameCmp(a, b)
{
    return StrCmp(a.m_name, b.m_name);
}
function StrCmp(a, b)
{
    ret=0;
   
    if(a.length==0 || b.length==0)
    {
	if(a.length==0 && b.length==0)
	{
	    ret = 0;
	}
	else if(a.length==0)
	{
	    ret = -1;
	}
	else if(b.length==0)
	{
	    ret = 1;
	}
    }
    else
    {
	aCode = GetSortCode(a.charAt(0));
	bCode = GetSortCode(b.charAt(0));

	if(aCode < bCode)
	{
	    ret = -1;
	}
	else if(bCode < aCode)
	{
	    ret = 1;
	}
	else
	{
	    subStrA = a.substr(1, a.length-1);
	    subStrB = b.substr(1, b.length-1);
	    ret = StrCmp(subStrA, subStrB);
	}
    }

    return ret;
}
function GetSortCode(c)
{
    code = -1;

    if(c <= 'Z') // dla przyspieszenia - zeby nie przegladac wszystkich else-�w (zwykle else-y niepotrzebne)
    {
	code = c.charCodeAt(0);
    }
    // jezyk polski
    else if(c == '\u0104') // A
    {
	code = 65.1; // 65,1 - miedzy 65 a 66, czyli miedzy A a B
    }
    else if(c == '\u0106') // C
    {
	code = 67.1;
    }
    else if(c == '\u0118') // E
    {
	code = 69.1;
    }
    else if(c == '\u0141') // L
    {
	code = 76.1;
    }
    else if(c == '\u0143') // N
    {
	code = 78.1;
    }
    else if(c == '\u00D3')// O
    {
	code = 79.1;
    }
    else if(c == '\u015A') // S
    {
	code = 83.1;
    }
    else if(c == '\u0179') // Z,
    {
	code = 90.1;
    }
    else if(c == '\u017B') // Z.
    {
	code = 90.2;
    }
    // jezyk niemiecki
    else if(c == '\u00C4') // A
    {
	code = 65.2;
    }
    else if(c == '\u00D6') // O
    {
	code = 79.2;
    }
    else if(c == '\u00DC') // U
    {
	code = 85.2;
    }
    else if(c == '\u00DF') // SS
    {
	code = 83.2;
    }
    // jezyk francuski - znaki diakrytyczne nie maja wplywu na sortowanie (mozna grupowac razem ze znakiem podstawowym)
    else if(c == '\u00C0' || c == '\u00C2') // min. A
    {
	code = 65;
    }
    else if(c == '\u00C7') // C
    {
	code = 67;
    }
    else if(c == '\u00C8' || c == '\u00C9' || c == '\u00CA' || c == '\u00CB') // min. E
    {
	code = 69;
    }
    else if(c == '\u00CE' || c == '\u00CF') // min. I
    {
	code = 73;
    }
    else if(c == '\u00DA' || c == '\u00DB' || c == '\u00DC') // min. U
    {
	code = 85;
    }
    else
    {
	code = c.charCodeAt(0);
    }

    return code;
}

function SetUniqArray( a_array )
{
    var unique_arr = new Array();
    var last = "";
    var indx = 0;
    for( var idx  = 0; idx < a_array.length; idx++ )
    {

     if( last == a_array[ idx ].m_name ) continue;

     unique_arr[ indx++ ] = a_array[ idx ];
     last = a_array[ idx ].m_name;
    }
    return unique_arr;
}

function JSCountry(a_id, a_name)
{
    this.m_id = a_id;
    this.m_name = a_name;
}

function SetBottomCombo(a_ob)
{ 
    if (a_ob.selectedIndex < 0)
    {
	a_ob.selectedIndex = 0;
    }
    var bottom_array = new Array();
    var up_country = a_ob.options[ a_ob.selectedIndex ].text;

    document.getElementById("et_actr").length = 0;
    for( var oidx = 0; oidx < dep_dict_country.length; oidx++ )
    {
	if (dep_dict_country[oidx].m_name == up_country)
	{
	    var k = 0;
	    for ( var i = 0; i < arr_dict_country.length; i++)
	    {
		if (GetBit(all_recs_array_country[oidx], i))
		{
		    bottom_array[k++] = new JSCountry(arr_dict_country[i].m_fid, arr_dict_country[i].m_name);
		}
	    }
	    continue;
	}
    }
    bottom_array.sort(NameCmp);
    var unique_arr = SetUniqArray( bottom_array );
    for( var idx = 0, ix =0; idx < unique_arr.length; idx++ )
    {
	document.getElementById("et_actr").options[ix++] = new Option( unique_arr[ idx ].m_name, unique_arr[ idx ].m_id );
    }
}

var dep_dict_country = new Array(new JSElem("636", "POLSKA"),new JSElem("7621", "SZWECJA"),new JSElem("22081", "WŁOCHY"),new JSElem("23742", "NIEMCY"),new JSElem("37241", "UKRAINA"),new JSElem("37505", "WIELKA BRYTANIA"),new JSElem("37506", "HOLANDIA"),new JSElem("62057", "BIAŁORUŚ"),new JSElem("62371", "FRANCJA"),new JSElem("113631", "NORWEGIA"),new JSElem("123519", "DANIA"),new JSElem("130571", "ROSJA"),new JSElem("130728", "AUSTRIA"),new JSElem("130729", "BELGIA"),new JSElem("130730", "BUŁGARIA"),new JSElem("130813", "LITWA"),new JSElem("133220", "HISZPANIA"),new JSElem("133221", "CZECHY"),new JSElem("157225", "SZWAJCARIA"),new JSElem("164353", "CHORWACJA"),new JSElem("164354", "ESTONIA"),new JSElem("164355", "GRECJA"),new JSElem("164356", "WĘGRY"),new JSElem("164357", "MACEDONIA"),new JSElem("164358", "MOŁDAWIA"),new JSElem("164361", "SŁOWACJA"),new JSElem("164362", "SŁOWENIA"),new JSElem("164364", "SERBIA"),new JSElem("164365", "IRLANDIA"),new JSElem("164366", "ŁOTWA"),new JSElem("164367", "LUKSEMBURG"),new JSElem("556749", "MONAKO"));
var arr_dict_country = new Array(new JSElem("636", "POLSKA"),new JSElem("7621", "SZWECJA"),new JSElem("22081", "WŁOCHY"),new JSElem("23742", "NIEMCY"),new JSElem("37241", "UKRAINA"),new JSElem("37505", "WIELKA BRYTANIA"),new JSElem("37506", "HOLANDIA"),new JSElem("62057", "BIAŁORUŚ"),new JSElem("62371", "FRANCJA"),new JSElem("113631", "NORWEGIA"),new JSElem("123519", "DANIA"),new JSElem("130571", "ROSJA"),new JSElem("130728", "AUSTRIA"),new JSElem("130729", "BELGIA"),new JSElem("130730", "BUŁGARIA"),new JSElem("130813", "LITWA"),new JSElem("133220", "HISZPANIA"),new JSElem("133221", "CZECHY"),new JSElem("157225", "SZWAJCARIA"),new JSElem("164353", "CHORWACJA"),new JSElem("164354", "ESTONIA"),new JSElem("164355", "GRECJA"),new JSElem("164356", "WĘGRY"),new JSElem("164357", "MACEDONIA"),new JSElem("164358", "MOŁDAWIA"),new JSElem("164361", "SŁOWACJA"),new JSElem("164362", "SŁOWENIA"),new JSElem("164364", "SERBIA"),new JSElem("164365", "IRLANDIA"),new JSElem("164366", "ŁOTWA"),new JSElem("164367", "LUKSEMBURG"),new JSElem("556749", "MONAKO"));
var all_recs_array_country = new Array();
all_recs_array_country[0] = "FFFFFFFF0";
all_recs_array_country[1] = "941200000";
all_recs_array_country[2] = "900920400";
all_recs_array_country[3] = "FCFA61020";
all_recs_array_country[4] = "100000000";
all_recs_array_country[5] = "140200000";
all_recs_array_country[6] = "B67200000";
all_recs_array_country[7] = "900020000";
all_recs_array_country[8] = "B47260000";
all_recs_array_country[9] = "941200000";
all_recs_array_country[10] = "941200000";
all_recs_array_country[11] = "900000000";
all_recs_array_country[12] = "500900000";
all_recs_array_country[13] = "B67200000";
all_recs_array_country[14] = "100000000";
all_recs_array_country[15] = "D00920020";
all_recs_array_country[16] = "100000000";
all_recs_array_country[17] = "D81860420";
all_recs_array_country[18] = "901060000";
all_recs_array_country[19] = "100000000";
all_recs_array_country[20] = "900000000";
all_recs_array_country[21] = "100000000";
all_recs_array_country[22] = "100000000";
all_recs_array_country[23] = "100000000";
all_recs_array_country[24] = "100000000";
all_recs_array_country[25] = "100000000";
all_recs_array_country[26] = "500020400";
all_recs_array_country[27] = "100000000";
all_recs_array_country[28] = "100000000";
all_recs_array_country[29] = "900820000";
all_recs_array_country[30] = "100000000";
all_recs_array_country[31] = "100000000";

function ShowTopCombo()
{
    document.write('<select name="dctr" id="et_dctr" class="test" onchange="SetBottomCombo(this);">');
    document.write('<option value="130728">AUSTRIA</option>');
    document.write('<option value="130729">BELGIA</option>');
    document.write('<option value="62057">BIAŁORUŚ</option>');
    document.write('<option value="130730">BUŁGARIA</option>');
    document.write('<option value="164353">CHORWACJA</option>');
    document.write('<option value="133221">CZECHY</option>');
    document.write('<option value="123519">DANIA</option>');
    document.write('<option value="164354">ESTONIA</option>');
    document.write('<option value="62371">FRANCJA</option>');
    document.write('<option value="164355">GRECJA</option>');
    document.write('<option value="133220">HISZPANIA</option>');
    document.write('<option value="37506">HOLANDIA</option>');
    document.write('<option value="164365">IRLANDIA</option>');
    document.write('<option value="130813">LITWA</option>');
    document.write('<option value="164367">LUKSEMBURG</option>');
    document.write('<option value="164366">ŁOTWA</option>');
    document.write('<option value="164357">MACEDONIA</option>');
    document.write('<option value="164358">MOŁDAWIA</option>');
    document.write('<option value="556749">MONAKO</option>');
    document.write('<option value="23742">NIEMCY</option>');
    document.write('<option value="113631">NORWEGIA</option>');
    document.write('<option value="636">POLSKA</option>');
    document.write('<option value="130571">ROSJA</option>');
    document.write('<option value="164364">SERBIA</option>');
    document.write('<option value="164361">SŁOWACJA</option>');
    document.write('<option value="164362">SŁOWENIA</option>');
    document.write('<option value="157225">SZWAJCARIA</option>');
    document.write('<option value="7621">SZWECJA</option>');
    document.write('<option value="37241">UKRAINA</option>');
    document.write('<option value="164356">WĘGRY</option>');
    document.write('<option value="37505">WIELKA BRYTANIA</option>');
    document.write('<option value="22081">WŁOCHY</option>');
    document.write('</select>');
}

function ShowBottomCombo()
{
    document.write('<select name="actr" id="et_actr">');
    document.write('<option>...</option>');
    document.write('</select>');
    SetBottomCombo(document.getElementById("et_dctr"));
}

