<?

header("Cache-Control: no-cache");
if ($ver=="wml")header ("Content-type:text/vnd.wap.wml; charset=utf-8");
else header("Content-Type:text/html; charset=UTF-8");

require("inc.php");
$link = connect_db();
list($row, $id, $ps, $fsize1, $fsize2) = check_login($link);
require("version.php");

if (isset($rm)) $takep2="&amp;rm=$rm&amp;ref=$ref";
else $takep2="&amp;ref=$ref";

$r = mysql_query("select count(readd) as num from zapiski where (idtowhom = '".$id."')and(ininc ='1')");
$a = mysql_fetch_array($r);
$num = $a["num"];
if ($num == 0){
if ($ver=="wml"){
echo $xml;
echo $dtd;
echo "<wml>\n";
echo "<card id=\"error\" title=\"Нет писем\" ontimer=\"chatmail.php?$ses$takep2\"><timer value=\"10\"/>\n";
echo "<p align=\"center\">\n";
}else{
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
echo "<head><link rel=\"stylesheet\" type=\"text/css\" href=\"css/css.css\"/>";
echo "<title>Нет писем</title>";
echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"2; URL=chatmail.php?$ses$takep2\">";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/></head><body>";
echo "<div align=\"center\">";
}
echo $fsize1;
echo "<b>У вас нет входящих писем</b>\n";
echo $fsize2;
if ($ver=="wml")echo "</p></card></wml>";
else echo "</div></body></html>";
mysql_close($link);   
exit;
}  
if(!isset($s))$s=1;
$mx=round(($num/10)+0.45);
if($s>$mx)$s=$mx;
if($s==0)$s=1;

if ($ver=="wml"){
echo $xml;
echo $dtd;
echo "<wml>\n";
echo "<head><meta http-equiv=\"Cache-Control\" content=\"no-cache\" forua=\"true\"/></head>\n";
echo "<card id=\"inbox\" title=\"Входящие\">\n";
echo "<do type=\"accept\" name=\"send\" label=\"Входящие\"><go href=\"enter.php?$ses&amp;ref=$ref\"/></do>\n";
echo "<p>\n";
}else{
echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
echo "<head><link rel=\"stylesheet\" type=\"text/css\" href=\"css/css.css\"/>";
echo "<title>Входящие</title>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/></head><body>";
echo "<div align=\"left\">";
}
$ot=(($s-1)*10)+1;
$do=$s*10;
if($do>$num)$do=$num;
$o=$ot-1;
$n=$ot;
if($do==0)$n=$o;
echo $fsize1;
echo "Показывает ".$n."-".$do." из ".$num."<br/>\n";
echo $divide;
echo $fsize2;
$r = mysql_query ("Select idwho,topic,date,klu4,readd from zapiski WHERE (idtowhom = '".$id."')and(ininc ='1') order by time desc LIMIT $o,$do");
for ($i=$ot;$i<=$do;$i++){
$a = mysql_fetch_array($r);
$idwho = $a ["idwho"];
$topic = $a ["topic"];
$date = $a ["date"];                    
$klu4 = $a ["klu4"];
$read = $a ["readd"];
$fromw = @mysql_fetch_array(@mysql_query ("Select user from users where id='".$idwho."' LIMIT 1;"));
if (mysql_affected_rows()!=0){
echo $fsize1;
if ($read == 1) echo "<a href=\"read.php?$ses&amp;im=$klu4&amp;s=$s$takep2\">".$fromw[0]." (".$topic.")</a>[".$date."]<br/>\n"; 
else  echo "<img src=\"smile/new.gif\" alt=\"Новое письмо\"/><b><a href=\"read.php?$ses&amp;im=$klu4&amp;s=$s$takep2\">".$fromw[0]." (".$topic.")</a>[".$date."]</b><br/>\n";
echo $fsize2;
}
}                                               
mysql_close($link);
$next=$s+1;
$prev=$s-1;
if ($num>$do) {
$ot=(($next-1)*10)+1;
$do=$next*10;
if($do>$num)$do=$num;
echo $fsize1;
echo "<a href=\"inbox.php?$ses&amp;s=$next$takep2\">&gt;&gt;".$ot."-".$do."&gt;&gt;</a><br/>\n";
echo $fsize2;
}
if($s>1) {
$ot=(($prev-1)*10)+1;
$do=$prev*10;
echo $fsize1;
echo "<a href=\"inbox.php?$ses&amp;s=$prev$takep2\">&lt;&lt;".$ot."-".$do."&lt;&lt;</a><br/>\n";
echo $fsize2;
}
echo $fsize1;
echo $divide;
echo "<a href=\"chatmail.php?$ses&amp;ref=$ref\">&#8592; Ваши письма</a><br/>\n";
if (isset($rm)) echo "<a href=\"chat.php?$ses&amp;rm=$rm$takep\">В чат</a><br/>";
echo "<a href=\"enter.php?$ses&amp;ref=$ref\">&#8592; Прихожая</a>\n"; 
echo $fsize2;
if ($ver=="wml")echo "</p></card></wml>";
else echo "</div></body></html>";
?>