<?php
include("includes/site_info.php");
include("session_handler.inc");
include("includes/functions.php");
include_once __INCLUDE_CLASS_PATH."/class.Picture.php";
$picture = new Picture();

$WUserID = $_SESSION['Sess_UserId'];
$WUserType = $_SESSION['Sess_UserType'];
if (isset($_REQUEST['folder_id']))  $folder_id=clearInput($_REQUEST['folder_id']);
if($folder=='- Select A Folder -') $folder='';
if (isset($_REQUEST['message_id'])) $message_id=clearInput($_REQUEST['message_id']);
if (isset($_REQUEST['sbt_value']))  $submit=clearInput($_REQUEST['sbt_value']);
if (isset($_REQUEST['style'])) $style=clearInput($_REQUEST['style']);

$query2="update messages set msg_read='R' where msg_id = '$message_id'";
$result2=mysql_query($query2,$link);

$suspended_list="0";
$mem_q=mysql_query("select * from blockmail where blk_receiverid='$WUserID'");
while($mem=mysql_fetch_array($mem_q))
{
$suspended_list.=",".$mem["blk_senderid"];
}

# select all messages to display

$msgr=mysql_query("SELECT * FROM messages LEFT JOIN adverts ON (msg_senderid=adv_userid) LEFT JOIN members ON (msg_senderid=mem_userid) WHERE msg_id='$message_id' AND adv_approved='1' AND msg_senderid not in ($suspended_list) ORDER BY msg_dateadded DESC") or die(mysql_error());
$sql_array = mysql_fetch_object($msgr);

$result1=mysql_query("SELECT * FROM message_folder where user_id='$WUserID'") or die(mysql_error());

?>

<form action="javascript:getPost();" name='form_incoming_mail' id='form_incoming_mail' style="padding:0; margin:0; border:0;">
	<table width="100%" border="0" align="left" cellpadding="5" cellspacing="0" class="white_back_t_xb p5">
		<tr class="grey_12 orange_row_back solid_line_separator">
			<td colspan="2">
					<div style="background-color:#0000FF; width:10px; height:10px; float:left; margin-top:3px; margin-right:5px;"><img src="images/spacer.gif" width="10" height="10"></div>
					<?=$sql_array->msg_title?></td>
		</tr>
		<?php if ($style=="back_sent") { ?>
		<tr class="blue_12b solid_line_separator">
			<td colspan="2"><input id="back_message" type="button" class="button" onclick="javascript:getSent(this.id, 'inbox-sent.php','sub_content');" value="Back"/>
				<input id="delete" type="button" class="button" value="<?=BUTTON_REMOVE?>" onclick="javascript:getSent(this.id, 'inbox-sent.php','sub_content');"/>
				<input type="hidden" id="message[]" value="<?=$message_id?>" />
			</td>
		</tr>
		<?php } elseif ($style=="back_trash") { ?>
		<tr class="blue_12b solid_line_separator">
			<td colspan="2"><input id="back_message" type="button" class="button" onclick="javascript:getSent(this.id, 'inbox-trash.php','sub_content');" value="Back"/>
				<input name="delete" type="button" class="button" id="delete" onclick="javascript:getSent(this.id, 'inbox-trash.php','sub_content');" value="<?=BUTTON_REMOVE?>"/>
				<input type="hidden" id="message[]" value="<?=$message_id.'/'.$sql_array->msg_senderid.'/'.$sql_array->msg_receiverid?>" />
			</td>
		</tr>
		<?php } elseif ($style=="back_message") { ?>
		<tr class="blue_12b solid_line_separator">
			<td colspan="2"><input id="back_message" type="button" class="button" onclick="javascript:getPost(this.id, 'inbox-msg.php');" value="Back"/>
				<select class="button" name="folder" size="1" id="folder">
					<option>- <?php echo "Select A Folder";?> -</option>
					<option value="0">Inbox</option>
					<?php 
			while($row=mysql_fetch_array($result1)){?>
					<option value="<?=$row['folder_id']; ?>" <?php if($_REQUEST['folder']==$row['folder_id'] || $_REQUEST['folder_id']==$row['folder_id']) echo "selected"; ?> >
					<?=$row['name']; ?>
					</option>
					<?php }?>
				</select>
				<input id="move" type="button" class="button" value="<?=Move?>" onclick="javascript:getPost(this.id, 'inbox-msg.php');"/>
				<input id="blocked" type="button" class="button" value="<?=Block?>" onclick="javascript:getPost(this.id, 'inbox-msg.php');"/>
				<input type="hidden" id="message[]" value="<?=$message_id?>" />
			</td>
		</tr>
		<?php } elseif ($style=="back_folder") { ?>
		<tr class="blue_12b solid_line_separator">
			<td colspan="2"><input id="back_message" type="button" class="button" onclick="javascript:getPost(this.id, 'inbox-msg.php');" value="Back"/>
				<select class="button" name="folder" size="1" id="folder">
					<option>- <?php echo "Select A Folder";?> -</option>
					<option value="0">Inbox</option>
					<?php 
			while($row=mysql_fetch_array($result1)){?>
					<option value="<?=$row['folder_id']; ?>" <?php if($_REQUEST['folder']==$row['folder_id'] || $_REQUEST['folder_id']==$row['folder_id']) echo "selected"; ?> >
					<?=$row['name']; ?>
					</option>
					<?php }?>
				</select>
				<input id="move" type="button" class="button" value="<?=Move?>" onclick="javascript:getPost(this.id, 'inbox-msg.php');"/>
				<input id="blocked" type="button" class="button" value="<?=Block?>" onclick="javascript:getPost(this.id, 'inbox-msg.php');"/>
				<input type="hidden" id="message[]" value="<?=$message_id?>" />
				<input type="hidden" id="folder_id" value="<?=$folder_id?>" />
			</td>
		</tr>
		<?php } ?>
		<?php 

if ($WUserID==$sql_array->msg_senderid) {
$photo_query=mysql_query("SELECT * FROM pictures WHERE pic_userid = '$sql_array->msg_senderid' and pic_default='Y'") or die(mysql_error()); 
} else {
$photo_query=mysql_query("SELECT * FROM pictures WHERE pic_userid = '$sql_array->msg_senderid' and pic_default='Y' and pic_private='N'") or die(mysql_error()); 
}
$pics_count = mysql_num_rows($photo_query);
$aPicture = mysql_fetch_object($photo_query);
if ($pics_count > 0) {
	$medium = $aPicture->pic_picture."_ps.".$aPicture->ext;
	$male1_picture1 = "members/$sql_array->adv_username/picture/$medium";
             }else{
$male1_picture1=$picture->GetProfilePicture($sql_array->msg_senderid);
}

if ($sql_array->mem_profileid != null) { $male1_name1= $sql_array->mem_profileid; }
else { $male1_name1= $sql_array->adv_username; }

 print("<tr><td align='left' width='50px' rowspan='2' valign='top'><img src='$CONST_LINK_ROOT/$male1_picture1' width='50' height='50' border='1' title='".ucwords($sql_array->msg_senderhandle)."'/></td>");
  print("<td align='left' width='100%'><a href='$CONST_LINK_ROOT/".$male1_name1."' class='blue_12b'><b>".ucwords($sql_array->msg_senderhandle)."</b></a> <span class='grey_10'>".date("d M'y h:ia",strtotime($sql_array->msg_dateadded))."</span></td>");
   print("</tr>");
  $sql_array->msg_text=stripslashes(nl2br(strip_tags(html_entity_decode($sql_array->msg_text, ENT_QUOTES))));
  print("<tr><td align='left'>$sql_array->msg_text</td>");
   print("</tr>");
   print('<tr class="solid_line_separator"><td colspan="2">&nbsp;</td></tr>');

	 if (($style=="back_message") || ($style=="back_folder")) { ?>
		<tr class="blue_12b solid_line_separator">
			<td colspan="2"><input id="reply" type="button" class="button" value="Reply" onclick="javascript:sendreply(<?=$sql_array->msg_senderid?>, '<?=$sql_array->msg_title?>');"/>
				<input name="delete" type="button" class="button" id="delete" onclick="javascript:getPost(this.id, 'inbox-msg.php');" value="<?=BUTTON_REMOVE?>"/></td>
		</tr>
		<?php 
		} 
	  
		?>
	</table>
</form>
