<?php
session_start();

error_reporting( 1 );

header( 'Content-type: text/html; charset=windows-1251' );

if (!(isset($_SESSION["username"]) && isset($_SESSION["password"]))) {
	exit();
}

if ( $_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest' ) {
    exit();
}
?>
<style type="text/css">
#comms {
    width: 94%;
    height: 100px;
    border: 1px solid #cccccc;
    -moz-box-shadow: 0 0 2px rgba(0,0,0,0.1) inset;
	-webkit-box-shadow: 0 0 2px rgba(0,0,0,0.1) inset;
    -khtml-box-shadow: 0 0 2px rgba(0,0,0,0.1) inset;
    box-shadow: 0 0 2px rgba(0,0,0,0.1) inset;
    font-size: 11px;
    border-top: 1px solid #BCBCBC;
    -moz-border-radius: 2px;
    -webkit-border-radius: 2px;
    -khtml-border-radius: 2px;
    border-radius: 2px;
    margin-bottom: 5px;
    padding: 10px;
    overflow: hidden;
}
#reyt {
    border: 1px solid #DDDDDD;
    border-radius: 2px 2px 2px 2px;
    box-shadow: 0 0 1px rgba(0, 0, 0, 0.1) inset;
    padding: 3px 5px;
    width: 180px;
}
.tabcom {
    background: #d5d5d5;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    -khtml-border-radius: 3px;
    border-radius: 3px;
    box-shadow: 0 1px 1px rgba(0,0,0,0.2) inset;
    padding: 5px 0;
    text-align: left;
    margin-top: 20px;
}
.tabcom td.lt {
    color: #212121;
    padding: 5px 10px;
}
.tabcom td.rt {
    font-size: 12px;
    color: #113b5b;
    padding: 5px 10px;
}
.tabcom td {
    background: #fdfdfd;
    padding: 5px;
}
.tabcom td table td {
    padding: 3px 0;
}
</style>
<script type="text/javascript">
function sendcom() {
    $.post('ajax.php?func=addcom',{
        id: <?=intval($_POST['id'])?>,
        comms: $('#comms').val(),
        reyt: $('#reyt').val()
    }, function(data){
        if (data == 1){
            $('#err').fadeIn(500).html('<font color="#286286">Ваш комментарий успешно добавлен!</font>');
            $('#content').remove();
        } else {
            $('#err').fadeIn(500).html('<font color="#800000">'+data+'</font>');
        }
    });
    return false;
}
$('#close_box').click(function(){
    FLAX.Html.thread['flax'].repeat();
    return false;
});
</script>
<?
include 'sfb.php';
if ($_GET['func'] == 'addcom') {
    $getcom = mysql_num_rows( mysql_query( "SELECT id FROM seo_taskcomms WHERE fromid = '" . $_SESSION['iduser'] . "' and ident = '" . intval($_POST['id']) . "'" ) );
    $getus = mysql_num_rows( mysql_query( "SELECT id FROM seo_tasks WHERE fromid = '" . $_SESSION['iduser'] . "' and id = '" . intval($_POST['id']) . "'" ) );
    if ($getcom > 0) {
        echo '<div align="center" style="color:#800000; padding:5px">Вы уже оставляли комментарий к этому заданию</div>';
        exit();
    } else
    if ($getus > 0) {
        echo '<div align="center" style="color:#800000; padding:5px">Вы автор задания. Вы не можете оставить комментарий</div>';
        exit();
    } else {
        ?>
        <div style="padding: 5px; color: #8D950B; font-size: 12px;" align="center">Ваш отзыв о задании</div>
        <div style="display: none; padding: 5px; text-align: center;" id="err"></div>
        <div id="content">
        <div><textarea id="comms"></textarea></div>
        <div align="right" style="padding: 5px 0"><select id="reyt">
        <option selected="selected" value="0">Воздержусь</option>
            <option value="5">5 - Отличное задание</option>
            <option value="4">4 - Хорошее задание</option>
            <option value="3">3 - Так себе</option>
            <option value="2">2 - Плохое задание</option>
            <option value="1">1 - Очень плохо</option>
        </select></div>
        <div style="padding: 5px 0;" align="right"><a href="#" onclick="sendcom(); return false;" class="big_btn">Отправить</a></div>
        </div>
        <?
    }
    exit();
}
if ($_GET['func'] == 'viewcom') {
    ?>
    <table width="100%" border="0" cellpadding="0" cellspacing="1" class="tabcom">
    <thead>
    <th colspan="2" style="background: #ebebeb; padding: 5px; color: #8D950B; font-size: 12px;" align="center">Отзывы о задании № <?=intval($_POST['id'])?></th>
    </thead>
    <?
    $comms = mysql_query( "SELECT fromid, time, text, reyt FROM seo_taskcomms WHERE ident = '" . intval($_POST['id']) . "' ORDER BY id DESC" );
    while ($comrow = mysql_fetch_array($comms)) {
        $getus = mysql_fetch_array( mysql_query( "SELECT avatar, username, id, taskreyt FROM tb_users WHERE id = '" . $comrow['fromid'] . "'" ) );
        ?>
            <tr>
			 <td width="170px" align="left" valign="top">
			    <a href="/id<?=$getus['id']?>" >
			        <img src="avp/<?=$getus['avatar']?>" width="40" height="40" class="avatar" style="margin-right:-5px; margin-bottom: -5px;"/>
			    </a>
				<div style="margin-left: 55px; margin-top: -50px;">
			    <span style="color: #800000;"><?=$getus['username']?> (ID: <?=$getus['id']?>)</span><br />Репутация <font color="8D950B"><?=$getus['taskreyt']?></font><br /><span class='rating<?=$comrow['reyt']; ?>'></span></div>
			</td>
                <td class="rt" valign="top"><div class="vcom">
				<?
				$array = explode(" ", $comrow['text']);
				for ($i = 0, $array_num = count($array); $i < $array_num; $i++) {
					$word_split = wordwrap($array[$i], 70, "<br />", true);
					echo $word_split . ' ';
				}
				?>
				</div><div style="font-size: 11px; color: #ebebeb;" align="right"><?=date('d.m.Y', $comrow['time'])?></a></div></td>
            </tr>
        <?
    }
    echo '</table>';
    exit();
}
?>