var offices_arr = Array();
var country_id_arr = Array();

function Offices( country_id )
{
	advAJAX.get
	({
		//url: "offices.xml",
		//url: "/modules/get_string.php",
		url: "/modules/get_contacts.php",
		parameters : {
			//"lc" : "en_00",
			//"pj" : "100",
			//"sid" : "13",
			//"unique" : "custom_unique_param"+( 100 * Math.random() )
		},
		headers : {
			 "Content-type" : "text/xml; charset=UTF-8"
		},
		timeout : 3000,
		retry: 2,
		retryDelay: 2000,
		onInitialization : function( obj )
		{
		},
		onLoading : function( obj )
		{
		},
		onSuccess : function( obj )
		{
			//alert( offices_xml_array.length );
		},
		onError : function( obj )
		{
			alert( "Error while loading XML offices data!\n"+obj.status );
		},
		onFinalization : function( obj )
		{
			//window.setTimeout( function() {		// <- Jesli potrzebne jest opoznienie
				// obj.responseText = obj.responseText.replace( "&", "&amp;" );	// <- Zamien znaki, ktore wywoluja bledy podczas parsowania XMLa
				//alert( obj.responseText );
				if( document.all )
				{
					var xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
					xmlDocument.async = false;
					xmlDocument.loadXML( obj.responseText );
					//var xmlDocument = new ActiveXObject('Microsoft.XMLDOM').loadXML( obj.responseText );
				}
				else
				{
					var xmlDocument = new DOMParser().parseFromString( obj.responseText, 'application/xml');
				}
				//alert( xmlDocument.documentElement.nodeName );
			//}, 5000 );
			//alert( xmlDocument );

			// Przygotuj objekt XML do dalszej obrobki
			if( document.all )  var xml = xmlDocument.documentElement;
			else var xml = xmlDocument.firstChild;

			// Tworz tablice z lista biur
			var nrChildren = xml.childNodes.length;
			for(var i=0; i<nrChildren; i++)
			{
				var node = xml.childNodes[i];

				if(node.nodeName != '#text')
				{
					var country_arr = Array();
					var nodeCountry = node.childNodes;
					var a = 0;
					while( nodeCountry[a] )
					{
						if( nodeCountry[a].nodeName != '#text' )
						{
							if( nodeCountry[a].nodeName == "select_name" ) var countryy_id = nodeCountry[a].childNodes[0].nodeValue; // Potrzebne do posortowania tablicy
							// if( nodeCountry[a].nodeName == "select_name" )
							if( nodeCountry[a].nodeName == "id" )
							{
								var array_key = nodeCountry[a].childNodes[0].nodeValue; // Potrzebne do posortowania tablicy
								array_key = array_key.replace( " ", "_" );	// Zamien spacje na podkrelsenia
								country_arr[ "array_key" ] = array_key; // Wstaw w dane biura "array_key", zeby moc je zdentyfikowac.
								if( countryy_id == country_id ) country_id =  array_key; // Jesli country_id == countryy_id, podmien country_id na array_key, aby dokonac auto selekcji biura.
							}
							//alert( countryy_id );

							if( nodeCountry[a].nodeName == "id" || nodeCountry[a].nodeName == "select_name" ) country_arr[ nodeCountry[a].nodeName ] = nodeCountry[a].childNodes[0].nodeValue;
							else if( nodeCountry[a].nodeName == "separator" ) country_arr.push( nodeCountry[a].nodeName );
							else
							{
								var field_arr = Array();
								var nodeField = nodeCountry[a].childNodes;
								var b = 0;
								while( nodeField[b] )
								{
									if( nodeField[b].nodeName != '#text' )
									{
									    var value = nodeField[b].childNodes[0].nodeValue;
									    value = value.replace( "_amp_", "&" );	// Zamien spacje na podkrelsenia
									    field_arr[ nodeField[b].nodeName ] = value;
									}
									b++;
								}

								country_arr.push( field_arr );

							}
						}
						a++;
					}

					country_id_arr.push( countryy_id );
					offices_arr[ countryy_id ] = country_arr;
					/*
					if( !offices_arr[ array_key ] )
					{
						country_id_arr.push( array_key );
						offices_arr[ array_key ] = country_arr;
					}
					else
					{
						offices_arr[ array_key ] = country_arr;
					}
					*/

				}

    			}

			//alert(offices_arr);

			country_id_arr = country_id_arr.sort();
			OfficesSelector( country_id );
		}
	});
}

function OfficesSelector( country_id )
{
	var res_selector = "";

	var sort_array = Array();
	for( country_id_ in offices_arr )
	{
		sort_array.push( offices_arr[ country_id_ ][ "select_name" ] );
	}
	//alert( sort_array );
	//return;

	//for( var i=0; i < offices_arr.length; i++ )
	for ( i in country_id_arr )
	{
		var country_arr = offices_arr[country_id_arr[i]];
		if( country_arr["select_name"] != "undefinied country" ){
			var selected = "";
			country_arr["array_key"] == country_id ? selected = " selected" : selected = "";
			//alert(country_id_arr[i]);
			res_selector += "<option value='"+country_id_arr[i]+"'"+selected+">"+country_arr["select_name"]+"</option>";
			if( country_arr["array_key"] == country_id ) OfficesZmien( country_id_arr[i] );
		}
	}


	res_selector = "<select onChange='OfficesZmien(this.value)'>"+res_selector+"</select>";
	document.getElementById("OfficesSelector").innerHTML = res_selector;
}

function OfficesZmien( country_id )
{
    //alert(country_id);
	office_arr = offices_arr[country_id];
	//if(!office_arr)
	//    return;

	//document.getElementById( "div_test" ).innerHTML = office_arr[0]["label"];

	var div_obj = document.getElementById( "OfficeData" );
	var res = "";
	for( var i = 0; i < office_arr.length; i++ )
	{
		if( office_arr[i] == "separator" ) { res += "<tr><td colspan='2'><hr></td></tr>"; }
		else
		{
			if( office_arr[i]["type"] == "url" ) var content = "<a href='http://"+office_arr[i]["content"]+"' target='_blank'>"+office_arr[i]["content"]+"<a>";
			else if( office_arr[i]["type"] == "email" ) var content = "<a href='mailto:"+office_arr[i]["content"]+"'>"+office_arr[i]["content"].replace("%26", "&") +"<a>";
			else var content = office_arr[i]["content"];
			if( office_arr[i]["lang"] ) var content = "("+office_arr[i]["lang"]+") "+content;
			res += "<tr valign='bottom'><td style='padding-right: 10px; text-align: right' width='200'><b>"+office_arr[i]["label"]+":</b></td><td>"+content+"</td></tr>\n";
			res += "<tr valign='bottom'><td colspan='2'><div style='height: 5px;'><!-- --><div></td></tr>\n";
		}
	}
	div_obj.innerHTML = "<table border='0' cellspacing='0' cellpadding='0'>\n"+res+"</table>";
}