<?php
ob_start();
if(!(is_readable(realpath(dirname(__FILE__)).'/include/includesroutines.inc'))) {
        die('Error - Could not open or read file '.realpath(dirname(__FILE__)).'/include/includesroutines.inc');
} else {
        include_once(realpath(dirname(__FILE__)).'/include/includesroutines.inc');
}

if(!(is_readable(realpath(dirname(__FILE__)).'/include/pollobjectsarc.inc'))) {
	die('Error - Could not open or read file '.realpath(dirname(__FILE__)).'/include/pollobjectsarc.inc');
} else {
	include_once(realpath(dirname(__FILE__)).'/include/pollobjectsarc.inc');
}

checkUserAndRedirectToLogin();

$cn = db_connect();
$lng = ( isset($_REQUEST['lang']) ? 'lang='.$_REQUEST['lang'] : '');
$lngid = db_getLanguageID($cn, getLanguage());
$lngshort = db_getLanguageShortByID($cn, empty($lngid) ? $_SESSION['user_idlanguageinterface'] : $lngid, true);
$contlng = $lng;
$contlngid = $lngid;
if (getSetting('portal','useRights',0) == 1){
	$contlng = 'lang='.db_getLanguageShortByID($cn, $_SESSION['user_idlanguagecontent']);
	$contlngid = $_SESSION['user_idlanguagecontent'];
}

if ( getSetting('portal','useRights',0) == 1 && !IsUserLogin()){
        header("Location: ".getCurPageURLWO()."/login.php");
        ob_end_flush();
        db_disconnect($cn);
        exit;
} else if ( GetSetting('portal','useRights',0) == 0 ) {
        header("Location: ".getCurPageURLWO()."/buisnessmodel.php");
        ob_end_flush();
        db_disconnect($cn);
        exit;
}

$id = 0;

if (!isset($_REQUEST['skpollingobject']) || !isset($_REQUEST['skpolltype']) || !is_numeric($_REQUEST['skpollingobject']) || !is_numeric($_REQUEST['skpolltype'])) {
        header("Location: ".getCurPageURLWO()."/pagenotfound.php?url=".getCurPageWithEncodeParams());
        ob_end_flush();
        $oguid = null;
        exit;
}

$skpollingobject = $_REQUEST['skpollingobject'];
$skpolltype =$_REQUEST['skpolltype'];
$usr_arr = GetUserIDs();

$skbranch = db_getSKBranchByUserAnswerValue($cn, $skpollingobject, $skpolltype , $usr_arr[0] );
$guids = db_getGuidsByPollingObject($cn, $skpollingobject);
$pollobjectoguid = $guids[0]['o_guid'];
$pOStatus = db_getPollObjectStatus($cn, $_REQUEST['skpollingobject'],$_REQUEST['skpolltype']);

if ($skbranch == 0 || $skbranch === NULL ) {
        db_disconnect($cn);
        header("Location: ".getCurPageURLWO()."/pagenotfound.php?url=".getCurPageWithEncodeParams());
        ob_end_flush();
        exit;
}

$allowComment = db_getPollTypeAllowComment($cn, $_REQUEST['skpolltype']);
if (isset($_REQUEST['valueadd'] )) {
        $uav_body = (isset($_REQUEST['message']) ? $_REQUEST['message'] : '');
        db_savePolltypeQuestionComment($cn, $skpollingobject, $skpolltype, $usr_arr[0], $uav_body);
        db_disconnect($cn);
        header("Location: ".getCurPageURLWO()."/service/iframereload.php");
        ob_end_flush();
        exit;        
}

?>
<!doctype html>
<html>
<head>
        <title><?php echo renderTitle();?></title>
        <? 
        renderCommonMeta(renderTitle()); 
        renderCommonCSS(); 
        ?>
</head>
<body>
                <div id="content" class="ui-layout-center" style="padding: 0px; width: 100vw;height: 99vh;visible: none; overflow: hidden;">
                <?php if ($allowComment){ ?>
                        <form name="sendForm" id="sendForm" action="" method="post" enctype="multipart/form-data">
                                <?php 
                                        $uav_value = 0; 
                                        $uav_body = '';
                                        $idforummessage = 0;
                                        $answer = db_getPolltypeQuestionAnswer($cn, $skpollingobject, $skpolltype, $usr_arr[0] );
                                        if ($answer !== NULL && is_array($answer) && sizeof($answer) > 0 && sizeof($answer[0]) > 0 ) {
                                                $uav_value = $answer[0]['uav_value']; 
                                                $uav_body = $answer[0]['uav_body'];
                                        }
                                ?>
                                <textarea id="message" name="message" <? if ($pOStatus != 1) { echo 'readonly="readonly"' /*62783*/;}?> style="width: 100%;"><?php echo  $uav_body;?></textarea>
                                <input type="hidden" id="valueadd" name="valueadd" value="true">
                                <input type="hidden" id="postonforum" name="postonforum" value="false">
                        </form>
                        <div id="outer" style="width: 100%;position:absolute;  bottom:0;">
                                <div id="inner" style="margin: 0; padding-bottom: 6px;padding-top: 10px;float: right;">
                                        <span style="margin-right: 10px;"><button name="buttonSendForm" id="buttonSendForm" class="btn-top btn-top-text" title="<?php getResource("button.Save.hint");?>"><span><i class="btn-top-text-icon btn-top-text-icon-ok"></i><?php getResource("button.Save");?></span></button></span>
                                        <span><button name="buttonCancel" id="buttonCancel" class="btn-top btn-top-text" title="<?php getResource("button.cancel.hint");?>"><span><i class="btn-top-text-icon btn-top-text-icon-cancel"></i><?php getResource("button.cancel");?></span></button></span>
                                </div>
                        </div>
                        
                <?}?>        
                </div>
        <?php renderFooterTemplate(); ?>
</body>
<script src="js/libs/jquery.js"></script>
<script>
var GForumOGUID = '<?php echo $pollobjectoguid;?>';
var gShortLang = '<?php echo $lngshort;?>';
var GForumEmprtyMessageAlert = '<?php getResource("message.messageIsEmpty");?>';
var GAnswerEmprtyMessageAlert = '<?php getResource("message.answerIsEmpty");?>';
</script>
<script src="ckeditor/ckeditor.js"></script>
<script src="js/script.js"></script>
<script src="js/pollobjectcomment.js?<?php echo createGuid();?>"></script>
</html>
<?php
db_disconnect($cn);
ob_end_flush(); ?>
