<?
  $page.="<p class='d'><b>Почта</b></p>";
  $player["mail"]=unserialize($player["mail"]);
  if ($_GET[mod]=="write") {
     if (isset($_POST[username])) {
     	$_POST[username]=htmlspecialchars($_POST[username]);
     	$_POST[post]=substr(htmlspecialchars($_POST[post]),0,999);
     	$sql=mysql_query("SELECT mail,id,char_name FROM users WHERE char_name='$_POST[username]' LIMIT 1");
     	$page.=mysql_error();
     	if (mysql_num_rows($sql)!=1) {$page.="<br/>Адресат не найден!<br/>";}
     	else {
     		$user=mysql_fetch_array($sql);
     		$user["mail"]=unserialize($user["mail"]);
     		$mail["time"]=time();
     		$mail["post"]=$_POST[post];
     		$mail[username]=$player[char_name];
     		$user["mail"][mailfrom][]=$mail;
     		foreach($user["mail"][mailfrom] as $key=>$value)
     		{if ((time()-$value["time"])>3*24*60*60){$user["mail"][mailfrom]=delete_element($user["mail"][mailfrom],$key);}}
     		$user["mail"]["new"]++;
     		$user["mail"]=serialize($user["mail"]);
     		$sql=mysql_query("UPDATE users SET mail='$user[mail]' WHERE id='$user[id]' LIMIT 1");
     		$page.=mysql_error();
     		$mail[username]=$_POST[username];
     		$player["mail"][mailto][]=$mail;
     		foreach($player["mail"][mailto] as $key=>$value)
     		{if ((time()-$value["time"])>3*24*3600){$player["mail"][mailto]=delete_element($player["mail"][mailto],$key);}}
     		$player["mail"]=serialize($player["mail"]);
     		$sql=mysql_query("UPDATE users SET mail='$player[mail]' WHERE id='$player[id]' LIMIT 1");
     		$page.="<br/>Письмо $user[char_name] доставлено!<br/>";
     	}
     }
     else {
  		 $page.="<br/><b>Напишите сообщение</b><br/>";
    	 $page.="<form action='./?do=mail&amp;mod=write' method='post'>
        	<br/>Ник<br/><input name='username' type='text' value='$_GET[to]' />
        	<br/>Сообщение<br/><input name='post' type='text' />
        	<br/><input type='submit' value='Отправить' />
			</form>";

	 }
	 $page.="<br/><a href='./?do=mail&amp;mod=mailfrom'>Входящие</a>";
  	$page.="<br/><a href='./?do=mail&amp;mod=mailto'>Исходящие</a>";
  	   $page.="<br/><a href='./?do=mail'>Почта</a><br/>";
  }
  elseif ($_GET[mod]=="mailfrom") {
      if (!is_array($player["mail"][mailfrom])) {$page.=$player["mail"][mailfrom]."<br/>Входящих сообщений нет!<br/>";}
      else {
      	 if (!isset($_GET[str])){$_GET[str]=1;}
      	 $page.="<br/><b>Входящие</b><br/>";
      	 $begin=sizeof($player["mail"][mailfrom])-($_GET[str]-1)*10-1;
         for ($i=$begin;$i>$begin-10;$i--){
         	if ($i<0) {break;}
           $page.="<br/><a href='./?do=mail&amp;mod=write&amp;to=".$player["mail"][mailfrom][$i][username]."'>".$player["mail"][mailfrom][$i][username]."</a> ";
           $page.=date("j.m.",$player["mail"][mailfrom][$i]["time"]).(date("Y",$player["mail"][mailfrom][$i]["time"])+170).date("  G:i",$player["mail"][mailfrom][$i]["time"]);
           $page.="<br/>".$player["mail"][mailfrom][$i][post]."<br/>";
         }
         $page.=nav_page(ceil(sizeof($player["mail"][mailfrom])/10),$_GET[str],"./?do=mail&amp;mod=mailfrom&amp;str=");
         $player["mail"]["new"]=0;
         $player["mail"]=serialize($player["mail"]);
         $sql=mysql_query("UPDATE users SET mail='$player[mail]' WHERE id='$player[id]' LIMIT 1");
      }
      $page.="<br/><a href='./?do=mail&amp;mod=mailto'>Исходящие</a>";
      $page.="<br/><a href='./?do=mail'>Почта</a><br/>";
  }
  elseif ($_GET[mod]=="mailto") {
  	  if (!is_array($player["mail"][mailto])) {$page.="<br/>Исходящих сообщений нет!<br/>";}
  	  else {
      	 if (!isset($_GET[str])){$_GET[str]=1;}
      	 $page.="<br/><b>Исходящие</b><br/>";
      	 $begin=sizeof($player["mail"][mailto])-($_GET[str]-1)*10-1;
         for ($i=$begin;$i>$begin-10;$i--){
         	if ($i<0) {break;}
           $page.="<br/><a href='./?do=mail&amp;mod=write&amp;to=".$player["mail"][mailto][$i][username]."'>".$player["mail"][mailto][$i][username]."</a> ";
           $page.=date("j.m.",$player["mail"][mailto][$i]["time"]).(date("Y",$player["mail"][mailto][$i]["time"])+170).date("  G:i",$player["mail"][mailto][$i]["time"]);
           $page.="<br/>".$player["mail"][mailto][$i][post]."<br/>";
         }
         $page.=nav_page(ceil(sizeof($player["mail"][mailto])/10),$_GET[str],"./?do=mail&amp;mod=mailto&amp;str=");
      }
  	$page.="<br/><a href='./?do=mail&amp;mod=mailfrom'>Входящие</a>";
      $page.="<br/><a href='./?do=mail'>Почта</a><br/>";
  }
  else{
  	if (empty($player["mail"]["new"])) {$player["mail"]["new"]=0;}
  	$page.="<br/>Новых: ".$player["mail"]["new"];
  	$page.="<br/><a href='./?do=mail&amp;mod=mailfrom'>Входящие</a>";
  	$page.="<br/><a href='./?do=mail&amp;mod=mailto'>Исходящие</a>";
  	$page.="<br/><a href='./?do=mail&amp;mod=write'>Написать письмо</a>";
  	$page.="<br/>";
  }
  $page.="<br/><p class='d'><a href='./'>В игру</a></p>";
?>