<?php
include_once("../include/main.inc.php");
check_session('MyCpanel','index.php');

################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start)==0 or $start=="")?$start=0:$start;
$pagesize = intval($pagesize)==0?$pagesize=20:$pagesize;

$heading="Manage Enquiries";
$record_type="Enquiry";
$script_name="manage_enquiries.php";
$table_name="enquiry_tbl";
######################################################
if ($action=="Send Mail"){
	for ($x=0; $x < sizeof($_POST[cid]); $x++){
		$arr=$_POST[cid];
		$res=getResult($table_name," where slno='$arr[$x]'");
		$To=$res[email]; 
		$from=SITE_EMAIL;
		$fromName=getAdminname();
		$mess=ms_htmlentities_decode($_POST[message]);
		$message='Dear '.$res[personName].',<br/><br/>'.$mess.'<br /><br />Regards<br /><br />Customer Support Team<br />'.$company;
		send_email($fromName,$from,$To,$subject,$message);
		updateTable($table_name," status='Y' where slno='$arr[$x]'");
	}
	$_SESSION[session_message]='Mail has been sent to selected member(s)';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=$start&pagesize=$pagesize");
	exit;
}

if($id=="View"){
	adminheader_popup("View Enquiry");
	echo view_enquiry($slno);
	adminfooter_popup();
	exit;
}

if($action=="Delete"){
	for ($x=0; $x < sizeof($_POST[cid]); $x++){
		$arr=$_POST[cid];
		db_query("delete from $table_name where slno='$arr[$x]'");
		echo db_error();
	}
	$_SESSION[session_message]='Record has been deleted from the admin panel.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=$start&pagesize=$pagesize");
	exit;
}

if ($id=="delete_record") {
	db_query("delete from $table_name where slno='$recordID'");
	$_SESSION[session_message]='Record has been deleted from the admin panel.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=$start&pagesize=$pagesize");
	exit;
}

admin_header();
?>
<table width="99%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
 <tr>
	<td align="left" width="50%" class="myTitle"><?=$heading;?></td>
	<td align="right">
	 <form name="pgcolor_form" action="<?=$PHP_SELF?>" method="post" style="margin:0px;padding:0px;">
	 <table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
	  <tr>
	   <td align="right">Records Per Page:
	    <select name="pagesize" onchange="this.form.submit();">
	     <option value="0">Select</option>
	     <?php
	     foreach($_GLOBALS[pgsize_arr] as $val) {
		     if(trim($pagesize)==trim($val)) {
			     echo '<option value="'.$val.'" selected>'.$val.'</option>';
		     }
		     else{
			     echo '<option value="'.$val.'">'.$val.'</option>';
		     }
	     }
	     ?>
	    </select>
		 </td>
		</tr>
	 </table>
	 </form>
	</td>
 </tr>
</table>
<hr size="2" width="99%" color="#DA251C">

<form name="srch" method="post" action="" style="display:inline" enctype="multipart/form-data">
<table width="99%"  border="0" cellpadding="0" cellspacing="2">
 <tr>
  <td align="right"><strong>Search Enquiry </strong> [by name, email, phone, address etc.] :-
   <input type="text" name="keyword" value="<?=$keyword;?>" size="40">
   <input type="submit" class="button" value="GO">   
  </td>
 </tr>   
</table>
</form>

<?php
$cond="";
if (strlen($keyword)) {
	?>
	<br/>
	<div style="width:99%;">
	 <div style="float:left;"><strong>Search For :</strong> <?=$keyword;?></div>
	 <div style="float:right;"><a href="<?=SITE_ADMIN_URL;?>/<?=$script_name;?>"><strong>View All</strong></a></div>
	</div> 
	<?php
	$cond=" and (personName like '%$keyword%' or lastName like '%$keyword%' or email like '%$keyword%' or phone like '%$keyword%' or address like '%$keyword%')";
}
$qry1= db_query("select * from $table_name where status!='D'");
$reccnt = NumRows($qry1);
$qry= db_query("select * from $table_name where status!='D' order by slno desc limit $start, $pagesize");
echo pageNavigation($reccnt,$pagesize,$start);
print_message();

if($reccnt>0){
	?>
	<form name="formn" action="" method="post" enctype="multipart/form-data">
	<table width="99%" border="0" cellpadding="2" cellspacing="2" class="lightBorder">
	 <tr height="25" bgcolor="#EAEAEA">
	  <td width="5%" align="left"><input type="checkbox" name="chk" onclick="CheckAll()" class="NoStyle"></td>
	  <td width="40%" align="left"><strong>Sender Details</strong></td>
	  <td width="32%" align="left"><strong>Enquiry Details</strong></td>
	  <td width="13%" align="left"><strong>Receive Date</strong></td>
	  <td width="10%" align="left"><strong>Options</strong></td>
	 </tr>
	 <?php
	 $sl=$start+1;
	 while ($arr=mysql_fetch_array($qry)) {
		 $arr=ms_htmlentities_decode($arr);
		 if($sl%2==0){
			 $color="f0f0f0";
		 }
		 else{
			 $color="f9f9f9";
		 }
		 ?>
		 <tr bgcolor="<?=$color;?>" align="left">
		  <td valign="top"><input type=checkbox name="cid[]" value="<?=$arr[slno]?>" class="NoStyle"></td>
		  <td align="left" valign="top">
		   Name: <strong><?=ucwords($arr[personName]);?></strong>
		   <?php
		   if($arr[status]=="Y"){
			   ?>
			   <span class='icon'>Replied</span>
			   <?php
		   }
		   if($arr[companyName]!=""){
			   ?>
			   <br>Company Name: <strong><?=$arr[companyName];?></strong>
			   <?php
		   }
		   ?>
		   <br>Email: <?php echo $arr[email];	   
		   if($arr[phone]!=""){
			   ?>
			   <br>Phone: <?php echo $arr[phone];
		   }
		   if($arr[fax]!=""){
			   ?>
			   <br>Fax: <?php echo $arr[fax];
		   }
		   if($arr[address]!=""){
			   ?>
			   <br>Address: <?php echo $arr[address];
		   }
		   if($arr[city]!=""){
			   ?>
			   <br>City: <?php echo ($arr[city]);
		   }
		   if($arr[state]!=""){
			   ?>
			   <br>State: <?php echo $arr[state];
		   }
		   if($arr[country]!=""){
			   ?>
			   <br>Country: <?php echo $arr[country];
		   }
		   ?>
		  </td>
		  <td align="left" valign="top"><?=nl2br($arr[comments]);?></td>
		  <td align="left" valign="top"><?=show_date($arr[recvDate])?></td>
		  <td align="left" valign="top">
		   &nbsp;<a href="javascript:void(0);" onclick="window.open('<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=View&slno=<?=$arr[slno]?>&start=<?=$start?>&pagesize=<?=$pagesize?>','pop','height=350,width=530,scrollbars=yes,resizable=yes');"><img src="<?=SITE_ADMIN_URL?>/images/edit.gif" alt="View This <?=$record_type;?>" title="View This <?=$record_type;?>" border="0"></a>
		   &nbsp;<a href="<?=$script_name;?>?id=delete_record&recordID=<?=$arr[slno]?>&start=<?=$start?>&pagesize=<?=$pagesize?>" onClick="return confirm('Are you sure you want to delete this record')"><img src="<?=SITE_ADMIN_URL?>/images/cancel.gif" alt="Delete This <?=$record_type;?>" title="Delete This <?=$record_type;?>" border="0"></a>
		  </td>
		 </tr>
		 <?php
		 $sl++;
	 }
	 ?>
	 <tr>
	  <td colspan="5">
	   <input type="submit" name="action" value="Delete"  onclick="return chkdelete();" class="button">
	   <input type="hidden" name="start" value="<?=$start;?>">
	   <input type="hidden" name="pagesize" value="<?=$pagesize;?>">
	  </td>
	 </tr>
	</table><br>
	
	<table width="100%"  border="0" cellpadding="6" cellspacing="2" class="lightBorder">
	 <tr>
	  <td width="10%" align="left"><strong>Subject *</strong></td>
	  <td align="left"><input type="text" name="subject" value="" style="width:594px;"></td>
	 </tr>
	 <tr>
	  <td valign="top"><strong>Message *</strong></td>
	  <td><textarea name="message" cols=100 rows=10></textarea></td>
	 </tr>
	 <tr>
	  <td class="lightGrayBg">&nbsp;</td>
	  <td align="left" class="lightGrayBg"><input class="button" type="submit" name="action" value="Send Mail" onclick="return chksendmail(this);"></td>
	 </tr>
	</table>	
	</form>
	<script type="text/javascript">
	var oFCKeditor=new FCKeditor('message');
	oFCKeditor.BasePath="fckeditor/";
	oFCKeditor.Width="600px";
	oFCKeditor.Height="400px";
	oFCKeditor.ReplaceTextarea();
	</script>
	<?php
}
else {
	?>
	<table width="100%"  border="0" cellpadding="6" cellspacing="2" class="lightBorder">
	 <tr><td><br /><strong>No Record Found.............</strong><br /><br /></td></tr>
	</table>
	<?php
}

admin_footer();
exit;
?>