<?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 faq";
$fres=mysql_query($fsql);
if($mode=='edit')
{
$eres=mysql_query("select * from faq where faq_id=$id");
$erow=mysql_fetch_array($eres);
}
$question=$_POST['txtQuestion'];

$question=str_replace("'" , "\'" , $question);

$answer=stripslashes( $_POST['txtAnswer']);
$answer=str_replace("'" , "\'" , $answer);

$cbocat=$_POST['cbocat'];
$cansave=$_POST['cansave'];
if($cansave=='')
{
		$cansave=$_GET['cansave'];
}
if($cansave==1)
{
if($mode=='add')
$sql="insert into faq (question,answer,h_cat) values('$question','$answer',$cbocat)";
else if($mode=='edit')
$sql="update faq set question='$question',answer='$answer',h_cat=$cbocat where faq_id=$id";
else if($mode=='delete')
{
$sql="delete from faq where faq_id='$id'";
}
$res=mysql_query($sql);

if($res)
{
if($mode=='add')
	$message="Faq added sucessfully";
else if($mode=='edit')
	$message="Faq edited sucessfully";
else if($mode=='delete')
	$fres=mysql_query($fsql);
else
	$message="Please try later";
echo '<meta http-equiv="refresh" content="0;url=help.php">';	
	
}
}
if($mode=='' || $mode=='delete' || ($mode=='add'&&$cansave==1))
		  {
?>
 <table align="center" width="80%" class="ft" cellpadding="2">
          <tr class="ct" height="25"> 
            <td height="24" colspan="3">Faq</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="help.php?curpage=<?=($curpage-1);?>&sno=<?=($curpage*10)-20; ?>" id="link2">Prev</a> 
            | 
            <?php
}
?>
            <?php
if($total_records > ($start + $end)) {
?>
            <a href="help.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['question']; ?></td>
		  	<td ><a href="help.php?mode=edit&id=<?=$frow['faq_id']; ?>" id="link2">Edit</a></td>
			<td ><a href="help.php?cansave=1&mode=delete&id=<?=$frow['faq_id']; ?>" id="link2" onClick="javascript:return condelete();">Delete</a></td></tr>
 		<tr><td  colspan="3"><?=$frow['answer']; ?></td></tr>
		<?php 
		
		$i++;
		} 
		
		?>
 		<tr><td  colspan="3" align="center">
		<input   onclick="location.href='help.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="95%" class="ft" cellpadding="2">
		  <tr><td colspan="2" align="center"><font face="Arial, Helvetica, sans-serif" style="font-weight:bold"><?=$message; ?></font></td></tr>
        
<?
 $sql="select * from help_categories"; 
 $res=mysql_query($sql);
?>
    
<tr height="25">
              <td width="20%" ><div align="right">Category</div></td>
              <td width="80%" ><select name=cbocat>
<?  
$h_cat = $erow['h_cat'];
 while($rec=mysql_fetch_array($res))
{ 
$category_id=$rec['category_id'];
?>
<option value="<?= $category_id ?>" <?if($category_id==$h_cat){ echo "selected";} ?>><?= $rec['category_name']?></option>
<?
}
?>
</td>
            </tr>       


<tr height="25">
              <td width="20%" ><div align="right">Question</div></td>
              <td width="80%" ><input type="text" class="bluetext"name="txtQuestion" value="<?=$erow['question']; ?>"></td>
            </tr>
            <tr>
              <td height="25" valign="top"><div align="right">Answer</div></td></tr><tr>
              <td colspan="2"  >
 <?php
include_once("../HTMLEditor/fckeditor.php") ;
?>
<?php
$oFCKeditor = new FCKeditor('txtAnswer') ;
$oFCKeditor->BasePath = '../HTMLEditor/' ;
$oFCKeditor->Value = $erow['answer'];
$oFCKeditor->Create() ;
?>

</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 faq");
return confrm;
}
function validate()
{
form=document.frm;
if(form.txtQuestion.value=="")
	{
	alert("Please enter the question");
	form.txtQuestion.focus();
	return false;
	}
if(form.txtAnswer.value=="")
	{
	alert("Please enter the answer");
	form.txtAnswer.focus();
	return false;
	}
form.cansave.value=1;
return true;
}
</script>