/*--------------------------------------------------------------------------
// _groups object
//..........................................................................
//		needs the com.js and iPhone.js and categories.js to be included
*/
function _groups ( category ) {
	// this is a singelton object
	groups = this;
	// private functions can now use this
	var that = this;
	// the communication object
	var com;
	// all group table containers
	var tables = ['group_table'];
	// all group dropdown containers
	var dropdowns = ['com_group_dropdown','group_dropdown'];
	// the group record list
	var list = [];
	// the seloected group id
	var selection;

	//--------------------------------------------------------------------------
	//  FormComplet														public
	//..........................................................................
	//	called from the _com object
	this.FormComplet = function ( ) {
		// check category dropdown
		if ( categories.none_selected() )
			alert ( _s("Es muss eine Kategorie ausgewaehlt werden.") );
		// check groups dropdown
		else if ( selection == -1 )
			alert ( _s("Es muss eine Gruppe ausgewaehlt werden.") );
		else 
			return true;
		return false;
	}

	//--------------------------------------------------------------------------
	//  getIndex														public
	//..........................................................................
	//	returns the user id
	this.getIndex = function ( ) {
		return selection;
	}

	//--------------------------------------------------------------------------
	//  makeID															public
	//..........................................................................
	//	returns a unique id
	this.makeID = function ( index ) {
		return index + 100;
	}

	//--------------------------------------------------------------------------
	//  deleteCom														public
	//..........................................................................
	//	delete a com record
	this.deleteCom = function ( index ) {
		com.remove ( index );
	}

	//--------------------------------------------------------------------------
	//  installCom														public
	//..........................................................................
	//	just call com.install
	this.installCom = function ( index ) {
		com.install ( index );
	}

	//--------------------------------------------------------------------------
	//  setComType														public
	//..........................................................................
	//	set the com type ['telefon','sms','email']
	this.setComType = function ( typeString ) {
		com.setType ( typeString );
	}

	//--------------------------------------------------------------------------
	//  isReadyToLoadCom												public
	//..........................................................................
	//	is always ready
	this.isReadyToLoadCom = function ( ) {
		return true;
	}

	//--------------------------------------------------------------------------
	//  initCom															public
	//..........................................................................
	//	called from the _com object at init
	this.initCom = function ( com ) {
		com.target = 'group';
		com.db = 'groups';
		com.owner_name =  _s('Gruppe');
		com.owner_query = "group=" + selection;
		com.owner_tables = ['group_com_table'];
		com.owner_obj_name = "groups";
	}

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

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

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

	//--------------------------------------------------------------------------
	//  fromDate														public
	//..........................................................................
	//	returns a start date string
	this.fromDate = function ( ) {
		return group_from_cal.selectedDate.timestamp();
	}

	//--------------------------------------------------------------------------
	//  untilDate														public
	//..........................................................................
	//	returns a stop (end) date string
	this.untilDate = function ( ) {
		return group_until_cal.selectedDate.timestamp();
	}

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

	//--------------------------------------------------------------------------
	//  info															public
	//..........................................................................
	//	returns the new group info plain or encoded
	this.info = function ( encoded ) {
//		if ( encoded )
//			return base64_encode ( document.getElementById("group_info").value );
//		return document.getElementById("group_info").value;
		return document.getElementById("group_info").value;
	}

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

	//--------------------------------------------------------------------------
	//  save															public
	//..........................................................................
	//	save a new group record to the server
	this.save = function ( ) {
		if ( inputChecked() ) {
			ajax("upload.php",
				 "job=save_group" +
				 "&name="	+ this.name(true) + 
				 "&info="	+ this.info(true) + 
 				 "&cat="	+ categories.selected() + 
 				 "&from="	+ this.fromDate() + 
 				 "&until="	+ this.untilDate(),
 				function ( reply) {
 				  	if ( "OK" == reply )
 						new _groups(categories.selected());
 					else
						alert (reply);
 				  } );
		}
	}
	function inputChecked ( ) {
		var dauer = group_until_cal.selectedDate.difference(group_from_cal.selectedDate);
		if ( ! spamCheck() )
			alert(_s("Du musst noch das Resultat in der Spamschutz Rechnung eintragen."));
		// check if a category is selected
		else if ( categories.none_selected() )
			alert ( _s("Es ist keine Kategorie ausgewaehlt.") );
		// check if a group name has been given
		else if ( that.name().length < 2 )
			alert ( _s("Der Gruppenname ist zu kurz.") );
		// the name must be unique within a category space
		else if ( that.exists() )
			alert ( _s("Diese Gruppe existiert schon.") );
		else if ( dauer == 0 )
			alert ( _s("Klicke auf das Enddatum, um eine Lebensdauer zu definieren.") );
		else 
			return true;
		return false;
	}

	//--------------------------------------------------------------------------
	//  save_com														public
	//..........................................................................
	//	just call com.save()
	this.save_com = function ( ) {
		com.save();
	}

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

	//--------------------------------------------------------------------------
	//  make_tables														public
	//..........................................................................
	//	make the group tables
	function make_tables ( ) {
		var html = ( list.length == 0 ? "" : makeTable( list ) );
		for ( var i = 0; i < tables.length; ++i )
			document.getElementById(tables[i]).innerHTML = html;
	}
	// make the table html
	function makeTable( list ) {
		html  = "<table cellspacing='0'>" +
					"<caption>" + _s('Bestehende Gruppen') + "</caption>" +
					"<thead>" +
						"<tr>" +
							"<th class='zahl'>" + _s('ID') + "</th>" +
							"<th class='txt'>" + _s('Bezeichnung') + "</th>" +
							"<th class='txt'>" + _s('Start') + "</th>" +
							"<th class='txt'>" + _s('Ende') + "</th>" +
							"<th class='txt'>" + _s('Info') + "</th>" +
						"</tr>" +
					"</thead>" +
					"<tbody>";
		for ( var i = 0; i < list.length; ++i ) {
			var start = new Date(list[i].start * 1000);
			var end = new Date(list[i].end * 1000);
			html += "<tr class='" + rowClass[i%2] + "'>" +
						"<td class='zahl'>" + list[i].id + "</td>" +
						"<td class='txt'>" + list[i].name + "</td>" +
						"<td class='txt'>" + start.localDate() + "</td>" +
						"<td class='txt'>" + end.localDate() + "</td>" +
						"<td class='txt'>" + list[i].info + "</td>" +
					"</tr>";
		}
		html += 	"</tbody>" +
				"</table>";
		return html;
	}

	//--------------------------------------------------------------------------
	//  make_tables														init
	//..........................................................................
	//	no category selected
	if ( categories.none_selected() ) {
		list = [];
		selection = -1;
		make_tables();
		make_dropdowns();
	}
	//	if a category is selected, get all groups from the server
	else
		ajax("download.php","job=groups_by_cat_id&format=jason&cat=" + categories.selected(), 
			function ( reply ) { alert('groups.js(298): ' + reply);
				list = eval ( '(' + reply + ')' );
				for ( var i = 0; i < list.length; ++i ) {
					list[i].name = list[i].name;
					list[i].category = list[i].category;
					list[i].info = list[i].info;
//					list[i].name = base64_decode ( list[i].name );
//					list[i].category = base64_decode ( list[i].category );
//					list[i].info = base64_decode ( list[i].info );
				}
				selection = ( list.length == 1 ? list[0].id : -1 );
				make_tables();
				make_dropdowns();
			});
}

