<?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(); }

$id_user = intval($_SESSION["iduser"]);
$vremy = time();

if($_POST['func'] == "summ") {
$summ = 15;
$skidka = 10;
$result = mysql_result(mysql_query("SELECT `r_money` FROM `b_users` WHERE `id`='$id_user'"),0,0);
$skid = mysql_result(mysql_query("SELECT `skidka` FROM `b_users` WHERE `id`='$id_user'"),0,0);
if ($result < $summ){
	echo "1"; 
}elseif ($skid != 0){
	echo "2";
}else{
$nowtime = time();
mysql_query("INSERT INTO `tb_allhistory` (idus,data,doing,title) values ('$id_user','$nowtime','7','Вы купили скидку $skidka % за $summ')") or die(mysql_error());
mysql_query("UPDATE `b_users` SET `r_money`=`r_money`-'$summ',`skidka`='$skidka',`vremy`='$vremy' WHERE `id`='$id_user'") or die(mysql_error()); 
    echo "3";
}
}

if($_POST['func'] == "summ1") {
$summ = 10;
$skidka = 5;
$result = mysql_result(mysql_query("SELECT `r_money` FROM `b_users` WHERE `id`='$id_user'"),0,0);
$skid = mysql_result(mysql_query("SELECT `skidka` FROM `b_users` WHERE `id`='$id_user'"),0,0);
if ($result < $summ){
	echo "1"; 
}elseif ($skid != 0){
	echo "2";
}else{
$nowtime = time();
mysql_query("INSERT INTO `tb_allhistory` (idus,data,doing,title) values ('$id_user','$nowtime','7','Вы купили скидку $skidka % за $summ')") or die(mysql_error());
mysql_query("UPDATE `b_users` SET `r_money`=`r_money`-'$summ',`skidka`='$skidka',`vremy`='$vremy' WHERE `id`='$id_user'") or die(mysql_error()); 
    echo "3";
}
}

if($_POST['func'] == "summ2") {
$summ = 20;
$skidka = 15;
$result = mysql_result(mysql_query("SELECT `r_money` FROM `b_users` WHERE `id`='$id_user'"),0,0);
$skid = mysql_result(mysql_query("SELECT `skidka` FROM `b_users` WHERE `id`='$id_user'"),0,0);
if ($result < $summ){
	echo "1"; 
}elseif ($skid != 0){
	echo "2";
}else{
$nowtime = time();
mysql_query("INSERT INTO `tb_allhistory` (idus,data,doing,title) values ('$id_user','$nowtime','7','Вы купили скидку $skidka % за $summ')") or die(mysql_error());
mysql_query("UPDATE `b_users` SET `r_money`=`r_money`-'$summ',`skidka`='$skidka',`vremy`='$vremy' WHERE `id`='$id_user'") or die(mysql_error()); 
    echo "3";
}
}
?>