/*--------------------------------------------------------------------------
// _iPhone object
//..........................................................................
//		needs the com.js to be included
*/
//--------------------------------------------------------------------------
// construct the iPhone object
//..........................................................................
//	load the iPhone and user data and create a new singelton object
function load_iPhone ( id ) {
	ajax("download.php",
		 "job=find_unique_id&format=jason&id=" + id, 
		 function ( reply ) {
			new _iPhone ( reply );
		});
}

//--------------------------------------------------------------------------
// _iPhone implementation
//..........................................................................
function _iPhone ( str ) {
	// this is a singelton object
	iPhone = this;
	// private functions can now use this
	var that = this;
	// the communication object
	var com;
	// the user record list
	this.users = [];
	// the selected user id
	this.user = -1;

	//--------------------------------------------------------------------------
	//  FormComplet														public
	//..........................................................................
	//	called from the _com object
	this.FormComplet = function ( ) {
		if ( ! this.is_valid() )
			alert ( _s("Es muss eine gueltige iPhone ID eingetragen werden.") );
		else if ( ! this.has_user_selected() )
			alert ( _s("Es ist kein Benutzer ausgewaehlt.") );
		else
			return true;
		return false;
	}

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

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

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

	//--------------------------------------------------------------------------
	//  installCom														public
	//..........................................................................
	//	copy a com record into the form and user dropdown
	this.installCom = function ( index ) {
		this.make_dropdowns ( com.install ( index ) );
	}

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

	//--------------------------------------------------------------------------
	//  isReadyToLoadCom												public
	//..........................................................................
	//	is ready with a valid iPhone id
	this.isReadyToLoadCom = function ( ) {
		if ( this.is_valid() ) return true;
		for (var i = 0; i < com_tables.length; ++i )
			document.getElementById(com_tables[i]).innerHTML = "";
		return false;
	}

	//--------------------------------------------------------------------------
	//  initCom															public
	//..........................................................................
	//	called from the _com object at init
	this.initCom = function ( com ) {
		com.target = 'user';
		com.db = 'user';
		com.owner_name =  _s('User');
		com.owner_query = "device=" + this.uniqueID + "&user=" + this.user;
		com.owner_tables = ['user_com_table'];
		com.owner_obj_name = "iPhone";
	}

	//--------------------------------------------------------------------------
	//  is_valid														public
	//..........................................................................
	//	check for a valid iPhone id
	this.is_valid = function ( ) {
		
		return ( this.uniqueID.length > 30 );
	}

	//--------------------------------------------------------------------------
	//  has_user_selected												public
	//..........................................................................
	//	check if a user is selected
	this.has_user_selected = function ( ) {
		return ( this.user != -1 );
	}

	//--------------------------------------------------------------------------
	//  user_exists												public
	//..........................................................................
	//	does the new user name exist already
	this.user_exists = function ( name ) {
		if ( name )
			return nameExists ( this.users, name );
		return nameExists ( this.users, user_name_input() );
	}

	//--------------------------------------------------------------------------
	//  user_changed													public
	//..........................................................................
	//	called from the user dropdown onchange
	this.user_changed = function ( selector ) {
		this.make_dropdowns( selector.options[selector.selectedIndex].value );
	}

	//--------------------------------------------------------------------------
	//  delete_user														private
	//..........................................................................
	//	returns the name input
	this.delete_user = function ( index ) {
		if ( deleteApproved ( this.users[index].name, this.users[index].id ) )
			ajax("upload.php","job=delete_user&id=" + this.users[index].id,
					function ( reply) {
					  	if ( "OK" == reply )
		 					load_iPhone ( that.uniqueID );
		 				else
					  		alert (reply);
					} );
	}
	// check the spam check and confirm the deletion
	function deleteApproved ( name, id ) {
		if ( spamCheck() )
			return confirm(sprintf(_s("Wollen Sie den Benutzer %s (%d) wirklich loeschen?"),name,id));
		alert(_s("Du musst noch das Resultat in der Spamschutz Rechnung eintragen."));
		return false;
	}

	//--------------------------------------------------------------------------
	//  save_user														public
	//..........................................................................
	this.save_user = function ( ) {
		if ( userInputChecked() )
			ajax("upload.php","job=save_user&name=" + user_name_input(true) + 
										 "&device=" + this.uniqueID,
					function ( reply) {
					  	if ( "OK" == reply )
					  		load_iPhone ( that.uniqueID );
					  	else
					  		alert (reply);
					} );
	}
	function userInputChecked ( ) {
		if ( ! spamCheck() )
			alert(_s("Du musst noch das Resultat in der Spamschutz Rechnung eintragen."));
		// check the name input
		else if ( ! that.is_valid() )
			alert ( _s("Es muss eine gueltige iPhone ID eingetragen werden.") );
		// check the name input
		else if ( user_name_input().length < 2 )
			alert ( _s("Der Benutzername ist zu kurz.") );
		// return false if the user exists
		else if ( that.user_exists() )
			alert ( _s("Dieser Benutzername existiert schon.") );
		else 
			return true;
		return false;
	}

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

	//--------------------------------------------------------------------------
	//  user_name_input													private
	//..........................................................................
	//	returns the name input
	function user_name_input ( encoded ) {
		var name = document.getElementById("user_name").value;
//		return ( encoded ? base64_encode ( name ) : name );
		return name;
	}

	//--------------------------------------------------------------------------
	//  make_dropdowns													public
	//..........................................................................
	//	make the user dropdowns
	this.make_dropdowns = function ( selected_id ) {
		if ( selected_id == undefined )
			selected_id = ( this.users.length == 1 ? this.users[0].id : -1 );
		var html = ( this.is_valid() ? makeDropdown( this.users, selected_id ) : "" );
		for (var i = 0; i < user_dropdowns.length; ++i )
			document.getElementById(user_dropdowns[i]).innerHTML = html;
		this.user = selected_id;
		new _members();
		com = new _com(this);
	}
	// make the dropdown html
	function makeDropdown( user_array, id ) {
		html  = "<select class='Select' onchange=\"iPhone.user_changed(this)\" >";
		var selected;
		if ( user_array.length > 1 ) {
			selected = ( id == -1 ? "selected=\"selected\"" : "" );
			html += "<option value='-1' " + selected + ">" + _s("Waehle einen Benutzer") + "</option>";
		}
		for ( var i = 0; i < user_array.length; ++i ) {
			selected = ( id == user_array[i].id ? "selected=\"selected\"" : "" );
			html += "<option value='" + user_array[i].id + "' " + selected;
			html += " >" + user_array[i].name + "</option>";
		}
		html += "</select>";
		return html;
	}

	//--------------------------------------------------------------------------
	//  make_tables													public
	//..........................................................................
	//	called from the user dropdown onchange
	this.make_tables = function ( ) {
		var html = makeTable( this.is_valid(), this.users );
		for (var i = 0; i < user_tables.length; ++i )
			document.getElementById(user_tables[i]).innerHTML = html;
	}
	// make the table html
	function makeTable( doit, user_array ) {
		if ( !doit )	return "";
		html  = "<table cellspacing='0' class='user_table'>" +
					"<caption>" + _s('Benutzer') + "</caption>" +
					"<thead>" +
						"<tr>" +
							"<th></th>" +
							"<th class='zahl'>" + _s('ID') + "</th>" +
							"<th class='txt'>" + _s('Name') + "</th>" +
						"</tr>" +
					"</thead>" +
					"<tbody>";
		for ( var i = 0; i < user_array.length; ++i ) {
			var id = user_array[i].id;
			var name = user_array[i].name;
			html += "<tr class='" + rowClass[i%2] + "'>";
			if ( user_array.length > 1 )
				html += "<td class='delete' onclick=\"iPhone.delete_user(" + i + ")\">" + _s('Loeschen') + "</td>";
			else
				html += "<td></td>";
			html += 	"<td class='zahl'>" + id + "</td>" +
						"<td class='txt'>" + name + "</td>" +
					"</tr>";
		}
		html += 	"</tbody>" +
				"</table>";
		return html;
	}
	// first onload call without a str parameter
	var inited = str != undefined;
	// init the str parameter on initial call
	if ( ! inited )
		str = "{uniqueID:0,users:[]}";
	var obj = eval ( '(' + str + ')' );
	this.uniqueID = obj.uniqueID;
	// decode all user names received from the server
	for ( var i = 0; i < obj.users.length; ++i )
//		this.users[i] = { id:obj.users[i].id, name:base64_decode(obj.users[i].name) }
		this.users[i] = { id:obj.users[i].id, name:obj.users[i].name }
	// write the iPhone id into all id input elements
	if ( this.is_valid() )
		for (var i = 0; i < id_inputs.length; ++i )
			document.getElementById(id_inputs[i]).value = this.uniqueID;
	this.make_dropdowns();
	this.make_tables();
	if ( this.uniqueID == 0 && inited )
		alert ( _s("Diese ID ist nicht bekannt.") );
}

//--------------------------------------------------------------------------
// iPhoneIDChanged
//..........................................................................
//	load the iPhone and user data and create a new singelton object
function iPhoneIDChanged( id_input, evt ) {
	var keynum = ((window.event) ? evt.keyCode : evt.which);
	var key = String.fromCharCode(keynum);
	var key_string = id_input.value.length > 30 ? key : id_input.value + key;
	if ( key_string.length < 3 )
		return true;
	load_iPhone ( id_input.value + key );
	return true;
}

