<?
// This ensures all code knows it's being called by the bandTopia app
// If this variable is missing when another page is loaded.  It will redirect to bandTopia home.
define('IN_BANDTOPIA', "You're here!!!");

ini_set("display_errors","1");
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);

// Starts the processing for the page.
require ("include/pageStart.php");

// Set main html page to use for page
$template -> set_filenames(array(
	"body" => "pro_main.htm"));
	
// Start Navigation
$nav = '<ul style="list-style-type:none;">';
	$nav .= "\t".'<li style="float: left; margin-right: 10px;"><a href="../index.php">Home</a></li>';
	$nav .= "\t".'<li style="float: left; margin-right: 10px;"><a href="index.php?action=inbox">My Inbox</a></li>';
	$nav .= "\t".'<li style="float: left; margin-right: 10px;"><a href="index.php?action=sendmessage">Send Message</a></li>';
$nav .= '<div style="clear:both;"></div></ul>';
if(defined('LOGGEDIN')) {
	$template -> assign_block_vars("logged_in", array());
}
else {
	$template -> assign_block_vars("logged_out", array());
}

//if(SESSION_USERTYPEID != 3 && SESSION_USERTYPEID != 4) {
//	header("Location: ../index.php?action=logout");
//}

if($action=="sendmessage") {
	$template -> assign_block_vars("page_sendmessage",array());
	$template -> assign_var("USERID", $_SESSION['UserId']);
} elseif($action == "sendmessagenow") {
	$title=$_POST['message_title'];
	$to=$_POST['message_to'];
	$content=$_POST['message_content'];
	$from=$_POST['message_from'];
	$time=$_POST['message_date'];
	
	$ck_reciever = "SELECT UserId FROM Users WHERE UserName = '".$to."' LIMIT 1";
	if( mysql_num_rows( mysql_query( $ck_reciever ) ) !== 0 ){
		$grab=mysql_fetch_array(mysql_query( $ck_reciever));
		$grabid=$grab[0]['UserId'];
		mysql_query("INSERT INTO messages (from_user, to_user, message_title, message_contents, message_date) VALUES ('$from','$grabid','$title','$content',NOW())") OR die("Could not send the message: <br>".mysql_error());
		header("Location: index.php?action=msgSent&prev_action=sendmessagenow");
	} else {
		header("Location: index.php?action=sendmessage&prev_action=sendmessagenow&error=nouser");
	}
} elseif($action=="msgSent") {
	$template -> assign_block_vars("page_msgSent",array());
	$template -> assign_var("USERID", $_SESSION['UserId']);
} elseif($action=="inbox"){
	$template -> assign_block_vars("page_inbox",array());
        $ibox = "SELECT from_user,message_title,message_date,message_contents FROM messages WHERE to_user = '{$_SESSION['UserId']}' LIMIT 25";
		$bbb=mysql_query( $ibox);
        if( mysql_num_rows($bbb) > 0 ){
			$mInbox .= "<table style='width: 100%; background: #fff; border: 1px solid #888;color: #333;'><thead><tr><th class='textleft'>From</th><th class='textleft'>Subject</th><th class='textleft'>Msg Preview</th><th class='textleft'>When</th></tr></thead><tbody>";
			while($ib=mysql_fetch_array($bbb)) {
				$from=$ib['from_user'];
				$froN=mysql_fetch_array(mysql_query("SELECT UserName FROM Users WHERE UserId='$from'"));
				//$froName=$froN[0]['uName'];
				$mInbox .= "<tr><td>".$froN[0]."</td><td>".$ib['message_title']."</td><td>".substr($ib['message_contents'],0,60)."...</td><td>".date("m-d-Y g:i A",strtotime($ib['message_date']))."</td></tr>";
             }
			 $mInbox.="</tbody></table>";
            }

	$template -> assign_var("DISPLAY_INBOX", $mInbox);

} else {
// Set main html page to use for page
//$template -> set_filenames(array(
//"body" => "pro_main.htm"));
	$template -> assign_block_vars('page_main', array());
}
$template->assign_var('NAV',$nav);

include(INCLUDE_PATH."pageend.php");
?>