function _categories ( ) {
	// this is a singelton object
	categories = this;
	// private functions can now use this
	var that = this;
	// id of the selected category
	var selection = -1;
	// all category table containers
	var tables = ['category_table'];
	// all category dropdown containers
	var dropdowns = ['com_cat_dropdown','category_dropdown','member_category_dropdown'];
	// the category record list
	var list = [];
	// download all categories
	ajax("download.php","job=category_list&format=jason", 
		function ( reply ) {
			try {
				list = eval ( '(' + reply + ')' );
			}
			catch (e) {
				alert(reply);
			}
//			make_tables();
			make_dropdowns();
			new _groups(selection);
		});

	//--------------------------------------------------------------------------
	//  selected													public
	//..........................................................................
	//	returns the id of the selected category
	this.selected = function ( ) {
		return selection;
	}

	//--------------------------------------------------------------------------
	//  selected													public
	//..........................................................................
	//	return true if no category is selected
	this.none_selected = function ( ) {
		return selection == -1;
	}

	//--------------------------------------------------------------------------
	//  exists															public
	//..........................................................................
	//	does the new category name exist already
	this.exists = function ( name ) {
		if ( name )
			return nameExists ( list, name );
		return nameExists ( list, this.name() );
	}

	//--------------------------------------------------------------------------
	//  name															public
	//..........................................................................
	//	returns the new category name plain or encoded
	this.name = function ( encoded ) {
		var name = document.getElementById('category_name').value;
		return name;
//		return ( encoded ? base64_encode ( name ) : name );
	}

	//--------------------------------------------------------------------------
	//  changed															public
	//..........................................................................
	//	called from the category dropdown
	this.changed = function ( selector ) {
		selection = selector.options[selector.selectedIndex].value;
		make_dropdowns();
		new _groups(selection);
	}

	//--------------------------------------------------------------------------
	//  make_dropdowns													public
	//..........................................................................
	//	make the category dropdowns
	function make_dropdowns ( ) {
		var html = makeDropdown( list, selection );
		for ( var i = 0; i < dropdowns.length; ++i )
			document.getElementById(dropdowns[i]).innerHTML = html;
	}
	// make the dropdown html
	function makeDropdown( cat_array, id ) {
		html  = "<select class='Select' onchange='categories.changed(this)' >";
		var selected = ( id == -1 ? "selected=\"selected\"" : "" );
		html += "<option value='-1' " + selected + ">" + _s('Waehle eine Kategorie') + "</option>";
		for ( var i = 0; i < cat_array.length; ++i ) {
			selected = ( id == cat_array[i].id ? "selected=\"selected\"" : "" );
			html += '<option value="' + cat_array[i].id + '"' + selected + '>'
					+ cat_array[i].name + '</option>';
		}
		html += "</select>";
		return html;
	}

	//--------------------------------------------------------------------------
	//  make_tables														public
	//..........................................................................
	//	make the category tables
	function make_tables ( ) {
		var html = makeTable( list );
		for ( var i = 0; i < tables.length; ++i )
			document.getElementById(tables[i]).innerHTML = html;
	}
	// make the table html
	function makeTable( cat_array ) {
		html  = "<table cellspacing='0'>" +
					"<caption>" + _s('Bestehende Kategorien') + "</caption>" +
					"<thead>" +
						"<tr>" +
							"<th class='zahl'>" + _s('ID') + "</th>" +
							"<th class='txt'>" + _s('Bezeichnung') + "</th>" +
						"</tr>" +
					"</thead>" +
					"<tbody>";
		for ( var i = 0; i < cat_array.length; ++i ) {
			html += "<tr class='" + rowClass[i%2] + "'>" +
						"<td class='zahl'>" + cat_array[i].id + "</td>" +
						"<td class='txt'>" + cat_array[i].name + "</td>" +
					"</tr>";
		}
		html += 	"</tbody>" +
				"</table>";
		return html;
	}

	//--------------------------------------------------------------------------
	//  save															public
	//..........................................................................
	//	save a new category record to the server
	this.save = function ( ) {
		if ( inputChecked ( this ) ) {
				ajax("upload.php","job=save_category&name=" + this.name(true), 
				 	function ( reply ) {
	 				  	if ( "OK" != reply )
	 				  		alert (reply);
	 				  	new _categories();
	 				  	new _members();
	 				  } );
		}
	}
	function inputChecked ( ) {
		if ( ! spamCheck() )
			alert(_s("Du musst noch das Resultat in der Spamschutz Rechnung eintragen."));
		// do we have significant name?
		else if ( that.name().length < 3 )
			alert ( _s("Der Kategorienname ist zu kurz.") );
		// is the new category unique?
		else if ( that.exists() )
			alert ( _s("Diese Kategorie existiert schon.") );
		else
			return true;
		return false;
	}
}

