<?php

if(!isset($_SESSION)) session_start();
include_once("config.php");
include("loginCheckPoint.php");
include_once("authorizations.php");
require_once("demosettings.php");

$mysqli = new mysqli($databaseHost, $databaseUser, $databasePass, $databaseName);
//Execute plan test for dates
//manipulateDateTest();

function getTopBannerDisplay()
{
	return $_REQUEST["topBannerDisplay"]? $_REQUEST["topBannerDisplay"] : $topBannerDisplay;
}

function manipulateDateTest()
{
	global $mysqli;	
	$sql = "SELECT MAX(id) AS i FROM files";	
	$res = $mysqli->query($sql);
	$row = $res->fetch_object();
	$i = $row->i;
	$j = 1;
	$day = 1;
	while ($i > 0){	
		if ($j <= 10){
			if ($day >= 28){
				$day = 1;
			}else{
				$day++;
			}
		}else 
			$j = 1;
		$sql = "UPDATE files 
				SET DATE='2015-05-$day'
				WHERE id=$i";	
		$res = $mysqli->query($sql);
		$i--;
	} 
}
	
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<link rel="icon" type="image/png" href="images/favicon.png" />
	<title>IntuScan Station</title>
    <link class="include" rel="stylesheet" type="text/css" href="dist/jquery.jqplot.min.css" />
    <link href="style.css" rel="stylesheet" type="text/css" />
    <!--[if lt IE 9]><script language="javascript" type="text/javascript" src="dist/excanvas.js"></script><![endif]-->
    <script class="include" type="text/javascript" src="dist/jquery.min.js"></script>
    <script class="include" type="text/javascript" src="dist/jquery.jqplot.min.js"></script>
    <script class="include" type="text/javascript" src="dist/plugins/jqplot.canvasTextRenderer.min.js"></script>
    <script class="include" type="text/javascript" src="dist/plugins/jqplot.canvasAxisLabelRenderer.min.js"></script>
    <script type="text/javascript" src="dist/plugins/jqplot.dateAxisRenderer.min.js"></script>
    <script type="text/javascript" src="dist/plugins/jqplot.highlighter.min.js"></script>
    <script type="text/javascript" src="dist/plugins/jqplot.enhancedLegendRenderer.min.js"></script>
    <script type="text/javascript" src="dist/plugins/jqplot.pieRenderer.min.js"></script>
	<script type="text/javascript" src="dist/plugins/jqplot.donutRenderer.min.js"></script>
	<script type="text/javascript" src="dist/plugins/jqplot.canvasTextRenderer.min.js"></script>
	<script type="text/javascript" src="dist/plugins/jqplot.canvasAxisLabelRenderer.min.js"></script>
	<script src="js/ivcommon.js"></script>
    <style>
        .chart { height:400px; width:700px; }
    </style>
</head>

<script type="text/javascript">

</script>

<body>
<div class="main">
 <div class="top-banner" style="display:<?php echo getTopBannerDisplay() ?>">
	 <a href="home.php"><img src="images/intuscan-img.png" style="border:0;"/></a>
	 <p style="padding:0 10px 10px 10px; float:right;"><b>version 1</b></p>
	 <p style="padding:0 10px 10px 10px; float:right;">Hi, <?php echo $_SESSION["username"] ?></p>
	 <p style="padding:0 10px 10px 10px; float:right;"><a style="color:red;" href="logoff.php">logoff</a></p>
 </div>
  <div class="header" style="display:<?php echo getTopBannerDisplay() ?>">
    
    <div class="header_block">
     
      <div class="clr"></div>
      <?php
      	include('general-toolbar.php'); 
      ?>
      <div class="clr"></div>
    </div>
    <div class="clr"></div>
  </div>
  <div class="content">    		
		<table id="infoTable" height="250px" >
			<tbody>
				<tr>
					<td width="80px">
						<h4>Dates Mentioned</h4>
					</td>	
					<td width="400px">
						<div id="DatesMentionedTextDiv" style="display: block;">				
							<b><u>From</u></b>&nbsp;&nbsp;&nbsp;Day&nbsp;&nbsp;
							<select name="DatesMentionedDayFromSelect" id="DatesMentionedDayFromSelect" >
								  <option value="None"></option>
								  <option value="1">1</option>
								  <option value="2">2</option>
								  <option value="3">3</option>
								  <option value="4">4</option>
								  <option value="5">5</option>
								  <option value="6">6</option>
								  <option value="7">7</option>
								  <option value="8">8</option>
								  <option value="9">9</option>
								  <option value="10">10</option>
								  <option value="11">11</option>
								  <option value="12">12</option>
								  <option value="13">13</option>
								  <option value="14">14</option>
								  <option value="15">15</option>
								  <option value="16">16</option>
								  <option value="17">17</option>
								  <option value="18">18</option>
								  <option value="19">19</option>
								  <option value="20">20</option>
								  <option value="21">21</option>
								  <option value="22">22</option>
								  <option value="23">23</option>
								  <option value="24">24</option>
								  <option value="25">25</option>
								  <option value="26">26</option>
								  <option value="27">27</option>
								  <option value="28">28</option>
								  <option value="29">29</option>
								  <option value="30">30</option>
								  <option value="31">31</option>																 																	
							</select>
							Month&nbsp;&nbsp;
							<select name="DatesMentionedMonthFromSelect" id="DatesMentionedMonthFromSelect">
								  <option value="None"></option>
								  <option value="1">1</option>
								  <option value="2">2</option>
								  <option value="3">3</option>
								  <option value="4">4</option>
								  <option value="5">5</option>
								  <option value="6">6</option>
								  <option value="7">7</option>
								  <option value="8">8</option>
								  <option value="9">9</option>
								  <option value="10">10</option>
								  <option value="11">11</option>
								  <option value="12">12</option>																 																	
							</select>
							Year&nbsp;&nbsp;
							<select name="DatesMentionedYearFromSelect" id="DatesMentionedYearFromSelect">
								  <option value="None"></option>
								  <option value="2015">2015</option><option value="2014">2014</option><option value="2013">2013</option><option value="2012">2012</option><option value="2011">2011</option><option value="2010">2010</option><option value="2009">2009</option><option value="2008">2008</option><option value="2007">2007</option><option value="2006">2006</option><option value="2005">2005</option><option value="2004">2004</option><option value="2003">2003</option><option value="2002">2002</option><option value="2001">2001</option><option value="2000">2000</option><option value="1999">1999</option><option value="1998">1998</option><option value="1997">1997</option><option value="1996">1996</option><option value="1995">1995</option><option value="1994">1994</option><option value="1993">1993</option><option value="1992">1992</option><option value="1991">1991</option><option value="1990">1990</option><option value="1989">1989</option><option value="1988">1988</option><option value="1987">1987</option><option value="1986">1986</option><option value="1985">1985</option><option value="1984">1984</option><option value="1983">1983</option><option value="1982">1982</option><option value="1981">1981</option><option value="1980">1980</option><option value="1979">1979</option><option value="1978">1978</option><option value="1977">1977</option><option value="1976">1976</option><option value="1975">1975</option><option value="1974">1974</option><option value="1973">1973</option><option value="1972">1972</option><option value="1971">1971</option><option value="1970">1970</option><option value="1969">1969</option><option value="1968">1968</option><option value="1967">1967</option><option value="1966">1966</option><option value="1965">1965</option><option value="1964">1964</option><option value="1963">1963</option><option value="1962">1962</option><option value="1961">1961</option><option value="1960">1960</option><option value="1959">1959</option><option value="1958">1958</option><option value="1957">1957</option><option value="1956">1956</option><option value="1955">1955</option><option value="1954">1954</option><option value="1953">1953</option><option value="1952">1952</option><option value="1951">1951</option><option value="1950">1950</option><option value="1949">1949</option><option value="1948">1948</option><option value="1947">1947</option><option value="1946">1946</option><option value="1945">1945</option><option value="1944">1944</option><option value="1943">1943</option><option value="1942">1942</option><option value="1941">1941</option><option value="1940">1940</option><option value="1939">1939</option><option value="1938">1938</option><option value="1937">1937</option><option value="1936">1936</option><option value="1935">1935</option><option value="1934">1934</option><option value="1933">1933</option><option value="1932">1932</option><option value="1931">1931</option><option value="1930">1930</option><option value="1929">1929</option><option value="1928">1928</option><option value="1927">1927</option><option value="1926">1926</option><option value="1925">1925</option><option value="1924">1924</option><option value="1923">1923</option><option value="1922">1922</option><option value="1921">1921</option><option value="1920">1920</option><option value="1919">1919</option><option value="1918">1918</option><option value="1917">1917</option><option value="1916">1916</option><option value="1915">1915</option><option value="1914">1914</option><option value="1913">1913</option><option value="1912">1912</option><option value="1911">1911</option><option value="1910">1910</option><option value="1909">1909</option><option value="1908">1908</option><option value="1907">1907</option><option value="1906">1906</option><option value="1905">1905</option><option value="1904">1904</option><option value="1903">1903</option><option value="1902">1902</option><option value="1901">1901</option><option value="1900">1900</option>																 																	
							</select>
							&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
							<b><u>To</u></b>&nbsp;&nbsp;&nbsp;
							Day&nbsp;&nbsp;
							<select name="DatesMentionedDayToSelect" id="DatesMentionedDayToSelect" >
								  <option value="None"></option>
								  <option value="1">1</option>
								  <option value="2">2</option>
								  <option value="3">3</option>
								  <option value="4">4</option>
								  <option value="5">5</option>
								  <option value="6">6</option>
								  <option value="7">7</option>
								  <option value="8">8</option>
								  <option value="9">9</option>
								  <option value="10">10</option>
								  <option value="11">11</option>
								  <option value="12">12</option>
								  <option value="13">13</option>
								  <option value="14">14</option>
								  <option value="15">15</option>
								  <option value="16">16</option>
								  <option value="17">17</option>
								  <option value="18">18</option>
								  <option value="19">19</option>
								  <option value="20">20</option>
								  <option value="21">21</option>
								  <option value="22">22</option>
								  <option value="23">23</option>
								  <option value="24">24</option>
								  <option value="25">25</option>
								  <option value="26">26</option>
								  <option value="27">27</option>
								  <option value="28">28</option>
								  <option value="29">29</option>
								  <option value="30">30</option>
								  <option value="31">31</option>																 																	
							</select>
							Month&nbsp;&nbsp;
							<select name="DatesMentionedMonthToSelect" id="DatesMentionedMonthToSelect" >
								  <option value="None"></option>
								  <option value="1">1</option>
								  <option value="2">2</option>
								  <option value="3">3</option>
								  <option value="4">4</option>
								  <option value="5">5</option>
								  <option value="6">6</option>
								  <option value="7">7</option>
								  <option value="8">8</option>
								  <option value="9">9</option>
								  <option value="10">10</option>
								  <option value="11">11</option>
								  <option value="12">12</option>																 																	
							</select>
							Year&nbsp;&nbsp;
							<select name="DatesMentionedYearToSelect" id="DatesMentionedYearToSelect" >
								  <option value="None"></option>
								  <option value="2015">2015</option><option value="2014">2014</option><option value="2013">2013</option><option value="2012">2012</option><option value="2011">2011</option><option value="2010">2010</option><option value="2009">2009</option><option value="2008">2008</option><option value="2007">2007</option><option value="2006">2006</option><option value="2005">2005</option><option value="2004">2004</option><option value="2003">2003</option><option value="2002">2002</option><option value="2001">2001</option><option value="2000">2000</option><option value="1999">1999</option><option value="1998">1998</option><option value="1997">1997</option><option value="1996">1996</option><option value="1995">1995</option><option value="1994">1994</option><option value="1993">1993</option><option value="1992">1992</option><option value="1991">1991</option><option value="1990">1990</option><option value="1989">1989</option><option value="1988">1988</option><option value="1987">1987</option><option value="1986">1986</option><option value="1985">1985</option><option value="1984">1984</option><option value="1983">1983</option><option value="1982">1982</option><option value="1981">1981</option><option value="1980">1980</option><option value="1979">1979</option><option value="1978">1978</option><option value="1977">1977</option><option value="1976">1976</option><option value="1975">1975</option><option value="1974">1974</option><option value="1973">1973</option><option value="1972">1972</option><option value="1971">1971</option><option value="1970">1970</option><option value="1969">1969</option><option value="1968">1968</option><option value="1967">1967</option><option value="1966">1966</option><option value="1965">1965</option><option value="1964">1964</option><option value="1963">1963</option><option value="1962">1962</option><option value="1961">1961</option><option value="1960">1960</option><option value="1959">1959</option><option value="1958">1958</option><option value="1957">1957</option><option value="1956">1956</option><option value="1955">1955</option><option value="1954">1954</option><option value="1953">1953</option><option value="1952">1952</option><option value="1951">1951</option><option value="1950">1950</option><option value="1949">1949</option><option value="1948">1948</option><option value="1947">1947</option><option value="1946">1946</option><option value="1945">1945</option><option value="1944">1944</option><option value="1943">1943</option><option value="1942">1942</option><option value="1941">1941</option><option value="1940">1940</option><option value="1939">1939</option><option value="1938">1938</option><option value="1937">1937</option><option value="1936">1936</option><option value="1935">1935</option><option value="1934">1934</option><option value="1933">1933</option><option value="1932">1932</option><option value="1931">1931</option><option value="1930">1930</option><option value="1929">1929</option><option value="1928">1928</option><option value="1927">1927</option><option value="1926">1926</option><option value="1925">1925</option><option value="1924">1924</option><option value="1923">1923</option><option value="1922">1922</option><option value="1921">1921</option><option value="1920">1920</option><option value="1919">1919</option><option value="1918">1918</option><option value="1917">1917</option><option value="1916">1916</option><option value="1915">1915</option><option value="1914">1914</option><option value="1913">1913</option><option value="1912">1912</option><option value="1911">1911</option><option value="1910">1910</option><option value="1909">1909</option><option value="1908">1908</option><option value="1907">1907</option><option value="1906">1906</option><option value="1905">1905</option><option value="1904">1904</option><option value="1903">1903</option><option value="1902">1902</option><option value="1901">1901</option><option value="1900">1900</option>																 																	
							</select>
					</div>
					</td>
					<td width="50px" >
						<button id="btnSubmit" type="button" onclick="takeInfo()">Submit</button>
					</td>
				</tr>
				<tr height="150px">
					<td>
						Entities&nbsp;&nbsp;
						<select name="EntitiesSelect" id="EntitiesSelect" style="width: 140px;">
							<optgroup label="Prominence">
								<option value="counted_theatre">Theatre</option>
								<option value="document_topic">Topic</option>
								<option value="ideological_stream">Political Orientation</option>
								<option value="islamic_sect">Religious affiliation</option>								
							</optgroup>
							<optgroup label="Sentiment">
								<option value="event">Event</option>
								<option value="organizational_identity">Organisation</option>
								<option value="person_object">Person</option>
								<option value="place_object">Location</option>							
							</optgroup>
							<optgroup label="Priority">
								<option value="threat_rating">Priority level</option>
							</optgroup> 	
						</select>
					</td>
					<td>
						<div style="width: 100%;">
							<div style="width: 450px; float: left;">
							&nbsp;&nbsp;Free&nbsp;text&nbsp;&nbsp; 
							<input id="freeTextArea" placeholder="Please type here"  onkeydown="if (event.keyCode == 13) document.getElementById('btnSubmit').click()" autocomplete="off">
									<table id="columnTable">
										    <tbody>
												<tr>
													<td>
													<label><input id="cbAvg" type="checkbox" name="avg" value="true" onchange="changeLable()">Collapse time intervals</label><br>
													</td>
													<td id="columnDiv" style="display: none; float: right;">&nbsp;&nbsp;Column&nbsp;amount&nbsp;&nbsp;
														<input id="columnNum" type="number" min="0" onkeypress='return event.charCode >= 48 && event.charCode <= 57'
																onkeydown="if (event.keyCode == 13) document.getElementById('btnSubmit').click()">
													</td>
												</tr>
											</tbody>
									</table>
								</div>
								<div style="margin-left: 520px;">
									<table id="cbRisk" style="display: none;">
										<tbody>
											<tr><td><label><input id="cbHigh" value="true" type="checkbox">High</label></td></tr>
											<tr><td><label><input id="cbMedium" value="true" type="checkbox">Medium</label></td></tr>
											<tr><td><label><input id="cbLow" value="true" type="checkbox">Low</label></td></tr>
											<tr><td><label><input id="cbNoRisk" value="true" type="checkbox">No Risk</label></td></tr>
										</tbody>	
									</table>
								</div>
							</div>			
						</td>
					  <td>	
					</td>
				</tr>
			</tbody>
		</table>	
      <div class="sidebar">
      </div>
      <div class="clr"></div>
  </div>
  	<div class="footer">
  </div>
</div>
<style>
#infoTable {
	table-layout: fixed;
	width: 100%;
	margin: 0 auto;
	padding: 50px;
	border-collapse:collapse;
}	
</style>
</body>
<script type="text/javascript">

$(document).ready(function(){
	$('#cbAvg').prop('checked', false);
	$('#cbHigh').prop('checked', false);
	$('#cbMedium').prop('checked', false);
	$('#cbLow').prop('checked', false);
	$('#cbNoRisk').prop('checked', false);
	$("#columnNum").val("");
	$("#EntitiesSelect").val("counted_theatre");
	$("#freeTextArea").val('');
	$("#DatesMentionedDayFromSelect").val('');
	$("#DatesMentionedMonthFromSelect").val('');
	$("#DatesMentionedYearFromSelect").val('');
	$("#DatesMentionedDayToSelect").val('');
	$("#DatesMentionedMonthToSelect").val('');
	$("#DatesMentionedYearToSelect").val('');
});

$("#EntitiesSelect").change(function(){
	if($("#EntitiesSelect option:selected").val() == "threat_rating"){
		$("#freeTextArea").val('');	
		$("#freeTextArea").prop('disabled', true);
		$("#cbRisk").css('display' , "block");
	}else{
		$("#freeTextArea").prop('disabled', false);
		$("#cbRisk").css('display' , "none");
	}	
});

function checkFilled(id) {
        document.getElementById(id).style.backgroundColor = "#FFB2B2";
}

function takeInfo(){

	var fromDay = validateDate($("#DatesMentionedDayFromSelect").val(), 1);
	var fromMonth = validateDate($("#DatesMentionedMonthFromSelect").val(),1);
	var fromYear = validateDate($("#DatesMentionedYearFromSelect").val(),1900);
	var toDay = validateDate($("#DatesMentionedDayToSelect").val(),31);
	var toMonth = validateDate($("#DatesMentionedMonthToSelect").val(),12);
	var toYear = validateDate($("#DatesMentionedYearToSelect").val(),2050);
	var freeTextInput = $("#freeTextArea").val();
	var columnNum = $("#columnNum").val();
	var entitiesSelection = $("#EntitiesSelect option:selected").val();

	var asAvg = getCBvalues('#cbAvg');

	var isHigh = getCBvalues('#cbHigh');
	var isMedium = getCBvalues('#cbMedium');
	var isLow = getCBvalues('#cbLow');
	var isNoRisk = getCBvalues('#cbNoRisk');

	var fromDate = new Date(fromMonth + "/" + fromDay + "/" +  fromYear);
	var toDate = new Date(toMonth + "/" + toDay + "/" + toYear);	

		if(compareDates(fromDate, toDate)){
	
			if(freeTextInput != "" && entitiesSelection != "threat_rating"){
	
				gotoTemplate("proSentResults", 
							   {"fromDay": fromDay,
								"fromMonth":fromMonth,
								"fromYear":fromYear,
								"toDay":toDay,
								"toMonth":toMonth,
								"toYear":toYear,
								"freeTextInput":freeTextInput,
								"entitiesSelection":entitiesSelection,
								"columnNum":columnNum,
								"asAvg":asAvg,
								"topBannerDisplay": "<?php echo getTopBannerDisplay() ?>"
								});
			}else if(entitiesSelection == "threat_rating"){
	
				if(isHigh == false && isMedium == false && isLow == false && isNoRisk == false){
					window.alert("Please select one of the risk options");
				}else{
					gotoTemplate("proSentResults", 
									   {"fromDay": fromDay,
										"fromMonth":fromMonth,
										"fromYear":fromYear,
										"toDay":toDay,
										"toMonth":toMonth,
										"toYear":toYear,
										"freeTextInput":freeTextInput,
										"entitiesSelection":entitiesSelection,
										"columnNum":columnNum,
										"asAvg":asAvg,
										"isHigh":isHigh,
										"isMedium":isMedium,
										"isLow":isLow,
										"isNoRisk":isNoRisk, 
										"topBannerDisplay": "<?php echo getTopBannerDisplay() ?>"
									   });	
					}
			}else{
				checkFilled("freeTextArea");
			}
	}else{
		window.alert("The Dates are invalid");
	}
}	

function getCBvalues(selector){
	
	var value;
		if ($(selector).is(':checked')) {
				var value = true;
			}else{
				var value = false;
			}
	return value;	
}	

function compareDates(date1, date2) {
	if(date1 < date2){
		return true;
	}else{
		return false;
	}
}

function validateDate(timeStamp, def) {
	
		if(timeStamp != "None"){
			return timeStamp;
		}else{
			return def;
		}
}

function changeLable(){

	if (document.getElementById('cbAvg').checked === true){
		document.getElementById("columnDiv").style.display = "block";			
	}else{
		document.getElementById("columnDiv").style.display = "none";
	}
}

</script>
</html>