<?
require_once "configure.php";

if(!isset($_SESSION["egoUserName"])){
	print("<script type='text/javascript'>location.href='signin.php'</script>");
}

if($_GET["act"]=="delete"){
	if($_GET["t"]=="d"){
		$sql="delete from  discussion where d_id='".$_GET["deleteid"]."'";
		@mysql_query($sql) or die(mysql_error());

		$sql="delete from  comment where c_discussion='".$_GET["deleteid"]."'";
		@mysql_query($sql) or die(mysql_error());

		print("<script type='text/javascript'>location.href='index.php'</script>");
	}else if($_GET["t"]=="c"){

		$sql="delete from  comment where c_id='".$_GET["deleteid"]."'";
		@mysql_query($sql) or die(mysql_error());


	}

}


$sql = "SELECT * from discussion left join user on d_user=id WHERE d_id='".$_GET["id"]."'";
$result =mysql_query($sql)or die(mysql_error());
while($myrow=MySQL_fetch_array($result))
{
	$dtitle=$myrow["d_title"];
	$duser=$myrow["d_user"];
	
	$by="<span class='textsmall'>".$myrow["name"]." &nbsp; ".$myrow["d_date"]."</span>";

	$comment1=$myrow["d_content"];
}



if(isset($_POST['btnSubmit'])) {
	$sql="
	INSERT INTO `comment` (c_discussion,`c_comment`,  c_user) VALUES ('".$_GET["id"]."',\"$content\",'".$_SESSION["egoUserID"]."')";
	@mysql_query($sql) or die(mysql_error());

	$subject=$_SESSION["egoUserName"]." post a comment for $dtitle";	
	mail($email,$subject,$content,'From:'.$_SESSION["egoUserEmail"]);	

}
$id=$_GET["id"];
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?=TITLE;?></title>
<link rel="stylesheet" type="text/css" href="style.css" >
<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.alerts.js" type="text/javascript"></script>
<link href="jquery.alerts.css" rel="stylesheet" type="text/css" media="screen" />

<script>
function ValidateForm(theform)
{
	var msg="";

	if(!theform.content.value)msg+="Comment is required. \n";
		
	if(msg){
		alert(msg)
		return false;
	}else return true;	
	
}



function callAlert(val,id,t)
{
	
		
	jConfirm('Are you sure want to delete?','SGC Forum', function(r) {
		if(r==true){
			location.href="comment.php?deleteid="+val+"&act=delete&id="+id+"&t="+t;
		}

	});
}



</script>
</head>

<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="46" bgcolor="#38abe3">
      <? require_once "header.php"; ?></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td><table width="1000" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td><table width="100%" border="0" cellpadding="0" cellspacing="0">
          <tr>
				<td width="84%" height="40" bgcolor="#cfecff" class="text"><strong><?=$dtitle;?></strong>
</td>
				<td width="16%" align="right" bgcolor="#cfecff" class="text"><a href="index.php"><strong>All Discussions</strong></a> &nbsp;  &nbsp; </td>
          </tr>
				 <tr><td height="30" colspan="2" bgcolor="#e3f4ff"><?=$by;?> &nbsp; 
                 <? 

				 if($_SESSION["egoUserType"]=="Admin" || ($_SESSION["egoUserType"]=="User" &&  $_SESSION["egoUserID"]==$duser) )
				echo "<a href='editcomment.php?id=$id&t=d'>Edit</a> &nbsp;  <a href='#' onclick=\"callAlert('$id','$id','d')\">Delete Discussion</a>";
				?>
				
                 
                 <p><span class="text"><?=str_replace("\r\n","<br>",$comment1);?></span></p></td>
			  </tr>
                <tr>
            <td height="1" colspan="2" bgcolor="#ccccc" class="text"></td>
          </tr>


          <?
  		    $sql = "SELECT * from comment left join user on c_user=id WHERE c_discussion='".$_GET["id"]."' order by c_id";
			$result =mysql_query($sql)or die(mysql_error());
			while($myrow=MySQL_fetch_array($result))
			{
				$by="";
				$cid=$myrow["c_id"];
				$by="<span class='textsmall'>".$myrow["name"]." &nbsp; ".$myrow["c_date"]."</span> &nbsp;";
				
				 if($_SESSION["egoUserType"]=="Admin" || ($_SESSION["egoUserType"]=="User" &&  $_SESSION["egoUserID"]==$myrow["c_user"]) )
				$by.="<a href='editcomment.php?id=$id&t=c&cid=$cid'>Edit</a> &nbsp;  <a href='#' onclick=\"callAlert('$cid','$id','c')\">Delete</a>";
				
				
			  echo ' <tr>
				<td colspan="2" height="30" bgcolor="#e3f4ff" >'.$by.'<p><span class="text">'.str_replace("\r\n","<br> &nbsp; ",$myrow["c_comment"]).'</span></p></td></tr>
			            <tr>
            <td height="1" colspan="2" bgcolor="#ccccc" class="text"></td>
          </tr>
			';

			}
		  
		  ?>
            <tr>
				<td height="30" colspan="2" bgcolor="#cfecff" class="text"><strong>Write Comment</strong></td>
				</tr>
            <tr>
              <td height="30" colspan="2" bgcolor="#cfecff" class="text">
              <form name="form" method="post" action="<?=$PHP_SELF;?>?id=<?=$_GET["id"];?>" ONSUBMIT="return ValidateForm(this)">
                <p>
                  <textarea name="content" rows="10" class="inputBoxDiscussion" id="content"></textarea>
                </p>
                <p>
                  <input type="submit" name="btnSubmit" id="button" value="Post Comment" />
                </p>
              </form>
              </td>
            </tr>
            <tr>
              <td height="30" colspan="2" bgcolor="#cfecff" class="text">&nbsp;</td>
            </tr>
       
     
        </table>
</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td><a href="http://egenos.com" target="_blank">Powered by eGen online solution</a></td>
      </tr>
    </table></td>
  </tr>
</table>
</body>
</html>
<?
function noofcomment($id)
{
	$sql = "SELECT * from comment WHERE c_discussion='$id'";
	$result =mysql_query($sql)or die(mysql_error());
	return mysql_num_rows($result);
}

function recentby($id)
{
	$sql = "SELECT * from comment WHERE d_discussion='$id' order by d_id desc limit 1";
	$result =mysql_query($sql)or die(mysql_error());
	while($myrow=MySQL_fetch_array($result))
	{
		return array("user"=>$myrow["d_user"],"date"=>$myrow["d_user"]);
	}
}