//--------------------------------------------------------------------------
// show / hide the mail cell in the com table
//..........................................................................
function show_mail ( nr ) {
	document.getElementById("email" + nr + "a").style.display = 'table-row';
	document.getElementById("email" + nr + "b").style.display = 'table-row';
}
function hide_mail ( nr ) {
	document.getElementById("email" + nr + "a").style.display = 'none';
	document.getElementById("email" + nr + "b").style.display = 'none';
}

//--------------------------------------------------------------------------
// enumerates
//..........................................................................
var rowClass = ["even","odd"];
var comTypes = ['telefon','sms','email'];

/*--------------------------------------------------------------------------
// _com object
//..........................................................................
//		com object for owner _iPhone and _group
*/
function _com ( owner ) {
	//---- to be set by the owners initCom() method
	this.target = '';			//'user' or'group'
	this.db = '';				//'user' or'groups'
	this.owner_name =  '';		// _s('User') or _s('User')
	this.owner_query = "";		// "device=" + this.uniqueID + "&user=" + this.user
	this.owner_tables = "";		// ['user_com_table'] or ['group_com_table']
	this.owner_obj_name = "";	// "iPhone" or "groups"
	owner.initCom(this);
	
	// this for private functions
	var that = this;
	// list of users
	var list = [];
	// upload job
	var upload_job;
	// index into comTypes = ['telefon','sms','email'];
	var typeIndex = -1;
	// owner specific ids
	var _ = { job: "job=" + this.target + "Com",
			  name:				document.getElementById( this.target + '_com_name' ),
			  telefon_number:	document.getElementById( this.target + '_telefon_number' ),
 			  sms_number:		document.getElementById( this.target + '_sms_number' ),
			  email_address:	document.getElementById( this.target + '_email_address' ),
			  email_subject:	document.getElementById( this.target + '_email_subject' ),
			  email_body:		document.getElementById( this.target + '_email_body' ),
			  radio: [], form: [] };
	for ( var i = 0; i < comTypes.length; ++i ) {
		_.radio[i] = document.getElementById( this.target + '_' + comTypes[i] + '_' + 'radio' );
		_.form[i]  = document.getElementById( this.target + '_' + comTypes[i] + '_' + 'data' );
		if ( _.radio[i].checked ) 
			typeIndex = i;
	}

	//--------------------------------------------------------------------------
	//  getType / setType												public
	//..........................................................................
	//	getter und setter for ['telefon','sms','email']
	this.getType = function ( ) {
		return comTypes[typeIndex];
	}
	this.setType = function ( typeString ) {
		for  ( var i = 0; i < comTypes.length; ++i )  {
			if ( _.radio[i].checked = ( comTypes[i] == typeString ) ) {
				_.form[i].style.display = 'block';
				typeIndex = i;
			}
			else
				_.form[i].style.display = 'none';
		}
	}

	//--------------------------------------------------------------------------
	//  exists															public
	//..........................................................................
	//	returns true if this user exists
	this.exists = function ( ) {
		for ( var i = 0; i < list.length; ++i ) {
			if ( list[i].index == owner.getIndex() &&
				 list[i].type == this.getType() &&
				 list[i].name == _.name.value )
					return list[i];
		}
		return false;
	}

	//--------------------------------------------------------------------------
	//  install															public
	//..........................................................................
	//	load a user com from the user com list into the form
	this.install = function ( index ) {
		_.name.value = list[index].name;
		this.setType ( list[index].type );
		switch ( typeIndex ) {
			case 0:	_.telefon_number.value = list[index].addr;
					break;
			case 1:	_.sms_number.value = list[index].addr;
					break;
			case 2:	_.email_address.value = list[index].addr;
					_.email_subject.value = list[index].subj;
					_.email_body.value    = list[index].data;
					break;
		}
		return list[index].target_id;
	}

	//--------------------------------------------------------------------------
	//  load															public
	//..........................................................................
	//	load com data from the server and make the com tables
	this.load = function ( ) {
		if ( owner.isReadyToLoadCom ( ) ) {
			ajax("download.php", _.job + "&format=jason&" + this.owner_query, 
				function ( reply ) {
					list = [];
					var html = "";
					if ( reply ) { alert(reply);
						list = eval ( '(' + reply + ')' );
						for (var i = 0; i < list.length; ++i ) {
							list[i].data = list[i].data;
							list[i].name = list[i].name;
							list[i].addr = list[i].addr;
							list[i].subj = list[i].subj;
							list[i].target_name = list[i].target_name;
//							list[i].data = base64_decode ( list[i].data );
//							list[i].name = base64_decode ( list[i].name );
//							list[i].addr = base64_decode ( list[i].addr );
//							list[i].subj = base64_decode ( list[i].subj );
//							list[i].target_name = base64_decode ( list[i].target_name );
						}
						html = that.makeTable();
					}
					for (var i = 0; i < that.owner_tables.length; ++i )
						document.getElementById(that.owner_tables[i]).innerHTML = html;
				});
		}
	}

	//--------------------------------------------------------------------------
	//  remove															public
	//..........................................................................
	//	remove a com record from the database
	//		index = index into list[]
	this.remove = function ( index ) {
		if ( ! spamCheck() )
			alert(_s("Du musst noch das Resultat in der Spamschutz Rechnung eintragen."));
		else if ( confirm(sprintf(_s("Wollen Sie den Kommunikationseintrag %s (%d) wirklich loeschen?"),
			list[index].name,list[index].id))) {
			ajax("upload.php","job=delete_com&id=" + list[index].id,
					function ( reply) {
	 				  	if ( "OK" == reply )
	 				  		that.load();
	 				  	else
	 				  		alert (reply);
					} );
		}
	}

	//--------------------------------------------------------------------------
	//  save															public
	//..........................................................................
	//	save a com record into the database
	this.save = function ( ) {
		if ( inputChecked() ) {
			var parameter = "job=" + upload_job;
			parameter += "&target=" + this.db;
			parameter += "&index=" + owner.getIndex();
			parameter += "&type=" + this.getType();
			parameter += "&name=" + _.name.value;
//			parameter += "&name=" + base64_encode ( _.name.value );
			switch ( typeIndex ) {
				case 0: parameter += "&addr=" + _.telefon_number.value;
//				case 0: parameter += "&addr=" + base64_encode ( _.telefon_number.value );
						break;
				case 1: parameter += "&addr=" + _.sms_number.value;
//				case 1: parameter += "&addr=" + base64_encode ( _.sms_number.value );
						break;
//				case 2: parameter += "&addr=" + base64_encode ( _.email_address.value );
//						parameter += "&subj=" + base64_encode ( _.email_subject.value );
//						parameter += "&data=" + base64_encode ( _.email_body.value );
				case 2: parameter += "&addr=" + _.email_address.value;
						parameter += "&subj=" + _.email_subject.value;
						parameter += "&data=" + _.email_body.value;
						break;
			}
			ajax("upload.php",parameter,
 				  function ( reply) {
 				  	if ( "OK" == reply )
 				  		that.load();
 				  	else
 				  		alert (reply);
 				  } );
		}
	}
	//	check all inputs in the com form
	function inputChecked ( ) {
		var existing_record;
		//	check the spam calc
		if ( ! spamCheck() )
			alert(_s("Du musst noch das Resultat in der Spamschutz Rechnung eintragen."));
		//	have the owner check his data
		else if ( ! owner.FormComplet() )
			return false;
		//	check the length of name string
		else if ( _.name.value.length < 2 )
			alert ( _s("Es muss noch ein Name eingetragen werden.") );
		else {
			//	this com record exists
			if ( existing_record = that.exists() )
				//	ask the user if he wants to update
				if ( confirm(_s("Diese Kommunikation existiert schon. Wollen Sie die Aenderungen speichern?")))
					upload_job = "update_com&id=" + existing_record.id;
				else
					return false;
			//	this is a new com record
			else
				upload_job = "insert_com";
			switch ( typeIndex ) {
				//	check the telefon number. could be improved
				case 0: if ( _.telefon_number.value.length < 3 )
							alert ( _s("Es muss noch eine Telefon Nummer eingetragen werden.") );
						else
							return true;
						break;
				//	check the sms number. could be improved
				case 1: if ( _.sms_number.value.length < 3 ) 
							alert ( _s("Es muss noch eine SMS Nummer eingetragen werden.") );
						else
							return true;
						break;
				//	check the email. could be improved
				case 2: if ( _.email_address.value.length < 8 )
							alert ( _s("Die Email Adresse muss noch eingetragen werden.") );
						else if ( _.email_subject.value.length < 1 )
							alert ( _s("Das Betreffnis muss noch eingetragen werden.") );
						else if ( _.email_body.value.length < 3 )
							alert ( _s("Die Email hat keinen Inhalt.") );
						else
							return true;
						break;
			}
		}
		return false;
	}

	//--------------------------------------------------------------------------
	//  makeTable														public
	//..........................................................................
	//	make a com table
	this.makeTable = function ( ) {
		html = "<table cellspacing='0' class='com_table'>" +
				"<caption>" + _s('Bestehende Kommunikation') + "</caption>" +
					"<thead>" +
						"<tr>" +
							"<th></th>" +
							"<th class='zahl'>" + _s('ID') + "</th>" +
							"<th class='txt'>" + this.owner_name + "</th>" +
							"<th class='txt'>" + _s('Type') + "</th>" +
							"<th class='txt'>" + _s('Name') + "</th>" +
							"<th class='txt'>" + _s('Adresse') + "</th>" +
							"<th>" + _s('Inhalt') + "</th>" +
						"</tr>" +
					"</thead>" +
					"<tbody>";
		for ( var i = 0; i < list.length; ++i ) {
			var deleteClick = " onclick=\"" + this.owner_obj_name + ".deleteCom(" + i + ")\"";
			var installCom  = " onclick=\"" + this.owner_obj_name + ".installCom("  + i + ")\"";
			html += "<tr class='" + rowClass[i%2] + " link'>" +
						"<td class='delete'" + deleteClick + ">" + _s('Loeschen') + "</td>" +
						"<td class='zahl'"	 + installCom  + ">" + list[i].id + "</td>" +
						"<td class='txt'"	 + installCom  + ">" + list[i].target_name + "</td>" +
						"<td class='txt'"	 + installCom  + ">" + list[i].type + "</td>" +
						"<td class='txt'"	 + installCom  + ">" + list[i].name + "</td>" +
						"<td class='txt'"	 + installCom  + ">" + list[i].addr + "</td>";
						;
			// extra line for the email data
			if ( 'email' == list[i].type ) {
				var id = owner.makeID(i);
				var openClick    = " onclick=\"show_mail("  + id + ")\"";
				var closeClick    = " onclick=\"hide_mail("  + id + ")\"";
				html += 	"<td class='open'" + openClick + ">" + _s( "open" ) + "</td>" +
						"</tr><tr class='" + rowClass[i%2] + " hidden' id= 'email" + id + "a'>" +
							"<td class='close'" + closeClick + "></td>" +
							"<td class='titel links' colspan=\"2\">" + _s('Titel') + "</td>" +
							"<td class='titel rechts' colspan=\"4\">" + _s('Inhalt') + "</td>" +
						"</tr><tr class='" + rowClass[i%2] + " hidden' id= 'email" + id + "b'>" +
							"<td class='close'" + closeClick + ">" + _s( "close" ) + "</td>" +
							"<td class='txt links unten'  colspan=\"2\">" + list[i].subj + "</td>" +
							"<td class='txt rechts unten' colspan=\"4\">" + emailSample(list[i]) + "</td>";
			}
			else
				html += "<td></td>"
			html += "</tr>";
		}
		html += 	"</tbody>" +
				"</table>";
		return html;
	}

	//--------------------------------------------------------------------------
	//  emailSample														private
	//..........................................................................
	//	takes the email body and inserts sample variable data
	function emailSample (the) {
		var user = (that.target == 'groups' ? 'HR' : the.owner );
		var jsdate = new Date();
		var date = jsdate.localDate();
		var time = jsdate.unixTime();
		var lat = 47.221835;
		var lon = 7.544303;
		var alt = 857;
		var link = "<a href=\"http://www.google.com/maps?q=" + lat + "," + lon + "%28" + user +
					 _s("+war+hier+um+") + time + "%29\">";
		return the.data.replace(/<mapLink>/g,link).replace(/<\/mapLink>/g,"</a>").
				replace(/<lat>/g,lat).replace(/<lon>/g,lon).replace(/<alt>/g,alt).
				replace(/<date>/g,date).replace(/<time>/g,time).replace(/<user>/g,the.target_name);
	}
	if ( typeIndex == -1 )
		this.setType ( comTypes[0] );
	this.load();
}

