<?php

require '../system/start.php';
require '../system/functions.php';
reg();
$str['mesto'] = 'Почта';
$str['block'] = $set['site'].' / Почта';
include '../style/'.$style.'/head.php';
switch($act)
{
	default:
	echo '<div class="on">
	<img class="ico" src="../style/'.$style.'/ico/mail.gif" alt="" />
	<a href="mail.php?act=send">Написать</a><br/>
	<img class="ico" src="../style/'.$style.'/ico/privat.gif" alt="" />
	<a href="mail.php?act=inbox">Входящие</a> ['.mysql_result(mysql_query("select count(*) from `mail` where `id_getter` = ".$user['id']." and `status` = 0"),0).'/'.mysql_result(mysql_query("select count(*) from `mail` where `id_getter` = ".$user['id'].""),0).']<br/>
	<img class="ico" src="../style/'.$style.'/ico/po4.png" alt="" />
	<a href="mail.php?act=outbox">Исходящие</a> ['.mysql_result(mysql_query("select count(*) from `mail` where `id_sender` = ".$user['id'].""),0).']</div>';
	break;
	case 'send':
	if(isset($_POST['login']) and !empty($_POST['login']) and !empty($_POST['msg']))
	{
		$login = mysql_fetch_assoc(mysql_query("select `id`,`login` from `users` where `login` = '".protect($_POST['login'])."'"));
		if(!$login)
		{
			echo '<div class="on">Пользователь не найден!</div>';
			include '../style/'.$style.'/foot.php';
			exit;
		}
		$flood = mysql_fetch_assoc(mysql_query("select `time` from `mail` where `id_sender` = ".$user['id']." order by `time` desc limit 1"));
		if($flood['time']<(time()-20))
		{
			mysql_query("insert into `mail`(id_getter,id_sender,text,time,status) values(".$login['id'].",".$user['id'].",'".protect($_POST['msg'])."',".time().",0)");
			echo '<div class="on">Сообщение успешно отправлено!</div>';
			include '../style/'.$style.'/foot.php';
			exit;
		}
		else
		{
			echo '<div class="on">Столь быстро!</div>';
			include '../style/'.$style.'/foot.php';
			exit;
		}
	}
	echo '<div class="mess">
	<form  action="mail.php?act=send" method="post"><div>
	Кому:<br/>
	<input name="login" type="text" value="'.protect($_GET['login']).'" /><br/>
	Сообщение:<br/>
	<textarea name="msg" cols="20" rows="3" id="msg"></textarea><br/>
	<input type="submit" value="Отправить"/>
	</div></form>
	</div>';
	break;
	case 'inbox':
	$query = mysql_query("select * from `mail` where `id_getter` = ".$user['id']." order by `id` desc limit ".$page.",".$set['str']."");
	$total = mysql_result(mysql_query("select count(*) from `mail` where `id_getter` = ".$user['id'].""),0);
	if($total == 0)
	{
		echo '<div class="mess">У вас еще нет входящих сообщений!</div>';
	}
	while($row = mysql_fetch_assoc($query))
	{
		if($row['status'] == 0)
		{
			mysql_query("update `mail` set `status` = 1 where `id` = ".$row['id']." limit 1");
		}
		$i++;
		echo ($i%2) ? '<div class="forum1">' : '<div class="forum2">';
		$u = mysql_fetch_assoc(mysql_query("select `login` from `users` where `id` = ".$row['id_sender'].""));
    	echo '<img class="ico" src="../style/'.$style.'/ico/personal.gif" alt="" /> <a href="../p/'.$u['login'].'">'.$u['login'].'</a>';
    	statusl($u['login']);
    	echo '('.clock($row['time']).')<div class="ftext">Сообщение: '.text($row['text']).'</div>
              <a href="mail.php?act=send&amp;login='.$u['login'].'">[ответить]</a></div>';
    }
    if($total > $set['str'])
	{
		pages($page, $total, 'mail.php?act=inbox&amp;', $set['str']);
	}
	break;
	case 'outbox':
	$query = mysql_query("select * from `mail` where `id_sender` = ".$user['id']." order by `id` desc limit ".$page.",".$set['str']."");
	$total = mysql_result(mysql_query("select count(*) from `mail` where `id_sender` = ".$user['id'].""),0);
	if($total == 0)
	{
		echo '<div class="mess">У вас еще нет исходящих сообщений!</div>';
	}
	while($row = mysql_fetch_assoc($query))
	{
		$i++;
		echo ($i%2) ? '<div class="forum1">' : '<div class="forum2">';
		$u = mysql_fetch_assoc(mysql_query("select `login` from `users` where `id` = ".$row['id_getter'].""));
    	echo '<img class="ico" src="../style/'.$style.'/ico/personal.gif" alt="" /> <a href="../p/'.$u['login'].'">'.$u['login'].'</a>';
    	statusl($u['login']);
    	echo '('.clock($row['time']).')<div class="ftext">Сообщение: '.text($row['text']).'</div></div>';
    }
    if($total > $set['str'])
	{
		pages($page, $total, 'mail.php?act=outbox&amp;', $set['str']);
	}
	break;

}
echo '<div class="on2">
<img class="ico" src="../style/'.$style.'/ico/personal.gif" alt="" /> <a href="../p/'.$user['login'].'">Моя страница</a><br />
</div><div class="on">
<img class="ico" src="../style/'.$style.'/ico/newpost.png" alt="" /> <a href="../pages/info.php?act=bbcode">BBcode</a><br />
<img class="ico" src="../style/'.$style.'/ico/newpost.png" alt="" /> <a href="../pages/info.php?act=smiles">Смайлы</a><br />
</div>';
include '../style/'.$style.'/foot.php';
?>