<?php

	include "library.php";

	$cxn = start_code();

	
			/* get variables */

	$id = val( $_SESSION, "id" );

	$box = val( $_GET, "box" );

		/* if the user is not logged in them direct them to the login page then back to here */

	if ($id == "")
	{
		$_SESSION['nextpage'] = url_encode( "mailboxes.php", "box", $box );

		jump_to_page( "login.php" );	/* must happen before print output */

		exit();
	}


	crash_if_missing( $box, "box missing in mailboxes", $cxn );

	
		/* start the page  */

	if ($box == "inbox")
	{
		page_header( "Inbox", $cxn );

		show_buttons( $cxn );

		$query = "SELECT message.*, profile.handle, reveal_first_name, first_name FROM message LEFT JOIN profile ON profile.id = message.sender_id WHERE show_in_inbox = 'yes' AND receiver_id = ".$id." ORDER BY sent";

		$result = run_query( $query, $cxn );

		echo "<form action='".url_encode( "remove_from_inbox.php" )."' method='POST'>";

		echo "<table width='100%'><td width='100%' align='center'><input type='image' src='buttons/".button_filename( "remove_selected" )."' border='0'></td></tr></table>";

		echo "<br><br>";

		echo "<table width='100%'><tr><td width='5'></td><td width='14%'>".black( "<b>Date</b>" )."</td><td width='10%'>".black( "<b>Type</b>" )."</td><td width='12%'>".black( "<b>From</b>" )."</td><td width='12%'>".black( "<b>Name</b>" )."</td><td width='59%'>".black( "<b>Subject Line</b>" )."</td></tr>";

		echo "<tr><td>".sp(1)."</td></tr>";

		if (mysql_num_rows($result) == 0)
			echo "<tr><td></td><td></td><td></td><td colspan='3'>".black( "<br>There are no messages in your inbox" )."</td></tr>";

		for ($i=0;$i < mysql_num_rows($result);$i++)
		{
			$data = get_row( $result );

				/* fill in blank subject lines */

			if ($data['type'] == "email" && $data['subject'] == "")
				$subj = "No subject";
			else
				$subj = convert_to_html( $data['subject'] );


				/* highlight kisses that haven't been replied to */

			$show_red = false;

			if ($data['type'] == 'kiss' && $data['has_been_replied_to'] != "yes")
			{
				$show_red = true;

				$url = url_encode( "display_profile.php", "showid", $data['sender_id'], "returnpoint", "inbox", "startpos", "" );

				$subj = "<a class='gred' href='".$url."'>".red( "<span style='{text-decoration: underline}'>Not replied to yet</span>" )."</a>";
			}

			if ($data['type'] == "kiss reply")
			{
				if ($data['message'] == "1")
					$subj = "kiss accepted";
				else
					$subj = "kiss declined";
			}


				/* selection tickbox */

			echo "<tr><td><input type='checkbox' name='id".$data['id']."'></td>";


				/* date */

			if ($show_red)
				echo "<td>".red( Date( "j M Y", strtotime( $data['sent'] )) )."</td>";
			else
				echo "<td>".black( Date( "j M Y", strtotime( $data['sent'] )) )."</td>";


				/* type */

			if ($show_red)
				echo "<td>".red( $data['type'] )."</td>";
			else
				echo "<td>".black( $data['type'] )."</td>";


				/* handle */

			echo "<td>";

			echo show_handle( $data['sender_id'], $data['handle'], "inbox", "" );

			echo "</td>";


				/* first name */

			echo "<td>";

			if (val( $data, 'reveal_first_name' ) == "on")
				echo black( strtolower( $data['first_name'] ) );

			echo "</td>";


				/* delete from inbox */

//			echo "<td>".show_link( "remove from inbox", "remove_from_inbox.php", "messageid", $data['id'] )."</td>";


				/* subject */

			if ($data['type'] == "email")
			{
				echo "<td>".show_link( $subj, "view_email.php", "emailid", $data['id'] )."</td></tr>";
			}
			else
			{
				if ($show_red)
					echo "<td>".red( $subj )."</td></tr>";
				else
					echo "<td>".black( $subj )."</td></tr>";
			}
		}

		echo "</form>";

	}
	else
	if ($box == "received")
	{
		page_header( "Received Messages", $cxn );

		show_buttons( $cxn );

		echo "<br><br>";

		$query = "SELECT message.*, profile.handle, reveal_first_name, first_name FROM message LEFT JOIN profile ON profile.id = message.sender_id WHERE receiver_id = ".$id." ORDER BY sent DESC";

		$result = run_query( $query, $cxn );

		echo "<table width='100%'><tr><td width='14%'>".black( "<b>Date</b>" )."</td><td width='8%'>".black( "<b>Type</b>" )."</td><td width='13%'>".black( "<b>Received</b>" )."</td><td width='10%'>".black( "<b>From</b>" )."</td><td width='10%'>".black( "<b>Name</b>" )."</td><td width='45%'>".black( "<b>Subject Line</b>" )."</td></tr>";

		echo "<tr><td>".sp(1)."</td></tr>";

		if (mysql_num_rows($result) == 0)
			echo "<tr><td></td><td></td><td></td><td colspan='3'>".black( "<br>There are no messages in your received box" )."</td></tr>";

		for ($i=0;$i < mysql_num_rows($result);$i++)
		{
			$data = get_row( $result );

				/* fill in blank subject lines */

			if ($data['type'] == "email" && $data['subject'] == "")
				$subj = "No subject";
			else
				$subj = convert_to_html( $data['subject'] );


				/* highlight kisses that haven't been replied to */

			$show_red = false;

			if ($data['type'] == 'kiss' && $data['has_been_replied_to'] != "yes")
			{
				$show_red = true;

				$url = url_encode( "display_profile.php", "showid", $data['sender_id'], "returnpoint", "received", "startpos", "" );

				$subj = "<a class='gred' href='".$url."'>".red( "<span style='{text-decoration: underline}'>Not replied to yet</span>" )."</a>";
			}

			if ($data['type'] == "kiss reply")
			{
				if ($data['message'] == "1")
					$subj = "kiss accepted";
				else
					$subj = "kiss declined";
			}

				/* date */

			if ($show_red)
				echo "<tr><td>".red( Date( "j M Y", strtotime( $data['sent'] )) )."</td>";
			else
				echo "<tr><td>".black( Date( "j M Y", strtotime( $data['sent'] )) )."</td>";


				/* type */

			if ($show_red)
				echo "<td>".red( $data['type'] )."</td>";
			else
				echo "<td>".black( $data['type'] )."</td>";


				/* comment to avoid confusion */

			if ($show_red)
				echo "<td>".red( "received from" )."</td>";
			else
				echo "<td>".black( "received from" )."</td>";


				/* handle */

			echo "<td>";

			echo show_handle( $data['sender_id'], $data['handle'], "received", "" );

			echo "</td>";


				/* first name */

			echo "<td>";

			if (val( $data, 'reveal_first_name' ) == "on")
				echo black( strtolower( $data['first_name'] ) );

			echo "</td>";


				/* subject */

			if ($data['type'] == "email")
			{
				echo "<td>".show_link( $subj, "view_email.php", "emailid", $data['id'] )."</td></tr>";
			}
			else
			{
				if ($show_red)
					echo "<td>".red( $subj )."</td></tr>";
				else
					echo "<td>".black( $subj )."</td></tr>";
			}
		}
	}
	else
	if ($box == "sent")
	{
		page_header( "Sent Messages", $cxn );

		show_buttons( $cxn );

		echo "<br><br>";
		
		$query = "SELECT message.*, profile.handle, reveal_first_name, first_name FROM message LEFT JOIN profile ON profile.id = message.receiver_id WHERE sender_id = ".$id." ORDER BY sent DESC";

		$result = run_query( $query, $cxn );

		echo "<table width='100%'><tr><td width='14%'>".black( "<b>Date</b>" )."</td><td width='8%'>".black( "<b>Type</b>" )."</td><td width='10%'>".black( "<b>Sent</b>" )."</td><td width='14%'>".black( "<b>To</b>" )."</td><td width='10%'>".black( "<b>Name</b>" )."</td><td width='44%'>".black( "<b>Subject Line</b>" )."</td></tr>";
//		echo "<table width='100%' border='0' cellpadding='0' cellspacing='0'><tr bgcolor='#d9ffff'><td width='14%'>".black( "Date" )."</td><td width='8%'>".black( "Type" )."</td><td width='10%'>".black( "Sent" )."</td><td width='14%'>".black( "To" )."</td><td width='10%'>".black( "Name" )."</td><td width='44%'>".black( "Subject Line" )."</td></tr>";

		echo "<tr><td>".sp(1)."</td></tr>";

		if (mysql_num_rows($result) == 0)
			echo "<tr><td></td><td></td><td></td><td colspan='3'><br>".black( "There are no messages in your sent box" )."</td></tr>";

		for ($i=0;$i < mysql_num_rows($result);$i++)
		{
			$data = get_row( $result );

				/* date  */

			echo "<tr><td>".black( Date( "j M Y", strtotime( $data['sent'] )) )."</td>";


				/* type */

			echo "<td>".black( $data['type'] )."</td>";


				/* comment to avoid confusion */

			echo "<td>".black( "sent to" )."</td>";


				/* handle */

			echo "<td>";

			if (val( $data, 'handle' ) == "")
				echo black( "<i>profile deleted</i>" );
			else
				echo show_handle( $data['receiver_id'], $data['handle'], "sent", "" );

			echo "</td>";


				/* first name */

			echo "<td>";

			if (val( $data, 'reveal_first_name' ) == "on")
				echo black( strtolower( $data['first_name'] ) );

			echo "</td>";


				/* subject */

			if ($data['type'] == "email" && $data['subject'] == "")
				$subj = "No subject";
			else
				$subj = convert_to_html( $data['subject'] );

			if ($data['type'] == "kiss reply")
			{
				if ($data['message'] == "1")
					$subj = "kiss acceptance";
				else
					$subj = "kiss declined";
			}

			if ($data['type'] == "email")
			{
				echo "<td>".show_link( $subj, "view_email.php", "emailid", $data['id'] )."</td></tr>";
			}
			else
				echo "<td>".black( $subj )."</td></tr>";
		}

		echo "</table></table>";
	}
	else
	if ($box == "credits")
	{
		page_header( "Credits History", $cxn );

		show_buttons( $cxn );

		echo "<br><br>";
		
		$query = "SELECT profile.credits_balance FROM profile WHERE id = ".$id;

		$result = run_query( $query, $cxn );

		$data = get_row( $result );

		if ($data['credits_balance'] == "1")
			echo red( "You have 1 credit<br><br>" );
		else
			echo red( "You have ".$data['credits_balance']." credits<br><br>" );

		$query = "SELECT credits.*, profile.id as receiver_id, profile.handle as receiver_handle, profile.credits_balance, message.subject FROM credits LEFT JOIN message ON credits.message_id = message.id LEFT JOIN profile ON profile.id = message.receiver_id WHERE credits.member_id = ".$id." ORDER BY credits.event_datetime DESC";

		$result = run_query( $query, $cxn );

		echo "<table width='100%'><tr><td width='14%'>".black( "<b>Date</b>" )."</td><td width='14%'>".black( "<b>Member</b>" )."</td><td width='20%'>".black( "<b>Event</b>" )."</td><td width='10%'>".black( "<b>Number</b>" )."</td><td width='52%'>".black( "<b>Message</b>" )."</td></tr>";

		echo "<tr><td>".sp(1)."</td></tr>";

		if (mysql_num_rows($result) == 0)
			echo "<tr><td></td><td></td><td colspan='3'>".black( "<br>There are no items in your credits history" )."</td></tr>";

		for ($i=0;$i < mysql_num_rows($result);$i++)
		{
			$data = get_row( $result );

				/* date  */

			echo "<tr><td>".black( Date( "j M Y", strtotime( $data['event_datetime'] )) )."</td>";


				/* handle */

			echo "<td>";

			if (val( $data, 'receiver_id' ) != "")
				echo show_handle( $data['receiver_id'], $data['receiver_handle'], "credits", "" );

			echo "</td>";


				/* type */

			echo "<td>".black( $data['event'] )."</td>";


				/* number */

			$num = abs( $data['number'] );

			echo "<td>".black( $num )."</td>";


				/* subject */

			echo "<td>";

			if (val( $data, 'message_id' ) != "" && val( $data, 'event' ) == 'email sent')
			{
				if (val( $data, 'subject' ) != "")
					echo show_link( $data['subject'], "view_email.php", "emailid", $data['message_id'] );
				else
					echo show_link( "No Subject", "view_email.php", "emailid", $data['message_id'] );
			}

			echo "</td>";
		}
	}
	else
	if ($box == "blocked")
	{
		page_header( "Blocked Members", $cxn );

		show_buttons( $cxn );

		echo "<br><br>";
		
		$query = "SELECT blocked_list.blockee_id, profile.handle FROM blocked_list LEFT JOIN profile ON blocked_list.blockee_id = profile.id WHERE blocker_id = ".$id." ORDER BY blockee_id";

		$result = run_query( $query, $cxn );

		echo "<table width='100%'><tr><td width='10%'></td><td width='10%'>".black( "<b>Member</b>" )."</td><td width='80%'>".black( "<b>Unblock</b>" )."</td></tr>";

		echo "<tr><td>".sp(1)."</td></tr>";

		if (mysql_num_rows($result) == 0)
			echo "<tr><td></td><td></td><td>".black( "<br>There are no members in your blocked list" )."</td></tr>";

		for ($i=0; $i < mysql_num_rows($result); $i++)
		{
			$data = get_row( $result );

				/* handle */

			echo "<td></td><td>";

			echo show_handle( $data['blockee_id'], $data['handle'], "blocked", "" );

			echo "</td>";


				/* unblock link */

			echo "<td>".show_link( "Unblock", "unblock.php", "unblockid", $data['blockee_id'] )."</td></tr>";
		}
	}
	else
	if ($box == "remembered")
	{
		page_header( "Remembered List", $cxn );

		show_buttons( $cxn );

		echo "<br><br>";
		
		$query = "SELECT remembered.member_id, remembered.remembered_id, profile.handle, reveal_first_name, first_name FROM remembered LEFT JOIN profile ON remembered.remembered_id = profile.id WHERE remembered.member_id = ".$id." ORDER BY remembered_id";

		$result = run_query( $query, $cxn );

		echo "<table width='100%'><tr><td width='14%' align='center'></td><td width='14%'>".black( "<b>Member</b>" )."</td><td width='10%'>".black( "<b>Name</b>" )."</td><td width='62%'>".black( "<b>Delete</b>" )."</td></tr>";

		echo "<tr><td>".sp(1)."</td></tr>";
		
		if (mysql_num_rows($result) == 0)
			echo "<tr><td></td><td></td><td colspan='2'>".black( "<br>There are no members in your remembered list" )."</td></tr>";

		for ($i=0; $i < mysql_num_rows($result); $i++)
		{
			$data = get_row( $result );

			echo "<tr>";

				/* photo */

			echo "<td align='center'>";

			show_photo( $data['remembered_id'], 80, -1, true, false, $cxn );

			echo "</td>";
				
				/* handle */

			echo "<td>";

			echo show_handle( $data['remembered_id'], $data['handle'], "remembered", "" );

			echo "</td>";


				/* first name */

			echo "<td>";

			if (val( $data, 'reveal_first_name' ) == "on")
				echo black( strtolower( $data['first_name'] ) );

			echo "</td>";


				/* delete */

			echo "<td>".show_link( "delete", "delete_from_remembered.php", "rememberedid", $data['remembered_id'] )."</td></tr>";
		}
	}
	else
	if ($box == "contacts")
	{
		page_header( "Contacts", $cxn );

		show_buttons( $cxn );

		echo "<br><br>";
		
		$query = "SELECT DISTINCT profile.id as noted_member_id, handle, reveal_first_name, first_name, online_status.id as logged_in, notes.text FROM profile LEFT JOIN online_status ON online_status.member_id = profile.id LEFT JOIN notes ON notes.member_id = ".$id." AND notes.noted_member_id = profile.id WHERE profile.id <> ".$id." AND (profile.id IN (SELECT DISTINCT sender_id FROM message WHERE receiver_id = ".$id.") OR profile.id IN (SELECT receiver_id FROM message WHERE sender_id = ".$id.")) ORDER BY reveal_first_name DESC, first_name, handle";

		$result = run_query( $query, $cxn );

		echo "<table width='100%'><tr><td width='5%'></td><td width='15%'>".black( "<b>Member</b>" )."</td><td width='10%'>".black( "<b>Name</b>" )."</td><td width='10%'>".black( "<b>Online</b>" )."</td><td width='15%'>".black( "<b>Notes</b>" )."</td><td width='45%'>".black( "<b>Notes Text</b>" )."</td></tr>";

		echo "<tr><td>".sp(1)."</td></tr>";
		
		if (mysql_num_rows($result) == 0)
			echo "<tr><td></td><td></td><td colspan='4'>".black( "<br>There are no members in your contacts list" )."</td></tr>";

		for ($i=0; $i < mysql_num_rows($result); $i++)
		{
			$data = get_row( $result );

			echo "<tr><td></td>";

				/* handle */

			echo "<td>";

			echo show_handle( $data['noted_member_id'], $data['handle'], "contacts", "" );

			echo "</td>";


				/* first name */

			echo "<td>";

			if (val( $data, 'reveal_first_name' ) == "on")
				echo black( strtolower( $data['first_name'] ) );

			echo "</td>";


				/* online */

			if ($data['logged_in'] != "")
				echo "<td>".watermark( "online" )."</td>";
			else
				echo "<td></td>";


				/* notes */

			echo "<td>".show_link( "notes", "notes.php", "notedid", $data['noted_member_id'] )."</td>";


				/* notes text */

			if (strlen( $data['text'] ) > 60)
				echo "<td>".substr( $data['text'], 0, 60 )."</td></tr>";
			else
				echo "<td>".$data['text']."</td></tr>";
		}
	}
	else
	{
		crash( "Internal Error: invalid box in mailboxes", $cxn );
	}


		/* show the line of buttons on the top of the screen */

	function show_buttons( $cxn )
	{
//		echo "<table width='100%'>";

//		$bg = "#d9ffff";
		$bg = "#73b8bb";

		echo "<table width='100%' border='0' cellspacing='0'><tr bgcolor='".$bg."'><td><font color='".$bg."'>.</font></td><td>";

		echo "<table width='100%' border='0' cellspacing='0'>";

		echo "<tr bgcolor='".$bg."'><td><span style='font-size:4pt; color:".$bg."'>.</span></td></tr>";

		echo "<tr bgcolor='".$bg."'>";

		echo "<tr><td width='14%' align='center'>";

		echo show_linkb( "&nbsp;&nbsp;Inbox&nbsp;&nbsp;", "mailboxes.php", "box", "inbox" );

		echo "</td>";

		echo "<td width='15%' align='center'>";

		echo show_linkb( "&nbsp;&nbsp;Sent&nbsp;&nbsp;", "mailboxes.php", "box", "sent" );

		echo "</td>";

		echo "<td width='14%' align='center'>";

		echo show_linkb( "&nbsp;&nbsp;Received&nbsp;&nbsp;", "mailboxes.php", "box", "received" );

		echo "</td>";

		echo "<td width='14%' align='center'>";

		echo show_linkb( "&nbsp;&nbsp;Contacts&nbsp;&nbsp;", "mailboxes.php", "box", "contacts" );

		echo "</td>";

		echo "<td width='14%' align='center'>";

		echo show_linkb( "&nbsp;&nbsp;Blocked&nbsp;&nbsp;", "mailboxes.php", "box", "blocked" );

		echo "</td>";

		echo "<td width='14%' align='center'>";

		echo show_linkb( "&nbsp;&nbsp;Remembered&nbsp;&nbsp;", "mailboxes.php", "box", "remembered" );

		echo "</td>";

		echo "<td width='15%' align='center'>";

		echo show_linkb( "&nbsp;&nbsp;Credits&nbsp;&nbsp;", "mailboxes.php", "box", "credits" );

		echo "</td>";

		echo "</tr>";

		echo "<tr bgcolor='".$bg."'><td><span style='font-size:4pt; color:".$bg."'>.</span></td></tr>";

		echo "</table>";

		echo "</tr></td></table>";

//		echo "<br><br>";

	}

?>

