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

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

# Insert folder for given user
$folder_cre=clearInput($_REQUEST["foldername"]);
if(isset($folder_cre) && $folder_cre!=''){
$fol="INSERT INTO message_folder (name, user_id) VALUES ('$folder_cre','$WUserID')";
$result=mysql_query($fol,$link) or die(mysql_error());
}# Deleting folder
if( isset( $folder_id ) && ($submit=="delete") ){
$msgr=mysql_query("SELECT * FROM messages LEFT JOIN adverts ON (msg_senderid=adv_userid) WHERE (msg_receiverid='$WUserID') AND msg_folder='$folder_id' AND msg_receiverdel='N' AND adv_approved='1' AND msg_senderid not in ($suspended_list) ORDER BY msg_dateadded DESC") or die(mysql_error());
 while ($sql_array = mysql_fetch_object($msgr) ) {
                $query="UPDATE messages SET msg_receiverdel = 'Y' WHERE msg_id=$sql_array->msg_id";
                $result1=mysql_query($query,$link) or die(mysql_error());
	}
$remove="delete from message_folder where folder_id='$folder_id' AND user_id='$WUserID'";
$result=mysql_query($remove,$link) or die(mysql_error());
}

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

$result=mysql_query("SELECT * FROM message_folder where user_id='$WUserID'") or die(mysql_error());
$Tot_fol = mysql_num_rows($result);
?>

<form action="javascript:getSent();" name='form_folder' id='form_folder' 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>
			<td colspan="4" class="grey_12 orange_row_back solid_line_separator">
					<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>
					Folders
					</td>
		</tr>
		<tr class="blue_12b solid_line_separator">
			<td colspan="4">
				
				<strong>Add Folder:</strong>
				<input type="text" size="30" name="foldername"/>
			<input id="Create" type="button" class="button" onclick="javascript:getFolder(this.id, 'inbox-folder.php');" value="Create" /></td>
		</tr>
		<tr class="blue_12b pink_row_back solid_line_separator">
			<td width="150"><strong>My Folders</strong></td>
			<td><strong>Messages</strong></td>
			<td><strong>Unread</strong></td>
			<td>&nbsp;</td>
		</tr>
		<?php 
	  	while($folder=mysql_fetch_array($que)){ 
		$total_U=mysql_result(mysql_query("SELECT Count(msg_read) FROM messages LEFT JOIN adverts ON (msg_senderid=adv_userid) WHERE (msg_receiverid='$WUserID') AND msg_folder='$folder[folder_id]' AND msg_receiverdel='N' AND adv_approved='1' AND msg_senderid not in ($suspended_list) AND msg_read='U'"),0,0);
		$total_R=mysql_result(mysql_query("SELECT Count(msg_read) FROM messages LEFT JOIN adverts ON (msg_senderid=adv_userid) WHERE (msg_receiverid='$WUserID') AND msg_folder='$folder[folder_id]' AND msg_receiverdel='N' AND adv_approved='1' AND msg_senderid not in ($suspended_list) AND msg_read='R'"),0,0);
?>
		<tr>
			<td><a href="javascript:void(null)" onclick="folderactive(<?=$folder[folder_id]?>,<?=$Tot_fol?>)" id='folder_In<?=$folder[folder_id]?>' name="folder_In[]">
				<?=$folder[name]?>
				</a></td>
			<td><?=$total_R?></td>
			<td><?=$total_U?></td>
			<td><input id="delete" type="button" class="button" value="<?=BUTTON_REMOVE?>" onclick="javascript:confirmDelete(this.id, <?=$folder[folder_id]?>);"/></td>
		</tr>
		<? } ?>
		<tr>
			<td colspan="4" class="solid_line_separator"></td>
		</tr>
	</table>
</form>
