<?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/bsservice.inc'))) {
	die('Error - Could not open or read file '.realpath(dirname(__FILE__)).'/include/bsservice.inc');
} else {
	include_once(realpath(dirname(__FILE__)).'/include/bsservice.inc');
}

$cn = db_connect();

$oguid = null;
if (isset($_REQUEST['oguid'])) {
	 $oguid = $_REQUEST['oguid'];
}

$rguid = null;
if (isset($_REQUEST['rguid'])) {
	 $rguid = $_REQUEST['rguid'];
}

$userids = null;

if (IsUserLogin() == 1){
	$userids = GetUserIDs();
}

if (isset($_REQUEST['idbranch'])) {
	$idbranch = $_REQUEST['idbranch'];
	$skbranch = db_getSKBranch($cn,$idbranch);	
} else {
	$skbranch = db_getMainSKBranch($cn);
	$idbranch = db_getMainIDBranch($cn);	
}

$contlng = 'lang='.db_getLanguageShortByID($cn, $_SESSION['user_idlanguagecontent']);
$contlngid = $_SESSION['user_idlanguagecontent'];


if (isset($_REQUEST['notice']) && $_REQUEST['notice'] == '1' && IsUserLogin() == 1 ){
	db_addReportsUsersQuery($cn, $userids[0], $oguid, $rguid, $contlngid, $skbranch);
}

if (isset($_REQUEST['stop']) && $_REQUEST['stop'] == '1' && IsUserLogin() == 1){
	$call = sendRepotStop($cn, $oguid, $rguid, $idbranch);
	if (IsUserLogin() == 1) {
		db_deleteReportsUsersQuery($cn, $userids[0], $oguid, $rguid, $contlngid, $skbranch);
	}
	if (!$call) {
	 	$url = getCurPageURLWO()."/bsserviceerror.php";
		header("Location: ".$url, true, 302);
		ob_end_flush();
		exit;
	}

}
$headerName ='BSPortalReport';
$headerValue = 'NotExists';
header($headerName.':'.$headerValue);
?>
<!doctype html>
<html>
<head>
	<?php if (!isset($_REQUEST['stop']) || $_REQUEST['stop'] != '1'){ ?>
	<!--<meta http-equiv="Refresh" content="3; URL=<?php echo getPageDencodeParams($_REQUEST['url']).'timer=1';?>">-->
	<?php } ?>
	<title><?php echo renderTitle();?></title>
	<? 
	renderCommonMeta(renderTitle()); 
	renderCommonCSS(); 
	?>
</head>
<body id="page-login" class="b-page-dialogue">
<div id="container">
	<div id="header">
		<div id="header-h2">Business Studio Portal</div>
		<!--STU: remove logo from corner<div id="header-h1"></div>-->
	</div>
	<div id="main" role="main">

<div class="b-dialogue-title">
<?php if (!isset($_REQUEST['stop']) || $_REQUEST['stop'] != '1'){ ?>
<h2><?php getResource("field.buildReport");?></h2>
<img src="./img/progressbar.gif" border="0"><br />
<?php } else { ?>
<h2><?php getResource("field.buildReport.Cancel");?></h2>
<?php } ?>
</div>
<div class="b-dialogue-status">
<div id="b-dialogue-message" name="b-dialogue-message" class="b-dialogue-message">
<?php
	if ((!isset($_REQUEST['stop']) || $_REQUEST['stop'] != '1') && isset($_REQUEST['call']) ){
		$timer = '0';
		if (isset($_REQUEST['timer'])){
			$timer = $_REQUEST['timer'];
		}
		$reportLangID = db_getReportIDLanguage($cn, $oguid, $rguid, $contlngid, $skbranch); //60628
		$call = sendRepotCall($cn, $oguid, $rguid, ($reportLangID != 0 ? db_getLanguageShortByID($cn, $reportLangID) : ""), "0", $timer, $idbranch);
		if (!$call) {
		 	$url = getCurPageURLWO()."/bsserviceerror.php?oguid=".$oguid."&rguid".$rguid;
			header("Location: ".$url);
			ob_end_flush();
			exit;
		}
		echo $call;
	}
?>
</div>
<?php if ((!isset($_REQUEST['stop']) || $_REQUEST['stop'] != '1')) { ?>
<div class="b-dialogue-control clearfix">
<div class="fright">
<form id="noticeform" action="" method="post">
   <input type="hidden" id="notice" name="notice" value="1"/>
<?php
// Сделать не активной кнопку если пользователь нажал на уведомить
if (getSetting('portal','useRights', 0) == 1 && (!db_isReportsUsersQuery($cn, $userids[0], $oguid, $rguid, $contlngid, $skbranch))) {
?>
<span><button class="btn-top btn-top-text" title="<?php getResource("field.notisethenreportbuild");?>"><span><?php getResource("field.notisethenreportbuild");?></span></button></span>
<?php } else { ?>
   <span><button disabled class="btn-top btn-top-text" style="cursor:default" title="<?php getResource("field.notisethenreportbuild");?>"><span class="btn-top-text-disabled"><?php getResource("field.notisethenreportbuild");?></span></button></span>
<?php } ?>
</form>
</div>
</div>
<?php } ?>
</div>
</div>
<?php
	if (!isset($_REQUEST['timer'])){
		$turl =  getPageDencodeParams($_REQUEST['url']).'&timer=1';
	} else {
		$turl =  getPageDencodeParams($_REQUEST['url']);
	}
?>
<script src="js/libs/jquery-1.8.0.min.js"></script>
<?php renderFooterTemplate(); ?>
<script>
var refreshIntervalId;
$(function() { 
	function call() {
		var oturl = '<?php echo $turl;?>' ; 
		var turl = '<?php echo $turl;?>' + '&call'; //67113
		$.ajax({
			url: turl,
			dataType: "html",
			type: "POST",
			complete: function(request) {
				headerValue = request.getResponseHeader('<?echo $headerName;?>'); 
				if (headerValue === '<?echo $headerValue;?>'){
					$("#b-dialogue-message").html($($.parseHTML(request.responseText)).find('#b-dialogue-message').html());
				} else {
					window.location.replace(oturl);
				}
			}
		});
	}
	
	setTimeout(function() {
		call();
		setTimeout(function() {
			call();
			refreshIntervalId = setInterval(function() {
					call();
					if (window.addEventListener) {
						window.addEventListener("message", stopInterval, false);
					} else {
						window.attachEvent("onmessage", stopInterval);
					}
			}, 5000); // Task 64746
		}, 1000);
	}, 200);

	function stopInterval (evt) {
		clearInterval(refreshIntervalId);
	}
});

</script>
</body>
</html>
<?php ob_end_flush(); ?>