<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<?php session_start();
if(isset($_SESSION['email']))
{
echo "";
}
else
{
	header( "Location: login.php" ); 
}
?>
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <style>
  td
{
	font-family: calibri;
	color: black;
	background-image:url('lightgray.jpg');
	background-size:100% 100%;
	background-repeat:no-repeat;
}
</style>
  <script type="text/javascript">
  function postfun()
  {
	  document.replypost.hi.value="submitted";
	  document.replypost.submit();
  }
  function deletef(recordid)
  {
	   document.replypost.deleteThreadId.value=recordid;
	  document.replypost.submit();
  }
  </script>
 </head>

 <body>
  <?php error_reporting (E_ALL ^ E_NOTICE);
  $threadid=$_POST["postf"];
  $emailor=$_SESSION['email'];
  $con = mysql_connect("localhost:3306","","");
	if (!$con)
	{
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db("test", $con);
	$idor=mysql_query("select userid from userdata where Email='$emailor';");
	//userid of the user in session
	while($row = mysql_fetch_array($idor))
	{
		$useridor=$row['userid'];
	}

	//threadowner of the thread
	$ownerres=mysql_query("select  threadowner,subject from thread where threadid=$threadid");
	while($row = mysql_fetch_array($ownerres))
	{
		$threadowner=$row['threadowner'];
		$subject=$row['subject'];
	}

	//inserting the post in the database
	$eventType = $_POST["hi"];
	if($eventType=="submitted")
	{
		$reply=$_POST["content"];
		mysql_query("insert into post(threadid,post,userid,date_entered) values($threadid,'$reply',$useridor,SYSDATE());");
	}
	//deleting selected posts
	$toBeDeletedRecordId=$_POST["deleteThreadId"];
	if($toBeDeletedRecordId!=NULL)
	{
		mysql_query("delete from post where id=$toBeDeletedRecordId;");
	}

	//displaying all the posts in the thread
	$postres=mysql_query("select * from post where threadid=$threadid;");
	$postnum=mysql_num_rows($postres);
	$i=0;
	while($row = mysql_fetch_array($postres))
	{
		$id[$i]=$row['id'];
		$userid[$i]=$row['userid'];
		$content[$i]=$row['post'];
		$date_entered[$i]=$row['date_entered'];
		$i++;
	}
	for($j=0;$j<$postnum;$j++)
	{
			$emailres=mysql_query("select Username from userdata where userid=$userid[$j];");
		
		//email of the user who posted
		while($row = mysql_fetch_array($emailres))
		{
			$email[$j]=$row['Username'];
		}
			if($userid[$j]==$useridor)
			{
			echo "<table style='border:2px solid black' width='100%'>";
			echo "<tr><td width='80%' text-align:left style='font-size:15px'><i>$email[$j]</i></td>";
			echo "<td width='20%' text-align:right style='font-size:15px'><i>$date_entered[$j]</i></td></tr>";
			echo "<tr><td width='80%' text-align:left style='font-size:20px'>$content[$j]</td>";
			echo "<td text-align:right><input type='button' value='delete' onclick='deletef($id[$j])'/></td></tr>";
			//echo "<input type='button' value='delete' onclick='deletef($id[$j])'/>"; 
			echo "</table></br>";
			}
			else
			{
			echo "<table style='border:2px solid black' width='760px'>";
			echo "<tr><td>";
			echo "<table width='760px'>";
			echo "<tr><td width='80%' text-align:left style='font-size:15px'><i>$email[$j]</i></td>";
			echo "<td width='20%' text-align:right style='font-size:15px'><i>$date_entered[$j]</i></td></tr></table>";
			echo "</td></tr><tr><td>";
			echo "<table width='760px'><tr>";
			echo "<td text-align:left style='font-size:20px'>$content[$j]</td></tr></table>";
			echo "</td></tr></table>.</br>";
			}
	}
	echo "<form name='replypost' action='post.php' method='post'>";
	echo "<textarea name='content' rows='10' cols='40'/></textarea></br>";
	echo "<input type='button' value='post reply' onclick='postfun()'/>";
	echo "<input type='hidden' name='hi' id='hi' value='not'/>";
	echo "<input type='hidden' name='postf' id='postf' value='$threadid'/>";
	echo "<input type='hidden' name='deleteThreadId' id='deleteThreadId'/>";
	echo "</form>";
	mysql_close($con);
  ?>
  
 </body>
</html>
