<?php
if (file_exists("functions/site_functions.php"))
	{
	include_once("functions/site_functions.php");
	}
else
	{
include_once("../../functions/site_functions.php");
	}
echo<<<a
<div id="marginn">
<div id="data_message">
</div>
<select id="filter" onchange="filter(this.value);" >
<option value="">Filter by:</option>
<option value="4">User</option>
<option value="5">Date</option>
</select>
<div id='filter_result'>
</div>
a;
db_open();
$o=0;
if (isset($_GET['a']) && !empty($_GET['a']) && isset($_GET['b']) && !empty($_GET['b']) && is_numeric($_GET['b']) && is_numeric($_GET['a']) && isset($_GET['c']) && !empty($_GET['c']) && is_numeric($_GET['c']))
{
$a= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET['a']))));
$b= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET['b']))));
$c= strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_GET['c']))));
echo'<div id="clear"><input type="button" value="Clear" onclick="messages('.$a.','.$b.',2);" /></div>';
	if ($a==2)
		{
		$sql="SELECT * FROM messages WHERE user_id='$b' order by message_id DESC"; 
		}
	else if ($a==3)
		{
		$year=$b[0].$b[1].$b[2].$b[3];
		$month=$b[4].$b[5];
		if ($year >=2012 && $year<=2100 && $month>=0 && $month<=12)
			{
			$date1=$year."-".$month."-01";
			$t=date("t",strtotime($date1));
			$date2=$year."-".$month.'-'.$t;
			$sql="SELECT * FROM messages WHERE dater BETWEEN '$date1' AND '$date2' order by user_id,message_id DESC"; 
			}
		else
			{
			die();
			}
		
		}
}		
else
{ 
$sql="SELECT * FROM messages order by message_id DESC"; 
$c=1;
}
echo<<<a
<div id="troubleshoot_field">
<table id="p_i" class="messages">
<tr>
<td class="td_title_troubleshoot_aa">A/A</td>
<td class="td_title_troubleshoot_small">Device_id</td>
<td class="td_title_troubleshoot_small">Date</td>
<td class="td_title_troubleshoot_big">Subject</td>
</tr>
a;
@ $result=mysql_query($sql);
$res=mysql_numrows($result);
if($c==1)
{
if ($res!=0)
{
while ($row=mysql_fetch_array($result))
{
$o++;
$id=$row['device_id'];
$date=$row['dater'];
$subject=$row['subject'];
$message_id=$row['message_id'];
$read=$row['r'];
if ($read==1)
	{
	$read="<a class='important'>(old)</a>";
	}
else
	{
	$read="<a class='important'>(new)</a>";
	}
echo<<<b
<tr class="tr_troubleshoot_hover">
<td class="td_data_troubleshoots">$o</td>
<td class="td_data_troubleshoots" title='device_id'>$id</td>
<td class="td_data_troubleshoots center" title='date'>$date</td>
<td class="td_data_troubleshoots center" title='subject of message'><a class="href" onclick="view_message($message_id);">$subject $read</a></td>
</tr>
b;

}
}
else
{
echo<<<b
<tr class="tr_troubleshoot_hover">
<td class="td_data_troubleshoots">-</td>
<td class="td_data_troubleshoots">-</td>
<td class="td_data_troubleshoots">-</td>
<td class="td_data_troubleshoots">-</td>
</tr>
b;
}
}
else
{
if ($res!=0)
{
$expression="";
while ($row=mysql_fetch_array($result))
	{
	$message_id=$row['message_id'];
	if ($expression=="")
		{
		$expression="'$message_id'";
		}
	else
		{
		$expression=$expression.",'$message_id'";
		}
	}
if ($expression!="")
	{
	$sql="DELETE FROM messages WHERE message_id IN($expression)";
	@ $result=mysql_query($sql);
	if ($result!=0)
		{
echo<<<b
<tr class="tr_troubleshoot_hover">
<td class="td_data_troubleshoots">-</td>
<td class="td_data_troubleshoots">-</td>
<td class="td_data_troubleshoots">-</td>
<td class="td_data_troubleshoots">Messages deleted!</td>
</tr>
b;
		}
	}
}
}	

db_close();
echo"</table>";
echo"</div>";
if ($o!=0)
	{
	echo '<div id="total">Total: '.$o.'</div>';
	}
else
	{
	echo '<div id="total">Total: - </div>';
	}
	
echo"</div>";
?>