<?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";
include_once "../classes/cls_products.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;
}
$artistlist = makeArtistList();

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

$image_flag = 0;

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

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

if($_POST['coupon_type']=="product"){
     if($_POST['product_id']!=''){
            $product_ids  = implode(',',$_POST['product_id']);
        }else {
            $product_ids ='';
        }
    //$txtProduct = $_POST['product_id'];
    $couponTypeQuery = ", productId = ".GetSQLValueString($product_ids,"text");
}elseif($_POST['coupon_type']=="category"){
    //$txtCategory = $_POST['category_id'];
    if($_POST['category_id']!=''){
            $product_cat_ids  = implode(',',$_POST['category_id']);
        }else {
            $product_cat_ids ='';
        }
    $couponTypeQuery = ", categoryId = ".GetSQLValueString($product_cat_ids,"text");
}            
$txtCouponStartDate=$_POST['ccStartDate'];
$txtCouponStartDate=explode('/',$txtCouponStartDate);
$txtCouponStartDate=$txtCouponStartDate[2]."-".$txtCouponStartDate[0]."-".$txtCouponStartDate[1];

$txtCouponEndDate=$_POST['ccEndDate'];
$txtCouponEndDate=explode('/',$txtCouponEndDate);
$txtCouponEndDate=$txtCouponEndDate[2]."-".$txtCouponEndDate[0]."-".$txtCouponEndDate[1];
	
 $update_gift_coupon = "UPDATE ".$tableprefix."couponcode
						  	   SET ccStartDate = ".GetSQLValueString($txtCouponStartDate,"text").",
						  	   	   ccEndDate = ".GetSQLValueString($txtCouponEndDate,"text").",
								   ccCode = ".GetSQLValueString($_POST['ccCode'],"text").",		
								   ccDescription = ".GetSQLValueString($_POST['ccDescription'],"text").",	
                                                                   sellerId = ".GetSQLValueString($_POST['sellerId'],"text").",    
								   ccDiscount = ".GetSQLValueString($_POST['ccDiscount'],"text");
	$update_gift_coupon .= $couponTypeQuery;
        $update_gift_coupon .= " WHERE ccID = ".$_REQUEST['edit_id'];
//	echo	$update_gift_coupon .= " WHERE ccID = ".$_REQUEST['edit_id'];								   

		$message = CC_UPDATE_SUCCESS;
					  
						  
	
		
		mysql_query($update_gift_coupon) or die(mysql_error());
	
		if($image_flag == 0)
		{	
			header("Location:couponCodes.php?".$_GET['qstring']."&txtSearch=".$_GET['txtSearch']."&msg=".$message);	
		}	
		
				  


	}


}



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

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

  if(check_fields('frmEditCoupon','ccDiscount'))	
  {
  
  	 if(trimSpaces(document.frmEditCoupon.ccCode.value==""))
	 {	 
	 	alert('Please Enter A Coupon Code.');
		document.frmEditCoupon.ccCode.focus();
		return false;		 	 
	 }
          if(trimSpaces(document.frmEditCoupon.ccCode.value)!='')
             {
                 if(isSpclChar(trimSpaces(document.frmEditCoupon.ccCode.value)))
                 {
                      alert("<?php echo SPECIAL_CHARA_NOT_ALLOWED;?> ");
                     return false;
                 }
             }
             
      
        if($('#coupon_type').val()=="seller"){
            if(trimSpaces(document.frmEditCoupon.sellerId.value==""))
            {	 
                   alert('<?php echo PLS_SELECT_SELLER;?>');
                   document.frmEditCoupon.sellerId.focus();
                   return false;		 	 
            }
        }else if($('input[name=coupon_type]:checked').val()=="category"){
           
            var size = $('#category_id option').size();
           
              if(size=='0'){
                     alert('<?php echo PLS_SELECT_CATEGORY;?>');
                     $("#category_id").focus();
                     return false;
               }
            
            
        }else if($('input[name=coupon_type]:checked').val()=="product"){
        
            var size = $('#product_id option').size();
           
              if(size=='0'){
                     alert('<?php echo PLS_SELECT_PRODUCT;?>');
                     $("#product_id").focus();
                     return false;
               }
                   
        }    
         
	 if(!check_numeric(trimSpaces(document.frmEditCoupon.ccDiscount.value)))
	 {
	 
	 	alert('<?php echo PLS_ENTER_NUM;?>');
		document.frmEditCoupon.ccDiscount.focus();
		return false;		 
	 
	 }
	 if(trimSpaces(document.frmEditCoupon.ccDiscount.value==""))
	 {	 
	 	alert('<?php echo PLS_ENTER_DISCOUNT;?>');
		document.frmEditCoupon.ccDiscount.focus();
		return false;		 	 
	 } 
 if(trimSpaces(document.frmEditCoupon.ccStartDate.value==""))
	 {	 
	 	alert('<?php echo PLS_START_DATE;?>');
		document.frmEditCoupon.ccStartDate.focus();
		return false;		 	 
	 } 	 
 if(trimSpaces(document.frmEditCoupon.ccEndDate.value==""))
	 {	 
	 	alert('<?php echo PLS_END_DATE;?>');
		document.frmEditCoupon.ccEndDate.focus();
		return false;		 	 
	 } 	 	 

  }
  else
  {
  
  		return false;
  
  }
   
}

$(document).ready(function(){
   $("#txtCouponDiscount").keydown(function (e) {
       // Allow: backspace, delete, tab, escape, enter and .
       if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
            // Allow: Ctrl+A
           (e.keyCode == 65 && e.ctrlKey === true) || 
            // Allow: home, end, left, right, down, up
           (e.keyCode >= 35 && e.keyCode <= 40)) {
                // let it happen, don't do anything
                return;
       }
       // Ensure that it is a number and stop the keypress
       if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
           e.preventDefault();
       }
   });
});

</script>

		<body>
		
		<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 EDIT_CC;?></div>
					<div align="center" class="text_information grey_tr"><?php echo $message?></div>
            		<br>
											
					<div align="left">									
					
					<form name="frmEditCoupon" action="#" method="POST" enctype="multipart/form-data" 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" class="message" align="left"><?php echo $message_image;?></td></tr>
					<tr><td colspan="3" align="right"><span class="required">*</span>&nbsp;<?php echo FIELDS_ARE_MANDATORY;?></td></tr>
																											
<?php

	$select_couponCode = "SELECT ccID,ccStartDate,ccEndDate,sellerId,ccCode,ccDescription,ccDiscount,subscriptionStatus,ccStatus,coupon_type,sellerId,categoryId,productId
								   FROM ".$tableprefix."couponcode
								   WHERE ccID = ".$_GET['edit_id'];
 	
							   
								   
	$result_couponCode = mysql_query($select_couponCode) or die(mysql_error());
	$result_couponCode_row = mysql_fetch_assoc($result_couponCode);	

	
$txtCouponStartDate=explode('-',$result_couponCode_row['ccStartDate']);
$txtCouponStartDate=$txtCouponStartDate[1]."/".$txtCouponStartDate[2]."/".$txtCouponStartDate[0];
$txtCouponEndDate=explode('-',$result_couponCode_row['ccEndDate']);
$txtCouponEndDate=$txtCouponEndDate[1]."/".$txtCouponEndDate[2]."/".$txtCouponEndDate[0];
$categoryList =    Products::getCategory_list_Seller($result_couponCode_row['sellerId']);      
$productList  =    Products::getProduct_list_Seller($result_couponCode_row['sellerId']);

?>																											
																											
																											
																											
					<tr><td width="26%" align="left" valign=top><?php echo COUPON_CODE;?>&nbsp;<span class="required">*</span></td><td width="2%" >&nbsp;</td>
						<td width="72%" align="left" valign=top><input class="textbox" type="text" name="ccCode" value="<?php echo $result_couponCode_row['ccCode']?>" size="25" maxlength="150"></td>
					</tr>                                        
                                        <?php
                                        $properties1 = 'multiple = "multiple"';
                                        if($result_couponCode_row['coupon_type']!="seller"){ ?>  
                                          <tr>
                                            <td align="left" ><?php echo SELLER; ?>&nbsp;<span class="required">*</span></td><td >&nbsp;</td>
                                            <td align="left">
                                            <?php
                                            echo makeDropDownList("sellerId",$artistlist,$result_couponCode_row['sellerId'], false, "textarea", "disabled", $behaviors);
                                            ?>
                                            <input type="hidden" id="sellerId" name="sellerId" value="<?php echo $result_couponCode_row['sellerId']; ?>">     
                                            </td>
                                         </tr>
                                        <?php } ?>
                                        <?php 
                                        
                                        if($result_couponCode_row['coupon_type']=="seller"){ ?>
                                        <tr>
                                            <td align="left" ><?php echo SELLER; ?>&nbsp;<span class="required">*</span></td><td >&nbsp;</td>
                                            <td align="left">
                                            <?php
                                            echo makeDropDownList("sellerId",$artistlist, $result_couponCode_row['sellerId'], false, "textarea", $properties, $behaviors);
                                            ?>
                                            </td>
                                         </tr>
                                        <?php }elseif($result_couponCode_row['coupon_type']=="category"){
                                            $selectedArray = explode(',',$result_couponCode_row['categoryId']);
                                           
                                            ?>
                                        <tr id="category_div" ><td align="left" ><?php echo CATEGORIES; ?>&nbsp;<span class="required">*</span></td><td >&nbsp;</td>
                                            <td align="left">
                                            <?php
                                            echo makeDropDownList("category_id[]",$categoryList, $selectedArray, false, "textarea", $properties1, $behaviors,'category_id');
                                            ?>
                                            </td>
                                        </tr>   
                                        <?php }elseif($result_couponCode_row['coupon_type']=="product"){
                                             $selectedArray = explode(',',$result_couponCode_row['productId']);
                                            ?>
                                        <tr id="product_div" ><td align="left" ><?php echo PRODUCTS; ?>&nbsp;<span class="required">*</span></td><td >&nbsp;</td>
                                            <td align="left">
                                            <?php
                                            echo makeDropDownList("product_id[]",$productList, $selectedArray, false, "textarea", $properties1, $behaviors,'product_id');
                                            ?>
                                            </td>
                                        </tr>    
                                        <?php } ?>
					<tr>
					  <td align="left" valign=top><?php echo DESCRIPTION;?>&nbsp;</td>
					  <td >&nbsp;</td>
						<td valign=top align="left">
							<textarea name="ccDescription" rows="8" cols="30" class="textbox"><?php echo $result_couponCode_row['ccDescription']?></textarea>
							<input type="hidden" name="edit_id" value="<?php echo $_REQUEST['edit_id']?>">
						</td>
					</tr>
<tr><td width="26%" align="left" valign=top><?php echo START_DATE;?>&nbsp;<span class="required">*</span></td><td width="2%" >&nbsp;</td>
						<td width="72%" align="left" valign=top>
						<input type="text" name="ccStartDate" class="textbox" readonly="readonly" value="<?php echo $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="26%" align="left" valign=top><?php echo END_DATE;?>&nbsp;<span class="required">*</span></td><td width="2%" >&nbsp;</td>
						<td width="72%" align="left" valign=top>
						<input type="text" name="ccEndDate" class="textbox" readonly="readonly" value="<?php echo $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="26%" align="left" valign=top><?php echo DISCOUNT;?>(%)&nbsp;<span class="required">*</span></td><td width="2%" >&nbsp;</td>
    <td width="72%" align="left" valign=top><input class="textbox" type="text" name="ccDiscount" id="txtCouponDiscount" value="<?php echo $result_couponCode_row['ccDiscount']?>" size="25" maxlength="150"></td>
					</tr>															
					<tr>
						<td colspan="3" align="center">
                                                        <input type="hidden" id="coupon_type" name="coupon_type" value="<?php echo $result_couponCode_row['coupon_type']; ?>"> 
							<input type="button" class="button" name="back" value="&nbsp;<?php echo BACK;?>&nbsp;" onClick="window.location='couponCodes.php'">
							<input type="submit" value="<?php echo UPDATE;?>" name="update" 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['frmEditCoupon'].elements['ccStartDate']);
	 cal1.year_scroll = true;
	 cal1.time_comp = false;

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