<?php
/*
  $Id: coupon_admin.php,v 1.5 2003/02/01 17:09:29 wilt Exp $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com
  Copyright (c) 2002 osCommerce

  Gift Voucher System 
  Copyright (c) 2001,2002,2003 Ian C Wilson
  http://www.phesis.co.uk

  Released under the GNU General Public License
*/

  require('includes/application_top.php');
  require(DIR_WS_CLASSES . 'currencies.php');
  if ($HTTP_GET_VARS['selected_box']) {
    $HTTP_GET_VARS['action']='';
    $HTTP_GET_VARS['old_action']='';
  }
  $currencies = new currencies();
  function create_coupon_code() {
    $salt= 'secret';
    $ccid = md5(uniqid("","salt"));
    $ccid .= md5(uniqid("","salt"));
    $ccid .= md5(uniqid("","salt"));
    $ccid .= md5(uniqid("","salt"));
    srand((double)microtime()*1000000); // seed the random number generator
    $random_start = @rand(0, (128-12));
    $id1=substr($ccid, $random_start,12);        
    return $id1;
  }
  function tep_draw_date_selector($prefix, $date='') {
    $month_array = array();
    $month_array[1] =_JANUARY;
    $month_array[2] =_FEBRUARY;
    $month_array[3] =_MARCH;
    $month_array[4] =_APRIL;
    $month_array[5] =_MAY;
    $month_array[6] =_JUNE;
    $month_array[7] =_JULY;
    $month_array[8] =_AUGUST;
    $month_array[9] =_SEPTEMBER;
    $month_array[10] =_OCTOBER;
    $month_array[11] =_NOVEMBER;
    $month_array[12] =_DECEMBER;
    $usedate = getdate($date);
    $day = $usedate['mday'];
    $month = $usedate['mon'];
    $year = $usedate['year'];		
    $date_selector = '<select name="'. $prefix .'_day">';
    for ($i=1;$i<32;$i++){
      $date_selector .= '<option value="' . $i . '"';
      if ($i==$day) $date_selector .= 'selected';
      $date_selector .= '>' . $i . '</option>';
    }
    $date_selector .= '</select>';
    $date_selector .= '<select name="'. $prefix .'_month">';
    for ($i=1;$i<13;$i++){
      $date_selector .= '<option value="' . $i . '"';
      if ($i==$month) $date_selector .= 'selected';      
      $date_selector .= '>' . $month_array[$i] . '</option>';
    }
    $date_selector .= '</select>';
    $date_selector .= '<select name="'. $prefix .'_year">';
    for ($i=2001;$i<2019;$i++){
      $date_selector .= '<option value="' . $i . '"';
      if ($i==$year) $date_selector .= 'selected';
      $date_selector .= '>' . $i . '</option>';
    }
    $date_selector .= '</select>';
    return $date_selector;
  }
  
  if ( ($HTTP_GET_VARS['action'] == 'send_email_to_user') && ($HTTP_POST_VARS['customers_email_address']) && (!$HTTP_POST_VARS['back_x']) ) {
    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 name, code from cc_coupons where cid = '" . $HTTP_GET_VARS['cid'] . "'");
    $coupon_result = tep_db_fetch_array($coupon_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['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
    $mimemessage->add_text($message);
    $mimemessage->build_message();
    
      $mimemessage->send($mail['customers_firstname'] . ' ' . $mail['customers_lastname'], $mail['customers_email_address'], '', $from, $subject);
// now create the tracking entry
//$gv_query=tep_db_query("insert into gv_tracking (gv_number, date_created, customer_id_sent, sent_firstname, sent_surname, emailed_to, gv_amount) values ('".$id1."', NOW(),'0','Sent by','Admin','".$mail['customers_email_address']."','".$HTTP_POST_VARS['amount']."')");
    }

    tep_redirect(tep_href_link(FILENAME_COUPON, 'mail_sent_to=' . urlencode($mail_sent_to)));
  }
 
 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']) {
    case 'confirmdelete':
    $delete_query=tep_db_query("update cc_coupons set coupon_active = 'N' where cid='".$HTTP_GET_VARS['cid']."'");
    break;
    case 'update':
    // get all HTTP_POST_VARS and validate
    $HTTP_POST_VARS['coupon_code'] = trim($HTTP_POST_VARS['coupon_code']);
    $HTTP_POST_VARS['coupon_name'] = trim($HTTP_POST_VARS['coupon_name']);
    $HTTP_POST_VARS['coupon_amount'] = trim($HTTP_POST_VARS['coupon_amount']);
    $update_errors = 0;
    if (!$HTTP_POST_VARS['coupon_name']) {
      $update_errors = 1;
      $messageStack->add(ERROR_NO_COUPON_NAME, 'error');
    }
    if (!$HTTP_POST_VARS['coupon_amount']) {
      $update_errors = 1;
      $messageStack->add(ERROR_NO_COUPON_AMOUNT, 'error');
    }
    if (!$HTTP_POST_VARS['coupon_code']) {
      $good_result = 0;
      while ($good_result == 0) {
        $coupon_code = create_coupon_code(); 
        $query = tep_db_query("select code from " . TABLE_COUPON . " where code = '" . tep_db_prepare_input($coupon_code) . "'");    
        if (tep_db_num_rows($query) == 0) $good_result = 1;
      }
    }
    if ($HTTP_POST_VARS['coupon_code']) $coupon_code = $HTTP_POST_VARS['coupon_code'];
    $query1 = tep_db_query("select code from " . TABLE_COUPON . " where code = '" . tep_db_prepare_input($coupon_code) . "'");    
    if (tep_db_num_rows($query1) && $HTTP_POST_VARS['coupon_code'] && $HTTP_GET_VARS['oldaction'] != 'voucheredit')  {
      $update_errors = 1;
      $messageStack->add(ERROR_COUPON_EXISTS, 'error');
    }
    if ($update_errors != 0) {
      $HTTP_GET_VARS['action'] = 'new';  
    } else {  
      $HTTP_GET_VARS['action'] = 'update_preview';
    }
    break;
    case 'update_confirm':
    if ( ($HTTP_POST_VARS['back_x']) || ($HTTP_POST_VARS['back_y']) ) {
      $HTTP_GET_VARS['action'] = 'new';
    } else {
    $coupon_type = "F";
    if (substr($HTTP_POST_VARS['coupon_amount'], -1) == '%') $coupon_type='P';
      $sql_data_array = array('name' => tep_db_prepare_input($HTTP_POST_VARS['coupon_name']),
                                  'description' => tep_db_prepare_input($HTTP_POST_VARS['coupon_desc']),
                                  'code' => tep_db_prepare_input($HTTP_POST_VARS['coupon_code']),
                                  'amount' => tep_db_prepare_input($HTTP_POST_VARS['coupon_amount']),
                                  'type' => tep_db_prepare_input($coupon_type),
                                  'uses_coupon' => tep_db_prepare_input($HTTP_POST_VARS['coupon_uses_coupon']),
                                  'uses_user' => tep_db_prepare_input($HTTP_POST_VARS['coupon_uses_user']),
                                  'min_order' => tep_db_prepare_input($HTTP_POST_VARS['coupon_min_order']),
                                  'products' => tep_db_prepare_input($HTTP_POST_VARS['coupon_products']),
                                  'categories' => tep_db_prepare_input($HTTP_POST_VARS['coupon_categories']),
                                  'startdate' => tep_date_raw($HTTP_POST_VARS['coupon_startdate']),
                                  'finishdate' => tep_date_raw($HTTP_POST_VARS['coupon_finishdate']),
                                  'date_created' => 'now()',
                                  'date_modified' => 'now()'); 
      if ($HTTP_GET_VARS['oldaction']=='voucheredit') {
        tep_db_perform(TABLE_COUPON, $sql_data_array, 'update', "cid='" . $HTTP_GET_VARS['cid']."'"); 
      } else {   
        tep_db_perform(TABLE_COUPON, $sql_data_array);
      }
    }
  }
?>
<!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/general.js"></script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
<?php 
    switch ($HTTP_GET_VARS['action']) {
      case 'preview_email': 
      $coupon_query = tep_db_query("select name, code from cc_coupons where cid = '" . $HTTP_GET_VARS['cid'] . "'");
      $coupon_result = tep_db_fetch_array($coupon_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="0">
      <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>

          <tr><?php echo tep_draw_form('mail', FILENAME_COUPON, '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_result['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 echo TEXT_MESSAGE; ?></b><br><?php echo nl2br(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>
                    <td align="right"><?php echo '<a href="' . tep_href_link(FILENAME_COUPON) . '">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL) . '</a> ' . tep_image_submit('button_send_mail.gif', IMAGE_SEND_EMAIL); ?></td>
                  </tr>
                </table></td>
              </tr>
            </table></td>
          </form></tr>
<?php
      
      
      
      
      
      break;       
      case 'email':
      $coupon_query = tep_db_query("select name, code from cc_coupons where cid = '" . $HTTP_GET_VARS['cid'] . "'");
      $coupon_result = tep_db_fetch_array($coupon_query);
      
?>
      <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
      <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>

          <tr><?php echo tep_draw_form('mail', FILENAME_COUPON, 'action=preview_email&cid='. $HTTP_GET_VARS['cid']); ?>
            <td><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><?php echo $coupon_result['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_RESTRICT; ?>&nbsp;&nbsp;</td>
                <td><?php echo tep_draw_checkbox_field('customers_restrict', $customers_restrict);?></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; ?>&nbsp;&nbsp;</td>
                <td><?php echo tep_draw_textarea_field('message', 'soft', '60', '15'); ?></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 echo tep_image_submit('button_send_mail.gif', IMAGE_SEND_EMAIL); ?></td>
              </tr>
            </table></td>
          </form></tr>

      </tr>
      </td>
<?php      
      break;
      case 'update_preview':
?>
      <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
      <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>
<?php echo tep_draw_form('coupon', 'coupon_admin.php', 'action=update_confirm&oldaction=' . $HTTP_GET_VARS['oldaction'] . '&cid=' . $HTTP_GET_VARS['cid']); ?>
      <table border="0" width="100%" cellspacing="0" cellpadding="6">
      <tr>
        <td align="left"><?php echo COUPON_NAME; ?></td>
        <td align="left"><?php echo $HTTP_POST_VARS['coupon_name']; ?></td>
      </tr>

      <tr>
        <td align="left"><?php echo COUPON_DESC; ?></td>
        <td align="left"><?php echo $HTTP_POST_VARS['coupon_desc']; ?></td>
      </tr>

      <tr>
        <td align="left"><?php echo COUPON_AMOUNT; ?></td>
        <td align="left"><?php echo $HTTP_POST_VARS['coupon_amount']; ?></td>
      </tr>
 
      <tr>
        <td align="left"><?php echo COUPON_MIN_ORDER; ?></td>
        <td align="left"><?php echo $HTTP_POST_VARS['coupon_min_order']; ?></td>
      </tr>

      <tr>
        <td align="left"><?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"><?php echo $coupon_code; ?></td>
      </tr>
      
      <tr>
        <td align="left"><?php echo COUPON_USES_COUPON; ?></td>
        <td align="left"><?php echo $HTTP_POST_VARS['coupon_uses_coupon']; ?></td>
      </tr>

      <tr>
        <td align="left"><?php echo COUPON_USES_USER; ?></td>
        <td align="left"><?php echo $HTTP_POST_VARS['coupon_uses_user']; ?></td>
      </tr>
      
       <tr>
        <td align="left"><?php echo COUPON_PRODUCTS; ?></td>
        <td align="left"><?php echo $HTTP_POST_VARS['coupon_products']; ?></td>
      </tr>


      <tr>
        <td align="left"><?php echo COUPON_CATEGORIES; ?></td>
        <td align="left"><?php echo $HTTP_POST_VARS['coupon_categories']; ?></td>
      </tr>
      <tr>
        <td align="left"><?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"><?php echo $start_date; ?></td>
      </tr>
      
      <tr>
        <td align="left"><?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"><?php echo $finish_date; ?></td>
      </tr>
<?php
echo tep_draw_hidden_field('coupon_name', $HTTP_POST_VARS['coupon_name']);
echo tep_draw_hidden_field('coupon_desc', $HTTP_POST_VARS['coupon_desc']);
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_code', $c_code);
echo tep_draw_hidden_field('coupon_uses_coupon', $HTTP_POST_VARS['coupon_uses_coupon']);
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_startdate', $start_date);
echo tep_draw_hidden_field('coupon_finishdate', $finish_date);
?>
      <tr>
        <td align="left"><?php echo tep_image_submit('button_confirm.gif',COUPON_BUTTON_CONFIRM); ?></td>
        <td align="left"><?php echo tep_image_submit('button_back.gif',COUPON_BUTTON_BACK, 'name=back'); ?></td>
      </td>
      </tr>
      
      </td></table></form>
      </tr>

      </table></td>
<?php      
   
      break;
      case 'voucheredit':
      $coupon_query=tep_db_query("select name, description, code, amount, type, min_order, startdate, finishdate, uses_coupon, uses_user, products, categories from " . TABLE_COUPON . " where cid = '" . $HTTP_GET_VARS['cid'] . "'");
      $coupon=tep_db_fetch_array($coupon_query);
      $coupon_name = $coupon['name'];
      $coupon_desc = $coupon['description'];
      $coupon_amount = $coupon['amount'];
      if ($coupon['type']=='P') {
        $coupon_amount .= '%';
      }
      $coupon_min_order = $coupon['min_order'];
      $coupon_code = $coupon['code'];
      $coupon_uses_coupon = $coupon['uses_coupon'];
      $coupon_uses_user = $coupon['uses_user'];
      $coupon_products = $coupon['products'];
      $coupon_categories = $coupon['categories'];
      $coupon_startdate = split("[- :]", $coupon['startdate']);
      $coupon_finishdate = split("[- :]", $coupon['finishdate']);
      
      case 'new':
// set some defaults
if (!$coupon_uses_user) $coupon_uses_user=1;
?>
      <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
      <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>
<?php echo tep_draw_form('coupon', 'coupon_admin.php', 'action=update&oldaction='.$HTTP_GET_VARS['action'] . '&cid=' . $HTTP_GET_VARS['cid']); ?>
      <table border="0" width="100%" cellspacing="0" cellpadding="6">
      <tr>
        <td align="left"><?php echo COUPON_NAME; ?></td>
        <td align="left"><?php echo tep_draw_input_field('coupon_name', $coupon_name); ?></td>
        <td align="left"><?php echo COUPON_NAME_HELP; ?></td>
      </tr>

      <tr>
        <td align="left"><?php echo COUPON_DESC; ?></td>
        <td align="left"><?php echo tep_draw_textarea_field('coupon_desc','physical','24','3', $coupon_desc); ?></td>
        <td align="left"><?php echo COUPON_DESC_HELP; ?></td>
      </tr>

      <tr>
        <td align="left"><?php echo COUPON_AMOUNT; ?></td>
        <td align="left"><?php echo tep_draw_input_field('coupon_amount', $coupon_amount); ?></td>
        <td align="left"><?php echo COUPON_AMOUNT_HELP; ?></td>
      </tr>
 
      <tr>
        <td align="left"><?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"><?php echo COUPON_MIN_ORDER_HELP; ?></td>
      </tr>

      <tr>
        <td align="left"><?php echo COUPON_CODE; ?></td>
        <td align="left"><?php echo tep_draw_input_field('coupon_code', $coupon_code); ?></td>
        <td align="left"><?php echo COUPON_CODE_HELP; ?></td>
      </tr>
      
      <tr>
        <td align="left"><?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"><?php echo COUPON_USES_COUPON_HELP; ?></td>
      </tr>

      <tr>
        <td align="left"><?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"><?php echo COUPON_USES_USER_HELP; ?></td>
      </tr>
      
       <tr>
        <td align="left"><?php echo COUPON_PRODUCTS; ?></td>
        <td align="left"><?php echo tep_draw_input_field('coupon_products', $coupon_products); ?> <A HREF="validproducts.php" TARGET="_blank" ONCLICK="window.open('validproducts.php', 'Valid_Products', 'scrollbars=yes,resizable=yes,menubar=yes,width=600,height=600'); return false">View</A></td>
        <td align="left"><?php echo COUPON_PRODUCTS_HELP; ?></td>
      </tr>


      <tr>
        <td align="left"><?php echo COUPON_CATEGORIES; ?></td>
        <td align="left"><?php echo tep_draw_input_field('coupon_categories', $coupon_categories); ?> <A HREF="validcategories.php" TARGET="_blank" ONCLICK="window.open('validcategories.php', 'Valid_Categories', 'scrollbars=yes,resizable=yes,menubar=yes,width=600,height=600'); return false">View</A></td>
        <td align="left"><?php echo COUPON_CATEGORIES_HELP; ?></td>
      </tr>
      <tr>
        <td align="left"><?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"><?php echo COUPON_STARTDATE_HELP; ?></td>
      </tr>
      
      <tr>
        <td align="left"><?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"><?php echo COUPON_FINISHDATE_HELP; ?></td>
      </tr>

      <tr>
        <td align="left"><?php echo tep_image_submit('button_preview.gif',COUPON_BUTTON_PREVIEW); ?></td>
        <td align="left"><?php echo '&nbsp;&nbsp;<a href="' . tep_href_link('coupon_admin.php', ''); ?>"><?php echo tep_image_button('button_cancel.gif', IMAGE_CANCEL); ?></a>
      </td>
      </tr>
      
      </td></table></form>
      </tr>

      </table></td>
<?php
      break;
      default:
?>    
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
            <td align="right"><table border="0" width="100%" cellspacing="0" cellpadding="0">
              <tr><?php echo tep_draw_form('status', FILENAME_COUPON, '', 'get'); ?>
<?php
$status_array = array();
$status_array[] = array('id' => 'Y', 'text' => TEXT_COUPON_ACTIVE);
$status_array[] = array('id' => '', 'text' => TEXT_COUPON_ALL);
$status_array[] = array('id' => 'N', 'text' => TEXT_COUPON_INACTIVE);
if ($HTTP_GET_VARS['status']) {
  $status = $HTTP_GET_VARS['status'];
} else {
  $status = 'Y';
}
?>
                <td class="smallText" align="right"><?php echo tep_draw_hidden_field(tep_session_name(), tep_session_id()); ?><?php echo HEADING_TITLE_STATUS . ' ' . tep_draw_pull_down_menu('status', $status_array, $status, 'onChange="this.form.submit();"'); ?></td>
              </form></tr>            
            </table></td>

          </tr>
        </table></td>
      </tr>
      <tr>
        <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td><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>
<?php
  if ($HTTP_GET_VARS['page'] > 1) $rows = $HTTP_GET_VARS['page'] * 20 - 20;
  if ($HTTP_GET_VARS['status']) {
   $status = tep_db_prepare_input($HTTP_GET_VARS['status']);
   $cc_query_raw = "select cid, name, code, amount, type, startdate,finishdate,uses_user,uses_coupon,use_count,products, categories, date_created,date_modified from cc_coupons where coupon_active='" . tep_db_input($status) . "'";
  } else {
   $cc_query_raw = "select cid, name, code, amount, type, startdate,finishdate,uses_user,uses_coupon,use_count,products, categories, date_created,date_modified from cc_coupons where coupon_active='Y'";  
  } 
  $cc_split = new splitPageResults($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['cid']) || (@$HTTP_GET_VARS['cid'] == $cc_list['cid'])) && (!$cInfo)) {
    $cInfo = new objectInfo($cc_list);
    }
if ( (is_object($cInfo)) && ($cc_list['cid'] == $cInfo->cid) ) {
        echo '          <tr class="dataTableRowSelected" onmouseover="this.style.cursor=\'hand\'" onclick="document.location.href=\'' . tep_href_link('coupon_admin.php', tep_get_all_get_params(array('cid', 'action')) . 'cid=' . $cInfo->cid . '&action=edit') . '\'">' . "\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('coupon_admin.php', tep_get_all_get_params(array('cid', 'action')) . 'cid=' . $cc_list['cid']) . '\'">' . "\n";
      }
?>
                <td class="dataTableContent"><?php echo $cc_list['name']; ?></td>
                <td class="dataTableContent" align="center">
                <?php  
                if ($cc_list['type'] == 'P') {
                  echo $cc_list['amount'] . '%';
                } else {
                  echo $currencies->format($cc_list['amount']);
                }
                ?>&nbsp;</td>
                <td class="dataTableContent" align="center"><?php echo $cc_list['code']; ?></td>
              </tr>
<?php
  }
?>
            </table></td>
          </td>
          <tr>
            <td colspan="4"><table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr>
                <td class="smallText">&nbsp;<?php echo $cc_split->display_count($cc_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $HTTP_GET_VARS['page'], TEXT_DISPLAY_NUMBER_OF_PRODUCTS); ?>&nbsp;</td>
                <td align="right" class="smallText">&nbsp;<?php echo $cc_split->display_links($cc_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $HTTP_GET_VARS['page']); ?>&nbsp;</td>
              </tr>

              <tr>
                <td align="right" colspan="2" class="smallText"><?php echo '<a href="' . tep_href_link('coupon_admin.php', 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $cInfo->cID . '&action=new') . '">' . tep_image_button('button_insert.gif', IMAGE_INSERT) . '</a>'; ?></td>
              </tr>

            </table></td>

          </tr>
        </table></td>
<?php
    $heading = array();
    $contents = array();
    switch ($HTTP_GET_VARS['action']) {
      case 'release':
      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:
    $heading[] = array('text'=>'['.$cInfo->cid.']  '.$cInfo->code);
    $amount = $cInfo->amount;
    if ($cInfo->type == 'P') {
      $amount .= '%';
    } else {
      $amount = $currencies->format($amount);
    }
    if ($HTTP_GET_VARS['action'] == 'voucherdelete') {
      $contents[] = array('text'=> TEXT_CONFIRM_DELETE . '</br></br>' . 
                    '<a href="'.tep_href_link('coupon_admin.php','action=confirmdelete&cid='.$cInfo->cid,'NONSSL').'">'.tep_image_button('button_confirm.gif','Confirm Delete Voucher').'</a>' .
                    '<a href="'.tep_href_link('coupon_admin.php','cid='.$cInfo->cid,'NONSSL').'">'.tep_image_button('button_cancel.gif','Cancel').'</a>'
                    );
    } else { 
      $prod_details = NONE;
      if ($cInfo->products) {
        $prod_details = '<A HREF="listproducts.php?cid=' . $cInfo->cid . '" TARGET="_blank" ONCLICK="window.open(\'listproducts.php?cid=' . $cInfo->cid . '\', \'Valid_Categories\', \'scrollbars=yes,resizable=yes,menubar=yes,width=600,height=600\'); return false">View</A>';
      }     
      $cat_details = NONE;
      if ($cInfo->categories) {
        $cat_details = '<A HREF="listcategories.php?cid=' . $cInfo->cid . '" TARGET="_blank" ONCLICK="window.open(\'listcategories.php?cid=' . $cInfo->cid . '\', \'Valid_Categories\', \'scrollbars=yes,resizable=yes,menubar=yes,width=600,height=600\'); return false">View</A>';
      }       
      $contents[] = array('text'=>COUPON_NAME . '&nbsp;::&nbsp; ' . $cInfo->name . '<br>' .
                        COUPON_AMOUNT . '&nbsp;::&nbsp; ' . $amount . '<br>' .
                        COUPON_STARTDATE . '&nbsp;::&nbsp; ' . tep_date_short($cInfo->startdate) . '<br>' .
                        COUPON_FINISHDATE . '&nbsp;::&nbsp; ' . tep_date_short($cInfo->finishdate) . '<br>' .
                        COUPON_USES_COUPON . '&nbsp;::&nbsp; ' . $cInfo->uses_coupon . '<br>' .
                        COUPON_USES_USER . '&nbsp;::&nbsp; ' . $cInfo->uses_user . '<br>' .
                        VOUCHER_NUMBER_USED . '&nbsp;::&nbsp; ' . $cInfo->use_count . '<br>' .
                        COUPON_PRODUCTS . '&nbsp;::&nbsp; ' . $prod_details . '<br>' .
                        COUPON_CATEGORIES . '&nbsp;::&nbsp; ' . $cat_details . '<br>' .
                        DATE_CREATED . '&nbsp;::&nbsp; ' . tep_date_short($cInfo->date_created) . '<br>' .
                        DATE_MODIFIED . '&nbsp;::&nbsp; ' . tep_date_short($cInfo->date_modified) . '<br><br>' .
                        '<center><a href="'.tep_href_link('coupon_admin.php','action=email&cid='.$cInfo->cid,'NONSSL').'">'.tep_image_button('button_email.gif','Email Voucher').'</a>' .
                        '<a href="'.tep_href_link('coupon_admin.php','action=voucheredit&cid='.$cInfo->cid,'NONSSL').'">'.tep_image_button('button_edit.gif','Edit Voucher').'</a>' .
                        '<a href="'.tep_href_link('coupon_admin.php','action=voucherdelete&cid='.$cInfo->cid,'NONSSL').'">'.tep_image_button('button_delete.gif','Delete Voucher').'</a>' .
                        '<br><a href="'.tep_href_link('coupon_admin.php','action=voucherreport&cid='.$cInfo->cid,'NONSSL').'">'.tep_image_button('button_report.gif','Voucher Report').'</a></center>'
                        );
    }
    break;
}
?>                       
    <td width="25%" valign="top">
<?php
    $box = new box;
    echo $box->infoBox($heading, $contents);
?>
      </td>
      </tr>
    </table></td>
<?php
}
?>
<!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
