function GroupsDropdown ( containerID, category, defaultText, callback, from, until ) {
	var container = document.getElementById(containerID);
	var selector;
	var groups;
	var period = "";
	if ( from )
		period = "&from=" + from;
	if ( until )
		period += "&until=" + until;
	ajax("download.php","job=groups_by_cat&format=jason&cat=" + category + period, 
		function ( reply ) {
			groups = eval ( "(" + reply + ")" );
			var onchange = (callback ? " onchange='" + callback + "()'" : "" );
			var html = "<span class='category'>" + category + "</span>";
			html += "<select id='" + category + "Select'" +  onchange + " class='GroupSelect' >";
			html += "<option value='-1' >" + defaultText + "</option>";
			for ( var i = 0; i < groups.length; ++i ) {
				html += "<option value='" + i + "'";
				if ( arg['group'] )
					for ( var g = 0; g < arg['group'].length; ++g ) {
						if (groups[i].name == unescape(arg['group'][g]) )
							html += " selected='selected'";
					}
				html += " >" + groups[i].name + "</option>";
			}
			html += "</select>";
			container.innerHTML = html;
			selector = document.getElementById(category + "Select");
		});
	this.selectedGroupName = function() {
		if ( group = selectedGroup() )
			return group.name;
		return "";
	}
	this.selectedGroupID = function() {
		if ( group = selectedGroup() )
			return group.id;
		return 0;
	}
	var selectedGroup = function() {
		var i = selectedValue();
		if ( i != -1 )
			return groups[i];
		return null;
	}
	var selectedValue = function() {
		return selector.options[selector.selectedIndex].value;
	}
}
function dbDropdown ( containerID, query, isSelected, callback, defaultText, title ) {
	var container = document.getElementById(containerID);
	var selector;
	var data;
	ajax("download.php",query, 
		function ( reply ) {
			data = eval ( "(" + reply + ")" );
			var onchange = (callback ? " onchange='" + callback + "()'" : "" );
			var html = title;
			html += "<select id='" + containerID + "Select'" +  onchange + " class='dbSelect' >";
			if ( defaultText )
				html += "<option value='-1' >" + defaultText + "</option>";
			for ( var i = 0; i < data.length; ++i ) {
				data[i].name = data[i].name;
//				data[i].name = base64_decode ( data[i].name );
				html += "<option value='" + i + "'" + isSelected(data[i].name);
				html += " >" + data[i].name + "</option>";
			}
			html += "</select>";
			container.innerHTML = html;
			selector = document.getElementById(containerID + "Select");
		});
	this.selectedDataName = function() {
		if ( group = this.selectedDataRecord() )
			return group.name;
		return "";
	}
	this.selectedDataID = function() {
		if ( group = this.selectedDataRecord() )
			return group.id;
		return 0;
	}
	this.selectedDataRecord = function() {
		var i = selectedValue();
		if ( i != -1 )
			return data[i];
		return null;
	}
	var selectedValue = function() {
		return selector.options[selector.selectedIndex].value;
	}
}
function GroupByCat ( containerID, callback, category, defaultText ) {
	return new dbDropdown (
		containerID,
		"job=groups_by_cat&format=jason&cat=" + category,
		function(name){
			for ( var g = 0; g < arg.groups.length; ++g ) {
				if ( name == unescape(arg.groups[g]) )
					return " selected='selected'";
			}
			return "";
		},
		callback,
		_s(defaultText),
		"<span class='category'>" + _s(category) + "</span>"
	);
}
function BallonWettbewerb ( containerID, callback ) {
	return GroupByCat (containerID, callback,
		'Ballon Wettfahrt', _s('Waehle einen Wettbewerb'));
}
function BallonKlasse ( containerID, callback ) {
	return GroupByCat (containerID, callback,
		'Ballon Klasse', _s('Waehle eine Klasse'));
}
function SegelflugWettbewerb ( containerID, callback ) {
	return GroupByCat (containerID, callback,
		'Segelflug Wettbewerb', _s('Waehle einen Wettbewerb'));
}
function SegelflugKlasse ( containerID, callback ) {
	return GroupByCat (containerID, callback,
		'Segelflug Klasse', _s('Waehle eine Klasse'));
}
