<?php
session_start();
header( 'Content-type: text/html; charset=windows-1251' );
error_reporting( 1 );
require($_SERVER['DOCUMENT_ROOT']."/sfb.php");

$id_user = (isset($_SESSION["iduser"]) && preg_match("|^[\d]{1,11}$|", trim($_SESSION["iduser"]))) ? intval(trim($_SESSION["iduser"])) : false;

if(!isset($_SESSION['iduser'])) { exit(); }
if(!isset($_POST['func'])) { exit(); }
if($_SERVER['HTTP_X_REQUESTED_WITH'] != "XMLHttpRequest") { exit(); }

if($_POST['func'] == "summ") {
$id_user = intval($_SESSION["iduser"]);
$summ = ($_POST["summ"]);
$komu = ($_POST["komu"]);
$result = mysql_result(mysql_query("SELECT `r_money` FROM `b_users` WHERE `id`='$id_user'"),0,0);
if ($result < $summ){
	echo "1"; 
}elseif ($komu == 0){
	echo "2";
}elseif ($komu == $id_user){
	echo "4";
}else{
$nowtime = time();
mysql_query("INSERT INTO `nano_podarok` (user,komu,summ) values ('$id_user','$komu','$summ')") or die(mysql_error());
mysql_query("INSERT INTO `tb_allhistory` (idus,data,doing,title) values ('$id_user','$nowtime','7','Вы подарили $summ Руб. пользователю $komu')") or die(mysql_error()); 
mysql_query("INSERT INTO `tb_allhistory` (idus,data,doing,title) values ('$komu','$nowtime','7','Вам подарили $summ Руб. пользователь $id_user')") or die(mysql_error()); 
mysql_query("UPDATE `b_users` SET `r_money`=`r_money`-'$summ',`reyting`=`reyting`+'1.5' WHERE `id`='$id_user'") or die(mysql_error()); 
    echo "3";
}
}
?>