<?
$sql="select price from tb_config where item='timer5'";
$res=mysql_query($sql);
$timer5=mysql_result($res,0,0);
$sql="select price from tb_config where item='vipblock'";
$res=mysql_query($sql);
$vipprice=mysql_result($res,0,0);
	$sql="select price from tb_config where item='mfadyn'";
	$res=mysql_query($sql);
	$dyn=mysql_result($res,0,0);

	$sql="select price from tb_config where item='mfahldyn'";
	$res=mysql_query($sql);
	$hldyn=mysql_result($res,0,0);
$sql="select price from tb_config where item='dopsec'";
$res=mysql_query($sql);
$ds=mysql_result($res,0,0);
$sql="select price from tb_config where item='captcha'";
$res=mysql_query($sql);
$captcha=mysql_result($res,0,0);

$res=mysql_query("select count(id) as kolvo from tb_ads where vip='1'");
$res=mysql_fetch_assoc($res);
$vipmest1=$res["kolvo"];
$res=mysql_query("select price from tb_config where item='vipcount'");
$res=mysql_fetch_assoc($res);
$vipcount=$res["price"];
$res=mysql_query("select count(id) as kolvo from tb_advertisers where vip='1'");
$res=mysql_fetch_assoc($res);
$vipmest2=$res["kolvo"];

$vipmest=$vipmest1+$vipmest2;
if (isset($_POST["url"]))
{

	$plan=intval($_POST["plan"]);
	if (is_numeric($plan)!="true" or $plan<=5)
	{
		echo "<div class=error>Введено неверное значение в поле 'Количество посещений', оно должно быть числовым и больше 5!!!</div>";
		include('footer.php');
		exit();
	}
	$timer=intval($_POST["timer"]);
	if ($timer<0 or $timer>4)
	{
		echo "<div class=error>Ошибка ввода!!!</div>";
		include('footer.php');
		exit();
	}
	$timer1=20+$timer*5;
	$vip=intval($_POST["vip"]);
	if ($vip!=0 and $vip!=1)
	{
		echo "<div class=error>Ошибка ввода!</div>";
		include('footer.php');
		exit();
	}
	$dopsec=intval($_POST["dopsec"]);
	if ($dopsec!=0 and $dopsec!=1 and $dopsec!=2)
	{
		echo "<div class=error>Ошибка ввода!!!</div>";
		include('footer.php');
		exit();
	}

	$url1=text($_POST["url"]);
	$description=text($_POST["description"]);
	$highlight=intval($_POST["highlight"]);

	if ($plan==""){echo "<div class=error>Указаны не все поля</div>";include('footer.php'); exit();}
	if ($url1==""){echo "<div class=error>Указаны не все поля</div>";include('footer.php'); exit();}
	if ($description==""){echo "<div class=error>Указаны не все поля</div>";include('footer.php'); exit();}

	$user=text($_SESSION["username"]);
        $wmid=$duser["wmid"];
	$res=mysql_query("select count(username) as kolvo from tb_users where username='$user'");
	$row=mysql_fetch_assoc($res);
	$kolvo=$row["kolvo"];

	$res=mysql_query("select money_cr from tb_users where username='$user'");
	$res=mysql_fetch_assoc($res);
	$money=$res["money_cr"];


	$amount=$plan*$dyn+$timer*$timer5+$vipprice*$vip;
	if ($highlight!='0') $amount=$amount+$hldyn;
	if ($dopsec==1) { $amount=$amount+$ds; }
	if ($dopsec==2) { $amount=$amount+$captcha; }

	if($kolvo==0) { echo "<div class=error>Ошибка! Для заказа рекламы с внутреннего счета Вам необходимо авторизоваться!</div>";include('footer.php'); exit(); }
	if($money<$amount) {echo "<div class=error>На Вашем балансе недостаточно средств!</div>";include('footer.php'); exit(); }

	$expa=$money-$amount;

	$fechainicia=time();

	$sql = "INSERT INTO tb_ads (fechainicia, paypalemail, plan, url, description, tipo, highlight, timer, vip, dopsec) VALUES('$fechainicia','$wmid','$plan','$url1','$description','ads','$highlight','$timer1','$vip','$dopsec')";
	mysql_query($sql) or die(mysql_error());

	$t=date("d.m.Y", time());
	$res=mysql_query("select count(id) as kol from tb_adsdate where data='$t'");
	$res=mysql_fetch_assoc($res);
	if($res["kol"]>0)
	{
		mysql_query("update tb_adsdate set kolvo=kolvo+1 where data='$t'");
	}else{
		mysql_query("insert into tb_adsdate (data,kolvo) values ('$t','1')");
	}

	$sql="update tb_users set money_cr='$expa' where username='$user'";
	mysql_query($sql) or die(mysql_error());

	echo "<div class='good'>Ваша ссылка добавлена</div>";
	include('footer.php');
	exit();
}
?>
<script language=javascript>
var dyn=<?=$dyn?>;
var hldyn=<?=$hldyn?>;
var timer5=<?=$timer5?>;
var ds=<?=$ds?>;
var vipprice=<?=$vipprice?>;
var captcha=<?=$captcha?>;

var celoe;
var drob;
var res;
function okrugl(nums)
{
	celoe=Math.floor(nums);
	drob=(nums-celoe)*100;
	drob=Math.floor(drob);
	if(drob>=10)
	{
		res=celoe+'.'+drob;
	}else{
		res=celoe+'.0'+drob;
	}
	return res;
}

function getZakaz(frm)
{
	frm.summa.value=frm.plan.value*dyn+frm.timer.value*timer5+vipprice*frm.vip.value;
	if(frm.highlight.value!=0) frm.summa.value=frm.summa.value*1+hldyn;
	if(frm.dopsec.value==1) frm.summa.value=frm.summa.value*1+ds*1;
	if(frm.dopsec.value==2) frm.summa.value=frm.summa.value*1+captcha*1;
	frm.summa.value=okrugl(frm.summa.value)+' руб.';
}
</script>
<br />
<form method="post" action="cr_moneyforadv.php?adv=dyn">
<table class=reyt width="100%" border="0" cellpadding='0' cellspacing='0' align="center">
<thead><tr>
<th align="left" width="40%">Параметр</th>
<th align=left>Значение параметра</th></tr></thead>
  <tr>
    <td align="left">Текст ссылки:</td>
    <td align="left"><input type="text" name="description" size="25" maxlength="100" autocomplete="off" class="field" value="" tabindex="2" /></td>
  </tr>
  <tr>
    <td align="left">URL:</td>
    <td align="left"><input type="text" name="url" size="25" maxlength="150" autocomplete="off" class="field" value="http://" tabindex="3" /></td>
  </tr>
  <tr>
    <td align="left"><b>Доп. защита от автокликеров:</b> </td>
  <td>
  <select name="dopsec"  autocomplete="off" onChange="getZakaz(this.form)" tabindex="7">
    <option value="2">Ввод каптчи</option>
  </select></td></tr>
  <tr>
    <td align="left">Цвет ссылки:</td>
    <td align="left"><select name="highlight" onChange="getZakaz(this.form)">
<option value="0">Не выделять</option>
<option value="1">Жёлтый</option>
</select>
</td>
  </tr>
  <tr>
    <td align="left"><b>Таймер:</b> </td>
  <td>
  <select name="timer"  autocomplete="off" onChange="getZakaz(this.form)" tabindex="5">
    <option value="0">20 секунд</option>
    <option value="1">25 секунд</option>
    <option value="2">30 секунд</option>
    <option value="3">35 секунд</option>
    <option value="4">40 секунд</option>
  </select></td></tr>
  <tr>
    <td align="left"><b>Разместить в VIP блоке:</b> </td>
  <td>
  <select name="vip"  autocomplete="off" onChange="getZakaz(this.form)" tabindex="8">
    <option value="0">Нет</option>
    <option value="1">Да</option>
  </select><? if($vipmest>=$vipcount) echo "<b>Свободных VIP-мест нет</b>"; ?></td></tr>
  <tr>
    <td align="left">Кол-во визитов:</td>
<td align="left"><input name="plan" onChange="getZakaz(this.form)" type="text" size="6" maxlength="6" />
</td>
</tr>

  <tr>
    <td align="left">Стоимость заказа: </td>
    <td align="left" class="price"><input class=val type='text' size='10' maxlength='20' readonly='readonly' name='summa' autocomplete="off" value="0.00 руб."/> </td>
  </tr>
  <tr>
    <td align="left">&nbsp;</td>
    <td align="left"><input type="submit"class="ok_wm" value="Конвертировать" class="submit" tabindex="6" />
        </td>
  </tr>
</table>
</form>
