<?php
session_start();
require 'include/header.php';
?>
<style type="text/css">
<!--
.style1 {
	color: #666666;
	font-weight: bold;
}
.style3 {color: #666666; font-size: 11px; font-family:Arial, Helvetica, sans-serif}
-->
</style><?php
require 'include/configurations.php'; // ITECHBIDS Configuration File
$mode=trim($_GET['mode']);
$id=$_GET['id'];
$fsql="select * from news";
$fres=mysql_query($fsql);
if(($mode=='edit')||($mode=='stats'))
{
$eres=mysql_query("select * from news where news_id=$id");
$erow=mysql_fetch_array($eres);
}

if($mode=='stats')
{
$stats = $erow['status'];
if($stats=='active')
{
$sql="update news set status='expired' where news_id=$id";
$res=mysql_query($sql);
}
if($stats=='expired')
{
$sql="update news set status='active' where news_id=$id";
$res=mysql_query($sql);
}
}

$title=$_POST['txtTitle'];
$newsdetails=$_POST['txtNewsdetails'];
$cansave=$_POST['cansave'];
if($cansave=='')
{
		$cansave=$_GET['cansave'];
}
if($cansave==1)
{
if($mode=='add')
$sql="insert into news (title,newsdetails) values('$title','$newsdetails')";
else if($mode=='edit')
$sql="update news set title='$title',newsdetails='$newsdetails' where news_id=$id";
else if($mode=='delete')
{
$sql="delete from news where news_id='$id'";
}
$res=mysql_query($sql);

if($res)
{
if($mode=='add')
	$message="News added sucessfully";
else if($mode=='edit')
	$message="News edited sucessfully";
else if($mode=='delete')
	$fres=mysql_query($fsql);
else
	$message="Please try later";
echo '<meta http-equiv="refresh" content="0;url=news.php">';	
	
}
}
if($mode=='' || $mode=='stats' || $mode=='delete' || ($mode=='add'&&$cansave==1))
		  {
?>
 <table align="center" width="80%" class="ft" cellpadding="2">
          <tr class="ct" height="25"> 
            <td height="24" colspan="7">News</td>
          </tr>
		        <?php
	$total_records=mysql_num_rows($fres);
	$curpage=$_GET['curpage'];
	if(strlen($_GET['curpage']) == 0) $curpage=1;
	$start=($curpage-1) * 10;
	$end=10;
	if($curpage==''|| $curpage==1)
	$i=1;
	else $i=$_GET['sno']+1;
	$fsql.=" limit $start,$end";
	$fres=mysql_query($fsql);
 ?>
        <tr> 
          <td align="right" colspan="2"> 
            <?php
	if($curpage!=1) {
?>
            <a href="news.php?curpage=<?=($curpage-1);?>&sno=<?=($curpage*10)-20; ?>" id="link2">Prev</a> 
            | 
            <?php
}
?>
            <?php
if($total_records > ($start + $end)) {
?>
            <a href="news.php?curpage=<?=($curpage+1);?>&sno=<?=$curpage*10; ?>" id="link2">Next</a> <?php
}
?>
          </td>
        </tr>
		
		  <?php
		   while($frow=mysql_fetch_array($fres))
		  {
		  ?>
		    <tr><td ><?=$i; ?>.&nbsp;<?=$frow['title']; ?></td><td  colspan="3"><?=$frow['newsdetails']; ?>
		  	<td><font color="red"><?=$frow['status']; ?></font>&nbsp;&nbsp;(<a href="news.php?mode=stats&id=<?=$frow['news_id']; ?>" id="link2">Change</a>)</td><td ><a href="news.php?mode=edit&id=<?=$frow['news_id']; ?>" id="link2">Edit</a></td>
			<td ><a href="news.php?cansave=1&mode=delete&id=<?=$frow['news_id']; ?>" id="link2" onClick="javascript:return condelete();">Delete</a></td></tr>
 		<tr></td></tr>
		<?php 
		
		$i++;
		} 
		
		?>
 		<tr><td  colspan="3" align="center">
		<input   onclick="location.href='news.php?mode=add'" type="button" name="btnadd" value=" add "/>
		</td></tr>		 
        </table>
		<?php } 
		else if($mode=='add' || $mode=='edit')
		{
		
		?>
		<form name="frm" method="post" onSubmit="return validate();">
		  <table align="center" width="80%" class="ft" cellpadding="2">
		  <tr><td colspan="2" align="center"><font face="Arial, Helvetica, sans-serif" style="font-weight:bold"><?=$message; ?></font></td></tr>
            <tr class="ct" height="25">
              <td width="20%" ><div align="right">Title</div></td>
              <td width="80%" ><input type="text" class="bluetext"name="txtTitle" size="48" value="<?=$erow['title']; ?>"></td>
            </tr>
            <tr>
              <td class="ct" height="25" valign="top"><div align="right">Newsdetails</div></td>
              <td   ><textarea name="txtNewsdetails" cols="35" rows="10"><?=$erow['newsdetails']; ?></textarea></td>
            </tr>
            <tr >
              <td align="center" colspan="2"><input type="hidden" name="cansave" value="0"><input type="submit" class="button"   name="submit" value="Submit" ></td>
            </tr>
          </table>
		</form>
		<?php
		}
 		?><?php
require 'include/footer.php';
?>
<script language="JavaScript">
function condelete()
{
var confrm;
confrm=window.confirm("Are You sure you want to delete this news");
return confrm;
}
function validate()
{
form=document.frm;
if(form.txtTitle.value=="")
	{
	alert("Please enter the title");
	form.txtTitle.focus();
	return false;
	}
if(form.txtNewsdetails.value=="")
	{
	alert("Please enter the newsdetails");
	form.txtNewsdetails.focus();
	return false;
	}
form.cansave.value=1;
return true;
}
</script>