// user has pressed the "down" key
void chatbox_recall_down()
{	
	// if we've got no recall lines, do nothing
	if(Chatbox_recall_count <= 0){
		return;
	}

	// if we can decrement down
	if(Chatbox_recall_index > 0){
		// if this is the last line we recalled, pre-decrement
		if(Chatbox_recall_last == Chatbox_recall_index){
			Chat_inputbox.set_text(Chatbox_recall_lines[--Chatbox_recall_index]);
			Chatbox_recall_last = Chatbox_recall_index;
		} 
		// otherwise post,decrement
		else {
			Chat_inputbox.set_text(Chatbox_recall_lines[Chatbox_recall_index--]);
			Chatbox_recall_last = Chatbox_recall_index + 1;
		}
	} 
	// if we can't decrement down
	else {		
		Chat_inputbox.set_text("");
		Chatbox_recall_last = -1;
	}	
}
// user has pressed the "up" key
void chatbox_recall_up()
{
	// if we've got no recall lines, do nothing
	if(Chatbox_recall_count <= 0){
		return;
	}

	// if we can increment up
	if(Chatbox_recall_index < (Chatbox_recall_count - 1)){
		// if this is the last line we recalled, pre-increment
		if(Chatbox_recall_last == Chatbox_recall_index){
			Chat_inputbox.set_text(Chatbox_recall_lines[++Chatbox_recall_index]);
			Chatbox_recall_last = Chatbox_recall_index;
		}
		// otherwise, post increment
		else {
			Chat_inputbox.set_text(Chatbox_recall_lines[Chatbox_recall_index++]);
			Chatbox_recall_last = Chatbox_recall_index - 1;
		}
	} 
	// if we can't increment up
	else {
		Chat_inputbox.set_text(Chatbox_recall_lines[Chatbox_recall_index]);
		Chatbox_recall_last = Chatbox_recall_index;
	}	
}
// automatically split up any input text, send it, and leave the remainder 
void chatbox_autosplit_line()
{
	char *remainder,msg[150];
	int msg_pixel_width;
	
	// if the chat line is getting too long, fire off the message, putting the last
	// word on the next input line.
	memset(msg,0,150);
	Chat_inputbox.get_text(msg);
	remainder = "";
	// determine if the width of the string in pixels is > than the inputbox width -- if so,
	// then send the message
	gr_get_string_size(&msg_pixel_width, NULL, msg);
	// if ( msg_pixel_width >= (Chatbox_inputbox_w - Player->short_callsign_width) ) {
	if ( msg_pixel_width >= (Chatbox_inputbox_w - 25)) {
		remainder = strrchr(msg, ' ');
		if ( remainder ) {
			*remainder = '\0';
			remainder++;
		} else {
			remainder = "";
		}	
		// if I'm the server, then broadcast the packet		
		chatbox_recall_add(msg);
  		send_game_chat_packet(Net_player, msg, MULTI_MSG_ALL,NULL);
		chatbox_add_line(msg, MY_NET_PLAYER_NUM);

		// display any remainder of text on the next line
		Chat_inputbox.set_text(remainder);
	} else if((Chat_inputbox.pressed() && (strlen(msg) > 0)) || (strlen(msg) >= CHATBOX_MAX_LEN)) { 
		// tack on the null terminator in the boundary case
		int x = strlen(msg);
		if(x >= CHATBOX_MAX_LEN){
			msg[CHATBOX_MAX_LEN-1] = '\0';
		}		
		// if I'm the server, then broadcast the packet		
		chatbox_recall_add(msg);
  		send_game_chat_packet(Net_player, msg, MULTI_MSG_ALL,NULL);
		chatbox_add_line(msg, MY_NET_PLAYER_NUM);

		// display any remainder of text on the next line
		Chat_inputbox.set_text(remainder);		
	}	
}
int player_select_create_new_pilot()
{
	int idx;

	// make sure we haven't reached the max
	if (Player_select_num_pilots >= MAX_PILOTS) {
		gamesnd_play_iface(SND_GENERAL_FAIL);
		return 0;
	}

	int play_scroll_sound = 1;

	if ( play_scroll_sound ) {
		gamesnd_play_iface(SND_SCROLL);
	}

	idx = Player_select_num_pilots;	

	// move all the pilots in the list up
	while (idx--) {
		strcpy(Pilots[idx + 1], Pilots[idx]);
	}

	// by default, set the default netgame protocol to be VMT
	Multi_options_g.protocol = NET_TCP;

	// select the beginning of the list
	Player_select_pilot = 0;
	Player_select_num_pilots++;
	Pilots[Player_select_pilot][0] = 0;
	Player_select_list_start= 0;

	// set us to be in input mode
	player_select_set_input_mode(1);

	// set the input box to have focus
	Player_select_input_box.set_focus();
	Player_select_input_box.set_text("");
	Player_select_input_box.update_dimensions(Choose_list_coords[gr_screen.res][0], Choose_list_coords[gr_screen.res][1], Choose_list_coords[gr_screen.res][2], gr_get_font_height());	

	return 1;
}
// creates a new pilot file
void pilot_manage_create_new_pilot()//UI_INPUTBOX *Inputbox)
{
	// check if too many pilots
	if (Num_pilots >= MAX_PILOTS) {
		gamesnd_play_iface(SND_GENERAL_FAIL);
		return;
	}

	// play sound for pilot creation
	gamesnd_play_iface(SND_SCROLL);
	
	// only write pilot file if there is an active pilot
	if (strlen(Player->callsign)) {
		write_pilot_file();
	}

	// move other pilot names and ranks down to make room for the new one
	int idx = Num_pilots;
	Assert(Num_pilots >= 0);
	while (idx--) {
		strcpy(Pilots[idx + 1], Pilots[idx]);
		Pilot_ranks[idx + 1] = Pilot_ranks[idx];
	}

	Selected_line = 0;
	Num_pilots++;
	Pilots[Selected_line][0] = 0;
	Pilot_ranks[Selected_line] = 0;
	List_scroll_offset = 0;

	// set mode to accept pilot name text
	pilot_manage_set_callsign_enter_mode(true);
	// set focus to input box
	Inputbox.set_focus();
	// set initial pilot name to ""
	Inputbox.set_text("");
	// reset size of input box to only 1 line
	Inputbox.update_dimensions(Pilot_manage_list_coords[gr_screen.res][PM_X_COORD], Pilot_manage_list_coords[gr_screen.res][PM_Y_COORD], Pilot_manage_list_coords[gr_screen.res][PM_W_COORD], gr_get_font_height());
	
  }
// automatically split up any input text, send it, and leave the remainder 
void chatbox_autosplit_line()
{
	char *remainder,msg[150];
	char temp[150];
	int msg_pixel_width;
	int target, target_length = -1; 
	
	// if the chat line is getting too long, fire off the message, putting the last
	// word on the next input line.
	memset(msg,0,150);
	Chat_inputbox.get_text(msg);
	remainder = "";

	// check if this message is supposed to have a recipient
	target = chatbox_get_msg_target_type(msg); 
	target_length = chatbox_get_msg_target_length(msg); 

	// determine if the width of the string in pixels is > than the inputbox width -- if so,
	// then send the message
	gr_get_string_size(&msg_pixel_width, NULL, msg);
	// if ( msg_pixel_width >= (Chatbox_inputbox_w - Player->short_callsign_width) ) {
	if ( msg_pixel_width >= (Chatbox_inputbox_w - 25)) {
		remainder = strrchr(msg, ' ');
		if ( remainder ) {
			*remainder = '\0';
			remainder++;
		} else {
			remainder = "";
		}	
		// if I'm the server, then broadcast the packet		
		chatbox_recall_add(msg);

		if (target != MULTI_MSG_EXPR) {
  			send_game_chat_packet(Net_player, msg, target);
		}
		else {
			// copy the name of the player the message is being sent to
			strncpy(temp, msg+1, target_length-2);
			temp[target_length-2] = '\0';
			send_game_chat_packet(Net_player, msg, target, NULL, temp);
		}
		chatbox_add_line(msg, MY_NET_PLAYER_NUM);

		if (target != MULTI_MSG_ALL) {
			// we need to add the target the message is going to before we add the rest of the string
			strncpy(temp, msg, target_length);
 			temp[target_length] = ' '; 
 			temp[target_length+1] = '\0'; 
			strcat_s(temp, remainder); 
			Chat_inputbox.set_text(temp);
		}
		else {
			// display any remainder of text on the next line
			Chat_inputbox.set_text(remainder);
		}
	} else if((Chat_inputbox.pressed() && (msg[0] != '\0')) || (strlen(msg) >= CHATBOX_MAX_LEN)) { 
		// tack on the null terminator in the boundary case
		int x = strlen(msg);
		if(x >= CHATBOX_MAX_LEN){
			msg[CHATBOX_MAX_LEN-1] = '\0';
		}
	
		// if I'm the server, then broadcast the packet		
		chatbox_recall_add(msg);
		if (target != MULTI_MSG_EXPR) {
  			send_game_chat_packet(Net_player, msg, target);
		}
		else {
			// copy the name of the player the message is being sent to
			strncpy(temp, msg+1, target_length-2);
			temp[target_length-2] = '\0';
			send_game_chat_packet(Net_player, msg, target, NULL, temp);
		}

		chatbox_add_line(msg, MY_NET_PLAYER_NUM);

		// display any remainder of text on the next line
		Chat_inputbox.set_text(remainder);		
	}	
}