<?php

/*

osCommerce, Open Source E-Commerce Solutions 
http://www.oscommerce.com 

Copyright (c) 2003 osCommerce 

osCommRes, Services Online 
http://www.oscommres.com 
Copyright (c) 2005 osCommRes 

Freeway eCommerce 
http://www.zac-ware.com/freeway 
Copyright (c) 2007 ZacWare

Released under the GNU General Public License
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
  require('includes/application_top.php');
  require('includes/classes/split_page_results_event.php');
  $server_date = getServerDate(true);	
  require(DIR_WS_CLASSES . 'currencies.php');
  $currencies = new currencies();
   //ajax working 
  $command = (isset($HTTP_GET_VARS['command'])?$HTTP_GET_VARS['command']:'');
  $coupon_id = (isset($HTTP_GET_VARS['id'])?$HTTP_GET_VARS['id']:'');
  $page = (isset($HTTP_GET_VARS['page'])?$HTTP_GET_VARS['page']:'1');
  $type = (isset($HTTP_GET_VARS['type'])?$HTTP_GET_VARS['type']:'');
  $prev_ids = (isset($HTTP_GET_VARS['prev_ids'])?$HTTP_GET_VARS['prev_ids']:'');
  if($command !=''){
  	 if($command=='get_details'){
	 	get_default_details($coupon_id);
	 }else if($command=='check_ccode'){
	 	 echo 'ccode';
	 	 $ccode = (isset($HTTP_GET_VARS['ccode'])?$HTTP_GET_VARS['ccode']:'');
		 $coupon_code_check_query = tep_db_query("select coupon_code from " . TABLE_COUPONS ."  where coupon_code = '" . $ccode . "'");
		 if(tep_db_num_rows($coupon_code_check_query)>0) echo 'failed';
		 else echo 'success';
	 }else if($command=='page_navigation'){
	     echo 'page_navigation';
		 table_write();   
  	}else if($command=='tbl_write'){
		echo 'insert_newc';
		 table_write();
	}else if($command=='st_change'){
		 echo 'st_change';
		 table_write();			
    } else if($command=='edit'){
	 	if($coupon_id!="")echo 'edit';	
		else echo 'newc';
		$languages = tep_get_languages();
 		$tax_class_array = array(array('id' => '0', 'text' => TEXT_NONE));
    	$tax_class_query = tep_db_query("SELECT tax_class_id, tax_class_title from " . TABLE_TAX_CLASS . " order by tax_class_title");
    	while ($tax_class = tep_db_fetch_array($tax_class_query)) {
      		$tax_class_array[] = array('id' => $tax_class['tax_class_id'],
                                 'text' => $tax_class['tax_class_title']);
   		 }  
	if($command=='edit'){
			for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
			  $language_id = $languages[$i]['id'];
			  $coupon_query = tep_db_query("select coupon_name,coupon_description from " . TABLE_COUPONS_DESCRIPTION . " where coupon_id = '" .  $coupon_id . "' and language_id = '" . $language_id . "'");
			  $coupon = tep_db_fetch_array($coupon_query);
			  $coupon_name[$language_id] = $coupon['coupon_name'];
			  $coupon_desc[$language_id] = $coupon['coupon_description'];
		
			}
			$coupon_query=tep_db_query("select coupon_code, coupon_amount, coupon_tax_class_id,coupon_type, coupon_minimum_order, coupon_start_date, coupon_expire_date, uses_per_coupon, uses_per_user, restrict_to_products, restrict_to_categories ,restrict_to_event_categories,restrict_to_events,restrict_to_sub_categories,restrict_to_subscription,restrict_to_service,restrict_to_service_categories from " . TABLE_COUPONS . " where coupon_id = '" . $coupon_id . "'");
			$coupon=tep_db_fetch_array($coupon_query);
		
			$coupon_amount = $coupon['coupon_amount'];
			if ($coupon['coupon_type']=='P') {
			  $coupon_amount .= '%';
			}
			if ($coupon['coupon_type']=='S') {
			  $coupon_free_ship .= true;
			}     
			$coupon_min_order = $coupon['coupon_minimum_order'];
			$coupon_code = $coupon['coupon_code'];
			$coupon_uses_coupon = $coupon['uses_per_coupon'];
			$coupon_uses_user = $coupon['uses_per_user'];
			$coupon_tax_class_id=$coupon['coupon_tax_class_id'];
			$coupon_products = $coupon['restrict_to_products'];
			$coupon_startdate=$coupon['coupon_start_date'];
			$coupon_expiredate=$coupon['coupon_expire_date'];
			$coupon_categories = $coupon['restrict_to_categories'];    
			$coupon_event_categories=$coupon['restrict_to_event_categories'];
			$coupon_events=$coupon['restrict_to_events']; 
			$coupon_sub_categories=$coupon['restrict_to_sub_categories'];
			$coupon_subscription=$coupon['restrict_to_subscription']; 
			$coupon_service=$coupon['restrict_to_service'];
			$coupon_service_categories=$coupon['restrict_to_service_categories'];
	}	
	if($command =='back'){
			for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
			  $language_id = $languages[$i]['id'];
			  $coupon_name[$language_id] = $HTTP_POST_VARS['coupon_name'];
			  $coupon_desc[$language_id] = $HTTP_POST_VARS['coupon_description'];
			}
			$coupon_amount = $HTTP_POST_VARS['coupon_amount'];
			if ($HTTP_POST_VARS['coupon_type']=='P') {
			  $coupon_amount .= '%';
			}
			if ($HTTP_POST_VARS['coupon_type']=='S') {
			  $coupon_free_ship .= true;
			} 
			$coupon_min_order = $HTTP_POST_VARS['coupon_minimum_order'];
			$coupon_code = $HTTP_POST_VARS['coupon_code'];
			$coupon_uses_coupon = $HTTP_POST_VARS['uses_per_coupon'];
			$coupon_uses_user = $HTTP_POST_VARS['uses_per_user'];
			$coupon_tax_class_id=$HTTP_POST_VARS['coupon_tax_class_id'];
			$coupon_products = $HTTP_POST_VARS['restrict_to_products'];
			$coupon_startdate=$HTTP_POST_VARS['coupon_start_date'];
			$coupon_expiredate=$HTTP_POST_VARS['coupon_expire_date'];
			$coupon_categories = $HTTP_POST_VARS['restrict_to_categories'];    
			$coupon_event_categories=$HTTP_POST_VARS['restrict_to_event_categories'];
			$coupon_events=$HTTP_POST_VARS['restrict_to_events']; 
			$coupon_sub_categories=$HTTP_POST_VARS['restrict_to_sub_categories'];
			$coupon_subscription=$HTTP_POST_VARS['restrict_to_subscription']; 
			$coupon_service=$HTTP_POST_VARS['restrict_to_service'];
			$coupon_service_categories=$HTTP_POST_VARS['restrict_to_service_categories'];
	
	}
	//print_r($coupon);
  //  echo tep_draw_form('coupon', 'sales_coupon.php', 'action=update&oldaction='.$HTTP_GET_VARS['action'] . '&cid=' . $HTTP_GET_VARS['cid'],'post', ' onSubmit="javascript:return validateForm()"'); 
?> 
      <table border="0" width="100%" cellspacing="0" cellpadding="6">
	  <?php if($coupon_id==""){	  ?>
	  <tr>
				 <td class="main" width="30%">&nbsp;</td>
				 <td class="main" align="right" colspan="2"><a style="cursor:pointer" onClick="do_close();" ><img src="images/template/img_closel.gif" alt="Close" style="border:none"></a></td>
          </tr>
	  	
<?php }
        $languages = tep_get_languages();
        for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
        $language_id = $languages[$i]['id'];
if($coupon_id==''){?>
		<tr>
			<td colspan="2" class="pageHeading" >New Coupon Details</td>
		</tr>
<?php } ?>		
      <tr>
        <td align="left" class="main" width="30%"><?php if ($i==0) echo COUPON_NAME; ?></td>
        <td align="left" width="30%"><?php echo tep_draw_input_field('coupon_name[' . $languages[$i]['id'] . ']', $coupon_name[$language_id]) . '&nbsp;' . tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']); ?></td>
        <td align="left" class="main" width="40%"><?php if ($i==0) echo COUPON_NAME_HELP; ?></td>
      </tr>
<?php }
        $languages = tep_get_languages();
        for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
        $language_id = $languages[$i]['id'];?>
      <tr>
        <td align="left" valign="top" class="main" width="30%"><?php if ($i==0) echo COUPON_DESC; ?></td>
        <td align="left" valign="top" width="30%"><?php echo tep_draw_textarea_field('coupon_desc[' . $languages[$i]['id'] . ']','physical','24','3', $coupon_desc[$language_id]) . '&nbsp;' . tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']); ?></td>
        <td align="left" valign="top" class="main" width="40%"><?php if ($i==0) echo COUPON_DESC_HELP; ?></td>
      </tr>
<?php }?>
          <tr>
				 <td class="main" width="30%"><?php echo TEXT_COUPON_TAX_CLASS; ?></td>
				 <td class="main" align="left" colspan="2"><?php echo  tep_draw_pull_down_menu('coupon_tax_class_id', $tax_class_array, $coupon_tax_class_id, 'onchange="updateGross()"'); ?></td>
          </tr>
          <tr>
				 <td class="main" width="30%"><?php echo TEXT_COUPON_PRICE_NET; ?></td>
				 <td class="main" align="left" width="30%"><?php echo  tep_draw_input_field('coupon_amount', $coupon_amount, 'onKeyUp="updateGross()"'); ?></td>
				 <td align="left" class="main"><?php echo COUPON_AMOUNT_HELP; ?></td>
          </tr>
          <tr>
				 <td class="main" width="30%"><?php echo TEXT_COUPON_PRICE_GROSS; ?></td>
				 <td class="main" align="left" colspan="2"><?php echo tep_draw_input_field('coupon_amount_gross', $coupon_amount, 'OnKeyUp="updateNet()"'); ?></td>
          </tr>
 		  <tr>
				 <td align="left" class="main" width="30%"><?php echo COUPON_MIN_ORDER; ?></td>
				 <td align="left" width="30%"><?php echo tep_draw_input_field('coupon_min_order', $coupon_min_order); ?></td>
				 <td align="left" class="main"><?php echo COUPON_MIN_ORDER_HELP; ?></td>
          </tr>
          <tr>
				<td align="left" class="main" width="30%"><?php echo COUPON_FREE_SHIP; ?></td>
				<td align="left" width="30%"><?php echo tep_draw_checkbox_field('coupon_free_ship', $coupon_free_ship); ?></td>
				<td align="left" class="main"><?php echo COUPON_FREE_SHIP_HELP; ?></td>
          </tr>
     	 <tr>
				<td align="left" class="main" width="30%"><?php echo COUPON_CODE; ?></td>
				<td align="left" width="30%"><?php echo tep_draw_input_field('coupon_code', $coupon_code,'onBlur="javascript:check_ccode()"'); ?></td>
				<td align="left" class="main"><?php echo COUPON_CODE_HELP; ?></td>
      	 </tr>
         <tr>
				<td align="left" class="main" width="30%"><?php echo COUPON_USES_COUPON; ?></td>
				<td align="left" width="30%"><?php echo tep_draw_input_field('coupon_uses_coupon', $coupon_uses_coupon); ?></td>
				<td align="left" class="main"><?php echo COUPON_USES_COUPON_HELP; ?></td>
         </tr>
         <tr>
				<td align="left" class="main" width="30%"><?php echo COUPON_USES_USER; ?></td>
				<td align="left" width="30%"><?php echo tep_draw_input_field('coupon_uses_user', $coupon_uses_user,'onBlur="javascript:compare()"'); ?></td>
				<td align="left" class="main"><?php echo COUPON_USES_USER_HELP; ?></td>
        </tr>
        <tr>
<?php
    if (!isset($coupon_startdate)) {
      $coupon_startdate = split("[-]", getServerDate());
    } else {
      $coupon_startdate = split("[-]", $coupon_startdate);
    }
    if (!isset($coupon_expiredate)) {
      $coupon_finishdate = split("[-]", getServerDate());
      $coupon_finishdate[0] = $coupon_finishdate[0] + 1;
    } else {
      $coupon_finishdate = split("[-]", $coupon_expiredate);
    }
?>
				<td align="left" class="main" width="30%"><?php echo COUPON_STARTDATE; ?></td>
				<td align="left" width="30%"><?php echo tep_draw_date_selector('coupon_startdate', mktime(0,0,0, $coupon_startdate[1], $coupon_startdate[2], $coupon_startdate[0], 0)); ?></td>
				<td align="left" class="main"><?php echo COUPON_STARTDATE_HELP; ?></td>
       </tr>
       <tr>
				<td align="left" class="main" width="30%"><?php echo COUPON_FINISHDATE; ?></td>
				<td align="left" width="30%"><?php echo tep_draw_date_selector('coupon_finishdate', mktime(0,0,0, $coupon_finishdate[1], $coupon_finishdate[2], $coupon_finishdate[0], 0)); ?></td>
				<td align="left" class="main"><?php echo COUPON_FINISHDATE_HELP; ?></td>
      </tr>
	  <?php  $style = "display:none";
	 	    if($coupon_categories !='' || $coupon_event_categories != '' || $coupon_sub_categories != '' || $coupon_service_categories !='' || $coupon_products !='' || $coupon_events!='' || $coupon_subscription !='' || $coupon_service!=''){ 
	  		$checked_sel = true;
			$checked_all = false;
	   		$style = "display:''";
			}else{
			$checked_all = true;
			$checked_sel = false;}?>

      <tr>
				<td align="left" class="main" width="30%"><?php echo TEXT_COUPON_ALL_APPLY  ; ?></td>
				<td align="left" colspan="2" class="main" width="30%"><?php echo tep_draw_radio_field('coupon_all','',$checked_all,'','onClick="javascript:apply_all()"');  ?> All Categories 
		<?php echo tep_draw_separator('pixel_trans.gif',25,5) . tep_draw_radio_field('coupon_selected','',$checked_sel,'','onClick="javascript:apply_selected()"'); ?> Selected Categories or List</td>
			
	  <?php
	  	$back = isset($HTTP_GET_VARS['back'])?$HTTP_GET_VARS['back']:'';
		 if($back){
		    $coupon_categories = $HTTP_POST_VARS['coupon_categories'];
		    $coupon_event_categories=$HTTP_POST_VARS['coupon_event_categories'];
			$coupon_sub_categories = $HTTP_POST_VARS['coupon_sub_categories'];
			$coupon_service_categories = $HTTP_POST_VARS['coupon_service_categories'];
			$coupon_products = $HTTP_POST_VARS['coupon_products'];
			$coupon_events = $HTTP_POST_VARS['coupon_events'];
			$coupon_subscription = $HTTP_POST_VARS['coupon_subscription'];
			$coupon_service = $HTTP_POST_VARS['coupon_service'];
			$back = false;
		} 
		?>
	  
	  
     <?php // Tabstrip started here // ?>
	  <tr>
	     <td id="tabstrib" style="display:none" colspan="3">
		 <table border="0" cellpadding="0" cellspacing="0">
		 <tr>	
		 	<td>
			<table border="0" cellpadding="0" cellspacing="0" width="100%">
				<tr height="20">
					<td class="tabGap" width="20">&nbsp;</td>
					<td class="tab" id="p" align="center" width="55" onClick="javascript:goto_ajax('p','<?php echo $coupon_categories; ?>')" onMouseOver="javascript:changeStyle(this,1);" onMouseOut="javascript:changeStyle(this,0);">Product Categories</td>
					<td class="tabGap">&nbsp;</td>
					<td class="tab" id="e" align="center"  width="55" onClick="javascript:goto_ajax('e','<?php echo $coupon_event_categories; ?>')" onMouseOver="javascript:changeStyle(this,1);" onMouseOut="javascript:changeStyle(this,0);">Event Categories</td>
					<td class="tabGap">&nbsp;</td>
					<td class="tab" id="s" align="center"  width="55" onClick="javascript:goto_ajax('s','<?php echo $coupon_sub_categories; ?>')" onMouseOver="javascript:changeStyle(this,1);" onMouseOut="javascript:changeStyle(this,0);">Subscription Categories</td>
					<td class="tabGap">&nbsp;</td>
					<td class="tab" id="v" align="center"  width="55" onClick="javascript:goto_ajax('v','<?php echo $coupon_service_categories; ?>')" onMouseOver="javascript:changeStyle(this,1);" onMouseOut="javascript:changeStyle(this,0);">Service Categories</td> 
					<td class="tabGap">&nbsp;</td>
					<td class="tab" id="pi" align="center"  width="55" onClick="goto_ajax('pi','<?php echo $coupon_products; ?>')" onMouseOver="javascript:changeStyle(this,1);" onMouseOut="javascript:changeStyle(this,0);">Product</td>
					<td class="tabGap">&nbsp;</td>
					<td class="tab" id="ei" align="center"  width="55" onClick="javascript:goto_ajax('ei','<?php echo $coupon_events; ?>')" onMouseOver="javascript:changeStyle(this,1);" onMouseOut="javascript:changeStyle(this,0);">Events</td>
					<td class="tabGap">&nbsp;</td>
					<td class="tab" id="si" align="center"  width="55" onClick="javascript:goto_ajax('si','<?php echo $coupon_subscription; ?>')" onMouseOver="javascript:changeStyle(this,1);" onMouseOut="javascript:changeStyle(this,0);">Subscription</td>
					<td class="tabGap">&nbsp;</td>
					<td class="tab" id="vi" align="center"  width="55" onClick="javascript:goto_ajax('vi','<?php echo $coupon_service ; ?>')" onMouseOver="javascript:changeStyle(this,1);" onMouseOut="javascript:changeStyle(this,0);">Service</td>
					<td class="tabGap"><?php echo tep_draw_separator('pixel_trans.gif','8',10); ?></td>
					<td ><?php echo tep_draw_separator('pixel_trans.gif','68',10);?></td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td colspan="5" id="gen_id">
			
			</td>
		</tr>  
	</table>
	</td>
	</tr>	
		<?php  
		echo tep_draw_hidden_field('coupon_categories',$coupon_categories); 
		echo tep_draw_hidden_field('coupon_event_categories',$coupon_event_categories); 
		echo tep_draw_hidden_field('coupon_sub_categories',$coupon_sub_categories); 
		echo tep_draw_hidden_field('coupon_service_categories',$coupon_service_categories); 
		echo tep_draw_hidden_field('coupon_products',$coupon_products);
		echo tep_draw_hidden_field('coupon_events',$coupon_events);
		echo tep_draw_hidden_field('coupon_subscription',$coupon_subscription);
		echo tep_draw_hidden_field('coupon_service',$coupon_service);
		echo tep_draw_hidden_field('coupon_alll',$coupon_all);
		$hidden = array("p"=>"$coupon_categories","e"=>"$coupon_event_categories","s"=>"$coupon_sub_categories","v"=>"$coupon_service_categories","pi"=>"$coupon_products","ei"=>"$coupon_events","si"=>"$coupon_subscription","vi"=>"$coupon_service");
		foreach($hidden as $k=>$v){
		  if(tep_not_null($v)){
		  	 $value = $v;
			 $type = $k;
			 break;
		  }
		}
		 ?><?php if($back){ ?> <script>//assign_back();</script><?php } ?>
		<script> if(document.coupon.coupon_selected.checked==true) {
					document.getElementById("tabstrib").style.display="";
					type="<?php echo $type!=''?$type:'p'; ?>";
					goto_ajax(type,'<?php echo $value; ?>');
				 } 
			</script>
	  <tr><td align="right" width="50%" colspan="3" ><table align="right" border="0" width="40%">
                 <tr>
				 <td align="left"><?php echo tep_image_submit('button_preview.gif',COUPON_BUTTON_PREVIEW) . '&nbsp;&nbsp;<a href="' . tep_href_link('sales_coupon.php', ''); ?>"><?php //echo tep_image_button('button_cancel.gif', IMAGE_CANCEL); ?></a></td></tr></table>
				 </td></tr>
      </table>
<!--</form>-->
<?php 	}else if($command=='load_categories'){
				  echo 'load';
				  if($prev_ids != ""){
				  $prev_ids = split(',',$prev_ids);
				  echo display_categories($type,$prev_ids);
				  }else
				  echo display_categories($type);
 		 }else if($command=='load_items'){
				  echo 'load';
				  if($prev_ids != ""){
				  $prev_ids = split(',',$prev_ids);
				  echo display_items($type,$prev_ids);
				  }else
				  echo display_items($type);
		 }else if($command=='preview'){
		 		echo 'preview';
				?>
 <table  border="0" width="80%" cellspacing="0" cellpadding="6" class="openContent">
		<tr>	
			<td width="2%">&nbsp;</td>
			<td colspan="2" class="pageHeading">Coupon Details</td>
		</tr>
<?php
        $languages = tep_get_languages();
        for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
            $language_id = $languages[$i]['id'];
?>
      <tr>
	  		<td width="2%">&nbsp;</td>
        	<td align="left" width="30%" class="main" align="center"><?php echo COUPON_NAME; ?></td>
        	<td align="left" class="main" align="center"><?php echo $HTTP_POST_VARS['coupon_name'][$language_id]; ?></td>
      </tr>
<?php
		}// end of language-1 forloop
		
        $languages = tep_get_languages();
        for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
            $language_id = $languages[$i]['id'];
?>
      <tr>
	  		<td width="2%">&nbsp;</td>
        	<td align="left" class="main" width="30%"><?php echo COUPON_DESC; ?></td>
        	<td align="left" class="main"><?php echo $HTTP_POST_VARS['coupon_desc'][$language_id]; ?></td>
      </tr>
<?php
		}// end of language-2 forloop
		$tax_id=tep_db_prepare_input($HTTP_POST_VARS['coupon_tax_class_id']);
		$tax_query=tep_db_query("SELECT tc.tax_class_title,tr.tax_rate from " . TABLE_TAX_CLASS . " tc," . TABLE_TAX_RATES . " tr where tc.tax_class_id='" . tep_db_input($tax_id) . "' and tc.tax_class_id=tr.tax_class_id");
		$tax_result=tep_db_fetch_array($tax_query);
		$tax_rate=$tax_result["tax_rate"];
		$tax_class=$tax_result["tax_class_title"];
		if ($tax_class=="") $tax_class="none";
		$amount=$HTTP_POST_VARS['coupon_amount'];
		$gross_amount=$amount;
		if ($tax_rate>0) $gross_amount=$amount+($amount*$tax_rate/100);
?>
		<tr>
			  <td width="2%">&nbsp;</td>
       		  <td align="left" class="main" width="30%"><?php echo TEXT_COUPON_TAX_CLASS; ?></td>
        	  <td align="left" class="main"><?php echo $tax_class; ?></td>
      </tr>
	   <tr>
	   		<td width="2%">&nbsp;</td>
       		 <td align="left" class="main" width="30%"><?php echo TEXT_COUPON_PRICE_NET; ?></td>
             <td align="left"class="main"><?php echo $amount; ?></td>
      </tr>
	  <tr>
	  		<td width="2%">&nbsp;</td>
        	<td align="left"class="main" width="30%"><?php echo TEXT_COUPON_PRICE_GROSS; ?></td>
       		 <td align="left"class="main"><?php echo $gross_amount; ?></td>
      </tr>
      <tr>
	  			<td width="2%">&nbsp;</td>
       		 <td align="left"class="main" width="30%"><?php echo COUPON_MIN_ORDER; ?></td>
       		 <td align="left"class="main"><?php echo $HTTP_POST_VARS['coupon_min_order']; ?></td>
      </tr>
      <tr>
	  		<td width="2%">&nbsp;</td>
        	<td align="left"class="main" width="30%"><?php echo COUPON_FREE_SHIP; ?></td>
<?php
    if ($HTTP_POST_VARS['coupon_free_ship']) {
?>
       		 <td align="left"class="main"><?php echo TEXT_FREE_SHIPPING; ?></td>
<?php
    } else { 
?>
        	<td align="left"class="main"><?php echo TEXT_NO_FREE_SHIPPING; ?></td>
<?php
    }
?>
      </tr>
      <tr>
	  		<td width="2%">&nbsp;</td>
        	<td align="left" class="main" width="30%"><?php echo COUPON_CODE; ?></td>
<?php
    if ($HTTP_POST_VARS['coupon_code']) {
      $c_code = $HTTP_POST_VARS['coupon_code'];
    } else {
      $c_code = $coupon_code;
    }
?>
        <td align="left" class="main"><?php echo $c_code; ?></td>
      </tr>
      <tr>
	  		<td width="2%">&nbsp;</td>
        	<td align="left" class="main" width="30%"><?php echo COUPON_USES_COUPON; ?></td>
        	<td align="left" class="main"><?php if(is_numeric($HTTP_POST_VARS['coupon_uses_coupon'])) echo $HTTP_POST_VARS['coupon_uses_coupon']; else echo TEXT_NO_LIMIT;  ?></td>
      </tr>
      <tr>
	  		<td width="2%">&nbsp;</td>
        	<td align="left" class="main" width="30%"><?php echo COUPON_USES_USER; ?></td>
        	<td align="left" class="main"><?php if(is_numeric($HTTP_POST_VARS['coupon_uses_user'])) echo $HTTP_POST_VARS['coupon_uses_user']; else echo TEXT_NO_LIMIT; ?></td>
      </tr>
	  <tr>
	  		<td width="2%">&nbsp;</td>	
        	<td align="left" class="main" width="30%"><?php echo COUPON_STARTDATE; ?> </td>
<?php
    $start_date = date(EVENTS_DATE_FORMAT, mktime(0, 0, 0, $HTTP_POST_VARS['coupon_startdate_month'],$HTTP_POST_VARS['coupon_startdate_day'] ,$HTTP_POST_VARS['coupon_startdate_year'] ));
?>  
        	<td align="left" class="main"><?php echo $start_date; ?><input type="hidden" name="coupon_startdate" value="<?php echo tep_output_string($start_date); ?>"></td>
      </tr>
      <tr>
	  		<td width="2%">&nbsp;</td>
        	<td align="left" class="main" width="30%"><?php echo COUPON_FINISHDATE; ?></td>
<?php
    $finish_date = date(EVENTS_DATE_FORMAT, mktime(0, 0, 0, $HTTP_POST_VARS['coupon_finishdate_month'],$HTTP_POST_VARS['coupon_finishdate_day'] ,$HTTP_POST_VARS['coupon_finishdate_year'] ));
?>
        	<td align="left" class="main"><?php echo $finish_date; ?><input type="hidden" name="coupon_finishdate" value="<?php echo tep_output_string($finish_date); ?>"></td>
      </tr>
	  <?php if($coupon_all=='on'){?>
	   <tr>
		<td width="2%">&nbsp;</td>
        <td align="left" class="main"><?php echo 'Apply Coupon' ?></td>

        <td align="left" class="main"><?php echo 'To All Categories'; ?></td>

      </tr>
	  <?php } else{
		 if($HTTP_POST_VARS['coupon_categories']){
		 $prd_name_cat_query= tep_db_query("select categories_name from " . TABLE_CATEGORIES_DESCRIPTION ." where language_id = $languages_id and categories_id in(". $HTTP_POST_VARS['coupon_categories'] . ")"); 
		  while($prd_cat_name = tep_db_fetch_array($prd_name_cat_query)){
		  		$prd_cat_names .=  $prd_cat_name['categories_name'] . '&nbsp;'.',' . ' ';
		  		}$prd_cat_names = substr($prd_cat_names,0,-2);
		?>
	   <tr>
	   	<td width="2%">&nbsp;</td>
        <td align="left" class="main" width="30%"><?php echo COUPON_CATEGORIES; ?></td>
        <td align="left" class="main"><?php echo $prd_cat_names; ?></td>
      </tr><?php }?>
	  
		<?php 
		 if($HTTP_POST_VARS['coupon_products']){
		 $prd_name_query= tep_db_query("select products_name from " . TABLE_PRODUCTS_DESCRIPTION ." where language_id = $languages_id and products_id in(". $HTTP_POST_VARS['coupon_products'] . ")"); 
		  while($prd_name = tep_db_fetch_array($prd_name_query)){
		  		$prd_names .=   $prd_name['products_name'] . '&nbsp;'. ',' .' ';
		  		}$prd_names = substr($prd_names,0,-2);
		?>
       <tr>
	   	<td width="2%">&nbsp;</td>
        <td align="left" class="main" width="30%"><?php echo COUPON_PRODUCTS; ?></td>
        <td align="left" class="main"><?php echo $prd_names; ?></td>
      </tr><?php }?>
	  
	  <?php 
		 if($HTTP_POST_VARS['coupon_event_categories']){ $back_cat_event = $HTTP_POST_VARS['coupon_event_categories'];
		 $event_name_cat_query= tep_db_query("select events_categories_name from " . TABLE_EVENTS_CATEGORIES_DESCRIPTION ." where language_id = $languages_id and events_categories_id in(". $HTTP_POST_VARS['coupon_event_categories'] . ")"); 
		  while($event_cat_name = tep_db_fetch_array($event_name_cat_query)){
		  		$event_cat_names .=  $event_cat_name['events_categories_name'] . '&nbsp;'.',' . ' ';
		  		}$event_cat_names = substr($event_cat_names,0,-2);
		?>
      <tr>
	  	<td width="2%">&nbsp;</td>
	   <Td align="left" class="main" width="30%"><?php echo COUPON_EVENT_CATEGORIES;?></Td>

			<td align="left" class="main"><?php echo $event_cat_names;?></td>
	   </tr><?php }?>
	   
		<?php 
		 if($HTTP_POST_VARS['coupon_events']){
		 $event_name_query= tep_db_query("select events_name from " . TABLE_EVENTS_DESCRIPTION ." where language_id = $languages_id and events_id in(". $HTTP_POST_VARS['coupon_events'] . ")"); 
		  while($event_name = tep_db_fetch_array($event_name_query)){
		  		$event_names .= $event_name['events_name'] . '&nbsp;'. ',' . ' ';
		  		}$event_names = substr($event_names,0,-2);
		?>
	   <tr>
			<td width="2%">&nbsp;</td>
	   		<Td align="left" class="main" width="30%"><?php echo COUPON_EVENTS;?></Td>

			<td align="left" class="main"><?php echo $event_names;?></td>

	   </tr><?php }?>
	   
	   <?php 
		 if($HTTP_POST_VARS['coupon_sub_categories']){
		 $sub_name_cat_query= tep_db_query("select subscription_categories_name from " . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION ." where language_id = $languages_id and subscription_categories_id in(". $HTTP_POST_VARS['coupon_sub_categories'] . ")"); 
		  while($sub_cat_name = tep_db_fetch_array($sub_name_cat_query)){
		  		$sub_cat_names .=  $sub_cat_name['subscription_categories_name'] .'&nbsp;'. ',' . ' ';
		  		}$sub_cat_names = substr($sub_cat_names,0,-2);
		?>
	   <tr>
	   	<td width="2%">&nbsp;</td>
	   <Td align="left" class="main" width="30%"><?php echo COUPON_SUB_CATEGORIES;?></Td>

			<td align="left" class="main"><?php echo $sub_cat_names;?></td>
	   </tr><?php }?>

	   <?php 
		 if($HTTP_POST_VARS['coupon_subscription']){
		 $sub_name_query= tep_db_query("select subscription_name from " . TABLE_SUBSCRIPTIONS_DESCRIPTION ." where language_id = $languages_id and subscription_id in(". $HTTP_POST_VARS['coupon_subscription'] . ")"); 
		  while($sub_name = tep_db_fetch_array($sub_name_query)){
		  		$sub_names .=  $sub_name['subscription_name'] .'&nbsp;' . ',' . ' ';
		  		}$sub_names = substr($sub_names,0,-2);
		?>
	     <tr><td width="2%">&nbsp;</td>
	   		<Td align="left" class="main" width="30%"><?php echo COUPON_SUBSCRIPTION;?></Td>
			<td align="left" class="main"><?php echo $sub_names;?></td>
	   </tr><?php }?>
	   
		<?php 
		 if($HTTP_POST_VARS['coupon_service_categories']){
		 $service_name_cat_query= tep_db_query("select services_categories_name from " . TABLE_SERVICES_CATEGORIES_DESCRIPTION ." where language_id = $languages_id and services_categories_id in(". $HTTP_POST_VARS['coupon_service_categories'] . ")"); 
		  while($service_cat_name = tep_db_fetch_array($service_name_cat_query)){
		  		$service_cat_names .=  $service_cat_name['services_categories_name'] .'&nbsp;'. ',' . ' ';
		  		}$service_cat_names = substr($service_cat_names,0,-2);
		  ?>
	   
	   <tr><td width="2%">&nbsp;</td>
	   <Td align="left" class="main" width="30%"><?php echo COUPON_SERVICE_CATEGORIES;?></Td>
			<td align="left" class="main"><?php echo $service_cat_names;?></td>
	   </tr><?php }?>
		   <?php 
		    if($HTTP_POST_VARS['coupon_service']){
		      $service_name_query= tep_db_query("select service_name from " . TABLE_SERVICES_DESCRIPTION ." where language_id = $languages_id and service_id in(". $HTTP_POST_VARS['coupon_service'] . ")"); 
		       while($service_name = tep_db_fetch_array($service_name_query)){
		  		  $service_names .=  $service_name['service_name'] .'&nbsp;'. ',' .' ';
		  		}$service_names = substr($service_names,0,-2);
		  ?>
	   <Td align="left" class="main" width="30%"><?php echo COUPON_SERVICE;?></Td>
			<td align="left" class="main"><?php echo $service_names;?></td>
	   </tr><?php } } ?>
	   
		
			 <tr><td width="2%">&nbsp;</td>
			 	<td align="left">&nbsp;<?php //echo '<a href="javascript:vv();">' . tep_image_button('button_back.gif',COUPON_BUTTON_BACK, 'name=back') . '</a>'; ?></td>
			   <td align="right" style="cursor:pointer;" colspan="2"><?php echo tep_image_button('button_confirm.gif',COUPON_BUTTON_CONFIRM,'onclick="javascript:confirm_coupon();"'); ?></td>
			</tr>
			<tr>
				<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif',10,1); ?></td>	
			
	  </tr></table>
<?php	//end of preview
	 } else if($command=='confirm'){ 
        $coupon_type = "F";
        if (substr($HTTP_POST_VARS['coupon_amount'], -1) == '%') $coupon_type='P';
        if ($HTTP_POST_VARS['coupon_free_ship']) $coupon_type = 'S';
		if($HTTP_POST_VARS['coupon_uses_coupon']=='') {
		$uses_per_coupon='0';
		}
		else $uses_per_coupon=$HTTP_POST_VARS['coupon_uses_coupon'];
		 if($HTTP_POST_VARS['coupon_min_order']=='') {
		 $coupon_minimum_order='0';
		 }
		 else $coupon_minimum_order=$HTTP_POST_VARS['coupon_min_order'];
		 if($HTTP_POST_VARS['coupon_uses_user']=='') $uses_per_user=0;
		 else $uses_per_user = $HTTP_POST_VARS['coupon_uses_user'];
		 
        $sql_data_array = array('coupon_code' => tep_db_prepare_input($HTTP_POST_VARS['coupon_code']),
                                'coupon_amount' => tep_db_prepare_input($HTTP_POST_VARS['coupon_amount']),
                                'coupon_type' => tep_db_prepare_input($coupon_type),
                                'uses_per_coupon' => tep_db_prepare_input($uses_per_coupon),
								'coupon_tax_class_id' => tep_db_prepare_input($HTTP_POST_VARS['coupon_tax_class_id']),
                                'uses_per_user' => tep_db_prepare_input($uses_per_user),
                                'coupon_minimum_order' => tep_db_prepare_input($coupon_minimum_order),
                                'restrict_to_products' => tep_db_prepare_input($HTTP_POST_VARS['coupon_products']),
                                'restrict_to_categories' => tep_db_prepare_input($HTTP_POST_VARS['coupon_categories']),
                                'restrict_to_event_categories' => tep_db_prepare_input($HTTP_POST_VARS['coupon_event_categories']),
								'restrict_to_events'=>tep_db_prepare_input($HTTP_POST_VARS['coupon_events']),
								'restrict_to_sub_categories'=>tep_db_prepare_input($HTTP_POST_VARS['coupon_sub_categories']),
								'restrict_to_subscription'=>tep_db_prepare_input($HTTP_POST_VARS['coupon_subscription']),
								'restrict_to_service'=>tep_db_prepare_input($HTTP_POST_VARS['coupon_service']),
								'restrict_to_service_categories'=>tep_db_prepare_input($HTTP_POST_VARS['coupon_service_categories']),
                                'coupon_start_date' => tep_convert_date_raw($HTTP_POST_VARS['coupon_startdate']),
                                'coupon_expire_date' => tep_convert_date_raw($HTTP_POST_VARS['coupon_finishdate']),
                                'date_created' => $server_date,
                                'date_modified' => $server_date);

        $languages = tep_get_languages();
        for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
          $language_id = $languages[$i]['id'];
          $sql_data_marray[$i] = array('coupon_name' => tep_db_prepare_input($HTTP_POST_VARS['coupon_name'][$language_id]),
                                 'coupon_description' => tep_db_prepare_input($HTTP_POST_VARS['coupon_desc'][$language_id])
                                 );
        }

//        $query = tep_db_query("select coupon_code from " . TABLE_COUPONS . " where coupon_code = '" . tep_db_prepare_input($HTTP_POST_VARS['coupon_code']) . "'");    

        if ($HTTP_GET_VARS['oldaction']=='voucheredit') { 
			$cid = $HTTP_POST_VARS['coupon_id'];
          	tep_db_perform(TABLE_COUPONS, $sql_data_array, 'update', "coupon_id='" . $HTTP_POST_VARS['coupon_id']."'"); 

          for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
          $language_id = $languages[$i]['id'];
            $update = tep_db_query("update " . TABLE_COUPONS_DESCRIPTION . " set coupon_name = '" . tep_db_prepare_input($HTTP_POST_VARS['coupon_name'][$language_id]) . "', coupon_description = '" . tep_db_prepare_input($HTTP_POST_VARS['coupon_desc'][$language_id]) . "' where coupon_id = '" . $HTTP_POST_VARS['coupon_id'] . "' and language_id = '" . $language_id . "'");
          }
		  get_default_details($cid);
        } else {   
          $query = tep_db_perform(TABLE_COUPONS, $sql_data_array);
          $insert_id = tep_db_insert_id();
		  $cid = $insert_id;
		 	
					
          for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
            $language_id = $languages[$i]['id'];
            $sql_data_marray[$i]['coupon_id'] = $insert_id;
            $sql_data_marray[$i]['language_id'] = $language_id;
            tep_db_perform(TABLE_COUPONS_DESCRIPTION, $sql_data_marray[$i]);            
          }
		
      }
	  
	  if($insert_id){ //tabel_write();
	  	echo 'coupon_new';
	  }
	 //end of confirm
	}else if($command=='delete'){
		$coupon_delete_name_query = tep_db_query("select coupon_name from " . TABLE_COUPONS_DESCRIPTION . " where coupon_id =" . $coupon_id);
		$coupon_name = tep_db_fetch_array($coupon_delete_name_query);
		$coupon_delete_string = 'delete';
		$coupon_delete_string .= '<table border="0" cellspacing="0" cellpadding="0" width="100%">';
		$coupon_delete_string .= '<tr><td class="main" align="center">'.tep_image('images/icons/delete_blue.gif').'</td><td class="inner_title">Delete Coupon</td></tr>';
		$coupon_delete_string .= '<tr><td>'.'&nbsp;'.'</td><td class="main">'.TEXT_CONFIRM_DELETE. '</td></tr>';
		$coupon_delete_string .= '<tr><td>&nbsp;</td><td class="main"><b>'.$coupon_name['coupon_name'] .'</b></td></tr>';
		$coupon_delete_string .='<tr><td colspan="2">'.tep_draw_separator('pixel_trans.gif',50,10).'</td></tr></table>';
		echo $coupon_delete_string;
	}else if($command=='delete_confirm'){
	     $delete_query=tep_db_query("delete from " . TABLE_COUPONS . "  where coupon_id='".$coupon_id."'");
		 tep_db_query("delete from ". TABLE_COUPONS_DESCRIPTION . " where coupon_id='". $coupon_id ."'");
	     $restart_query=tep_db_query( "select coupon_id, coupon_code, coupon_amount,coupon_tax_class_id, coupon_type, coupon_start_date,coupon_expire_date,uses_per_user,uses_per_coupon,restrict_to_products, restrict_to_categories,restrict_to_events,restrict_to_event_categories,restrict_to_sub_categories,restrict_to_subscription, date_created,date_modified from " . TABLE_COUPONS ." where coupon_active='" . tep_db_input($status) . "' and coupon_type != 'G'");
	   	 echo 'page_navigation';
		 table_write();
	}else if($command=='email')	 {
			echo 'email';
			$coupon_query = tep_db_query("select coupon_code from " . TABLE_COUPONS . " where coupon_id = '" . $coupon_id . "'");
			$coupon_result = tep_db_fetch_array($coupon_query);
			$coupon_name_query = tep_db_query("select coupon_name from " . TABLE_COUPONS_DESCRIPTION . " where coupon_id = '" . $coupon_id . "' and language_id = '" . $languages_id . "'");
			$coupon_name = tep_db_fetch_array($coupon_name_query);
?>
      <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
      <!--<tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php //echo HEADING_TITLE; ?></td>
            <td class="pageHeading" align="right"><?php echo tep_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
          </tr>
        </table></td>
      </tr>-->
      <tr>
          <?php //echo tep_draw_form('mail', FILENAME_COUPON_ADMIN, 'action=preview_email&cid='. $coupon_id); ?>
            <td><!--<form id="mail" name="mail" action="<?php echo FILENAME_COUPON_ADMIN.'?action=preview_email&cid='. $coupon_id;?>" >-->
				<table border="0" cellpadding="0" cellspacing="2">
              <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
<?php
			$customers = array();
			$customers[] = array('id' => '', 'text' => TEXT_SELECT_CUSTOMER);
			$customers[] = array('id' => '***', 'text' => TEXT_ALL_CUSTOMERS);
			$customers[] = array('id' => '**D', 'text' => TEXT_NEWSLETTER_CUSTOMERS);
			$mail_query = tep_db_query("select customers_email_address, customers_firstname, customers_lastname from " . TABLE_CUSTOMERS . " order by customers_lastname");
			while($customers_values = tep_db_fetch_array($mail_query)) {
			  $customers[] = array('id' => $customers_values['customers_email_address'],
								   'text' => $customers_values['customers_lastname'] . ', ' . $customers_values['customers_firstname'] . ' (' . $customers_values['customers_email_address'] . ')');
			}
?>
              <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td class="main"><?php echo TEXT_COUPON .':'; ?>&nbsp;&nbsp;</td>
                <td class="main"><?php echo $coupon_name['coupon_name']; ?></td>
              </tr>
              <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td class="main"><?php echo TEXT_CUSTOMER; ?>&nbsp;&nbsp;</td>
                <td><?php echo tep_draw_pull_down_menu('customers_email_address', $customers, $HTTP_GET_VARS['customer']);?></td>
              </tr>
              <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td class="main"><?php echo TEXT_FROM; ?>&nbsp;&nbsp;</td>
                <td><?php echo tep_draw_input_field('from', EMAIL_FROM); ?></td>
              </tr>
              <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td class="main"><?php echo TEXT_SUBJECT; ?>&nbsp;&nbsp;</td>
                <td><?php echo tep_draw_input_field('subject'); ?></td>
              </tr>
              <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td valign="top" class="main"><?php echo TEXT_MESSAGE; ?></td>
                <td><?php echo tep_draw_textarea_field('message', 'soft', '70', '20'); ?></td>      
              </tr>
              <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                 <td colspan="2" align="right">
                 <?php if (HTML_AREA_WYSIWYG_DISABLE_EMAIL == 'Enable'){ echo tep_image_submit('button_send_email.gif', IMAGE_SEND_EMAIL,'onClick="preview_email('.$coupon_id.')"');// echo tep_image_submit('button_send_email.gif', IMAGE_SEND_EMAIL, 'onClick="validate();return returnVal;"');
                   		} else {
                			echo tep_image_submit('button_send_email.gif', IMAGE_SEND_EMAIL,'onClick="preview_email('.$coupon_id.')"'); }?>
                </td>
              </tr>
			   <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
            </table><!--</form>--></td>
         </tr>
      
      </td>
	  
<?php //end of email
		}else if($command=='preview_email'){
			
			echo 'pre_view_email';
			$coupon_query = tep_db_query("select coupon_code from " .TABLE_COUPONS . " where coupon_id = '" . $coupon_id . "'");
			$coupon_result = tep_db_fetch_array($coupon_query);
			$coupon_name_query = tep_db_query("select coupon_name from " . TABLE_COUPONS_DESCRIPTION . " where coupon_id = '" . $coupon_id . "' and language_id = '" . $languages_id . "'");
			$coupon_name = tep_db_fetch_array($coupon_name_query);
		require(DIR_WS_INCLUDES . 'languages/'. $language .'/mail.php');
	
    switch ($HTTP_POST_VARS['customers_email_address']) {
    case '***':
      $mail_sent_to = TEXT_ALL_CUSTOMERS;
      break;
    case '**D':
      $mail_sent_to = TEXT_NEWSLETTER_CUSTOMERS;
      break;
    default:
      $mail_sent_to = $HTTP_POST_VARS['customers_email_address'];
      break;
    }
?>
      <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
          <tr><?php //echo tep_draw_form('mail', FILENAME_COUPON_ADMIN, 'action=send_email_to_user&cid=' .$coupon_id); ?>
            <td><table border="0" width="100%" cellpadding="0" cellspacing="2">
              <tr>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td class="smallText"><b><?php echo TEXT_CUSTOMER; ?></b><br><?php echo $mail_sent_to; ?></td>
              </tr>
              <tr>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td class="smallText"><b><?php echo TEXT_COUPON; ?></b><br><?php echo $coupon_name['coupon_name']; ?></td>
              </tr>
              <tr>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td class="smallText"><b><?php echo TEXT_FROM; ?></b><br><?php echo htmlspecialchars(stripslashes($HTTP_POST_VARS['from'])); ?></td>
              </tr>
              <tr>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td class="smallText"><b><?php echo TEXT_SUBJECT; ?></b><br><?php echo htmlspecialchars(stripslashes($HTTP_POST_VARS['subject'])); ?></td>
              </tr>
              <tr>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td class="smallText"><b><?php if (HTML_AREA_WYSIWYG_DISABLE_EMAIL == 'Enable') { echo (stripslashes($HTTP_POST_VARS['message'])); } else { echo htmlspecialchars(stripslashes($HTTP_POST_VARS['message'])); } ?></td>
              </tr>
              <tr>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
              </tr>
              <tr>
                <td>
<?php
/* Re-Post all POST'ed variables */
    reset($HTTP_POST_VARS);
    while (list($key, $value) = each($HTTP_POST_VARS)) {
      if (!is_array($HTTP_POST_VARS[$key])) {
        echo tep_draw_hidden_field($key, htmlspecialchars(stripslashes($value)));
      }
    }
?>
                <table border="0" width="100%" cellpadding="0" cellspacing="2">
                  <tr>
                    <td><?php ?>&nbsp;</td>
                     <tr>
                    <td align="right"><?php echo '<a href="' . tep_href_link(FILENAME_MAIL) . '">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL) . '</a> ' . tep_image_submit('button_send_email.gif', IMAGE_SEND_EMAIL,'onClick="send_email('.$coupon_id.')"'); ?></td>
                    </tr>
                    <td class="smallText">
                <?php if (HTML_AREA_WYSIWYG_DISABLE_EMAIL == 'Disable'){echo tep_image_submit('button_back.gif', IMAGE_BACK, 'name="back"');
                } ?><?php if (HTML_AREA_WYSIWYG_DISABLE_EMAIL == 'Disable') {echo(TEXT_EMAIL_BUTTON_HTML);
                 } else { echo(TEXT_EMAIL_BUTTON_TEXT); } ?>
                    </td>
                  </tr>
				   <tr>
                    <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
                   </tr>
                </table></td>
             </tr>
            </table></td>
         <!-- </form>--></tr>
<?php //end of preview_email
		}else if($command=='send_mail_to_user'){
		
		   if(($HTTP_POST_VARS['customers_email_address']) && (!$HTTP_POST_VARS['back_x'])){
		   	  echo 'sent'.$coupon_id;
			switch ($HTTP_POST_VARS['customers_email_address']) {
			case '***':
			  $mail_query = tep_db_query("select customers_firstname, customers_lastname, customers_email_address from " . TABLE_CUSTOMERS);
			  $mail_sent_to = TEXT_ALL_CUSTOMERS;
			  break;
			case '**D':
			  $mail_query = tep_db_query("select customers_firstname, customers_lastname, customers_email_address from " . TABLE_CUSTOMERS . " where customers_newsletter = '1'");
			  $mail_sent_to = TEXT_NEWSLETTER_CUSTOMERS;
			  break;
			default:
			  $customers_email_address = tep_db_prepare_input($HTTP_POST_VARS['customers_email_address']);
			  $mail_query = tep_db_query("select customers_firstname, customers_lastname, customers_email_address from " . TABLE_CUSTOMERS . " where customers_email_address = '" . tep_db_input($customers_email_address) . "'");
			  $mail_sent_to = $HTTP_POST_VARS['customers_email_address'];
			  break;
			}
			$coupon_query = tep_db_query("select coupon_code from " . TABLE_COUPONS . " where coupon_id = '" . $coupon_code . "'");
			$coupon_result = tep_db_fetch_array($coupon_query);
			$coupon_name_query = tep_db_query("select coupon_name from " . TABLE_COUPONS_DESCRIPTION . " where coupon_id = '" . $coupon_code . "' and language_id = '" . $languages_id . "'");
			$coupon_name = tep_db_fetch_array($coupon_name_query);
			
			$from = tep_db_prepare_input($HTTP_POST_VARS['from']);
			$subject = tep_db_prepare_input($HTTP_POST_VARS['subject']);
		
			while ($mail = tep_db_fetch_array($mail_query)) {
			  	$message = tep_db_prepare_input($HTTP_POST_VARS['message']);
			  	$message .= "\n\n" . TEXT_TO_REDEEM . "\n\n";
			  	$message .= TEXT_VOUCHER_IS . $coupon_result['coupon_code'] . "\n\n";
			  	$message .= TEXT_REMEMBER . "\n\n";
			  	$message .= TEXT_VISIT . "\n\n";
		
			  //Let's build a message object using the email class
			  $mimemessage = new email(array('X-Mailer: osCommerce bulk mailer'));
			  // add the message to the object
		
		// MaxiDVD Added Line For WYSIWYG HTML Area: BOF (Send TEXT Email when WYSIWYG Disabled)

			if (HTML_AREA_WYSIWYG_DISABLE_EMAIL == 'Disable') {
			$mimemessage->add_text($message);
			} else {
			$mimemessage->add_html($message);
			}

// MaxiDVD Added Line For WYSIWYG HTML Area: EOF (Send HTML Email when WYSIWYG Enabled)
		  $mimemessage->build_message();    
		  $mimemessage->send($mail['customers_firstname'] . ' ' . $mail['customers_lastname'], $mail['customers_email_address'], '', $from, $subject);
    	}
	}else{
		echo 'Not_sent'.$coupon_id;
	}
    //tep_redirect(tep_href_link(FILENAME_COUPON_ADMIN, 'mail_sent_to=' . urlencode($mail_sent_to)));
		}else if($command=='report'){
			echo 'report';
			global $languages_id;
			$cc_query_raw = "select * from " . TABLE_COUPON_REDEEM_TRACK . " where coupon_id = '" . $coupon_id . "'";
			//echo $cc_query_raw;
			$cc_split = new splitPageResultsEvent($HTTP_GET_VARS['page'], MAX_DISPLAY_SEARCH_RESULTS, $cc_query_raw, $cc_query_numrows);
			$cc_query = tep_db_query($cc_query_raw);
			$vals="";
			while ($cc_list = tep_db_fetch_array($cc_query)) {
			
				$customer_query = tep_db_query("select customers_firstname, customers_lastname from " . TABLE_CUSTOMERS . " where customers_id = '" . $cc_list['customer_id'] . "'");
				$customer = tep_db_fetch_array($customer_query);
					$vals .= $cc_list['customer_id'].'#'.$customer['customers_firstname'] . ' ' . $customer['customers_lastname'].'#'.$cc_list['redeem_ip'].'#'.tep_convert_date_raw($cc_list['redeem_date']).'@';
			} echo $vals;
				echo ','.TEXT_REDEMPTIONS .'&nbsp;&nbsp;' .TEXT_REDEMPTIONS_TOTAL . '=' . tep_db_num_rows($cc_query);
		
 //end of report
		}
  		exit;
  	}
  
  

  if ($HTTP_GET_VARS['selected_box']) {

    $HTTP_GET_VARS['action']='';

    $HTTP_GET_VARS['old_action']='';

  }
//print_r($HTTP_POST_VARS);
 // echo $HTTP_POST_VARS['coupon_startdate'].'sg';

 // if (($HTTP_GET_VARS['action'] == 'send_email_to_user') && ($HTTP_POST_VARS['customers_email_address']) && (!$HTTP_POST_VARS['back_x'])) {}

 

  if ( ($HTTP_GET_VARS['action'] == 'preview_email') && (!$HTTP_POST_VARS['customers_email_address']) ) {

    $HTTP_GET_VARS['action'] = 'email';    

    $messageStack->add(ERROR_NO_CUSTOMER_SELECTED, 'error');

  }



  if ($HTTP_GET_VARS['mail_sent_to']) {

    $messageStack->add(sprintf(NOTICE_EMAIL_SENT_TO, $HTTP_GET_VARS['mail_sent_to']), 'notice');

  }



  //switch ($HTTP_GET_VARS['action']) {}

    #get tax descriptions to display in tax selection

   
?>

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">

<html <?php echo HTML_PARAMS; ?>>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/menu.js"></script>
<script language="javascript" src="includes/general.js"></script>
<script language="javascript" src="includes/http.js"></script>
<link rel="stylesheet" type="text/css" href="includes/javascript/spiffyCal/spiffyCal_v2_1.css">
<script language="JavaScript" src="includes/javascript/spiffyCal/spiffyCal_v2_1.js"></script>
<script type="text/javascript" src="htmlarea/htmlarea.js"></script>
<script type="text/javascript" src="htmlarea/editor.js"></script>
<script language="javascript">
  var dateAvailable = new ctlSpiffyCalendarBox("dateAvailable", "new_product", "products_date_available","btnDate1","<?php echo $pInfo->products_date_available; ?>",scBTNMODE_CUSTOMBLUE);
</script>
<script language="JavaScript">
function changeStyle(element,mode){
	if (selectedTab && selectedTab.id==element.id) return;
}
</script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<div id="spiffycalendar" class="text"></div>
<!-- header //-->
<?php 
	require(DIR_WS_INCLUDES . 'header.php');
	require(DIR_WS_INCLUDES . 'sales_coupon.js.php'); 
?>
<!-- header_eof //-->
<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr> 
<!-- body_text //-->
<?php 
  switch ($HTTP_GET_VARS['action']) {
  case 'voucherreport':

    $cc_query_raw = "select * from " . TABLE_COUPON_REDEEM_TRACK . " where coupon_id = '" . $HTTP_GET_VARS['cid'] . "'";

    $cc_split = new splitPageResultsEvent($HTTP_GET_VARS['page'], MAX_DISPLAY_SEARCH_RESULTS, $cc_query_raw, $cc_query_numrows);

    $cc_query = tep_db_query($cc_query_raw);

    while ($cc_list = tep_db_fetch_array($cc_query)) {

      $rows++;

      if (strlen($rows) < 2) {

        $rows = '0' . $rows;

      }

      if (((!$HTTP_GET_VARS['uid']) || (@$HTTP_GET_VARS['uid'] == $cc_list['unique_id'])) && (!$cInfo)) {

        $cInfo = new objectInfo($cc_list);

      }

      if ( (is_object($cInfo)) && ($cc_list['unique_id'] == $cInfo->unique_id) ) {

        echo '          <tr class="dataTableRowSelected" onMouseOver="this.style.cursor=\'hand\'" onClick="document.location.href=\'' . tep_href_link('sales_coupon.php', tep_get_all_get_params(array('cid', 'action', 'uid')) . 'cid=' . $cInfo->coupon_id . '&action=voucherreport&uid=' . $cinfo->unique_id) . '\'">' . "\n";

      } else {

        echo '          <tr class="dataTableRow" onMouseOver="this.className=\'dataTableRowOver\';this.style.cursor=\'hand\'" onMouseOut="this.className=\'dataTableRow\'" onClick="document.location.href=\'' . tep_href_link('sales_coupon.php', tep_get_all_get_params(array('cid', 'action', 'uid')) . 'cid=' . $cc_list['coupon_id'] . '&action=voucherreport&uid=' . $cc_list['unique_id']) . '\'">' . "\n";

      }

$customer_query = tep_db_query("select customers_firstname, customers_lastname from " . TABLE_CUSTOMERS . " where customers_id = '" . $cc_list['customer_id'] . "'");

$customer = tep_db_fetch_array($customer_query);



?>

                <td class="dataTableContent"><?php echo $cc_list['customer_id']; ?></td>

                <td class="dataTableContent" align="center"><?php echo $customer['customers_firstname'] . ' ' . $customer['customers_lastname']; ?></td>

                <td class="dataTableContent" align="center"><?php echo $cc_list['redeem_ip']; ?></td>

                <td class="dataTableContent" align="center"><?php echo format_date($cc_list['redeem_date']); ?></td>

                <td class="dataTableContent" align="right"><?php if ( (is_object($cInfo)) && ($cc_list['unique_id'] == $cInfo->unique_id) ) { echo tep_image(DIR_WS_IMAGES . 'icon_arrow_right.gif'); } else { echo '<a href="' . tep_href_link(FILENAME_COUPON_ADMIN, 'page=' . $HTTP_GET_VARS['page'] . '&cid=' . $cc_list['coupon_id']) . '">' . tep_image(DIR_WS_IMAGES . 'icon_info.gif', IMAGE_ICON_INFO) . '</a>'; } ?>&nbsp;</td>

              </tr>

<?php } ?>
             </table></td>

<?php

    $heading = array();

    $contents = array();

      $coupon_description_query = tep_db_query("select coupon_name from " . TABLE_COUPONS_DESCRIPTION . " where coupon_id = '" . $HTTP_GET_VARS['cid'] . "' and language_id = '" . $languages_id . "'");

      $coupon_desc = tep_db_fetch_array($coupon_description_query);

      $count_customers = tep_db_query("select * from " . TABLE_COUPON_REDEEM_TRACK . " where coupon_id = '" . $HTTP_GET_VARS['cid'] . "' and customer_id = '" . $cInfo->customer_id . "'");

       

      $heading[] = array('text' => '<b>[' . $HTTP_GET_VARS['cid'] . ']' . COUPON_NAME . ' ' . $coupon_desc['coupon_name'] . '</b>');

      $contents[] = array('text' => '<b>' . TEXT_REDEMPTIONS . '</b>');

      $contents[] = array('text' => TEXT_REDEMPTIONS_TOTAL . '=' . tep_db_num_rows($cc_query));

      $contents[] = array('text' => TEXT_REDEMPTIONS_CUSTOMER . '=' . tep_db_num_rows($count_customers));

      $contents[] = array('text' => '');

?>

    <td width="25%" valign="top">

<?php

      $box = new box;

      echo $box->infoBox($heading, $contents);

      echo '            </td>' . "\n";

?>

<?php

    break;

  case 'preview_email': 

    $coupon_query = tep_db_query("select coupon_code from " .TABLE_COUPONS . " where coupon_id = '" . $HTTP_GET_VARS['cid'] . "'");

    $coupon_result = tep_db_fetch_array($coupon_query);

    $coupon_name_query = tep_db_query("select coupon_name from " . TABLE_COUPONS_DESCRIPTION . " where coupon_id = '" . $HTTP_GET_VARS['cid'] . "' and language_id = '" . $languages_id . "'");

    $coupon_name = tep_db_fetch_array($coupon_name_query);

    switch ($HTTP_POST_VARS['customers_email_address']) {

    case '***':

      $mail_sent_to = TEXT_ALL_CUSTOMERS;

      break;

    case '**D':

      $mail_sent_to = TEXT_NEWSLETTER_CUSTOMERS;

      break;

    default:

      $mail_sent_to = $HTTP_POST_VARS['customers_email_address'];

      break;

    }

?>

      <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">

     
      <tr>

          <tr><?php echo tep_draw_form('mail', FILENAME_COUPON_ADMIN, 'action=send_email_to_user&cid=' . $HTTP_GET_VARS['cid']); ?>

            <td><table border="0" width="100%" cellpadding="0" cellspacing="2">

              <tr>

                <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>

              </tr>

              <tr>

                <td class="smallText"><b><?php echo TEXT_CUSTOMER; ?></b><br><?php echo $mail_sent_to; ?></td>

              </tr>

              <tr>

                <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>

              </tr>

              <tr>

                <td class="smallText"><b><?php echo TEXT_COUPON; ?></b><br><?php echo $coupon_name['coupon_name']; ?></td>

              </tr>

              <tr>

                <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>

              </tr>

              <tr>

                <td class="smallText"><b><?php echo TEXT_FROM; ?></b><br><?php echo htmlspecialchars(stripslashes($HTTP_POST_VARS['from'])); ?></td>

              </tr>

              <tr>

                <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>

              </tr>

              <tr>

                <td class="smallText"><b><?php echo TEXT_SUBJECT; ?></b><br><?php echo htmlspecialchars(stripslashes($HTTP_POST_VARS['subject'])); ?></td>

              </tr>

              <tr>

                <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>

              </tr>

              <tr>

                <td class="smallText"><b><?php if (HTML_AREA_WYSIWYG_DISABLE_EMAIL == 'Enable') { echo (stripslashes($HTTP_POST_VARS['message'])); } else { echo htmlspecialchars(stripslashes($HTTP_POST_VARS['message'])); } ?></td>

              </tr>

              <tr>

                <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>

              </tr>

              <tr>

                <td>

<?php

/* Re-Post all POST'ed variables */

    reset($HTTP_POST_VARS);

    while (list($key, $value) = each($HTTP_POST_VARS)) {

      if (!is_array($HTTP_POST_VARS[$key])) {

        echo tep_draw_hidden_field($key, htmlspecialchars(stripslashes($value)));

      }

    }

?>

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

                  <tr>

                    <td><?php ?>&nbsp;</td>

                     <tr>

                    <td align="right"><?php echo '<a href="' . tep_href_link(FILENAME_MAIL) . '">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL) . '</a> ' . tep_image_submit('button_send_email.gif', IMAGE_SEND_EMAIL); ?></td>

                    </tr>

                    <td class="smallText">

                <?php if (HTML_AREA_WYSIWYG_DISABLE_EMAIL == 'Disable'){echo tep_image_submit('button_back.gif', IMAGE_BACK, 'name="back"');

                } ?><?php if (HTML_AREA_WYSIWYG_DISABLE_EMAIL == 'Disable') {echo(TEXT_EMAIL_BUTTON_HTML);

                 } else { echo(TEXT_EMAIL_BUTTON_TEXT); } ?>

                    </td>

                  </tr>

                </table></td>

             </tr>

            </table></td>

          </form></tr>

<?php 

    break;       


  case 'update_preview':

?>

      <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">

      <tr>

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

          <tr>

            <td class="pageHeading"><?php //echo HEADING_TITLE; ?></td>

            <td class="pageHeading" align="right"><?php echo tep_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>

          </tr>

        </table></td>

      </tr>

      <tr>

      <td>
	  
	  <script>
	  function vv(){
	<?php 
	echo 'document.coupon.action="' . tep_href_link(FILENAME_COUPON_ADMIN  , 'back=back&action=' . $HTTP_GET_VARS['oldaction'] . '&cid=' . $HTTP_GET_VARS['cid']).'";';
	//echo 'document.coupon.action="' . tep_href_link(FILENAME_COUPON_ADMIN  , 'oldaction=' . $HTTP_GET_VARS['oldaction'] . '&cid=' . $HTTP_GET_VARS['cid']).'";';
	?>
	//alert (document.coupon.action);
	document.coupon.submit();
	//return;

}
	  
	  </script>

<?php echo tep_draw_form('coupon', 'sales_coupon.php', 'action=update_confirm&oldaction=' . $HTTP_GET_VARS['oldaction'] . '&cid=' . $HTTP_GET_VARS['cid']); ?>

      <table bgcolor="#E6EBF0" style="border-style: solid; border-width: 1px;" border="0" width="80%" cellspacing="0" cellpadding="6">

<?php

        $languages = tep_get_languages();

        for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {

            $language_id = $languages[$i]['id'];

?>

      <tr>

        <td align="left" width="" class="main"><?php echo COUPON_NAME; ?></td>

        <td align="left" class="main"><?php echo $HTTP_POST_VARS['coupon_name'][$language_id]; ?></td>

      </tr>

<?php

}

?>

<?php

        $languages = tep_get_languages();

        for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {

            $language_id = $languages[$i]['id'];

?>

      <tr>

        <td align="left" class="main"><?php echo COUPON_DESC; ?></td>

        <td align="left" class="main"><?php echo $HTTP_POST_VARS['coupon_desc'][$language_id]; ?></td>

      </tr>

<?php

}

	// get the tax details

	$tax_id=tep_db_prepare_input($HTTP_POST_VARS['coupon_tax_class_id']);

	$tax_query=tep_db_query("SELECT tc.tax_class_title,tr.tax_rate from " . TABLE_TAX_CLASS . " tc," . TABLE_TAX_RATES . " tr where tc.tax_class_id='" . tep_db_input($tax_id) . "' and tc.tax_class_id=tr.tax_class_id");

	$tax_result=tep_db_fetch_array($tax_query);

	$tax_rate=$tax_result["tax_rate"];

	$tax_class=$tax_result["tax_class_title"];

	if ($tax_class=="") $tax_class="none";

	$amount=$HTTP_POST_VARS['coupon_amount'];

	$gross_amount=$amount;

	if ($tax_rate>0) $gross_amount=$amount+($amount*$tax_rate/100);

?>

	 <tr>

        <td align="left" class="main"><?php echo TEXT_COUPON_TAX_CLASS; ?></td>

        <td align="left" class="main"><?php echo $tax_class; ?></td>

      </tr>

	 <tr>

        <td align="left" class="main"><?php echo TEXT_COUPON_PRICE_NET; ?></td>

        <td align="left"class="main"><?php echo $amount; ?></td>

      </tr>

	 <tr>

        <td align="left"class="main"><?php echo TEXT_COUPON_PRICE_GROSS; ?></td>

        <td align="left"class="main"><?php echo $gross_amount; ?></td>

      </tr>

 

      <tr>

        <td align="left"class="main"><?php echo COUPON_MIN_ORDER; ?></td>

        <td align="left"class="main"><?php echo $HTTP_POST_VARS['coupon_min_order']; ?></td>

      </tr>



      <tr>

        <td align="left"class="main"><?php echo COUPON_FREE_SHIP; ?></td>

<?php

    if ($HTTP_POST_VARS['coupon_free_ship']) {

?>

        <td align="left"class="main"><?php echo TEXT_FREE_SHIPPING; ?></td>

<?php

    } else { 

?>

        <td align="left"class="main"><?php echo TEXT_NO_FREE_SHIPPING; ?></td>

<?php

    }

?>

      </tr>

      <tr>

        <td align="left" class="main"><?php echo COUPON_CODE; ?></td>

<?php

    if ($HTTP_POST_VARS['coupon_code']) {

      $c_code = $HTTP_POST_VARS['coupon_code'];

    } else {

      $c_code = $coupon_code;

    }

?>

        <td align="left" class="main"><?php echo $coupon_code; ?></td>

      </tr>

      

      <tr>

        <td align="left" class="main"><?php echo COUPON_USES_COUPON; ?></td>

        <td align="left" class="main"><?php if(is_numeric($HTTP_POST_VARS['coupon_uses_coupon'])) echo $HTTP_POST_VARS['coupon_uses_coupon']; else echo TEXT_NO_LIMIT;  ?></td>

      </tr>



      <tr>

        <td align="left" class="main"><?php echo COUPON_USES_USER; ?></td>

        <td align="left" class="main"><?php if(is_numeric($HTTP_POST_VARS['coupon_uses_user'])) echo $HTTP_POST_VARS['coupon_uses_user']; else echo TEXT_NO_LIMIT; ?></td>

      </tr>
	  <tr>

        <td align="left" class="main"><?php echo COUPON_STARTDATE; ?> </td>

<?php

    $start_date = date(DATE_FORMAT, mktime(0, 0, 0, $HTTP_POST_VARS['coupon_startdate_month'],$HTTP_POST_VARS['coupon_startdate_day'] ,$HTTP_POST_VARS['coupon_startdate_year'] ));

?>

        <td align="left" class="main"><?php echo $start_date; ?></td>

      </tr>

      

      <tr>

        <td align="left" class="main"><?php echo COUPON_FINISHDATE; ?></td>

<?php

    $finish_date = date(DATE_FORMAT, mktime(0, 0, 0, $HTTP_POST_VARS['coupon_finishdate_month'],$HTTP_POST_VARS['coupon_finishdate_day'] ,$HTTP_POST_VARS['coupon_finishdate_year'] ));

?>

        <td align="left" class="main"><?php echo $finish_date; 
		
		
		?></td>

      </tr>
	  
	  <?php if($coupon_all=='on'){?>
	   <tr>

        <td align="left" class="main"><?php echo 'Apply Coupon' ?></td>

        <td align="left" class="main"><?php echo 'To All Categories'; ?></td>

      </tr>
	  <?php } else{
		 if($HTTP_POST_VARS['coupon_categories']){
		 $prd_name_cat_query= tep_db_query("select categories_name from " . TABLE_CATEGORIES_DESCRIPTION ." where language_id = $languages_id and categories_id in(". $HTTP_POST_VARS['coupon_categories'] . ")"); 
		  while($prd_cat_name = tep_db_fetch_array($prd_name_cat_query)){
		  		$prd_cat_names .=  $prd_cat_name['categories_name'] . '&nbsp;'.',' . ' ';
		  		}$prd_cat_names = substr($prd_cat_names,0,-2);
		?>
	   <tr>
        <td align="left" class="main"><?php echo COUPON_CATEGORIES; ?></td>
        <td align="left" class="main"><?php echo $prd_cat_names; ?></td>
      </tr><?php }?>
	  
		<?php 
		 if($HTTP_POST_VARS['coupon_products']){
		 $prd_name_query= tep_db_query("select products_name from " . TABLE_PRODUCTS_DESCRIPTION ." where language_id = $languages_id and products_id in(". $HTTP_POST_VARS['coupon_products'] . ")"); 
		  while($prd_name = tep_db_fetch_array($prd_name_query)){
		  		$prd_names .=   $prd_name['products_name'] . '&nbsp;'. ',' .' ';
		  		}$prd_names = substr($prd_names,0,-2);
		?>
       <tr>
        <td align="left" class="main"><?php echo COUPON_PRODUCTS; ?></td>
        <td align="left" class="main"><?php echo $prd_names; ?></td>
      </tr><?php }?>
	  
	  <?php 
		 if($HTTP_POST_VARS['coupon_event_categories']){ $back_cat_event = $HTTP_POST_VARS['coupon_event_categories'];
		 $event_name_cat_query= tep_db_query("select events_categories_name from " . TABLE_EVENTS_CATEGORIES_DESCRIPTION ." where language_id = $languages_id and events_categories_id in(". $HTTP_POST_VARS['coupon_event_categories'] . ")"); 
		  while($event_cat_name = tep_db_fetch_array($event_name_cat_query)){
		  		$event_cat_names .=  $event_cat_name['events_categories_name'] . '&nbsp;'.',' . ' ';
		  		}$event_cat_names = substr($event_cat_names,0,-2);
		?>
      <tr>
	   <Td align="left" class="main"><?php echo COUPON_EVENT_CATEGORIES;?></Td>

			<td align="left" class="main"><?php echo $event_cat_names;?></td>
	   </tr><?php }?>
	   
		<?php 
		 if($HTTP_POST_VARS['coupon_events']){
		 $event_name_query= tep_db_query("select events_name from " . TABLE_EVENTS_DESCRIPTION ." where language_id = $languages_id and events_id in(". $HTTP_POST_VARS['coupon_events'] . ")"); 
		  while($event_name = tep_db_fetch_array($event_name_query)){
		  		$event_names .= $event_name['events_name'] . '&nbsp;'. ',' . ' ';
		  		}$event_names = substr($event_names,0,-2);
		?>
	   <tr>

	   		<Td align="left" class="main"><?php echo COUPON_EVENTS;?></Td>

			<td align="left" class="main"><?php echo $event_names;?></td>

	   </tr><?php }?>
	   
	   <?php 
		 if($HTTP_POST_VARS['coupon_sub_categories']){
		 $sub_name_cat_query= tep_db_query("select subscription_categories_name from " . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION ." where language_id = $languages_id and subscription_categories_id in(". $HTTP_POST_VARS['coupon_sub_categories'] . ")"); 
		  while($sub_cat_name = tep_db_fetch_array($sub_name_cat_query)){
		  		$sub_cat_names .=  $sub_cat_name['subscription_categories_name'] .'&nbsp;'. ',' . ' ';
		  		}$sub_cat_names = substr($sub_cat_names,0,-2);
		?>
	   <tr>
	   <Td align="left" class="main"><?php echo COUPON_SUB_CATEGORIES;?></Td>

			<td align="left" class="main"><?php echo $sub_cat_names;?></td>
	   </tr><?php }?>

	   <?php 
		 if($HTTP_POST_VARS['coupon_subscription']){
		 $sub_name_query= tep_db_query("select subscription_name from " . TABLE_SUBSCRIPTIONS_DESCRIPTION ." where language_id = $languages_id and subscription_id in(". $HTTP_POST_VARS['coupon_subscription'] . ")"); 
		  while($sub_name = tep_db_fetch_array($sub_name_query)){
		  		$sub_names .=  $sub_name['subscription_name'] .'&nbsp;' . ',' . ' ';
		  		}$sub_names = substr($sub_names,0,-2);
		?>
	     <tr>
	   		<Td align="left" class="main"><?php echo COUPON_SUBSCRIPTION;?></Td>
			<td align="left" class="main"><?php echo $sub_names;?></td>
	   </tr><?php }?>
	   
		<?php 
		 if($HTTP_POST_VARS['coupon_service_categories']){
		 $service_name_cat_query= tep_db_query("select services_categories_name from " . TABLE_SERVICES_CATEGORIES_DESCRIPTION ." where language_id = $languages_id and services_categories_id in(". $HTTP_POST_VARS['coupon_service_categories'] . ")"); 
		  while($service_cat_name = tep_db_fetch_array($service_name_cat_query)){
		  		$service_cat_names .=  $service_cat_name['services_categories_name'] .'&nbsp;'. ',' . ' ';
		  		}$service_cat_names = substr($service_cat_names,0,-2);
		  ?>
	   
	   <tr>
	   <Td align="left" class="main"><?php echo COUPON_SERVICE_CATEGORIES;?></Td>
			<td align="left" class="main"><?php echo $service_cat_names;?></td>
	   </tr><?php }?>
		   <?php 
		    if($HTTP_POST_VARS['coupon_service']){
		      $service_name_query= tep_db_query("select service_name from " . TABLE_SERVICES_DESCRIPTION ." where language_id = $languages_id and service_id in(". $HTTP_POST_VARS['coupon_service'] . ")"); 
		       while($service_name = tep_db_fetch_array($service_name_query)){
		  		  $service_names .=  $service_name['service_name'] .'&nbsp;'. ',' .' ';
		  		}$service_names = substr($service_names,0,-2);
		  ?>
	   <Td align="left" class="main"><?php echo COUPON_SERVICE;?></Td>
			<td align="left" class="main"><?php echo $service_names;?></td>
	   </tr><?php } } ?>
      

<?php

        $languages = tep_get_languages();

        for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {

          $language_id = $languages[$i]['id'];

          echo tep_draw_hidden_field('coupon_name[' . $languages[$i]['id'] . ']', $HTTP_POST_VARS['coupon_name'][$language_id]);

          echo tep_draw_hidden_field('coupon_desc[' . $languages[$i]['id'] . ']', $HTTP_POST_VARS['coupon_desc'][$language_id]);

       }

    echo tep_draw_hidden_field('coupon_amount', $HTTP_POST_VARS['coupon_amount']);

    echo tep_draw_hidden_field('coupon_min_order', $HTTP_POST_VARS['coupon_min_order']);

    echo tep_draw_hidden_field('coupon_free_ship', $HTTP_POST_VARS['coupon_free_ship']);

    echo tep_draw_hidden_field('coupon_code', $c_code);

    echo tep_draw_hidden_field('coupon_uses_coupon', $HTTP_POST_VARS['coupon_uses_coupon']);

   	echo tep_draw_hidden_field('coupon_tax_class_id', $HTTP_POST_VARS['coupon_tax_class_id']);

    echo tep_draw_hidden_field('coupon_uses_user', $HTTP_POST_VARS['coupon_uses_user']);

    echo tep_draw_hidden_field('coupon_products', $HTTP_POST_VARS['coupon_products']);

    echo tep_draw_hidden_field('coupon_categories', $HTTP_POST_VARS['coupon_categories']);
    
    echo tep_draw_hidden_field('coupon_event_categories',$HTTP_POST_VARS['coupon_event_categories']);

	echo tep_draw_hidden_field('coupon_events', $HTTP_POST_VARS['coupon_events']);
	
	echo tep_draw_hidden_field('coupon_sub_categories',$HTTP_POST_VARS['coupon_sub_categories']);

	echo tep_draw_hidden_field('coupon_subscription', $HTTP_POST_VARS['coupon_subscription']);

	echo tep_draw_hidden_field('coupon_service',$HTTP_POST_VARS['coupon_service']);

	echo tep_draw_hidden_field('coupon_service_categories',$HTTP_POST_VARS['coupon_service_categories']);

    echo tep_draw_hidden_field('coupon_startdate', date('Y-m-d', mktime(0, 0, 0, $HTTP_POST_VARS['coupon_startdate_month'],$HTTP_POST_VARS['coupon_startdate_day'] ,$HTTP_POST_VARS['coupon_startdate_year'] )));

    echo tep_draw_hidden_field('coupon_finishdate', date('Y-m-d', mktime(0, 0, 0, $HTTP_POST_VARS['coupon_finishdate_month'],$HTTP_POST_VARS['coupon_finishdate_day'] ,$HTTP_POST_VARS['coupon_finishdate_year'] )));

?>

     <!--<tr>

        <td align="left"><?php echo tep_image_submit('button_confirm.gif',COUPON_BUTTON_CONFIRM); ?></td>
<?php // echo '<a href="' . tep_href_link($back_url, $back_url_params, 'NONSSL') . '">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>'; ?>
        <td align="left"><?php echo '<a href="javascript:vv();">' . tep_image_button('button_back.gif',COUPON_BUTTON_BACK, 'name=back'); ?></td>

      </td>

      </tr>-->

      

      </td></table>
	  <tr>
	  	<td><table border="0" cellpadding="0" cellspacing="0" width="80%">
		     <tr height="20"><td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif',100,10); ?></td> 
			 </tr>
			 <tr>
			 	<td align="left"><?php echo tep_draw_separator('pixel_trans.gif',60,10) .'<a href="javascript:vv();">' . tep_image_button('button_back.gif',COUPON_BUTTON_BACK, 'name=back'); ?></td>
			   <td align="center"><?php echo tep_image_submit('button_confirm.gif',COUPON_BUTTON_CONFIRM); ?></td>
			 </tr>
			 </table>
			 </td>
	  </tr>
	  
	  </form>

      </tr>



      </table></td>

<?php      

    break;

  case 'voucheredit':

    $languages = tep_get_languages();

    for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {

      $language_id = $languages[$i]['id'];

	 // echo "select coupon_name,coupon_description from " . TABLE_COUPONS_DESCRIPTION . " where coupon_id = '" .  $HTTP_GET_VARS['cid'] . "' and language_id = '" . $language_id . "'";

      $coupon_query = tep_db_query("select coupon_name,coupon_description from " . TABLE_COUPONS_DESCRIPTION . " where coupon_id = '" .  $HTTP_GET_VARS['cid'] . "' and language_id = '" . $language_id . "'");

      $coupon = tep_db_fetch_array($coupon_query);

      $coupon_name[$language_id] = $coupon['coupon_name'];

      $coupon_desc[$language_id] = $coupon['coupon_description'];

    }

	//echo "select coupon_code, coupon_amount, coupon_tax_class_id,coupon_type, coupon_minimum_order, coupon_start_date, coupon_expire_date, uses_per_coupon, uses_per_user, restrict_to_products, restrict_to_categories ,restrict_to_events,restrict_to_subscription from " . TABLE_COUPONS . " where coupon_id = '" . $HTTP_GET_VARS['cid'] . "'";

    $coupon_query=tep_db_query("select coupon_code, coupon_amount, coupon_tax_class_id,coupon_type, coupon_minimum_order, coupon_start_date, coupon_expire_date, uses_per_coupon, uses_per_user, restrict_to_products, restrict_to_categories ,restrict_to_event_categories,restrict_to_events,restrict_to_sub_categories,restrict_to_subscription,restrict_to_service,restrict_to_service_categories from " . TABLE_COUPONS . " where coupon_id = '" . $HTTP_GET_VARS['cid'] . "'");

    $coupon=tep_db_fetch_array($coupon_query);

    $coupon_amount = $coupon['coupon_amount'];

	

    if ($coupon['coupon_type']=='P') {

      $coupon_amount .= '%';

    }

    if ($coupon['coupon_type']=='S') {

      $coupon_free_ship .= true;

    }     

    $coupon_min_order = $coupon['coupon_minimum_order'];

    $coupon_code = $coupon['coupon_code'];

    $coupon_uses_coupon = $coupon['uses_per_coupon'];
	

    $coupon_uses_user = $coupon['uses_per_user'];

	$coupon_tax_class_id=$coupon['coupon_tax_class_id'];

    $coupon_products = $coupon['restrict_to_products'];

	$coupon_startdate=$coupon['coupon_start_date'];

	$coupon_expiredate=$coupon['coupon_expire_date'];

    $coupon_categories = $coupon['restrict_to_categories'];    
    
    $coupon_event_categories=$coupon['restrict_to_event_categories'];

	$coupon_events=$coupon['restrict_to_events']; 
	
	$coupon_sub_categories=$coupon['restrict_to_sub_categories'];

	$coupon_subscription=$coupon['restrict_to_subscription']; 

	$coupon_service=$coupon['restrict_to_service'];

	$coupon_service_categories=$coupon['restrict_to_service_categories'];

	

  case 'new':

// set some defaults

    if (!$coupon_uses_user) $coupon_uses_user=1;
	//if(!$coupon_uses_coupon) $coupon_uses_coupon=0;
?>

      <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>

      <td>

	  <!--<link rel="stylesheet" type="text/css" href="includes/javascript/spiffyCal/spiffyCal_v2_1.css">-->

<script language="javascript"><!--
/*function validateForm(){
	var netValue = document.coupon.coupon_amount.value;
	var grossValue = document.coupon.coupon_amount_gross.value;
	if(netValue.lastIndexOf("%")!=-1)
		netValue = netValue.substring(0,netValue.length-1);

	if(grossValue.lastIndexOf("%")!=-1)
		grossValue = grossValue.substring(0,grossValue.length-1);
				
	if(isNaN(netValue) || isNaN(grossValue)){
		alert("* <?php echo TEXT_ERROR_PRICE;?>");
		return false;
	}
	if(document.coupon.coupon_selected.checked==true){
	frm = document.coupon;
		if(frm.coupon_categories.value<1 && frm.coupon_event_categories.value<1 && frm.coupon_sub_categories.value<1 && frm.coupon_service_categories.value <1 && frm.coupon_products.value<1 && frm.coupon_events.value<1 && frm.coupon_subscription.value<1 && frm.coupon_service.value<1){
			alert('<?php echo ERROR_NO_SELECT; ?>');
			return false;
			}
	}
	return true;
}
*/

//--></script>
<script language="javascript">
	
function assign_back(){
		frm = document.coupon;alert(frm.coupon_categories);
		frm.coupon_categories.value = '<?php echo $HTTP_POST_VARS['coupon_categories'];  ?>';
		//alert(frm.coupon_categories.value);
		frm.coupon_event_categories.value = '<?php echo $HTTP_POST_VARS['coupon_event_categories'];  ?>';
		frm.coupon_sub_categories.value = '<?php echo $HTTP_POST_VARS['coupon_sub_categories'];  ?>';
		frm.coupon_service_categories.value = '<?php echo $HTTP_POST_VARS['coupon_service_categories']; ?>';
		frm.coupon_products.value = '<?php echo $HTTP_POST_VARS['coupon_products']; ?>';
		frm.coupon_events.value ='<?php echo $HTTP_POST_VARS['coupon_events']; ?>';
		frm.coupon_subscription.value = '<?php echo $HTTP_POST_VARS['coupon_subscription']; ?>';
		frm.coupon_service.value = '<?php echo $HTTP_POST_VARS['coupon_service']; ?>';
}

<!--

updateGross();

//--></script>


<?php 

    echo tep_draw_form('coupon', 'sales_coupon.php', 'action=update&oldaction='.$HTTP_GET_VARS['action'] . '&cid=' . $HTTP_GET_VARS['cid'],'post', ' onSubmit="javascript:return validateForm()"'); 

?>

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

<?php

        $languages = tep_get_languages();

        for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {

        $language_id = $languages[$i]['id'];

?>

      <tr>

        <td align="left" class="main"><?php if ($i==0) echo COUPON_NAME; ?></td>

        <td align="left"><?php echo tep_draw_input_field('coupon_name[' . $languages[$i]['id'] . ']', $coupon_name[$language_id]) . '&nbsp;' . tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']); ?></td>

        <td align="left" class="main" width="40%"><?php if ($i==0) echo COUPON_NAME_HELP; ?></td>

      </tr>

<?php

}

?>

<?php

        $languages = tep_get_languages();

        for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {

        $language_id = $languages[$i]['id'];

?>



      <tr>

        <td align="left" valign="top" class="main"><?php if ($i==0) echo COUPON_DESC; ?></td>

        <td align="left" valign="top"><?php echo tep_draw_textarea_field('coupon_desc[' . $languages[$i]['id'] . ']','physical','24','3', $coupon_desc[$language_id]) . '&nbsp;' . tep_image(DIR_WS_CATALOG_LANGUAGES . $languages[$i]['directory'] . '/images/' . $languages[$i]['image'], $languages[$i]['name']); ?></td>

        <td align="left" valign="top" class="main"><?php if ($i==0) echo COUPON_DESC_HELP; ?></td>

      </tr>

<?php

}
			  ?>

          <tr >

            <td class="main"><?php echo TEXT_COUPON_TAX_CLASS; ?></td>

            <td class="main" align="left"><?php echo  tep_draw_pull_down_menu('coupon_tax_class_id', $tax_class_array, $coupon_tax_class_id, 'onchange="updateGross()"'); ?></td>

          </tr>

          <tr >

            <td class="main"><?php echo TEXT_COUPON_PRICE_NET; ?></td>

		

            <td class="main" align="left"><?php echo  tep_draw_input_field('coupon_amount', $coupon_amount, 'onKeyUp="updateGross()"'); ?></td>

			 <td align="left" class="main"><?php echo COUPON_AMOUNT_HELP; ?></td>

          </tr>

          <tr >

            <td class="main"><?php echo TEXT_COUPON_PRICE_GROSS; ?></td>

            <td class="main" align="left"><?php echo tep_draw_input_field('coupon_amount_gross', $coupon_amount, 'OnKeyUp="updateNet()"'); ?></td>

          </tr>

         



 <tr>

        <td align="left" class="main"><?php echo COUPON_MIN_ORDER; ?></td>

        <td align="left"><?php echo tep_draw_input_field('coupon_min_order', $coupon_min_order); ?></td>

        <td align="left" class="main"><?php echo COUPON_MIN_ORDER_HELP; ?></td>

      </tr>

      <tr>

        <td align="left" class="main"><?php echo COUPON_FREE_SHIP; ?></td>

        <td align="left"><?php echo tep_draw_checkbox_field('coupon_free_ship', $coupon_free_ship); ?></td>

        <td align="left" class="main"><?php echo COUPON_FREE_SHIP_HELP; ?></td>

      </tr>

      <tr>

        <td align="left" class="main"><?php echo COUPON_CODE; ?></td>

        <td align="left"><?php echo tep_draw_input_field('coupon_code', $coupon_code); ?></td>

        <td align="left" class="main"><?php echo COUPON_CODE_HELP; ?></td>

      </tr>

      <tr>

        <td align="left" class="main"><?php echo COUPON_USES_COUPON; ?></td>

        <td align="left"><?php echo tep_draw_input_field('coupon_uses_coupon', $coupon_uses_coupon); ?></td>

        <td align="left" class="main"><?php echo COUPON_USES_COUPON_HELP; ?></td>

      </tr>

      <tr>

        <td align="left" class="main"><?php echo COUPON_USES_USER; ?></td>

        <td align="left"><?php echo tep_draw_input_field('coupon_uses_user', $coupon_uses_user); ?></td>

        <td align="left" class="main"><?php echo COUPON_USES_USER_HELP; ?></td>

      </tr>

      <tr>

	  

	  

<?php

    if (!isset($coupon_startdate)) {

      $coupon_startdate = split("[-]", getServerDate());

    } else {

      $coupon_startdate = split("[-]", $coupon_startdate);

    }

    if (!isset($coupon_expiredate)) {

      $coupon_finishdate = split("[-]", getServerDate());

      $coupon_finishdate[0] = $coupon_finishdate[0] + 1;

    } else {

      $coupon_finishdate = split("[-]", $coupon_expiredate);

    }

?>

        <td align="left" class="main"><?php echo COUPON_STARTDATE; ?></td>

        <td align="left"><?php echo tep_draw_date_selector('coupon_startdate', mktime(0,0,0, $coupon_startdate[1], $coupon_startdate[2], $coupon_startdate[0], 0)); ?></td>

        <td align="left" class="main"><?php echo COUPON_STARTDATE_HELP; ?></td>

      </tr>

      <tr>

        <td align="left" class="main"><?php echo COUPON_FINISHDATE; ?></td>

        <td align="left"><?php echo tep_draw_date_selector('coupon_finishdate', mktime(0,0,0, $coupon_finishdate[1], $coupon_finishdate[2], $coupon_finishdate[0], 0)); ?></td>

        <td align="left" class="main"><?php echo COUPON_FINISHDATE_HELP; ?></td>

      </tr>
	  <?php  $style = "display:none";
	 	    if($coupon_categories !='' || $coupon_event_categories != '' || $coupon_sub_categories != '' || $coupon_service_categories !='' || $coupon_products !='' || $coupon_events!='' || $coupon_subscription !='' || $coupon_service!=''){ 
	  		$checked_sel = true;
			$checked_all = false;
	   		$style = "display:''";
			}else{
			$checked_all = true;
			$checked_sel = false;}?>

      <tr>
        <td align="left" class="main"><?php echo TEXT_COUPON_ALL_APPLY  ; ?></td>
        <td align="left" colspan="2" class="main"><?php echo tep_draw_radio_field('coupon_all','',$checked_all,'','onClick="javascript:apply_all()"');  ?> All Categories 
		<?php echo tep_draw_separator('pixel_trans.gif',25,5) . tep_draw_radio_field('coupon_selected','',$checked_sel,'','onClick="javascript:apply_selected()"'); ?> Selected Categories or List</td>
			
	<!--<td align="left" class="main"><?php //echo TEXT_COUPON_ALL_APPLY_HELP; ?></td>-->
      </tr>
	<!--  <tr>
	     <td align="left" class="main"><?php echo TEXT_COUPON_SELECTED_ONLY; ?></td> 
		 <td align="left" class="main"><?php echo tep_draw_radio_field('coupon_selected','','','','onClick="javascript:apply_selected()"'); ?></td>
		 <td align="left" class="main"><?php echo TEXT_COUPON_SELECTED_ONLY_HELP; ?></td> 	
	  </tr>	 -->
      </td></table>
	  </tr>
	  <?php
	  	$back = isset($HTTP_GET_VARS['back'])?$HTTP_GET_VARS['back']:'';
		 if($back){
		    $coupon_categories = $HTTP_POST_VARS['coupon_categories'];
		    $coupon_event_categories=$HTTP_POST_VARS['coupon_event_categories'];
			$coupon_sub_categories = $HTTP_POST_VARS['coupon_sub_categories'];
			$coupon_service_categories = $HTTP_POST_VARS['coupon_service_categories'];
			$coupon_products = $HTTP_POST_VARS['coupon_products'];
			$coupon_events = $HTTP_POST_VARS['coupon_events'];
			$coupon_subscription = $HTTP_POST_VARS['coupon_subscription'];
			$coupon_service = $HTTP_POST_VARS['coupon_service'];
			$back = false;
		} 
		?>
	  
	  
     <?php // Tabstrip started here // ?>
	  <tr>
	     <td id="tabstrib" style="display:none">
		 <table border="0" cellpadding="0" cellspacing="0">
		 <tr>	
		 	<td>
			<table border="0" cellpadding="0" cellspacing="0" width="100%">
				<tr height="20">
					<td class="tabGap" width="20">&nbsp;</td>
					<td class="tab" id="p" align="center" width="55" onClick="javascript:goto_ajax('p','<?php echo $coupon_categories; ?>')" onMouseOver="javascript:changeStyle(this,1);" onMouseOut="javascript:changeStyle(this,0);">Product Categories</td>
					<td class="tabGap">&nbsp;</td>
					<td class="tab" id="e" align="center"  width="55" onClick="javascript:goto_ajax('e','<?php echo $coupon_event_categories; ?>')" onMouseOver="javascript:changeStyle(this,1);" onMouseOut="javascript:changeStyle(this,0);">Event Categories</td>
					<td class="tabGap">&nbsp;</td>
					<td class="tab" id="s" align="center"  width="55" onClick="javascript:goto_ajax('s','<?php echo $coupon_sub_categories; ?>')" onMouseOver="javascript:changeStyle(this,1);" onMouseOut="javascript:changeStyle(this,0);">Subscription Categories</td>
					<td class="tabGap">&nbsp;</td>
					<td class="tab" id="v" align="center"  width="55" onClick="javascript:goto_ajax('v','<?php echo $coupon_service_categories; ?>')" onMouseOver="javascript:changeStyle(this,1);" onMouseOut="javascript:changeStyle(this,0);">Service Categories</td> 
					<td class="tabGap">&nbsp;</td>
					<td class="tab" id="pi" align="center"  width="55" onClick="goto_ajax('pi','<?php echo $coupon_products; ?>')" onMouseOver="javascript:changeStyle(this,1);" onMouseOut="javascript:changeStyle(this,0);">Product</td>
					<td class="tabGap">&nbsp;</td>
					<td class="tab" id="ei" align="center"  width="55" onClick="javascript:goto_ajax('ei','<?php echo $coupon_events; ?>')" onMouseOver="javascript:changeStyle(this,1);" onMouseOut="javascript:changeStyle(this,0);">Events</td>
					<td class="tabGap">&nbsp;</td>
					<td class="tab" id="si" align="center"  width="55" onClick="javascript:goto_ajax('si','<?php echo $coupon_subscription; ?>')" onMouseOver="javascript:changeStyle(this,1);" onMouseOut="javascript:changeStyle(this,0);">Subscription</td>
					<td class="tabGap">&nbsp;</td>
					<td class="tab" id="vi" align="center"  width="55" onClick="javascript:goto_ajax('vi','<?php echo $coupon_service ; ?>')" onMouseOver="javascript:changeStyle(this,1);" onMouseOut="javascript:changeStyle(this,0);">Service</td>
					<td class="tabGap"><?php echo tep_draw_separator('pixel_trans.gif','8',10); ?></td>
					<td ><?php echo tep_draw_separator('pixel_trans.gif','68',10);?></td>
				</tr>
			</table>
			</td>
		</tr>
		<tr>
			<td colspan="5" id="gen_id">
			
			</td>
		</tr>  
	</table>
	</td>
	</tr>	
		<?php  
		echo tep_draw_hidden_field('coupon_categories',$coupon_categories); 
		echo tep_draw_hidden_field('coupon_event_categories',$coupon_event_categories); 
		echo tep_draw_hidden_field('coupon_sub_categories',$coupon_sub_categories); 
		echo tep_draw_hidden_field('coupon_service_categories',$coupon_service_categories); 
		echo tep_draw_hidden_field('coupon_products',$coupon_products);
		echo tep_draw_hidden_field('coupon_events',$coupon_events);
		echo tep_draw_hidden_field('coupon_subscription',$coupon_subscription);
		echo tep_draw_hidden_field('coupon_service',$coupon_service);
		echo tep_draw_hidden_field('coupon_alll',$coupon_all);
		$hidden = array("p"=>"$coupon_categories","e"=>"$coupon_event_categories","s"=>"$coupon_sub_categories","v"=>"$coupon_service_categories","pi"=>"$coupon_products","ei"=>"$coupon_events","si"=>"$coupon_subscription","vi"=>"$coupon_service");
		foreach($hidden as $k=>$v){
		  if(tep_not_null($v)){
		  	 $value = $v;
			 $type = $k;
			 break;
		  }
		}
		 ?><?php if($back){ ?> <script>//assign_back();</script><?php } ?>
		<script> if(document.coupon.coupon_selected.checked==true) {
					document.getElementById("tabstrib").style.display="";
					type="<?php echo $type!=''?$type:'p'; ?>";
					goto_ajax(type,'<?php echo $value; ?>');
				 } 
			</script>
	  <tr><td align="right" ><table align="right" border="0" width="50%">
                 <tr>
				 <td align="left"><?php echo tep_image_submit('button_preview.gif',COUPON_BUTTON_PREVIEW) . '&nbsp;&nbsp;<a href="' . tep_href_link('sales_coupon.php', ''); ?>"><?php echo tep_image_button('button_cancel.gif', IMAGE_CANCEL); ?></a></td></tr></table>
				 </td></tr>
      </table></td>
</form>
<?php

    break;

  default:

?>    

    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">

      <tr>

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

          <tr class="dataTableHeadingRow">
			
            <!--<td class="pageHeading"><?php //echo HEADING_TITLE; ?></td>!-->
			<td><?php echo '<a id="img_new"  href="javascript: expand_category(0,2);" onMouseOut="javascript:do_hover_change(this,1);" onClick="javascript:do_hover_change(this,3);" onMouseOver="javascript:do_hover_change(this,2);">'.tep_image(DIR_WS_IMAGES.'template/img_new.gif','New Coupon','22','22','align=absmiddle').'</a>'; ?> 
			<?php echo tep_draw_separator('pixel_trans.gif','225','10'); ?> </td>

            <td class="main" align="right"><?php echo tep_draw_form('status', FILENAME_COUPON_ADMIN, '', 'get'); ?>

<?php

    $status_array[] = array('id' => 'Y', 'text' => TEXT_COUPON_ACTIVE);

    $status_array[] = array('id' => 'N', 'text' => TEXT_COUPON_INACTIVE);

    $status_array[] = array('id' => '*', 'text' => TEXT_COUPON_ALL);



    if ($HTTP_GET_VARS['status']) {

      $status = tep_db_prepare_input($HTTP_GET_VARS['status']);

    } else { 

      $status = 'Y';

    } 

    echo HEADING_TITLE_STATUS . ' ' . tep_draw_pull_down_menu('status', $status_array, $status, 'onChange="expand_category(\'\',6);"'); 

?>

              </form>

           </td>

          </tr>

        </table></td>

      </tr>

      <tr>

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

          <tr>

            <td valign="top">
           <?php echo tep_draw_form('new_coupon',FILENAME_COUPON_ADMIN, '' ,'post','onsubmit="javascript:return validateForm(this);"');?>
			<table border="0" width="100%" cellspacing="0" cellpadding="2">

              <tr class="dataTableHeadingRow">

                <td class="dataTableHeadingContent"><?php echo COUPON_NAME; ?></td>

                <td class="dataTableHeadingContent" align="center"><?php echo COUPON_AMOUNT; ?></td>	

                <td class="dataTableHeadingContent" align="center"><?php echo COUPON_CODE; ?></td>	
              </tr>
			   <tr><td colspan="3" class="main" id="res_display" style="display:none"></td></tr>
			  <tr> 
			  <td id="ajax_details" colspan="3" width="100%">
	       	  <?php echo table_write(); ?>	
			 </td>	
			 </tr>	
			 <tr>
			 	<td colspan="3" style="display:none" id="preview"></td>
			 </tr>
			 <tr>
			 	<td colspan="3" style="display:none" id="newc"></td>
			 </tr>
			 </table>
             <input type="hidden" name="coupon_id" id="coupon_id">
			 </form>
			 </td>
			 	        	   		

<?php



    $heading = array();

    $contents = array();



    switch ($HTTP_GET_VARS['action']) {

    case 'release':

      break;

    case 'voucherreport':

      $heading[] = array('text' => '<b>' . TEXT_HEADING_COUPON_REPORT . '</b>');

      $contents[] = array('text' => TEXT_NEW_INTRO);
	  
      break;

    case 'neww':

      $heading[] = array('text' => '<b>' . TEXT_HEADING_NEW_COUPON . '</b>');

      $contents[] = array('text' => TEXT_NEW_INTRO);

      $contents[] = array('text' => '<br>' . COUPON_NAME . '<br>' . tep_draw_input_field('name'));

      $contents[] = array('text' => '<br>' . COUPON_AMOUNT . '<br>' . tep_draw_input_field('voucher_amount'));

      $contents[] = array('text' => '<br>' . COUPON_CODE . '<br>' . tep_draw_input_field('voucher_code'));

      $contents[] = array('text' => '<br>' . COUPON_USES_COUPON . '<br>' . tep_draw_input_field('voucher_number_of'));
	 
      break;

    default:
	
      if($cc_query_numrows>0){
	  $heading[] = array('text'=>'['.$cInfo->coupon_id.']  '.$cInfo->coupon_code);

      $amount = $cInfo->coupon_amount;

	

      if ($cInfo->coupon_type == 'P') {

        $amount .= '%';

      } else {

        $amount = $currencies->format($cInfo->amount);

      }

      if ($HTTP_GET_VARS['action'] == 'voucherdelete') {

        $contents[] = array('text'=> TEXT_CONFIRM_DELETE . '</br></br>' . 

                '<a href="'.tep_href_link('sales_coupon.php','action=confirmdelete&cid='.$HTTP_GET_VARS['cid'],'NONSSL').'">'.tep_image_button('button_confirm.gif','Confirm Delete Voucher').'</a>' .

                '<a href="'.tep_href_link('sales_coupon.php','cid='.$cInfo->coupon_id,'NONSSL').'">'.tep_image_button('button_cancel.gif','Cancel').'</a>'

                );

      } else { 

        $prod_details = NONE;
		$coupon_selected_list ="";
		$coupon_all_list="";
        if ($cInfo->restrict_to_products) {

          $prod_details = '<A HREF="sales_coupon_listproducts.php?cid=' . $cInfo->coupon_id . '" TARGET="_blank" ONCLICK="window.open(\'sales_coupon_listproducts.php?cid=' . $cInfo->coupon_id . '\', \'Valid_Categories\', \'scrollbars=yes,resizable=yes,menubar=yes,width=600,height=600\'); return false">View</A>';
		  $coupon_selected_list .= COUPON_PRODUCTS . '&nbsp;::&nbsp; ' . $prod_details . '<br>'  ;
        }     

        $cat_details = NONE;

        if ($cInfo->restrict_to_categories) {

          $cat_details = '<A HREF="sales_coupon_listcategories.php?cid=' . $cInfo->coupon_id . '" TARGET="_blank" ONCLICK="window.open(\'sales_coupon_listcategories.php?cid=' . $cInfo->coupon_id . '\', \'Valid_Categories\', \'scrollbars=yes,resizable=yes,menubar=yes,width=600,height=600\'); return false">View</A>';
		 $coupon_selected_list .=	 COUPON_CATEGORIES . '&nbsp;::&nbsp; ' . $cat_details . '<br>' ;
        }
        $event_cat_details=none;
        
        if ($cInfo->restrict_to_event_categories) {

          $event_cat_details = '<A HREF="sales_coupon_listeventcategories.php?cid=' . $cInfo->coupon_id . '" TARGET="_blank" ONCLICK="window.open(\'sales_coupon_listeventcategories.php?cid=' . $cInfo->coupon_id . '\', \'Valid_event_Categories\', \'scrollbars=yes,resizable=yes,menubar=yes,width=600,height=600\'); return false">View</A>';
		  $coupon_selected_list .= COUPON_EVENT_CATEGORIES .'&nbsp;::&nbsp; ' . $event_cat_details . '<br>'; 
        }
        

		$events_details=NONE;

		if($cInfo->restrict_to_events){

			$events_details = '<A HREF="sales_coupon_listevents.php?cid=' . $cInfo->coupon_id . '" TARGET="_blank" ONCLICK="window.open(\'sales_coupon_listevents.php?cid=' . $cInfo->coupon_id . '\', \'Valid_Events\', \'scrollbars=yes,resizable=yes,menubar=yes,width=600,height=600\'); return false">View</A>';
			$coupon_selected_list .= COUPON_EVENTS .'&nbsp;::&nbsp;' . $events_details .'<br>' ;
		}
		
		$sub_cat_details=NONE;

		if($cInfo->restrict_to_sub_categories){

			$sub_cat_details = '<A HREF="sales_coupon_listsubcategories.php?cid=' . $cInfo->coupon_id . '" TARGET="_blank" ONCLICK="window.open(\'sales_coupon_listsubcategories.php?cid=' . $cInfo->coupon_id . '\', \'Valid_sub_categories\', \'scrollbars=yes,resizable=yes,menubar=yes,width=600,height=600\'); return false">View</A>';
			$coupon_selected_list .= COUPON_SUB_CATEGORIES .'&nbsp;::&nbsp;' . $sub_cat_details .'<br>';
		}
		

		$subscription_details=NONE;

		if($cInfo->restrict_to_subscription){

			$subscription_details = '<A HREF="sales_coupon_listsubscription.php?cid=' . $cInfo->coupon_id . '" TARGET="_blank" ONCLICK="window.open(\'sales_coupon_listsubscription.php?cid=' . $cInfo->coupon_id . '\', \'Valid_subscription\', \'scrollbars=yes,resizable=yes,menubar=yes,width=600,height=600\'); return false">View</A>';
			$coupon_selected_list .=  COUPON_SUBSCRIPTION .'&nbsp;::&nbsp;' . $subscription_details . '<br>';
		}

		$service_details=NONE;

		if($cInfo->restrict_to_service){

			$service_details = '<A HREF="sales_coupon_listservice.php?cid=' . $cInfo->coupon_id . '" TARGET="_blank" ONCLICK="window.open(\'sales_coupon_listservice.php?cid=' . $cInfo->coupon_id . '\', \'Valid_service\', \'scrollbars=yes,resizable=yes,menubar=yes,width=600,height=600\'); return false">View</A>';
			$coupon_selected_list .=  COUPON_SERVICE .'&nbsp;::&nbsp;'. $service_details .'<br>';
		}

		if($cInfo->restrict_to_service_categories){

			$service_category_details = '<A HREF="sales_coupon_listservicecategories.php?cid=' . $cInfo->coupon_id . '" TARGET="_blank" ONCLICK="window.open(\'sales_coupon_listservicecategories.php?cid=' . $cInfo->coupon_id . '\', \'Valid_service_categories\', \'scrollbars=yes,resizable=yes,menubar=yes,width=600,height=600\'); return false">View</A>';
			$coupon_selected_list .= COUPON_SERVICE_CATEGORIES .'&nbsp;::&nbsp;'. $service_category_details .'<br>';
		}

		if($coupon_selected_list=="") $coupon_all_list .= TEXT_COUPON_ALL_APPLY . '::' . '&nbsp;&nbsp;'. 'All Categories' .'<br>';

        $coupon_name_query = tep_db_query("select coupon_name from " . TABLE_COUPONS_DESCRIPTION . " where coupon_id = '" . $cInfo->coupon_id . "' and language_id = '" . $languages_id . "'");

        $coupon_name = tep_db_fetch_array($coupon_name_query);       

        $contents[] = array('text'=>COUPON_NAME . '&nbsp;::&nbsp; ' . $coupon_name['coupon_name'] . '<br>' .

                     COUPON_AMOUNT . '&nbsp;::&nbsp; ' . $amount . '<br>' .

                     COUPON_STARTDATE . '&nbsp;::&nbsp; ' . format_date($cInfo->coupon_start_date) . '<br>' .

                     COUPON_FINISHDATE . '&nbsp;::&nbsp; ' . format_date($cInfo->coupon_expire_date) . '<br>' .

                     COUPON_USES_COUPON . '&nbsp;::&nbsp; ' . $cInfo->uses_per_coupon . '<br>' .

                     COUPON_USES_USER . '&nbsp;::&nbsp; ' . $cInfo->uses_per_user . '<br>' .
					
					 $coupon_selected_list .
					 
					 $coupon_all_list . 	
					 
					 DATE_CREATED . '&nbsp;::&nbsp; ' . format_date($cInfo->date_created) . '<br>' .

                     DATE_MODIFIED . '&nbsp;::&nbsp; ' . format_date($cInfo->date_modified) . '<br><br>' .

                     '<center><a href="'.tep_href_link('sales_coupon.php','action=email&cid='.$cInfo->coupon_id,'NONSSL').'">'.tep_image_button('button_email.gif','Email Voucher').'</a>&nbsp;' .

                     '<a href="'.tep_href_link('sales_coupon.php','action=voucheredit&cid='.$cInfo->coupon_id,'NONSSL').'">'.tep_image_button('button_edit.gif','Edit Voucher').'</a>&nbsp;' .

                     '<a href="'.tep_href_link('sales_coupon.php','action=voucherdelete&cid='.$cInfo->coupon_id,'NONSSL').'">'.tep_image_button('button_delete.gif','Delete Voucher').'</a>' .

                     '<br><a href="'.tep_href_link('sales_coupon.php','action=voucherreport&cid='.$cInfo->coupon_id,'NONSSL').'">'.tep_image_button('button_report.gif','Voucher Report').'</a></center>'

                     );

        }

        break;
		}
		
      }

    }

?>

      </tr>

    </table></td>

<!-- body_text_eof //-->

  </tr>

</table>
<?php
	function get_default_details($coupon_id){
	    global $languages_id,$currencies;
		$string_vals = $coupon_id.'^';
		
		$coupon_details_query = "select c.coupon_amount,date_format(c.coupon_start_date,'%Y-%m-%d') as start_date,date_format(c.coupon_expire_date,'%Y-%m-%d') as end_date,c.uses_per_coupon,c.uses_per_user,
		c.restrict_to_products,c.restrict_to_categories,date_format(c.date_created,'%Y-%m-%d') as date_created,date_format(c.date_modified,'%Y-%m-%d') as date_modified,c.restrict_to_events,c.restrict_to_subscription,
		c.restrict_to_service_categories,c.restrict_to_event_categories,c.restrict_to_sub_categories,c.restrict_to_service,cd.coupon_name,c.coupon_code from " . TABLE_COUPONS ." c, " . TABLE_COUPONS_DESCRIPTION . " cd  where c.coupon_id = cd.coupon_id and c.coupon_id =  '". $coupon_id . "' and cd.language_id = '" . $languages_id . "'";
		$coupon_details_query = tep_db_query($coupon_details_query);
		$coupon = tep_db_fetch_array($coupon_details_query);
		while(list($key,$vals)=each($coupon)){
			if(($key=='start_date') || ($key =='end_date') || ($key=='date_created') || ($key=='date_modified')) $vals = tep_convert_date_raw($vals);
			
			if($key=='coupon_amount') $vals = $currencies->format($vals);
			$string_vals .= $vals .'^'	;
		} 
		echo 'get_details' . $string_vals;
	}


	function table_write(){ 
	global $languages_id,$currencies,$page,$coupon_id,$status;
	?>		  	
	<table border="0" cellpadding="0" cellspacing="0" width="100%">
	<?php
    /*if ($HTTP_GET_VARS['page'] > 1) $rows = $HTTP_GET_VARS['page'] * 20 - 20;
	if($HTTP_GET_VARS['status']!='') $status = $HTTP_GET_VARS['status'];*/
    if ($status != '*'  && $status!='') 
      $cc_query_raw = "select coupon_id, coupon_code, coupon_amount,coupon_tax_class_id, coupon_type, coupon_start_date,coupon_expire_date,uses_per_user,uses_per_coupon,restrict_to_products, restrict_to_categories,restrict_to_event_categories,restrict_to_events,restrict_to_sub_categories,restrict_to_subscription,restrict_to_service,restrict_to_service_categories, date_created,date_modified from " . TABLE_COUPONS ." where coupon_active='" . tep_db_input($status) . "' and coupon_type != 'G' order by coupon_id desc";
    else 
      $cc_query_raw = "select coupon_id, coupon_code, coupon_amount,coupon_tax_class_id, coupon_type, coupon_start_date,coupon_expire_date,uses_per_user,uses_per_coupon,restrict_to_products, restrict_to_categories,restrict_to_event_categories,restrict_to_events,restrict_to_sub_categories,restrict_to_subscription,restrict_to_service,restrict_to_service_categories, date_created,date_modified from " . TABLE_COUPONS . " where coupon_type != 'G' order by coupon_id desc";
    $cc_split = new splitPageResultsEvent($page, MAX_DISPLAY_SEARCH_RESULTS, $cc_query_raw, $cc_query_numrows);

    $cc_query = tep_db_query($cc_query_raw);
	$class_name='dataTableRowOdd';
    while ($cc_list = tep_db_fetch_array($cc_query)) {  
	  $class_name=($class_name=='dataTableRowOdd')?'dataTableRowEven':'dataTableRowOdd';	
      $rows++;
      if (strlen($rows) < 2) 
		$rows = '0' . $rows;

	  $tax_rate=tep_get_tax_rate($cc_list["coupon_tax_class_id"]);

	  $amount=tep_add_tax($cc_list["coupon_amount"],$tax_rate);

      if (((!$HTTP_GET_VARS['cid']) || (@$HTTP_GET_VARS['cid'] == $cc_list['coupon_id'])) && (!$cInfo)) {

        $cInfo = new objectInfo($cc_list);

		$cInfo->amount=$amount;

      }
	  
	echo '<tr  class="'.$class_name.'" onmouseover="rowOverEffect(this);" onmouseout="rowOutEffect(this);" onclick="javascript:if(document.getElementById(\'head_'.$cc_list['coupon_id'].'\').style.display==\'none\') expand_category(' . $cc_list['coupon_id'] . ',3)">' . "\n";
    $coupon_description_query = tep_db_query("select coupon_name from " . TABLE_COUPONS_DESCRIPTION . " where coupon_id = '" . $cc_list['coupon_id'] . "' and language_id = '" . $languages_id . "'");
  	$coupon_desc = tep_db_fetch_array($coupon_description_query);

?>
	<td>
		<div id='<?php echo 'data_' . $cc_list['coupon_id'] ;  ?>'>
		<table border="0" cellpadding="4" cellspacing="0" width="100%">
		  <tr>
                <td class="dataTableContent" width="32%"><?php echo $coupon_desc['coupon_name']; ?></td>
                <td class="dataTableContent" align="center" width="38%">
<?php  
      if ($cc_list['coupon_type'] == 'P') {
        echo $cc_list['coupon_amount'] . '%';
      } elseif ($cc_list['coupon_type'] == 'S') {
		echo TEXT_FREE_SHIPPING;
      } else {
        echo $currencies->format($amount);
      }
?>
            	&nbsp;</td>
              <td class="dataTableContent" align="center"><?php echo $cc_list['coupon_code']; ?></td>
              </tr> </table></div>
			  <div id='<?php echo 'head_' . $cc_list['coupon_id'];  ?>' style="display:none" class="openContent" ></div>
			  </td></tr>
<?php 
    }
?>
		<?php	if($cc_query_numrows<1){ ?>
			   <tr> 
			       <td align="center" colspan="4" class="dataTableContent" id="no_coupon">Coupon Not Found</td>
			   </tr> 
		<?php } ?>	 
          <tr>
            <td colspan="5">
			<table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr id="pag_nav">
                <td class="smallText">&nbsp;<?php echo $cc_split->display_count($cc_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $page, TEXT_DISPLAY_NUMBER_OF_COUPONS); ?>&nbsp;</td>
                <td align="right" class="smallText">&nbsp;<?php echo $cc_split->display_script_links($cc_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $page,1,'expand_category(\'\'','\'\''); ?>&nbsp;</td>
              </tr>
            </table></td>
          </tr>
        </table></td>
<?php 

}

function display_items($type,$prev_ids=''){?>
 <?php //preparing the data to load listbox 
					  
					  $items = array('id'=>0,'text'=>'SELECT');
					  $prev_len = count($prev_ids);
					  switch ($type){
					  case 'pi': 
					  	$items_qry=tep_db_query("SELECT * FROM products, products_description WHERE products.products_id = products_description.products_id and products_description.language_id = '" . 1 . "' ORDER BY products_description.products_name"); 
						//echo "SELECT * FROM products, products_description WHERE products.products_id = products_description.products_id and products_description.language_id = '" . 1 . "' ORDER BY products_description.products_name"; 
						while($items_result=tep_db_fetch_array($items_qry)){
							  $items[] = array('id'=>$items_result['products_id'],'text'=>$items_result['products_name']);	
						}
						break;
					  case 'ei':
					    $items_qry=tep_db_query("SELECT * FROM events e, events_description ed  WHERE e.events_id = ed.events_id and language_id = '" . 1 . "' ORDER BY events_name"); 
						while($items_result=tep_db_fetch_array($items_qry)){
							  $items[] = array('id'=>$items_result['events_id'],'text'=>$items_result['events_name']);	
						}
						break;
					  case 'si':
					   $items_qry=tep_db_query("SELECT * FROM subscriptions s, subscriptions_description sd  WHERE s.subscription_id = sd.subscription_id and language_id = '" . 1 . "' ORDER BY subscription_name"); 
						while($items_result=tep_db_fetch_array($items_qry)){
							  $items[] = array('id'=>$items_result['subscription_id'],'text'=>$items_result['subscription_name']);	
						}
						break;
					  case 'vi':
					   $items_qry=tep_db_query("select * from services s ,services_description sd where s.service_id=sd.service_id and language_id = '" . 1 . "' order by sd.service_name"); 
						while($items_result=tep_db_fetch_array($items_qry)){
							  $items[] = array('id'=>$items_result['service_id'],'text'=>$items_result['service_name']);	
						}
						break;
						}	
					   
					 ?>
 <table class="tabContent" border="0" cellpadding="0" cellspacing="0" width="90%" >
			  <tr> 
			    <td class="main"><table border="0" cellpadding="0" cellspacing="0" width="100%">
				     <tr height="10"><td colspan="7"><?php echo tep_draw_separator('pixel_trans.gif','100%',5); ?></td>
					 </tr>
					 <tr>
					   <td width="10"></td>
					   <td class="tab_strib" align="center" >Items Available</td>
					   <td class="tab_strib" align="center" width="10"> </td>
					   <td class="tab_strib" align="center" ></td>
					   <td class="tab_strib" align="center" width="10"> </td>
					   <td class="tab_strib" align="center" >Selected Items</td>
					   <td width="10"></td>
					 </tr>
					 <tr><td colspan="7"><?php echo tep_draw_separator('pixel_trans.gif','100%',5); ?></td>
					 </tr>

					 <?php if($prev_ids != ""){  
						$prev_selected = array('id'=>0,'text'=>'select');
						$avail_len = count($items); 
						for($icnt=0;$icnt<$avail_len;$icnt++)	{
						  for($jcnt=0;$jcnt<$prev_len;$jcnt++){
						 	if($items[$icnt]['id']==$prev_ids[$jcnt]){
								$prev_selected[] = array('id'=>$items[$icnt]['id'],'text'=>$items[$icnt]['text']);
						 		unset($items[$icnt]['id'],$items[$icnt]['text']);	
									} }}}
					  		else{
							   $default = array('id'=>0,'text'=>'select');
							   $default[] = array('id'=>$items[0]['id'],'text'=>$items[0]['text']);
					  		}
					  ?> 
					 <tr> <td width="10"></td>
						<td valign="top" ><?php  echo tep_draw_mselect_menu('items_coupon_avail',$items,$default,'id=items_coupon_avail valign=top style="width:255px;height:100px"'); ?></td>
						<td width="10"></td>
						<td align="center" ><input size="4" type="button" value="&nbsp;>&nbsp;" onClick="javascript:load_selected_items('<?php echo $type; ?>')"><br>
						<input size="4" type="button" value="&nbsp;<&nbsp;" onClick="javascript:remove_selected_items('<?php echo $type; ?>')"><br>
						<input size="4" type="button" value=">>" onClick="javascript:load_all_items('<?php echo $type; ?>')"><br>
						<input size="4" type="button" value="<<" onClick="javascript:remove_all_items('<?php echo $type; ?>')"></td>
						<td width="10"></td>
						
						<?php if($prev_ids != ""){ ?>
						<td align="right"><?php 		
									echo tep_draw_mselect_menu('items',$prev_selected,$prev_selected,'id=items valign=top style="width:255px;height:100px"');
						} else {?></td>
						<td align="right"><select name="items" id="items" style="width:255px;height:100px" multiple="multiple"></select></td>
						<?php } ?>
						
						<td class="" width="10"></td>
					 </tr>
					  <tr height="20"><td colspan="5"><?php echo tep_draw_separator('pixel_trans.gif',5,5); ?></td>
					 </table>
					 
				<?php	
					 
					 }

  
 function display_categories($type,$prev_ids=''){?>
 <?php //preparing the data to load listbox 
					  $category = array('id'=>0,'text'=>'SELECT');
					  $prev_len = count($prev_ids);
					  switch ($type){
					  case 'p': 
					  	$category_qry=tep_db_query("SELECT * FROM categories, categories_description WHERE categories.categories_id = categories_description.categories_id and categories_description.language_id = '" . 1 . "' ORDER BY categories.categories_id");
					   	while($category_result = tep_db_fetch_array($category_qry)){
					        $category[] = array('id'=>$category_result['categories_id'],'text'=> $category_result['categories_name']);
					   }
						break;
					  case 'e':
					  	$category_qry = tep_db_query("select * from events_categories e ,events_categories_description ed where e.events_categories_id=ed.events_categories_id and language_id = '" . 1 . "' order by ed.events_categories_name");  	
					  	while($category_result = tep_db_fetch_array($category_qry)){
					        $category[] = array('id'=>$category_result['events_categories_id'],'text'=> $category_result['events_categories_name']);
					   }
						break;
					  case 's':
					  	$category_qry = tep_db_query("select * from subscription_categories s ,subscription_categories_description sd where s.subscription_categories_id=sd.subscription_categories_id and language_id = '" . 1 . "' order by sd.subscription_categories_name");
					  	while($category_result = tep_db_fetch_array($category_qry)){
					        $category[] = array('id'=>$category_result['subscription_categories_id'],'text'=> $category_result['subscription_categories_name']);
					    }
						break;
					  case 'v':
					    $category_qry = tep_db_query("SELECT * FROM services_categories, services_categories_description WHERE services_categories.services_categories_id = services_categories_description.services_categories_id and services_categories_description.language_id = '" . 1 . "' ORDER BY services_categories.services_categories_id");
						while($category_result = tep_db_fetch_array($category_qry)){
					        $category[] = array('id'=>$category_result['services_categories_id'],'text'=> $category_result['services_categories_name']);
					   }
						break;
						}	
					   
					 ?>
 <table class="tabContent"  border="0" cellpadding="0" cellspacing="0" width="90%">
			  <tr> 
			    <td class="main"><table border="0"  cellpadding="0" cellspacing="0" width="100%">
				    <tr height="10"><td colspan="7"><?php echo tep_draw_separator('pixel_trans.gif','100%',5); ?></td>
					</tr>	
					 <tr>
					   <td width="10"></td>
					   <td class="tab_strib" align="center" >Categories Available</td>
					   <td width="10"></td>
					   <td class="tab_strib" align="center" ></td>
					   <td width="10"></td>
					   <td class="tab_strib" align="center" >Selected Categories</td>
					   <td width="10"></td>
					 </tr>
					 <tr> <td colspan="7"><?php echo tep_draw_separator('pixel_trans.gif','100%',5); ?></td>
					 </tr>
					 
					 <?php if($prev_ids != ""){
						$prev_selected = array('id'=>0,'text'=>'selcet');
						$avail_len = count($category); 
							  for($icnt=0;$icnt<$avail_len;$icnt++)	{
							     for($jcnt=0;$jcnt<$prev_len;$jcnt++){
								 	if($category[$icnt]['id']==$prev_ids[$jcnt]){
						  				$prev_selected[] = array('id'=>$category[$icnt]['id'],'text'=>$category[$icnt]['text']);
										  unset($category[$icnt]['id'],$category[$icnt]['text']);            
													  }}}}
								else{
								   $default = array('id'=>0,'text'=>'select');
								   $default[] = array('id'=>$category[0]['id'],'text'=>$category[0]['text']);
								}
						?>
					 <tr>
					    <td width="10">
					    <td valign="top" ><?php  echo tep_draw_mselect_menu('cat_coupon_avail',$category,$default,'id=cat_coupon_avail valign=top style="width:255px;height:100px"'); ?></td>
						<td width="10"></td>
						<td align="center" ><input size="4" type="button" value="&nbsp;>&nbsp;" onClick="javascript:load_selected('<?php echo $type; ?>')"><br>
						<input size="4" type="button" value="&nbsp;<&nbsp;" onClick="javascript:remove_selected('<?php echo $type; ?>')"><br>
						<input size="4" type="button" value=">>" onClick="javascript:load_all('<?php echo $type; ?>')"><br>
						<input size="4" type="button" value="<<" onClick="javascript:remove_all('<?php echo $type; ?>')"><br></td>
						<td width="10"></td>
						<?php if($prev_ids != ""){?>
						<td align="right">
							<?php echo tep_draw_mselect_menu('category',$prev_selected,$prev_selected,'id=category valign=top style="width:255px;height:100px"');
						} else {?></td>
						<td align="right"><select name="category" id="category" multiple="multiple" style="width:255px;height:100px"></select> </td>
					 	<?php } ?>
						<td width="10"></td>
					 </tr>
					 <tr height="20"><td colspan="3"><?php echo tep_draw_separator('pixel_trans.gif',5,5); ?></td>
					 </table>
					 
				<?php	
					 
					 }

?>


</tr></table>
<!-- body_eof //-->

<!-- footer //-->

<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>

<!-- footer_eof //-->

</body>

</html>

<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>


