tx_zwexttaddressresponsibility_pi1.cookSelector = function() {
	var req = GXmlHttp.create();
	req.open('GET', tx_zwexttaddressresponsibility_pi1.url+'&type=501&tx_zwexttaddressresponsibility_pi1[action]=getcountries', true);
	req.onreadystatechange = function() {
		if(req.readyState == 4) {
			try{
				eval(req.responseText);
			} catch(ex) {};
			var cont_countries = document.getElementById('cont_countries');
			if(typeof(cont_countries) != 'undefined') {
				var html = '<select onchange="tx_zwexttaddressresponsibility_pi1.selectorChange();" id="sel_country"><option value="">'+tx_zwexttaddressresponsibility_pi1.lang.select_country+'</option>';
				for(var i in tx_zwexttaddressresponsibility_pi1.countries) {
					html += '<option value="'+i+'">'+tx_zwexttaddressresponsibility_pi1.countries[i].name+'</option>'
				}
				html += '</select>';
				cont_countries.innerHTML = html;
				var sel_country = document.getElementById('sel_country');
				sel_country.options[0].selected = true;
			};
		};
	};
	req.send(null);
};

tx_zwexttaddressresponsibility_pi1.selectorChange = function() {
	var sel_country = document.getElementById('sel_country');
	if(typeof(sel_country) != 'undefined' && sel_country.value != '') {
		tx_zwexttaddressresponsibility_pi1.showRelations(sel_country.value);
	};
};

tx_zwexttaddressresponsibility_pi1.showRelations = function(country) {
	country = tx_zwexttaddressresponsibility_pi1.countries[country];
	if(country.rels) {
		tx_zwexttaddressresponsibility_pi1.showUids(country.rels);
	};
	if(
		typeof(map) != 'undefined' &&
		typeof(country.min_lon) != 'undefined' &&
		typeof(country.min_lat) != 'undefined' &&
		typeof(country.max_lon) != 'undefined' &&
		typeof(country.max_lat) != 'undefined' &&
		typeof(country.center_lon) != 'undefined' &&
		typeof(country.center_lat) != 'undefined'
	) {
		var bounds = new GLatLngBounds();
		bounds.extend(new GLatLng(parseFloat(country.min_lat),parseFloat(country.min_lon)));
		bounds.extend(new GLatLng(parseFloat(country.max_lat),parseFloat(country.max_lon)));
        map.setCenter(
        	new GLatLng(parseFloat(country.center_lat),parseFloat(country.center_lon)),
        	map.getBoundsZoomLevel(bounds)
        );
	};
};

tx_zwexttaddressresponsibility_pi1.showUids = function(uids) {
	var loadUids = [];
	for(var i=0;i<uids.length;i++) {
		uids[i] = parseInt(uids[i]);
		if(!tx_zwexttaddressresponsibility_pi1.cache[uids[i]]) loadUids.push(uids[i]);
	};
	if(loadUids.length > 0) {
		cache = tx_zwexttaddressresponsibility_pi1.loadUids(loadUids,function(newUids) {
			tx_zwexttaddressresponsibility_pi1.cacheUids(newUids);
			tx_zwexttaddressresponsibility_pi1._showUids(uids);
		});
	} else tx_zwexttaddressresponsibility_pi1._showUids(uids);
};

tx_zwexttaddressresponsibility_pi1._showUids = function(uids) {
	var country_result = document.getElementById('country_result');
	if(typeof(country_result) != 'undefined') {
		var html = '<h3>'+tx_zwexttaddressresponsibility_pi1.lang.show_uids_header+':</h3>';
		/*
		 * Hier wieder die Krücke zum Sortieren von assoiziativen Arrays.
		 * Wir müssen den zu sortierenden Wert und den Schlüssel verketten.
		 * Dann sortieren. Dann vorn den Wert wegschneiden. Oh Gott!
		 */
		var order = [];
		for(var i=0;i<uids.length;i++) {
			order.push(tx_zwexttaddressresponsibility_pi1.uidNames[uids[i]]+'_'+uids[i]);
		};
		order.sort();
		for(var i=0;i<order.length;i++) {
			html += tx_zwexttaddressresponsibility_pi1.cache[order[i].replace(/^.*_([0-9]+)$/,"$1")];
		};
		country_result.innerHTML = html;
	};
};

tx_zwexttaddressresponsibility_pi1.cacheUids = function(uids) {
	for(var i in uids) {
		var template = tx_zwexttaddressresponsibility_pi1.template;
		for(var j in uids[i]) {
			if(uids[i][j]) {
				eval('template = template.replace(/<!--\\s+###IF_'+j.toUpperCase()+'###\\s+-->/gi,"");');
				eval('template = template.replace(/###'+j.toUpperCase()+'###/gi,uids[i][j]);');
			} else {
				eval('template = template.replace(/<!--\\s+###IF_'+j.toUpperCase()+'###\\s+-->.*?<!--\\s+###IF_'+j.toUpperCase()+'###\\s+-->/gi,"");');
			};
		};
		if($chk(tx_zwexttaddressresponsibility_pi1.groupnames)) {
			var template2 = '';
			for(var j in tx_zwexttaddressresponsibility_pi1.groupnames) {
				var groupname = tx_zwexttaddressresponsibility_pi1.groupnames[j];
				var template3 = tx_zwexttaddressresponsibility_pi1.template2;
				if($chk(uids[i].groups[j])) template3 = template3.replace(/###GROUPICON###/gi,'<img src="'+tx_zwexttaddressresponsibility_pi1.icons.yes+'" />');
				else template3 = template3.replace(/###GROUPICON###/gi,'<img src="'+tx_zwexttaddressresponsibility_pi1.icons.no+'" />');
				template3 = template3.replace(/###GROUPNAME###/gi,groupname);
				template2 += template3;
			};
			template = template.replace(/###GROUPSLIST###/gi,template2);
		} else {
			template = template.replace(/###GROUPSLIST###/gi,"");
		};
		//Überflüssige brs löschen
		template = template.replace(/<br\s*\/><br\s*\/>(?:<br\s*\/>)+/gi,"<br /><br />");
		template = template.replace(/<br\s*\/><div/gi,"<div");
		tx_zwexttaddressresponsibility_pi1.cache[i] = template;
		var finalSortField = '';
		tx_zwexttaddressresponsibility_pi1.sortFields.some(function(sortField) {
			if($chk(uids[i][sortField]) && uids[i][sortField] != '') {
				finalSortField = sortField;
				return true;
			} else return false;
		});
		tx_zwexttaddressresponsibility_pi1.uidNames[i] = uids[i][finalSortField];
	};
};

tx_zwexttaddressresponsibility_pi1.loadUids = function(uids,callback) {
	var req = GXmlHttp.create();
	req.open('GET', tx_zwexttaddressresponsibility_pi1.url+'&type=501&tx_zwexttaddressresponsibility_pi1[action]=getaddresses&tx_zwexttaddressresponsibility_pi1[uid]='+uids.join(','), true);
	req.onreadystatechange = function() {
		if(req.readyState == 4) {
			var records = {};
			try{
				eval(req.responseText);
			} catch(ex) {};
			callback(records);
		};
	};
	req.send(null);
};