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("22081", "WŁOCHY"),new JSElem("23742", "NIEMCY"),new JSElem("37505", "WIELKA BRYTANIA"),new JSElem("37506", "HOLANDIA"),new JSElem("62371", "FRANCJA"),new JSElem("130729", "BELGIA"),new JSElem("157225", "SZWAJCARIA"));
var arr_dict_country = new Array(new JSElem("22081", "WŁOCHY"),new JSElem("23742", "NIEMCY"),new JSElem("37505", "WIELKA BRYTANIA"),new JSElem("37506", "HOLANDIA"),new JSElem("62371", "FRANCJA"),new JSElem("130729", "BELGIA"),new JSElem("157225", "SZWAJCARIA"),new JSElem("636", "POLSKA"));
var all_recs_array_country = new Array();
all_recs_array_country[0] = "F70";
all_recs_array_country[1] = "080";
all_recs_array_country[2] = "8A0";
all_recs_array_country[3] = "8A0";
all_recs_array_country[4] = "EB0";
all_recs_array_country[5] = "8A0";
all_recs_array_country[6] = "EB0";
all_recs_array_country[7] = "080";

function ShowTopCombo()
{
    document.write('<select name="dctr" id="et_dctr" class="test" onchange="SetBottomCombo(this);">');
    document.write('<option value="130729">BELGIA</option>');
    document.write('<option value="62371">FRANCJA</option>');
    document.write('<option value="37506">HOLANDIA</option>');
    document.write('<option value="23742">NIEMCY</option>');
    document.write('<option value="636">POLSKA</option>');
    document.write('<option value="157225">SZWAJCARIA</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"));
}

