<?php

include("../includes/site_config.php");
include('../session_handler.inc');

$area = 'member';

	if(isset($_REQUEST['submit'])){
	
		$code=form_get('code');
			$days=form_get('days');
				$profiles=form_get('profiles');
			$ins="INSERT INTO coupons set coupon_number='$code', days='$days', use_limit='$profiles'";
		mysql_query($ins);

	}

if($_REQUEST['co_id']){
		$code_id=form_get('co_id');
		mysql_query("DELETE from coupons where md5(coupon_id)='$code_id'");
	}

	
?>
<script>
function valCoupon(){
if(document.coupForm.code.value.length<5 || document.coupForm.code.value.length >5)
		{
		alert('<?=ALERT_COUPON_CODE?>');
		document.coupForm.code.focus();
		return false;	
		}
		
if(document.coupForm.days.value=='')
		{
		alert('<?=ALERT_COUPON_DAYS_CODE?>');
		document.coupForm.days.focus();
		return false;	
		}		

if(document.coupForm.profiles.value=='')
		{
		alert('<?=ALERT_COUPON_PROFILES?>');
		document.coupForm.profiles.focus();
		return false;	
		}	
}

</script>

<?php include("include/header.php"); ?><div class="mainwidth">
<div class="admin-page-header"><div class="admin-page-heading">&nbsp;&nbsp;<?=COUPON_ADMIN?></div></div>
<div class="clear"></div>
<div class="admin-page">
<table width="100%" align="<?php print("$CONST_TABLE_ALIGN"); ?>" border="0" cellspacing="<?php print("$CONST_TABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_TABLE_CELLPADDING"); ?>"><!--DWLayoutTable-->
<form action="<?=$_SERVER['PHP_SELF']?>" name="coupForm" method="post">
<tr><td>
<table width="350" border="0">
  <tr>
    <td><strong>
      <?=COUPON_CODE?>
      &nbsp;</strong></td>
    <td><input type="text" name="code" maxlength="5"></td>
  </tr>
  <tr>
    <td><strong>
      <?=COUPON_DAYS?>
      &nbsp;</strong></td>
    <td><input type="text" name="days"></td>
  </tr>
  <tr>
    <td><strong>
      <?=NUMBER_OF_PROFILE?>
      &nbsp;</strong></td>
    <td><input type="text" name="profiles"></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="submit" value="Create" onclick="return valCoupon();"></td>
  </tr>
  </form>
</table>
</td></tr>
  <tr>
    <td width="10" height="3">
      <?php
	  
        $query = "  SELECT *
                    FROM coupons 
                 ";
$result=mysql_query($query, $link) or die(mysql_error());
$num = mysql_num_rows($result);
?>
<table width=100% align="left">
    <!--<tr align=left>
      <td align=center colspan=3><?=$num?> <?php echo GENERAL_FOUND_USERS ?></td>
    </tr>-->
    <tr align=left>
      <td><div align="center"><strong><?php echo COUPON_CODE ?></strong></div></td><td><div align="center"><strong><?php echo COUPON_DAYS ?></strong></div></td><td><div align="center"><strong>
        <?=NUMBER_OF_PROFILE?>
      </strong></div></td><td><div align="center"><strong>
        <?=USED_BY_PROFILE?>
      </strong></div></td><td><div align="center"></div></td>
	</tr>
<?
while ($row = mysql_fetch_object($result)){?>
    <tr>
    <td width="20%"><div align="center">
      <?=$row->coupon_number?>
    </div></td><td width=20%><div align="center">
      <?=$row->days?>
    </div></td><td width=20%><div align="center">
      <?=$row->use_limit?>
    </div></td><td width=20%><div align="center">
      <?=$row->usedBy_profiles?>
    </div></td><td><div align="center">
      <? if($DEMO==false){?>
      <a href="<?=$_SERVER['PHP_SELF']?>?co_id=<?=md5($row->coupon_id)?>" onClick="javascript:return confirm('Are you sure you want to Delete this Coupon?')">delete</a>
      <? }?>
    </div></td>      </tr>
<? } ?>
   <!-- <tr align=left>
      <td align=center colspan=3><?=$num?> <?php echo GENERAL_FOUND_USERS ?></td>
    </tr>-->
  </table>
    </td>
  </tr>
</table>
</div><div class="admin-page-bottom"></div></div>
<?php include("include/footer.php"); ?>