<?PHP
ini_set("display_errors",1);
error_reporting(E_ALL); 
 if (!defined('beerman')) { exit(); }

if(count($_POST) > 0) {
$title = (isset($_POST["title"])) ? text_limit(validetext($_POST["title"]),1000) : false;
$description = (isset($_POST["description"])) ? text_limit(validetext($_POST["description"]),1000) : false;
$type = (isset($_POST["type"]) && (intval($_POST["type"]) == 0 | intval($_POST["type"])==1)) ? intval(trim($_POST["type"])) : "0";
$url = (isset($_POST["url"])) ? validetext($_POST["url"]) : false;
$country = (isset($_POST["geo"])) ? strtoupper(htmlentities(implode(",",array_map('mysql_real_escape_string',($_POST["geo"]))))) : "0";
$question_1 = (isset($_POST["question_1"])) ? text_limit(validetext($_POST["question_1"]),150) : false;
$question_2 = (isset($_POST["question_2"])) ? text_limit(validetext($_POST["question_2"]),150) : false;
$reply_11 = (isset($_POST["reply_11"])) ? text_limit(validetext($_POST["reply_11"]),150) : false;
$reply_12 = (isset($_POST["reply_12"])) ? text_limit(validetext($_POST["reply_12"]),150) : false;
$reply_13 = (isset($_POST["reply_13"])) ? text_limit(validetext($_POST["reply_13"]),150) : false;
$reply_21 = (isset($_POST["reply_21"])) ? text_limit(validetext($_POST["reply_21"]),150) : false;
$reply_22 = (isset($_POST["reply_22"])) ? text_limit(validetext($_POST["reply_22"]),150) : false;
$reply_23 = (isset($_POST["reply_23"])) ? text_limit(validetext($_POST["reply_23"]),150) : false;
$ts_user = (isset($_POST["ts_user"]) && (intval($_POST["ts_user"]) >= 0 | intval($_POST["ts_user"]) <= 4)) ? intval(trim($_POST["ts_user"])) : "0";

if($question_1 == false OR $question_2 == false OR $reply_11 == false OR $reply_12 == false OR $reply_13 == false OR $reply_21 == false OR $reply_22 == false OR $reply_23 == false){
echo "$infstart Заполнены не всё поля $infend"; include(FOOTER_FILE); exit(); }
if($title == false) { echo "$infstart Не указанзаголовок $infend"; include(FOOTER_FILE); exit(); }
if($description == false) { echo "$infstart Не указана инструкция $infend"; include(FOOTER_FILE); exit(); }
if($url == false) { echo "$infstart Ссылка указана неверно $infend"; include(FOOTER_FILE); exit(); }
if(is_url($url) != "true") { echo $infstart."".is_url($url)."".$infend ; include(FOOTER_FILE); exit(); }



$site = getHost($url);
$site = str_ireplace("www.","",$site);
$bll = mysql_query("SELECT `reason` FROM `seo_blacklist` WHERE `site`='$site'");
if(mysql_num_rows($bll) > 0){
$res34 = mysql_fetch_array($bll);
echo "$infstart Ваш сайт находится в чёрном списке.Причина: ".$res34["reason"]." $infend";
include(FOOTER_FILE);exit(); }

$precio = $add_test_price-($skid*$add_test_price/100);
	
if(!isset($_GET["edit"])){
$random = rand(1, 3);
if($random == '3'){ $replys_11 = $reply_13; $replys_12 = $reply_11; $replys_13 = $reply_12; $replys_21 = $reply_21; $replys_22 = $reply_23; $replys_23 = $reply_22; $prv_1 = '2'; $prv_2 = '1';}
elseif($random == '2'){ $replys_11 = $reply_12; $replys_12 = $reply_13; $replys_13 = $reply_11; $replys_21 = $reply_22; $replys_22 = $reply_21; $replys_23 = $reply_23; $prv_1 = '3'; $prv_2 = '2'; }
else{ $replys_11 = $reply_11; $replys_12 = $reply_12; $replys_13 = $reply_13; $replys_21 = $reply_23; $replys_22 = $reply_22; $replys_23 = $reply_21; $prv_1 = '1'; $prv_2 = '3';}
	
mysql_query("INSERT INTO `b_all_history` (`idus`,`data`,`doing`,`title`) VALUES ('$id_user','".time()."','2','$title')");
mysql_query("INSERT INTO `b_tests` (`title`, `type`, `description`, `url`, `question_1`, `question_2`, `reply_11`, `reply_12`, `reply_13`, `reply_21`, `reply_22`, `reply_23`, `prv_1`, `prv_2`, `ts_user`, `country`, `price`, `payforad`, `fromus`, `modered`) VALUES ('$title', '$type', '$description', '$url', '$question_1', '$question_2', '$replys_11', '$replys_12', '$replys_13', '$replys_21', '$replys_22', '$replys_23', '$prv_1', '$prv_2', '$ts_user', '$country', '$precio', '$pay_test_price', '$id_user', '$auto_moder')") or die(mysql_error());
}else{
$ident = (isset($_GET["edit"])) ? intval($_GET["edit"]) : false;

$sql = mysql_query("SELECT * FROM `b_tests` WHERE `paused`='1' AND `fromus`='$id_user' AND `id`='$ident'") or die(mysql_error());
if(mysql_num_rows($sql) > 0){
$res = mysql_fetch_assoc($sql);
$moderation = NULL;
if($res["description"]!=$description OR $res["url"]!=$url OR $res["title"]!=$title){
$moderation = "`modered`='$auto_moder'";
}
mysql_query("UPDATE `b_tests` SET `title`='$title', `type`='$type', `description`='$description', `url`='$url', `question_1`='$question_1', `question_2`='$question_2', `reply_11`='$reply_11', `reply_12`='$reply_12', `reply_13`='$reply_13', `reply_21`='$reply_21', `reply_22`='$reply_22', `reply_23`='$reply_23', `ts_user`='$ts_user', `country`='$country', `price`='$precio', `payforad`='$pay_test_price' $moderation WHERE `id`='$ident' AND `fromus`='$id_user'") or die(mysql_error());
}
}
echo '<script type="text/javascript">location.replace("/advmanager.php?adv=tests");</script>';
echo '<noscript><meta http-equiv="refresh" content="0; url=/advmanager.php?adv=tests"></noscript>';
}

echo "
<table class='profile' width='100%' border='0' cellpadding='0' cellspacing='0' style='margin-bottom: 0;'>
<tr><td class='value1'>
Регистрация в тестах запрещена, для этого используйте задания. Тесты это отличный способ разрекламировать свой проект.
Тесты с неправельным типом или не верными ответами не будут проходить модерацию.
</td></tr>
</table>
";

if(isset($_GET["edit"])){
echo "
";
}
if(isset($_GET["edit"])){
$ident = intval($_GET["edit"]);
$sql = mysql_query("SELECT * FROM `b_tests` WHERE `paused`='1' AND `fromus`='$id_user' AND `id`='$ident'") or die(mysql_error());
if(mysql_num_rows($sql)==0){
echo "<span class='orangenote'><center>$infstart Перед редактированием необходимо приостановить рекламную кампанию $infend</center></span>";
include('footer.php'); exit();
}
$res = mysql_fetch_assoc($sql);
$prv_1 = $res['prv_1'];
$prv_2 = $res['prv_2'];

if($prv_1 == '1'){ $reply_11 = $res['reply_11']; $reply_12 = $res['reply_12']; $reply_13 = $res['reply_13'];}
elseif($prv_1 == '2'){ $reply_11 = $res['reply_12']; $reply_12 = $res['reply_13']; $reply_13 = $res['reply_11'];}
else{ $reply_11 = $res['reply_13']; $reply_12 = $res['reply_11']; $reply_13 = $res['reply_12'];}

if($prv_2 == '1'){ $reply_21 = $res['reply_21']; $reply_22 = $res['reply_23']; $reply_23 = $res['reply_22'];}
elseif($prv_2 == '2'){ $reply_21 = $res['reply_22']; $reply_22 = $res['reply_21']; $reply_23 = $res['reply_23'];}
else{ $reply_21 = $res['reply_23']; $reply_22 = $res['reply_21']; $reply_23 = $res['reply_22'];}

}
?>  
<form name="tests_form" method="post" action="" onsubmit="return testform_sbm(); return false;">
<table class='profile' width='100%' border='0' cellpadding='0' cellspacing='0' style='margin-bottom: 0;'>
<thead>
<tr>
<th align='center' width='42%' nowrap='nowrap'>Параметр</th>
<th align='center' nowrap='nowrap'>Значение</th>
</tr>
</thead>
<tbody>
<tr>
<td><b>Заголовок</b></td>
<td class="value"><input class="val" type="text" name="title" maxlength="55" value="<? if(isset($res["title"])) echo $res["title"]; ?>" /></td>
</tr>
<tr>
<td><b>Тип теста</b></td>
<td class="value">
<select name="type" class="val">
<?PHP
$type_test = array(
'0' => 'Тип теста',
'1' => 'Клик по рекламе',
'2' => 'Несколько переходов по сайту',
'3' => 'Голосование',
'4' => 'Просмотр видео',
'5' => 'Другое'
);
foreach($type_test AS $test_value => $test_key){
$type_sel = NULL;
if(isset($res["type"]) && $test_value == $res["type"]) $type_sel = "selected";
echo "<option value='$test_value' $type_sel>$test_key</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td><b>Инструкция для теста</b></td>
<td>
<span class="btn-bold" title="Выделить жирным" onClick="wrapText('descr','[b]','[/b]','counter'); return false;"></span>
<span class="btn-italic" title="Выделить курсивом" onClick="wrapText('descr','[i]','[/i]','counter'); return false;"></span>
<span class="btn-uline" title="Выделить подчёркиванием" onClick="wrapText('descr','[u]','[/u]','counter'); return false;"></span>
<span class="btn-url" title="Выделить URL" onClick="wrapText('descr','[url]','[/url]','counter'); return false;"></span>
<input class="scount" type="text" id="counter" value="Осталось 1000 символов" readonly="readonly" />
</td>
</tr>
<tr>
<td colspan="2" align="center"><textarea name="description" id="descr" maxlength="1000" class="expanding" onkeyup="descchange('descr','counter');" style="width:99%;height:170px;resize:none;margin-left:7px;">
<?PHP
if(isset($res["description"])) echo $res["description"];
?>
</textarea></td>
</tr>
<tr>
<td><b>URL сайта</b> (включая http://)</td>
<td class="value"><input class="val" type="text" name="url" maxlength="150" value="<? if(isset($_GET["edit"])) { echo $res["url"]; }else{ echo "http://"; } ?>" /></td>
</tr>
<tr><td colspan="2" style="text-align:center;"><b>Вопрос 1</b></td></tr>
<tr>
<td>Вопрос</td>
<td class="value"><input class="val" type="text" value="<? if(isset($res["question_1"])) { echo $res["question_1"]; } ?>" name="question_1" maxlength="150"></td>
</tr>
<tr>
<td>Вариант ответа (<span style="color:#269B14;">правильный</span>)</td>
<td class="value"><input class="val" style="color:#269B14;" type="text" value="<? if(isset($reply_11)) { echo $reply_11; } ?>" name="reply_11" maxlength="150"></td>
</tr>
<tr>
<td>Вариант ответа (<span style="color:#BF2424;">ложный</span>)</td>
<td class="value"><input class="val" style="color:#BF2424;" type="text" value="<? if(isset($reply_12)) { echo $reply_12; } ?>" name="reply_12" maxlength="150"></td>
</tr>
<tr>
<td>Вариант ответа (<span style="color:#BF2424;">ложный</span>)</td>
<td class="value"><input class="val" style="color:#BF2424;" type="text" value="<? if(isset($reply_13)) { echo $reply_13; } ?>" name="reply_13" maxlength="150"></td>
</tr>
<tr><td colspan="2" style="text-align:center;"><b>Вопрос 2</b></td></tr>
<tr>
<td>Вопрос</td>
<td class="value"><input class="val" type="text" value="<? if(isset($res["question_2"])) { echo $res["question_2"]; } ?>" name="question_2" maxlength="150"></td>
</tr>
<tr>
<td>Вариант ответа (<span style="color:#269B14;">правильный</span>)</td>
<td class="value"><input class="val" style="color:#269B14;" type="text" value="<? if(isset($reply_21)) { echo $reply_21; } ?>" name="reply_21" maxlength="150"></td>
</tr>
<tr>
<td>Вариант ответа (<span style="color:#BF2424;">ложный</span>)</td>
<td class="value"><input class="val" style="color:#BF2424;" type="text" value="<? if(isset($reply_22)) { echo $reply_22; } ?>" name="reply_22" maxlength="150"></td>
</tr>
<tr>
<td>Вариант ответа (<span style="color:#BF2424;">ложный</span>)</td>
<td class="value"><input class="val" style="color:#BF2424;" type="text" value="<? if(isset($reply_23)) { echo $reply_23; } ?>" name="reply_23" maxlength="150"></td>
</tr>
<tr><td colspan="2" style="text-align:center;"><b>Дополнительные настройки</b></td></tr>
<tr>
<td>Кто может проходить</td>
<td class="value">
<select class="val" name="ts_user">
<?PHP
$ts_user_test = array(
'0' => 'Все',
'1' => 'До недели после регистрации',
'2' => 'От недели после регистрации',
'3' => 'От месяца после регистрации',
'4' => 'От 2-х месяцов после регистрации',
);
foreach($ts_user_test AS $ts_user_value => $ts_user_key){
$ts_user_sel = NULL;
if(isset($res["ts_user"]) && $ts_user_value == $res["ts_user"]) $ts_user_sel = "selected";
echo "<option value='$ts_user_value' $ts_user_sel>$ts_user_key</option>";
}
?>
</select>
</td>
</tr>
</table>
<span onclick="drop_list('opt_geo');" class="task-subtitle1" style="cursor:pointer;">Настройки Геотаргетинга</span>
<div id="opt_geo" style="display:none;">
 <table class="taskquestion">
 <thead><tr><td colspan="6">
 Задание будет доступно пользователям из указанных стран, так же вы можете исключить одну или несколько стран.
 </td></tr></thead>
 <tbody>
	<tr>
		<td width="15px"><input type="checkbox" name="geo[]" value="AU" id="checkbox1"></td><td><img src="/images/flags/au.png" align="absmiddle"> Австралия</td>
		<td width="15px"><input type="checkbox" name="geo[]" value="AI" id="checkbox2"></td><td><img src="/images/flags/ai.png" align="absmiddle"> Ангилья</td>
	  <td width="15px"><input type="checkbox" name="geo[]" value="AZ" id="checkbox3"></td><td><img src="/images/flags/az.png" align="absmiddle"> Азербайджан</td>
	</tr>
	<tr>
	  <td width="15px"><input type="checkbox" name="geo[]" value="AT" id="checkbox4"></td><td><img src="/images/flags/at.png" align="absmiddle"> Авcтрия</td>
		<td width="15px"><input type="checkbox" name="geo[]" value="AM" id="checkbox5"></td><td><img src="/images/flags/am.png" align="absmiddle"> Армения</td>
		<td width="15px"><input type="checkbox" name="geo[]" value="BE" id="checkbox6"></td><td><img src="/images/flags/be.png" align="absmiddle"> Бельгия</td>
	</tr>
	<tr>
		<td width="15px"><input type="checkbox" name="geo[]" value="BG" id="checkbox7"></td><td><img src="/images/flags/bg.png" align="absmiddle"> Болгария</td>
		<td width="15px"><input type="checkbox" name="geo[]" value="BR" id="checkbox8"></td><td><img src="/images/flags/br.png" align="absmiddle"> Бразилия</td>
		<td width="15px"><input type="checkbox" name="geo[]" value="BY" id="checkbox9"></td><td><img src="/images/flags/by.png" align="absmiddle"> Белоруссия</td>
	</tr>
	<tr>
		<td width="15px"><input type="checkbox" name="geo[]" value="HU" id="checkbox10"></td><td><img src="/images/flags/hu.png" align="absmiddle"> Венгрия</td>
		<td width="15px"><input type="checkbox" name="geo[]" value="DE" id="checkbox11"></td><td><img src="/images/flags/de.png" align="absmiddle"> Германия</td>
		<td width="15px"><input type="checkbox" name="geo[]" value="GR" id="checkbox12"></td><td><img src="/images/flags/gr.png" align="absmiddle"> Греция</td>
	</tr>
	<tr>
		<td width="15px"><input type="checkbox" name="geo[]" value="KZ" id="checkbox13"></td><td><img src="/images/flags/kz.png" align="absmiddle"> Казахстан</td>
		<td width="15px"><input type="checkbox" name="geo[]" value="LV" id="checkbox14"></td><td><img src="/images/flags/lv.png" align="absmiddle"> Латвия</td>
		<td width="15px"><input type="checkbox" name="geo[]" value="LT" id="checkbox15"></td><td><img src="/images/flags/lt.png" align="absmiddle"> Литва</td>
	</tr>
	<tr>
		<td width="15px"><input type="checkbox" name="geo[]" value="MD" id="checkbox16"></td><td><img src="/images/flags/md.png" align="absmiddle"> Молдавия</td>
		<td width="15px"><input type="checkbox" name="geo[]" value="RU" id="checkbox17"></td><td><img src="/images/flags/ru.png" align="absmiddle"> Россия</td>
		<td width="15px"><input type="checkbox" name="geo[]" value="RO" id="checkbox18"></td><td><img src="/images/flags/ro.png" align="absmiddle"> Румыния</td>
	</tr>
	<tr>
		<td width="15px"><input type="checkbox" name="geo[]" value="SK" id="checkbox19"></td><td><img src="/images/flags/sk.png" align="absmiddle"> Словакия</td>
		<td width="15px"><input type="checkbox" name="geo[]" value="US" id="checkbox20"></td><td><img src="/images/flags/us.png" align="absmiddle"> США</td>
		<td width="15px"><input type="checkbox" name="geo[]" value="TJ" id="checkbox21"></td><td><img src="/images/flags/tj.png" align="absmiddle"> Таджикистан</td>
	</tr>
	<tr>
		<td width="15px"><input type="checkbox" name="geo[]" value="UZ" id="checkbox22"></td><td><img src="/images/flags/uz.png" align="absmiddle"> Узбекистан</td>
		<td width="15px"><input type="checkbox" name="geo[]" value="UA" id="checkbox23"></td><td><img src="/images/flags/ua.png" align="absmiddle"> Украина</td>
		<td width="15px"><input type="checkbox" name="geo[]" value="CZ" id="checkbox24"></td><td><img src="/images/flags/cz.png" align="absmiddle"> Чехия</td>
	</tr>
	<tr>
		<td width="15px"><input type="checkbox" name="geo[]" value="EE" id="checkbox25"></td><td><img src="/images/flags/ee.png" align="absmiddle"> Эстония</td>
		<td width="15px"><input type="checkbox" name="geo[]" value="JP" id="checkbox26"></td><td> <img src="/images/flags/jp.png" align="absmiddle"> Япония</td>
		<td width="15px"><input type="checkbox" name="geo[]" value="0" id="checkbox27" onclick="all_g();"></td><td> <img src="/images/geo.png" align="absmiddle"> Все страны</td>
	</tr>
 </tbody>
 </table>

</div>
<table class='profile' width='100%' border='0' cellpadding='0' cellspacing='0' style='margin-bottom: 0;'> 
<tr>
<td><b>Стоимость (со скидкой <?=$skid?> %)</b></td>
<td class="value">
<input type="text" id="testprice" maxlength="5" value="<?if(isset($res["price"]) && $res["price"] > 0) { echo $res["price"]; }else{ echo $add_test_price-($skid*$add_test_price/100); } ?>" readonly="readonly" />
</td>
</tr>
</tbody>
</table>

<div class='centerbtn'>
<div class='centerbtn'><span class='taskquestion'><center>
<p><a href="/rules.html" target="_blank">Прочтите правила (ссылка откроется в новом окне)</a><br />
<input type="checkbox" name="tosaccept" value="1" />Я согласен(на) с правилами размещения рекламы на <?=$sitename ?>
</p></center></div><br />
<input type='submit' class='button-green' style='height:26px;' value='Сохранить' />
</div>
</form>
<script>
function testform_sbm() {
if (document.forms['testsform'].title.value == '') {
alert('Вы не указали заголовок теста');
document.forms['testsform'].title.focus();
return false;
}
if (document.forms['testsform'].type.value == 0) {
alert('Выберите тип теста');
document.forms['testsform'].type.focus();
return false;
}
if (document.forms['testsform'].description.value == '') {
alert('Вы не указали инструкцию теста');
document.forms['testsform'].description.focus();
return false;
}
if ((document.forms['testsform'].url.value == '')|(document.forms['testsform'].url.value == 'http://')) {
alert('Вы не указали URL-адрес ссылки');
document.forms['testsform'].url.focus();
return false;
}
if (document.forms['testsform'].tosaccept.checked == false) {
alert('Если вы прочитали правила, с ними нужно согласиться');
return false;
}
document.forms['testsform'].submit();
return true;
}

function drop_list(id){ if (document.getElementById(id).style.display == 'none') { document.getElementById(id).style.display = ''; } else { document.getElementById(id).style.display = 'none'; } return false; }

function take_ch() {
for(i=1;i<=26;i++){
document.getElementById('checkbox'+i).checked = '';
}
}
function put_ch() {
for(i=1;i<=26;i++){
document.getElementById('checkbox'+i).checked = '1';
document.getElementById('checkbox'+i).disabled = false;
}
document.getElementById('checkbox27').checked = '';
}
function all_g() {
var all = document.getElementById('checkbox27');
if(all.checked) {
for(i=1;i<=26;i++){
document.getElementById('checkbox'+i).checked = '';
document.getElementById('checkbox'+i).disabled = true;
}
}else{
for(i=1;i<=26;i++){
document.getElementById('checkbox'+i).checked = '';
document.getElementById('checkbox'+i).disabled = false;
}
}
}
</script>