<?php
ob_start();
/* 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 (get_magic_quotes_gpc()) {
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE); 
} 

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


include "../includes/htmltop.php";

if(trim($_POST['txtCouponStartDate'])<>""){
$txtCouponStartDate=$_POST['txtCouponStartDate'];
$txtCouponStartDate=explode('/',$txtCouponStartDate);
$fromDate=mktime(0, 0, 0, $txtCouponStartDate[0], $txtCouponStartDate[1], $txtCouponStartDate[2]);
}
if(trim($_POST['txtCouponEndDate'])<>""){
$txtCouponEndDate=$_POST['txtCouponEndDate'];
$txtCouponEndDate=explode('/',$txtCouponEndDate);
$toDate=mktime(0, 0, 0, $txtCouponEndDate[0], $txtCouponEndDate[1], $txtCouponEndDate[2]);
}


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

	/*check whether coupon with the same name exists in the database*/	
 	$select_coupon = "SELECT * FROM ".$tableprefix."couponcode WHERE LOWER(ccCode) = ".GetSQLValueString(strtolower($_POST['txtCouponCode']),"text");
	$result_coupon = mysql_query($select_coupon) or die(mysql_error());
	if(mysql_num_rows($result_coupon)>0)
	{
		$message = CC_EXISTS;
	}
		elseif($fromDate>$toDate)
	{
		$message = END_DATE_ERROR;
	}
	else
	{										
	$txtCouponStartDate=$_POST['txtCouponStartDate'];
	$txtCouponStartDate=explode('/',$txtCouponStartDate);
	$txtCouponStartDate=$txtCouponStartDate[2]."-".$txtCouponStartDate[0]."-".$txtCouponStartDate[1];
	
	$txtCouponEndDate=$_POST['txtCouponEndDate'];
	$txtCouponEndDate=explode('/',$txtCouponEndDate);
	$txtCouponEndDate=$txtCouponEndDate[2]."-".$txtCouponEndDate[0]."-".$txtCouponEndDate[1];

	
 	$insert_gift_coupon = "INSERT INTO ".$tableprefix."couponcode(ccStartDate, ccEndDate, ccCode, ccDescription, ccDiscount,subscriptionStatus,ccStatus)
										   VALUES(".GetSQLValueString($txtCouponStartDate,"text").","
													.GetSQLValueString($txtCouponEndDate,"text").","
													.GetSQLValueString($_POST['txtCouponCode'],"text").","
													.GetSQLValueString($_POST['txtCouponDescription'],"text").","
													.GetSQLValueString($_POST['txtCouponDiscount'],"text").",'Y','Y')";
																				
					mysql_query($insert_gift_coupon) or die(mysql_error());
					
					$insert_id = mysql_insert_id();									
					header('Location:couponCodes.php?msg='.CC_ADDED_SUCC);
					exit();					  
						  
	}						  





}




?>
<script src="../js/calendar2.js"></script>
<script>
function checkAddCouponForm()
{

  var form_name = new Array('frmAddCoupon');		
  var display_name = new Array('Coupon Name');
  var count = 2;	

  if(check_fields('frmAddCoupon','ccDiscount'))	
  {
  
  	 if(trimSpaces(document.frmAddCoupon.txtCouponCode.value==""))
	 {	 
	 	alert('<?php echo PLS_ENTER_CC;?>');
		document.frmAddCoupon.txtCouponCode.focus();
		return false;		 	 
	 }  
         
         if(trimSpaces(document.frmAddCoupon.txtCouponCode.value)!='')
             {
                 if(isSpclChar(trimSpaces(document.frmAddCoupon.txtCouponCode.value)))
                 {
                	alert('<?php echo SPECIAL_CHARA_NOT_ALLOWED;?>');
                     return false;
                 }
             }

	
 if(trimSpaces(document.frmAddCoupon.txtCouponStartDate.value==""))
	 {	 
	 alert('<?php echo PLS_START_DATE;?>');
		document.frmAddCoupon.txtCouponStartDate.focus();
		return false;		 	 
	 } 	 
 if(trimSpaces(document.frmAddCoupon.txtCouponEndDate.value==""))
	 {	 
	 alert('<?php echo PLS_END_DATE;?>');
		document.frmAddCoupon.txtCouponEndDate.focus();
		return false;		 	 
	 } 	 	 

 if(!check_numeric(trimSpaces(document.frmAddCoupon.txtCouponDiscount.value)))
	 {
	 
	 alert('<?php echo PLS_ENTER_NUM;?>');
		document.frmAddCoupon.txtCouponDiscount.focus();
		return false;		 
	 
	 }
	 if(trimSpaces(document.frmAddCoupon.txtCouponDiscount.value==""))
	 {	 
		 alert('<?php echo PLS_ENTER_DISCOUNT;?>');
		document.frmAddCoupon.txtCouponDiscount.focus();
		return false;		 	 
	 } 	 
  }
  else
  {
  
  		return false;
  
  }
   
}

</script>

		<body onLoad="document.getElementById('txtCouponCode').focus();">
		
		<center>
		
		<div align="left" id="layout">
		
		
				
				
				<!--header area start -->
<?php

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

?>
				
				<!--header area end -->
				
				
				
				
				<div align="left" class="main_body">
				<!--main body start -->
				
				
				
<?php
		include_once("../includes/admintopmenu.php")
?>		
																					
								
					<div align="left" class="content_area">
					
					<!--content area start -->
							
					<div align="left" class="page_title"><?php echo ADD_CC;?></div>
					<div align="center" class="text_information grey_tr"><?php echo $message?></div>
            		<br>
											
					<div align="left">									
															
					<form name="frmAddCoupon" action="#" method="POST" onSubmit="return checkAddCouponForm();">
					
					<table width="100%" border="0" cellpadding="0" cellspacing="0" class="manage_table">
					
						<tr><td colspan="3">&nbsp;</td></tr>
						
						<tr><td colspan="3" align="right"><span class="required">*</span>&nbsp;<?php echo FIELDS_ARE_MANDATORY;?></td></tr>
						<tr><td width="21%" align="left" valign=top><?php echo COUPON_CODE;?>&nbsp;<span class="required">*</span></td><td width="2%" >&nbsp;</td>
							<td width="77%" align="left" valign=top><input class="textbox" type="text" name="txtCouponCode" id="txtCouponCode" value="<?php echo $_POST['txtCouponCode'];?>" size="25" maxlength="150"></td>
						</tr>						
						<tr>
						  <td align="left" valign=top><?php echo DESCRIPTION;?>&nbsp;</td>
						  <td >&nbsp;</td>
							<td valign=top align="left">
								<textarea name="txtCouponDescription" rows="8" cols="30" class="textbox"><?php echo $_POST['txtCouponDescription'];?></textarea>
							</td>
						</tr>
						<tr><td width="21%" align="left" valign=top><?php echo START_DATE;?>&nbsp;<span class="required">*</span></td><td width="2%" >&nbsp;</td>
							<td width="77%" align="left" valign=top>
<input type="text" name="txtCouponStartDate" class="textbox" readonly="readonly" value="<?php $_POST['txtCouponStartDate'];?>">
										<a href="javascript:cal1.popup();"><img src="../images/cal.gif" width="16" height="16" border="0" Title="Click Here to Pick up From Date"></a>
							</td>
						</tr>	
						<tr><td width="21%" align="left" valign=top><?php echo END_DATE;?>&nbsp;<span class="required">*</span></td><td width="2%" >&nbsp;</td>
							<td width="77%" align="left" valign=top>
							<input type="text" name="txtCouponEndDate" class="textbox" readonly="readonly" value="<?php $_POST['txtCouponEndDate'];?>">
							<a href="javascript:cal2.popup();"><img src="../images/cal.gif" width="16" height="16" border="0" Title="Click Here to Pick up From Date"></a>
							</td>
						</tr>	
						<tr><td width="21%" align="left" valign=top><?php echo DISCOUNT;?>(%) &nbsp;<span class="required">*</span></td><td width="2%" >&nbsp;</td>
							<td width="77%" align="left" valign=top><input class="textbox" type="text" name="txtCouponDiscount" value="<?php echo $_POST['txtCouponDiscount'];?>" size="25" ></td>
						</tr>																	
						<tr>
							<td colspan="3" align="center">
								<input type="button" class="button" name="back" value="&nbsp;<?php echo BACK;?>&nbsp;" onClick="Javascript:history.go(-1)">
								<input type="submit" value="&nbsp;<?php echo ADD_TEXT;?>&nbsp;" name="add" class="button">
							</td>
						</tr>
						<tr><td colspan="3">&nbsp;</td></tr>
					</table>
					
				</form>
				
				</div>
									
				<!--content area end-->
								
				</div>
				
								
					<div align="left" class="clear_float"><img src="<?php echo $imagefolder;?>/clear.gif" alt="" width="1" height="1" /></div>
				<!--main body end -->
				</div>
<script>
 var cal1 = new calendar2(document.forms['frmAddCoupon'].elements['txtCouponStartDate']);
	 cal1.year_scroll = true;
	 cal1.time_comp = false;

var cal2 = new calendar2(document.forms['frmAddCoupon'].elements['txtCouponEndDate']);
	 cal2.year_scroll = true;
	 cal2.time_comp = false;		 	
	 
</script>				
				
<?php
	include "../includes/footer.php";
	
	ob_end_flush();
?>