<? require('header.php');

if(!(isset($_SESSION["username"]) && isset($_SESSION["password"])))
{
	?>
	<script type="text/javascript">
	location.replace("login.php");
	</script>
	<noscript>
	<meta http-equiv="refresh" content="0; url=login.php">
	</noscript>
	<?
	exit();
}
require('konkurs/setup.php');
?>
<span class='top-bl-s-verh'>Конкурсы от реферера</span>
<div class='menucenter-top1'>
<table class='profile' width='100%' border='0' cellpadding='0' cellspacing='0' style='margin-bottom: 0;'>
<tr><td class='value1'><center>
<img src='images/yeskon.png' alt='' align='absmiddle' width='16px' height='16px'> Активный конкурс.&nbsp;&nbsp;&nbsp;&nbsp;
<img src='images/stopkon.png' alt='' align='absmiddle' width='16px' height='16px'> Завершенный конкурс.&nbsp;&nbsp;&nbsp;&nbsp;
<img src='images/infokon.png' alt='' align='absmiddle' width='16px' height='16px'> Не начатый конкурс.
</center></td></tr>
</table>

<?
$ss = mysql_query("SELECT * FROM `b_users` WHERE `username`='".$_SESSION["username"]."'");
$duser = mysql_fetch_assoc($ss);

if($_GET["idk"]==""){
$refer_kon=mysql_query("SELECT * FROM still_konkurs_ref WHERE idref=(SELECT id FROM b_users WHERE username='".$duser["referer"]."') ORDER BY time_start DESC");

if(mysql_num_rows($refer_kon) <= 0){

	echo "<br /><span class='taskquestion'><center>На данный момент нет конкурсов от вашего реферера</center></span><br />
	</div>";include("footer.php");exit;

}

while ($rk=mysql_fetch_assoc($refer_kon)){
$mest = explode(",",$rk["mesto"]);
$start = date("d.m.Yг. в H:i",$rk["time_start"]);
$end = date("d.m.Yг. в H:i",$rk["time_start"]+($rk["time_end"]*86400));

if($rk["time_start"] > time()){$status_k = "infokon.png";}
elseif($rk["time_start"] < time() and $rk["time_start"]+($rk["time_end"]*86400) > time()){$status_k = "yeskon.png";}
else{$status_k = "stopkon.png";} ;?>

<div class='menukon' style='margin-top:10px;'>
<div class='namekon'>
Старт конкурса:<?=$start?> ; 
Окончание конкурса:<?=$end?>
</div>
<table class='profile1' width='100%' border='0' cellpadding='0' cellspacing='0' style='margin-bottom: 0;'>
<tr>
<td width='35px' align='left'><img src='images/<?=$status_k?>' alt='' ></td>
<td width='470px'>Конкурс: <a href="referal_kon.php?idk=<?=$rk["id"]?>"><?=$con_kon["vid"][intval($rk["vid"])]?></a> <br />
<span class='serfinfotextkon'>Призы: <?$i_p=0;foreach($mest as $value){$i_p++;?>
<font color='red'><?=$value?></font> руб &nbsp;<small>|</small>&nbsp;<?}?></span>
</td>
<td><a href="referal_kon.php?idk=<?=$rk["id"]?>"><small>Подробнее »</small></a></td>
</tr>
</table>
</div>
<?}
}elseif($_GET["idk"]!=""){

$refer_kon=mysql_query("SELECT * FROM still_konkurs_ref WHERE (idref=(SELECT id FROM b_users WHERE username='".$duser["referer"]."') and id='".intval($_GET["idk"])."') or (idref = '".$duser["id"]."' and id='".intval($_GET["idk"])."')");

if(mysql_num_rows($refer_kon) <= 0){

	echo "<div class=error>Это не Ваш конкурс или такого конкурса нет <br> <a href=\"#\" onclick=\"history.back();return false;\">Вернуться назад</a></div>";include("footer.php");exit;

}

$rk=mysql_fetch_assoc($refer_kon);
$mest = explode(",",$rk["mesto"]);
$start = date("d.m.Y",$rk["time_start"]);
$end = date("d.m.Y",$rk["time_start"]+($rk["time_end"]*86400));
if($rk["time_start"] > time()){$status_k = "infokon.png";}
elseif($rk["time_start"] < time() and $rk["time_start"]+($rk["time_end"]*86400) > time()){$status_k = "yeskon.png";}
else{$status_k = "stopkon.png";} ;?>
<br />
<table class='profile1' width='100%' border='0' cellpadding='0' cellspacing='0' style='margin-bottom: 0;'>
<tr>
<td width='35px' align='left'><img src='images/<?=$status_k?>' alt='' ></td>
<td width='470px'>Конкурс: <a href="referal_kon.php?idk=<?=$rk["id"]?>"><?=$con_kon["vid"][intval($rk["vid"])]?></a> <br />
<span class='serfinfotextkon'>Призы: <?$i_p=0;foreach($mest as $value){$i_p++;?>
<font color='red'><?=$value?></font> руб. | <?}?></span>
</tr>
</table>
<br />
<span class='taskquestion'><center><b>Описание конкурса</b></center><br />
<?=$rk["text"]?><br /><br />
<table width="100%" border="0">
  <tr>
    <td width="50%" align="center"><b>Начало: <?=$start?></b></td>
    <td width="50%" align="center"><b>Окончание: <?=$end?></b></td>
  </tr>
</table>
</span>
<br />
<table class='profile' width='100%' border='0' cellpadding='0' cellspacing='0' style='margin-bottom: 0;'>
 <thead>
 <tr>
 <th align='center' width='10%' nowrap='nowrap'>Место</th>
 <th align='center' nowrap='nowrap'>Логин</th>
 <th align="center" width='33%' >Набрано очков</th>
 </tr>
 </thead>
<tbody>

<? $top_kon=mysql_query("SELECT danie,(SELECT username FROM b_users WHERE id=still_top_kon.idu) as nam
FROM still_top_kon WHERE idk='".$rk["id"]."' ORDER BY danie DESC LIMIT 15");

if(mysql_num_rows($top_kon) <= 0){ ?>
<tr>
<td colspan='3' style='border-bottom:0px;'><center>Нет участников</center></td>
</tr>
<?}else{
  $i_top = 0;
  while ($tk=mysql_fetch_assoc($top_kon)){$i_top++;?>
<tr>
<td align="center" width="10%"><center><?=$i_top?>.</center></td>
<td align="left" class="value"><?=$tk["nam"]?></td>
<td align="center" width="33%"><center><?=round($tk["danie"],2)?></center></td>
</tr>
<?
  }

}?></table></div>
<?}?>

<? require 'footer.php'; ?>