<?php

	include "library.php";

	$cxn = start_code();

    exit_if_search_bot();

    exit_if_session_expired( $cxn );


			/* 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 DESC LIMIT 1200";

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

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

		echo "<table width='100%'><td width='100%' align='center'>";
		
		show_green_button_submit( "Remove Selected", 170 );
		
		echo "</td></tr></table>";


		echo "<br><br>";


		if (! low_res_screen())
			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='25%'>".black( "<b>Subject Line</b>" )."</td><td width='19%'></td><td width='8%'></td></tr>";
		else
			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>";


		if (! low_res_screen())
		{
			echo "<tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td width='25%' rowspan='35' valign='top' align='center'>";
			
			show_ads_medium_skyscraper();
			
			echo "</td></tr>";
		}


		if (query_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 < query_num_rows($result);$i++)
		{
			$data = get_row( $result );

				/* fill in blank subject lines */

            $subject = $data['subject'];
            
            if (strpos( $subject, "(" ) !== false)
                $subject = substr( $subject, 0, strpos( $subject, "(" ) );
            
			if ($data['type'] == "email" && $data['subject'] == "")
				$subj = "No subject";
			else
				$subj = convert_to_html( $subject );


//            if ($data['has_been_opened'] == "no")
//                $display_bold = true;
//            else

                $display_bold = false;
                

				/* 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_init.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

            if ($display_bold)
				echo "<td class='gblack' style='font-weight: bold'>".black( Date( "j", strtotime( $data['sent'] ))."&nbsp;".Date( "M", strtotime( $data['sent'] )) )."</td>";
			else
				echo "<td>".black( Date( "j", strtotime( $data['sent'] ))."&nbsp;".Date( "M", strtotime( $data['sent'] )) )."</td>";


				/* type */

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

            if ($display_bold)
				echo "<td class='gblack' style='font-weight: bold'>".$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 */


			if (val( $data, 'reveal_first_name' ) == "on")
			{
                if ($display_bold)
	    			echo "<td class='gblack' style='font-weight: bold'>".strtolower( $data['first_name'] )."</td>";
	    		else
    				echo "<td>".black( strtolower( $data['first_name'] ) )."</td>";
			}
            else
    			echo "<td></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>";

            // leave the lines in below as space for the skyscraper ad
            
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";
        echo "<tr><td>&nbsp;</td></tr>";

	}
	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 LIMIT 1200";

		$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>&nbsp;</td></tr>";

		if (query_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 < query_num_rows($result);$i++)
		{
			$data = get_row( $result );

            $subject = $data['subject'];
            
            if (strpos( $subject, "(" ) !== false)
                $subject = substr( $subject, 0, strpos( $subject, "(" ) );


				/* fill in blank subject lines */

			if ($data['type'] == "email" && $data['subject'] == "")
				$subj = "No subject";
			else
				$subj = convert_to_html( $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_init.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 "<td>".black( Date( "j", strtotime( $data['sent'] ))."&nbsp;".Date( "M", 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
                echo "</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 LIMIT 1200";

		$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='8%'>".black( "<b>To</b>" )."</td><td width='8%'>".black( "<b>Name</b>" )."</td><td width='34%'>".black( "<b>Subject Line</b>" )."</td></tr>";

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

		if (query_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 < query_num_rows($result);$i++)
		{
			$data = get_row( $result );

				/* date  */

			echo "<tr><td>".black( Date( "j", strtotime( $data['sent'] ))."&nbsp;".Date( "M", 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>";


				/* read status */

//			echo "<td>";
//
//			if ($data['type'] == "email")
//			{
//			    if ($data['has_been_opened'] == 'no')
//      			echo black( "Message has not been opened" );
//        		else
//			    if ($data['has_been_opened'] == 'yes')
//        			echo black( "Message has been read" );
//			}
//
//			echo "</td>";


				/* subject */

            $subject = $data['subject'];
            
            if (strpos( $subject, "(" ) !== false)
                $subject = substr( $subject, 0, strpos( $subject, "(" ) );


			if ($data['type'] == "email" && $data['subject'] == "")
				$subj = "No subject";
			else
				$subj = convert_to_html( $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 == "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>&nbsp;</td></tr>";

		if (query_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 < query_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 == "chat_history")
	{
		page_header( "Chat History", $cxn );

		show_buttons( $cxn );

		echo "<br><br>";

		$query = "SELECT profile.id, profile.handle, chat_history.started FROM chat_history INNER JOIN profile ON profile.id = chat_history.responder_id WHERE initiator_id = ".$id." ORDER BY started DESC";

		$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>Date</b>" )."</td></tr>";

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

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

				/* handle */

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

			echo show_handle( $data['id'], $data['handle'], "chat_history", "" );
				
			echo "</td>";
			
			echo "<td>".black( Date( "j M Y", strtotime( $data['started'] )) )."</td>";
			
			echo "<tr>";
			
		}


		$query = "SELECT profile.id, profile.handle, chat_history.started FROM chat_history INNER JOIN profile ON profile.id = chat_history.initiator_id WHERE responder_id = ".$id." ORDER BY started DESC";

		$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>Date</b>" )."</td></tr>";

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

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

				/* handle */

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

			echo show_handle( $data['id'], $data['handle'], "chat_history", "" );
				
			echo "</td>";
			
			echo "<td>".black( Date( "j M Y", strtotime( $data['started'] )) )."</td>";
			
			echo "<tr>";

		}

		
		if (query_num_rows($result) == 0)
			echo "<tr><td></td><td></td><td>".black( "<br>There are no members in your chat history" )."</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>&nbsp;</td></tr>";
		
		if (query_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 < query_num_rows($result); $i++)
		{
			$data = get_row( $result );

			echo "<tr>";

				/* photo */

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

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

			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, notes.text FROM profile 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='15%'>".black( "<b>Notes</b>" )."</td><td width='45%'>".black( "<b>Notes Text</b>" )."</td></tr>";

		echo "<tr><td>&nbsp;</td></tr>";
		
		if (query_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 < query_num_rows($result); $i++)
		{
			$data = get_row( $result );

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

            $amp = false;
            
			show_photo( $data['noted_member_id'], -1, true, false, $cxn, $amp, 85 );

			echo "</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>";


				/* notes */

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


				/* notes text */

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



    echo "</body>";
	
    echo "</html>";



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

	function show_buttons( $cxn )
	{
		$bg = "#73b8bb";

        echo "<style>";
        echo "table.menubartable td {";
        echo "    color: #000000;";
//        echo "    line-height: 25pt;";
        echo "    font-size: ".fs( 13 )."pt;";
        echo "    font-family:verdana;";
        echo "    background-color: #73b8bb;";
        echo "}";
        
        echo "table.menubartable td:hover {";
        echo "      background-color: #eeeeee;";
        echo "      cursor: pointer;";
        echo "}";        
        echo "</style>";

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

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

		echo show_one_item( "Inbox", "inbox" );

		echo show_one_item( "Sent", "sent" );

		echo show_one_item( "Received", "received" );

		echo show_one_item( "Contacts", "contacts" );

		echo show_one_item( "Blocked", "blocked" );

		echo show_one_item( "Remembered", "remembered" );

		echo show_one_item( "Chat History", "chat_history" );

		echo "</tr>";

		echo "</table>";
	}
	
	function show_one_item( $prompt, $box )
	{
		return "<td height='40' style='background-color: #73b8bb' width='2%'></td><td width='10%' align='center' onclick='location.href=\"".url_encode( "mailboxes.php", "box", $box )."\"'>".$prompt."</td><td style='background-color: #73b8bb' width='2%'></td>";
	}
	

?>