<!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="">
  <script type="text/javascript">
  function submitform()
  {
	  var subject=document.getElementById("subject").value;
	  var content=document.getElementById("content").value;
	  if(subject=="")
	  {
		alert("Please enter the name of the topic"); 
		return false;
	  }
	   if(content=="")
	  {
		alert("Please enter the content"); 
		return false;
	  }
	  document.create.eventType.value="submitted";
	  document.create.submit();
  }
  function forumsubmit()
  {
	  document.back.submit();
  }
  </script>
 </head>

 <body>
 <form name="create" action="createthread.php" method="post">
 <input type="hidden" id="eventType" name="eventType"/>
 Subject:<input type="text" name="subject" id="subject" /></br>
 Content:<textarea name="content" id="content" rows="10" cols="90"></textarea></br>
 <input type="button" value="Post" onclick="submitform()"/></br>
 </form>
 <form name="back" action="forum.php">
<input type="button" value="Back to Forum" onclick="forumsubmit()"/>
</form>
  <?php error_reporting (E_ALL ^ E_NOTICE);
if($_POST["eventType"]=="submitted")
{
	$con = mysql_connect("localhost:3306","","");
	if (!$con)
	{
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db("test", $con);
  $subject = $_POST["subject"];
  $content=$_POST["content"];
$email=$_SESSION['email'];
$email_result=mysql_query("select userid,Username from userdata where Email='$email';") or die(mysql_error());
while($row = mysql_fetch_array($email_result))
{
	$userid = $row['userid'];
	$username=$row['Username'];
}
echo $userid;
echo $username;
$thread_result=mysql_query("select max(threadid) as maximum from thread") or die(mysql_error());
while($row = mysql_fetch_array($thread_result))
{
	$threadid = $row['maximum'];
}
$currentThread=$threadid+1;
echo $currentThread;
	mysql_query("insert into thread(threadid,subject,createdDate,threadowner) values($currentThread,'$subject',SYSDATE(),'$username');") or die(mysql_error());
	mysql_query("insert into post(threadid,post,userid,date_entered) values($currentThread,'$content',$userid,SYSDATE());") or die(mysql_error());
echo "<SCRIPT LANGUAGE='javascript'>forumsubmit();</SCRIPT>";

}
  ?>
 </body>
</html>
