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", "POLAND"),new JSElem("7621", "SWEDEN"),new JSElem("22081", "ITALY"),new JSElem("23742", "GERMANY"),new JSElem("37241", "UKRAINE"),new JSElem("37505", "UNITED KINGDOM"),new JSElem("37506", "NETHERLANDS"),new JSElem("62057", "BELARUS"),new JSElem("62371", "FRANCE"),new JSElem("113631", "NORWAY"),new JSElem("123519", "DENMARK"),new JSElem("130571", "RUSSIA"),new JSElem("130728", "AUSTRIA"),new JSElem("130729", "BELGIUM"),new JSElem("130730", "BULGARIA"),new JSElem("130813", "LITHUANIA"),new JSElem("133220", "SPAIN"),new JSElem("133221", "CZECH REPUBLIC"),new JSElem("157225", "SWITZERLAND"),new JSElem("164353", "CROATIA"),new JSElem("164354", "ESTONIA"),new JSElem("164355", "GREECE"),new JSElem("164356", "HUNGARY"),new JSElem("164357", "MACEDONIA"),new JSElem("164358", "MOLDOVA"),new JSElem("164361", "SLOVAKIA"),new JSElem("164362", "SLOVENIA"),new JSElem("164364", "SERBIA"),new JSElem("164365", "IRELAND"),new JSElem("164366", "LATVIA"),new JSElem("164367", "LUXEMBOURG"),new JSElem("556749", "MONACO"));
var arr_dict_country = new Array(new JSElem("636", "POLAND"),new JSElem("7621", "SWEDEN"),new JSElem("22081", "ITALY"),new JSElem("23742", "GERMANY"),new JSElem("37241", "UKRAINE"),new JSElem("37505", "UNITED KINGDOM"),new JSElem("37506", "NETHERLANDS"),new JSElem("62057", "BELARUS"),new JSElem("62371", "FRANCE"),new JSElem("113631", "NORWAY"),new JSElem("123519", "DENMARK"),new JSElem("130571", "RUSSIA"),new JSElem("130728", "AUSTRIA"),new JSElem("130729", "BELGIUM"),new JSElem("130730", "BULGARIA"),new JSElem("130813", "LITHUANIA"),new JSElem("133220", "SPAIN"),new JSElem("133221", "CZECH REPUBLIC"),new JSElem("157225", "SWITZERLAND"),new JSElem("164353", "CROATIA"),new JSElem("164354", "ESTONIA"),new JSElem("164355", "GREECE"),new JSElem("164356", "HUNGARY"),new JSElem("164357", "MACEDONIA"),new JSElem("164358", "MOLDOVA"),new JSElem("164361", "SLOVAKIA"),new JSElem("164362", "SLOVENIA"),new JSElem("164364", "SERBIA"),new JSElem("164365", "IRELAND"),new JSElem("164366", "LATVIA"),new JSElem("164367", "LUXEMBOURG"),new JSElem("556749", "MONACO"));
var all_recs_array_country = new Array();
all_recs_array_country[0] = "FFFFFFFF0";
all_recs_array_country[1] = "941200000";
all_recs_array_country[2] = "100820600";
all_recs_array_country[3] = "BCFA01020";
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] = "B47200000";
all_recs_array_country[9] = "941200000";
all_recs_array_country[10] = "941200000";
all_recs_array_country[11] = "900000000";
all_recs_array_country[12] = "100800000";
all_recs_array_country[13] = "B67200000";
all_recs_array_country[14] = "100024200";
all_recs_array_country[15] = "D00920020";
all_recs_array_country[16] = "100000000";
all_recs_array_country[17] = "580C24620";
all_recs_array_country[18] = "100000000";
all_recs_array_country[19] = "100000000";
all_recs_array_country[20] = "900000000";
all_recs_array_country[21] = "100000000";
all_recs_array_country[22] = "100424200";
all_recs_array_country[23] = "100000000";
all_recs_array_country[24] = "100000000";
all_recs_array_country[25] = "500424200";
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="62057">BELARUS</option>');
    document.write('<option value="130729">BELGIUM</option>');
    document.write('<option value="130730">BULGARIA</option>');
    document.write('<option value="164353">CROATIA</option>');
    document.write('<option value="133221">CZECH REPUBLIC</option>');
    document.write('<option value="123519">DENMARK</option>');
    document.write('<option value="164354">ESTONIA</option>');
    document.write('<option value="62371">FRANCE</option>');
    document.write('<option value="23742">GERMANY</option>');
    document.write('<option value="164355">GREECE</option>');
    document.write('<option value="164356">HUNGARY</option>');
    document.write('<option value="164365">IRELAND</option>');
    document.write('<option value="22081">ITALY</option>');
    document.write('<option value="164366">LATVIA</option>');
    document.write('<option value="130813">LITHUANIA</option>');
    document.write('<option value="164367">LUXEMBOURG</option>');
    document.write('<option value="164357">MACEDONIA</option>');
    document.write('<option value="164358">MOLDOVA</option>');
    document.write('<option value="556749">MONACO</option>');
    document.write('<option value="37506">NETHERLANDS</option>');
    document.write('<option value="113631">NORWAY</option>');
    document.write('<option value="636">POLAND</option>');
    document.write('<option value="130571">RUSSIA</option>');
    document.write('<option value="164364">SERBIA</option>');
    document.write('<option value="164361">SLOVAKIA</option>');
    document.write('<option value="164362">SLOVENIA</option>');
    document.write('<option value="133220">SPAIN</option>');
    document.write('<option value="7621">SWEDEN</option>');
    document.write('<option value="157225">SWITZERLAND</option>');
    document.write('<option value="37241">UKRAINE</option>');
    document.write('<option value="37505">UNITED KINGDOM</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"));
}

