<?php

	include "library.php";

	$cxn = start_code();


			/* get variables */

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

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

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

	if ($account_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.id as message_id, message.type, message.subject, message.sender_id, message.sent, account.username FROM message LEFT JOIN account ON account.id = message.sender_id WHERE show_in_inbox = 'yes' AND receiver_id = ".$account_id." ORDER BY sent DESC";

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

		echo "<form name='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 "<table width='100%'><td width='100%' align='center'>";
		
		show_green_button_submit( "Remove Selected", 160 );
		
		echo "</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='30%'>".black( "<b>Subject Line</b>" )."</td><td>".black( "<b>Action</b>" )."</td></tr>";
		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='30%'>".black( "<b>Subject Line</b>" )."</td><td>".black( "<b>Action</b>" )."</td></tr>";

		echo "<tr><td>".sp(1)."</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 */

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


			$show_red = false;


				/* selection tickbox */

			echo "<tr><td><input type='checkbox' name='id".$data['message_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>";


				/* username */

			echo "<td>";

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

			echo "</td>";



				/* subject */

			if ($data['type'] == "email")
			{
				echo "<td>".show_link( $subj, "view_email.php", "emailid", $data['message_id'] )."</td>";
			}
			else
			{
				if ($show_red)
					echo "<td>".red( $subj )."</td></tr>";
				else
					echo "<td>".black( $subj )."</td></tr>";
			}
			
			echo "<td>".show_link( "Reply", "send_message.php", "sendtoid", $data['sender_id'], "sendtype", "email", "returnpoint", "inbox", 0 )."</td></tr>";
		}

		echo "</form>";

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

		show_buttons( $cxn );

		echo "<br><br>";

		$query = "SELECT *, message.id as message_id FROM message LEFT JOIN account ON account.id = message.sender_id WHERE receiver_id = ".$account_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 "<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='45%'>".black( "<b>Subject Line</b>" )."</td></tr>";

		echo "<tr><td>".sp(1)."</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 );

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


				/* username */

			echo "<td>";

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

			echo "</td>";


				/* name */

//			echo "<td>";

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

//			echo "</td>";


				/* subject */

			if ($data['type'] == "email")
			{
				echo "<td>".show_link( $subj, "view_email.php", "emailid", $data['message_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.id as message_id FROM message LEFT JOIN account ON account.id = message.receiver_id WHERE sender_id = ".$account_id." ORDER BY sent DESC";

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

//		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 "<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%'><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='44%'>".black( "<b>Subject Line</b>" )."</td></tr>";

		echo "<tr><td>".sp(1)."</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 M Y", strtotime( $data['sent'] )) )."</td>";


				/* type */

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


				/* comment to avoid confusion */

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


				/* username */

			echo "<td>";

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

		
//			else
//				echo show_handle( $data['receiver_id'], $data['handle'], "sent", "" );

			echo "</td>";


				/* name */

//			echo "<td>";

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

//			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['message_id'] )."</td></tr>";
			}
			else
				echo "<td>".black( $subj )."</td></tr>";
		}

		echo "</table></table>";
	}
	else
	if ($box == "remembered")
	{
		page_header( "Remembered List", $cxn );

		show_buttons( $cxn );

		echo "<br><br>";
		
		$query = "SELECT remembered.*, account.*, listing.headline_title, listing.id as listing_id  FROM remembered INNER JOIN listing ON remembered.remembered_id = listing.id INNER JOIN account ON account.id = listing.account_id WHERE remembered.account_id = ".$account_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>Listing</b>" )."</td><td width='10%'>".black( "<b>Username</b>" )."</td><td width='62%'>".black( "<b>Delete</b>" )."</td></tr>";

		echo "<tr><td>".sp(1)."</td></tr>";
		
		if (query_num_rows($result) == 0)
			echo "<tr><td></td><td></td><td colspan='2'>".black( "<br>There are no listings 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'>";

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

			echo "</td>";
				
				/* listing title */

			echo "<td>";

//			echo show_handle( $data['remembered_id'], $data['handle'], "remembered", "" );
//			echo black( $data['username'] );
			echo show_listing_heading($data['listing_id'], $data['headline_title'], "search", 0 );
			
			echo "</td>";


				/* name */

			echo "<td>";

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

			echo "</td>";


				/* delete */

			echo "<td>".show_link( "delete", "delete_from_remembered.php", "rememberedid", $data['remembered_id'] )."</td></tr>";
		}
	}
	else
	{
		crash( "Internal Error: invalid box in mailboxes", $cxn );
	}


	echo "</table><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>";
	
//	show_ads( $cxn );
	
	
    function show_buttons( $cxn )
	{
		$bg = "#73b8bb";

        echo "<style>";
        echo "table.menubartable td {";
        echo "    color: #000000;";
//        echo "    line-height: 25pt;";
        echo "    font-size: ".fs( 12) ."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( "Remembered", "remembered" );

		echo "</tr>";

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


?>