<?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['sbt_value']))  $submit=clearInput($_REQUEST['sbt_value']);
if (isset($_REQUEST['message'])) $message=clearInput($_REQUEST['message']);

$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"];
}

# check to see whether the mode is to delete mail elements (if $message exists)
if ( $submit=='delete' && isset($message) ) { // the message variable is a list of msg_ids to delete from the email
        foreach ( $message as $key=>$value) {
                $query="UPDATE messages SET msg_senderdel = 'Y' WHERE msg_id=$value";
                $result=mysql_query($query,$link) or die(mysql_error());
				        }
						}


# select all messages to display
$msgr=mysql_query("SELECT * FROM messages LEFT JOIN adverts ON (msg_receiverid=adv_userid) LEFT JOIN members ON (msg_receiverid=mem_userid) WHERE (msg_senderid='$WUserID') AND msg_senderdel='N' AND adv_approved='1' AND msg_receiverid not in ($suspended_list) ORDER BY msg_dateadded DESC") or die(mysql_error());
$TOTAL = mysql_num_rows($msgr);
?>

<form action="javascript:getSent();" 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="5">
					<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>
					Sent
					</td>
		</tr>
		<tr class="blue_12b solid_line_separator">
			<td colspan="5"><input id="delete" type="button" class="button" value="<?=BUTTON_REMOVE?>" onclick="javascript:getSent(this.id, 'inbox-sent.php','sub_content');"/>
			</td>
		</tr>
		<tr class="solid_line_separator">
			<td><strong>
				<input onclick="checkAllFields(1);" id="checkAll" type="checkbox" class="button">
				</strong></td>
			<td colspan="2"><?=GENERAL_TO?></td>
			<td>Message</td>
			<td>Status</td>
		</tr>
		<?php 
if ($TOTAL > 0) {
        while ($sql_array = mysql_fetch_object($msgr) ) {

$time=time();
$timeout = time()-900;
$query="SELECT COUNT(userid) FROM active_users where timestamp > $timeout AND userid=$sql_array->msg_receiverid";
$reton=mysql_query($query,$link) or die(mysql_error());
$totalon=mysql_result($reton,0,0);

if($totalon>0)
{$status="<span style=\"color:green\">Online!</span>";}
else{$status="Offline!";}

    $short_title=stripslashes(substr($sql_array->msg_title,0,30));
	$short_msg_text=stripslashes(substr(strip_tags(html_entity_decode($sql_array->msg_text, ENT_QUOTES)),0,100));
	if (strlen (strip_tags(html_entity_decode($sql_array->msg_text, ENT_QUOTES)))>100) $short_msg_text.="...";
	
    if ($sql_array->msg_read=='R') {$newmail="blue_12xb"; $newback="";}
	else {$newmail="blue_12b"; $newback="purple_row_back";}

    $compvalue=$sql_array->msg_id;

if ($WUserID==$sql_array->msg_receiverid) {
$photo_query=mysql_query("SELECT * FROM pictures WHERE pic_userid = '$sql_array->msg_receiverid' and pic_default='Y'") or die(mysql_error()); 
} else {
$photo_query=mysql_query("SELECT * FROM pictures WHERE pic_userid = '$sql_array->msg_receiverid' 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_receiverid);
}

if ($sql_array->mem_profileid != null) { $male1_name1= $sql_array->mem_profileid; }
else { $male1_name1= $sql_array->adv_username; }
	?>
		<tr class='<?=$newback?> solid_line_separator'>
			<td><input type='checkbox' name='message[]' value='<?=$compvalue?>' onclick='checkAllFields(2);' id='message'></td>
			<td align='left' width='50px'><img src='<?=$CONST_LINK_ROOT.'/'.$male1_picture1?>' class="img_b_p3" border='0' title='<?=ucwords($sql_array->adv_username)?>'/></td>
			<td align='left' width='100px'><a href='<?=$CONST_LINK_ROOT?>/<?=$male1_name1?>'><b>
				<?=ucwords($sql_array->adv_username)?>
				</b><br>
				</a><span class='grey_10'>
				<?=date("d M'y h:ia",strtotime($sql_array->msg_dateadded))?>
				</span></td>
			<td align='left' width='200px'><a href="javascript:void(null)" class='<?=$newmail?>' onclick="javascript:inboxview(<?=$sql_array->msg_id?>, 'back_sent', '')">
				<?=$short_title?>
				</a><br>
				<?=$short_msg_text?></td>
			<td><?=$status?></td>
		</tr>
		<?php
    }
} else {
     print("<tr class='$newback solid_line_separator'><td align='center' colspan='5'>".NO_RECORDS_SECTION_NAME."</td></tr>");
}
?>
	</table>
</form>
