<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Programmer<programmer@armia.com>        		              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com � 2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
?>
<?php
include "../includes/config.php";
include "../includes/session.php";
include "../includes/functions.php";

if(isset( $_SESSION["sess_adminname"]) and $_SESSION["sess_adminname"]!= ""){
	$adminname = $_SESSION["sess_adminname"];
}else{
	header("Location:login.php");
	exit;
}




if(isset($_GET["txtSearch"]) and $_GET["txtSearch"]!="")
{
	$txtSearch = $_GET["txtSearch"];
	$cmbSearchType = $_GET["cmbSearchType"];
}
else if(isset($_POST["txtSearch"]) and $_POST["txtSearch"]!="")
{
	$txtSearch = $_POST["txtSearch"];
	$cmbSearchType = $_POST["cmbSearchType"];
}


/*---------Patch-----------*/


if(isset($_GET['msg']))
{
	$message = $_GET["msg"];
}
if(isset($_GET['msg1']))
{
	$message = CC_STATUS_CHANGED;
}

/*---------Patch-----------*/
if($_GET['delete_permanent'] == 'Y' && $_GET['delete_status']<>"")
{

	$delete_coupon = "DELETE FROM ".$tableprefix."couponcode WHERE ccID = ".$_GET['delete_status'];
	mysql_query($delete_coupon) or die(mysql_error());
	header("Location:".$_SERVER['PHP_SELF']."?msg=".COUPON_DELETED);

}



if(isset($_POST['gotoPage']))
{

	header("Location:".$_SERVER['PHP_SELF']."?page=".$_POST['gotoPage']);


}

$qryopt="";
if($txtSearch != "")
{


	if($cmbSearchType == "couponname")
	{
		$qryopt .= "  AND product_name  like '" . addslashes($txtSearch) . "%'";
	}
	if($cmbSearchType == "couponamount")
	{
		$qryopt .= "  AND product_price = '".$txtSearch."'";
	}
}

if(isset($_GET['status']))
{
	if($_GET['status']<>""){
	$sql_updateStatus="update ".$tableprefix."couponcode SET ccStatus='".trim($_GET['cmd'])."' WHERE ccID=".trim($_GET['status']);
	mysql_query($sql_updateStatus);
	header("Location:couponCodes.php?msg1=changed");
	exit;
	}

}


$sql = "SELECT ccID,ccStartDate,ccEndDate,ccCode,ccDescription,ccDiscount,subscriptionStatus,ccStatus
		FROM ".$tableprefix."couponcode ".
		$qryopt . " ORDER BY ccID DESC";
if(!(isset($pagenum)))
{
	$pagenum = 1;
}

$limit = getSettingsValue('npagination_limit');

$page = isset($_GET['page'])? trim($_GET['page']):1;


$page = $_GET['page'];

if(empty($page))
{
	$page = 1;
}

$result=mysql_query($sql) or die(mysql_error());

$totalrows = mysql_num_rows($result);

$limitvalue = ($page - 1) * $limit;

$query_string = " LIMIT $limitvalue, $limit";

$sql = $sql.$query_string;

$rs = mysql_query($sql) or die(mysql_error());

$numofpages = ceil($totalrows / $limit);


include "../includes/htmltop.php";
?>
<script language="JavaScript" type="text/JavaScript">
function clickSearch()
{

  var form_name = new Array('txtSearch');		
  var display_name = new Array('Search Keyword');
  var count = 1;	

  if(check_fields('frmGiftCoupon',form_name,display_name,count))	
  {
  
  	
	 document.frmGiftCoupon.submit();	
   
  }
  else
  {
  
  		return false;
  
  }
     
}


function blockCertificate(ddlval)
{


			
	if(ddlval == 'N')
	{

		return confirm('<?php echo DISABLE_CC;?>')
	
	}
	else if(ddlval == 'Y')
	{
	
		return confirm('<?php echo ENABLE_CC;?>')
	
	}	

}

function deleteCoupon()
{
	return confirm('<?php echo DELETE_CC;?>')
}

</script>

<body>

	<center>

		<div align="left" id="layout">




			<!--header area start -->
			<?php

			include_once("../includes/topheader_admin.php")

			?>

			<!--header area end -->




			<div align="right" class="main_body">
				<!--main body start -->



				<?php
				include_once("../includes/admintopmenu.php")
				?>


				<div align="right" class="content_area">

					<!--content area start -->

					<div align="left" class="page_title">
						<?php echo COUPON_CODES;?>
					</div>
					<div align="center" class="text_information_success grey_tr">
						<?php echo $message?>
					</div>
                                        
					<br>

					<div align="right">

						<form action="" method="get" name="frmGiftCoupon">

							<table width="100%" border="0" cellpadding="0" cellspacing="0"
								class="manage_top">
								<tr>
									
									<td width="75%" align="right" valign="middle"
										class=''>
										<!--								Search ::
									<select name="cmbSearchType" class="textbox">
									<option value="couponname" <?php if($cmbSearchType == "couponname" || $cmbSearchType == ""){ echo("selected"); } ?>>Certificate Name</option>
									<option value="couponamount"  <?php if($cmbSearchType == "couponamount" || $cmbSearchType == ""){ echo("selected"); } ?>>Amount</option>
									</select> &nbsp;
									<input type="text" name="txtSearch" size="20" maxlength="50" value="<?php echo(htmlentities($txtSearch)); ?>" class="textbox">
									&nbsp;
									<a style="cursor:pointer" onClick="return clickSearch();"><img src='<?php echo $imagefolder;?>/go.jpg' border='0'></a>
									-->
									</td>
                                                                        <td align="right"><a href="couponCode_add.php"
                                                                                             style="cursor: pointer" class="add_new_button"><?php echo BTN_ADD_NEW; ?></a></td>
								</tr>
							</table>


						</form>

					</div>

					<div align="left">

						<form action="" method="post" name="frmGiftCoupon1">

							<table border="0" width="100%" cellpadding="0" cellspacing="0"
								class="manage_table">

								<tr class="">
									<th width="4%" align="right">#</th>
									<th width="15%" align="right" class=""><?php echo COUPON_CODE;?>
									</th>
									<th width="21%" align="right"><?php echo DESCRIPTION;?></th>
									<th width="14%" align="right"><?php echo START_DATE;?></th>
									<th width="13%" align="right"><?php echo END_DATE;?></th>
									<th width="15%" align="right"><?php echo DISCOUNT;?>(%)</th>
									<th width="9%" align="right"><?php echo STATUS;?></th>
									<th width="9%" align="right"><?php echo ACTIONS;?></th>
								</tr>
								<?php
								if(mysql_num_rows($result) > 0 )
								{
									$i = 1;

									while($row = mysql_fetch_array($rs))
									{


										$txtCouponStartDate=explode('-',$row["ccStartDate"]);
										$txtCouponStartDate=$txtCouponStartDate[2]."-".$txtCouponStartDate[1]."-".$txtCouponStartDate[0];

										$txtCouponEndDate=explode('-',$row["ccEndDate"]);
										$txtCouponEndDate=$txtCouponEndDate[2]."-".$txtCouponEndDate[1]."-".$txtCouponEndDate[0];

										?>
								<tr class="">
								<td align="right"><?php echo (($page*$limit)+$i-$limit)?></td>
								<td align="right"><?php echo $link .htmlentities($row["ccCode"])."</a>";?>
									</td>
									<td align="right"><?php 
									if(strlen(htmlentities($row["ccDescription"]))>20){
									echo "<span title='".htmlentities(stripslashes($row["ccDescription"]))."'>".substr(htmlentities(stripslashes($row["ccDescription"])),0,20)."....</span>";
									}
									else
										echo (stripslashes($row["ccDescription"]));
									?>
									</td>
									<td align="right"><?php echo $txtCouponStartDate; ?></td>
									<td align="right"><?php echo $txtCouponEndDate; ?></td>
									<td align="center" colspan=""><?php echo $row["ccDiscount"]; ?>
									</td>								
									<td align="center"><?php if($row["ccStatus"]=="Y") 
									{
											
										if($row["ccEndDate"]<date('Y-m-d'))
										{

											echo EXPIRED;

										}
										else
										{
											echo "<a href='couponCodes.php?cmd=N&status=".$row["ccID"]."' onClick='Javascript:return blockCertificate('N');><font color='#009966'><strong>".ACTIVE."</strong></font></a>";

										}
									}
									else  { echo "<a href='couponCodes.php?cmd=Y&status=".$row["ccID"]."' onClick='Javascript:return blockCertificate('Y');'> <font color='#FF0000'><strong>".INACTIVE."</strong></font>";
} ?></td>
									<td align="center"><a
										href="couponCode_edit.php?edit_id=<?php echo $row["ccID"]?>&qstring=<?php echo $_SERVER['QUERY_STRING']?>"><img
											src="../images/btn_edit.gif" border="0" title="Edit"> </a>&nbsp;/&nbsp;<a
										href="couponCodes.php?delete_status=<?php echo $row['ccID']?>&delete_permanent=Y"
										style="cursor: pointer"
										onClick="Javascript:return deleteCoupon('<?php echo $row['ccID']?>')"><img
											src="../images/btn_del.gif" border="0" title="Delete"> </a></td>
							
								</tr>
								<?php
								$i++;

									}

								}
								else
								{

									?>
								<tr>
									<td align="center" valign="middle" colspan="9"
										class="text_information"><?php echo NO_RECORDS_FOUND;?> !!</td>
								</tr>
								<?php										
								}


								if($page > 1)
								{
									$pageprev = $page-1;

									$previous_page = "<a href=".$_SERVER['PHP_SELF']."?page=$pageprev alt='Previous' border='0' class='small_gray_button'>".TEXT_PREV."</a>";

								}
									

								if($page < $numofpages)
								{
									$pagenext = ($page + 1);

									$next = "<a href=".$_SERVER['PHP_SELF']."?page=$pagenext alt='Next' border='0' class='small_gray_button'>".TEXT_NEXT."</a>";

								}




							 if($page < $numofpages || $page > 1)
							 {
							  ?>
								<tr>
									<td align="right" valign="middle" class="page_nos" colspan="8">
										<table width="100%" border="0" cellspacing="10" cellpadding="0">
											<tr align="left" valign="middle">
												<td width="21%" align="right"><?php echo $previous_page?></td>
												<td width="23%" align="right">
													<?php echo PAGE."(".S.")";?> <?php echo $page ?>/<?php echo $numofpages?>&nbsp;&nbsp;<?php echo GO_TO;?>
												</td>
												<td width="8%" align="right">
													<select name="gotoPage" style="padding:4px 5px; margin-bottom:1px; ">
											<?php
											for($i=1;$i<=$numofpages;$i++)
											{


												if($i == $_GET['page'])
												{
													$page_selected = "selected";
												}
												else
												{
													$page_selected = "";
												}


												?>
											<option value="<?php echo $i?>" <?php echo $page_selected?>>
												<?php echo $i?>
											</option>

											<?php
											}

											?>
									</select>
												</td>
												<td width="7%">
													<a onClick="Javascript:document.frmGiftCoupon1.submit();" class="go_button_2"><?php echo GO; ?></a>
												</td>
												<td width="41%" align="center"><?php echo $next?></td>
											</tr>
										</table>
										 
									</td>
								</tr>
								<?php
							 }
							 ?>
							</table>
						</form>



					</div>


					<!--content area end-->

				</div>


				<div align="right" class="clear_float">
					<img src="<?php echo $imagefolder;?>/clear.gif" alt="" width="1"
						height="1" />
				</div>
				<!--main body end -->
			</div>


			<?php
			include "../includes/footer.php";
			?>