<?php

/*

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

Copyright (c) 2003 osCommerce 

osCommRes, Services Oline 
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');
  tep_get_last_access_file();require(DIR_WS_CLASSES . 'currencies.php');
  $currencies = new currencies();
  $command=isset($HTTP_GET_VARS['command'])?tep_db_prepare_input($HTTP_GET_VARS['command']):'';
  
  
  $affiliates_payment_id = isset($HTTP_GET_VARS['id'])?(int)$HTTP_GET_VARS['id']:'';
  if($command != ''){
  if($command=='get_details'){
  		echo get_default_details($affiliates_payment_id);
  }else if($command=='delete'){
  	$affiliate_payment_delete_query = tep_db_query("select affiliate_firstname,affiliate_lastname from " . TABLE_AFFILIATE_PAYMENT . " where affiliate_payment_id =" . (int)$affiliates_payment_id);
		$affiliate_name = tep_db_fetch_array($affiliate_payment_delete_query);
		$affiliate_delete_string = 'delete';
		$affiliate_delete_string .= '<table border="0" cellspacing="0" cellpadding="0" width="100%">';
		$affiliate_delete_string .= '<tr><td class="main" align="center">'.tep_image('images/icons/delete_blue.gif').'</td><td class="inner_title">Delete Affiliate Payment</td></tr>';
		$affiliate_delete_string .= '<tr><td>'.'&nbsp;'.'</td><td class="main">'.TEXT_INFO_DELETE_INTRO. '</td></tr>';
		$affiliate_delete_string .= '<tr><td>&nbsp;</td><td class="main"><b>'.$affiliate_name['affiliate_firstname'] . '&nbsp;'. $affiliate_name['affiliate_lastname'] .'</b></td></tr>';
		$affiliate_delete_string .='<tr><td colspan="2">'.tep_draw_separator('pixel_trans.gif',50,10).'</td></tr></table>';
		echo $affiliate_delete_string;
	}else if($command=='delete_confirm'){
		$pID = isset($HTTP_GET_VARS['id'])?$HTTP_GET_VARS['id']:'';
		tep_db_query("delete from " . TABLE_AFFILIATE_PAYMENT . " where affiliate_payment_id = '" . tep_db_input($pID) . "'");
      	tep_db_query("delete from " . TABLE_AFFILIATE_PAYMENT_STATUS_HISTORY . " where affiliate_payment_id = '" . tep_db_input($pID) . "'");
		echo 'success delete';
  }else if($command=='edit'){
  		echo 'edit';
		$pID = isset($HTTP_GET_VARS['id'])?$HTTP_GET_VARS['id']:'';
    	$payments_query = tep_db_query("select p.*,  a.affiliate_payment_check, a.affiliate_payment_paypal, a.affiliate_payment_bank_name, a.affiliate_payment_bank_branch_number, a.affiliate_payment_bank_swift_code, a.affiliate_payment_bank_account_name, a.affiliate_payment_bank_account_number from " .  TABLE_AFFILIATE_PAYMENT . " p, " . TABLE_AFFILIATE . " a where affiliate_payment_id = '" . tep_db_input($pID) . "' and a.affiliate_id = p.affiliate_id");
   	 	$payments_exists = true;
    	if (!$payments = tep_db_fetch_array($payments_query)) {
      		$payments_exists = false;
      		$messageStack->add(sprintf(ERROR_PAYMENT_DOES_NOT_EXIST, $pID), 'error');
   		 }
		 $payments_statuses = array();
  		$payments_status_array = array();
  		$payments_status_query = tep_db_query("select affiliate_payment_status_id, affiliate_payment_status_name from " . TABLE_AFFILIATE_PAYMENT_STATUS . " where affiliate_language_id = '" . (int)$languages_id . "'");
  		while ($payments_status = tep_db_fetch_array($payments_status_query)) {
   		 $payments_statuses[] = array('id' => $payments_status['affiliate_payment_status_id'],
                                 'text' => $payments_status['affiliate_payment_status_name']);
    	 $payments_status_array[$payments_status['affiliate_payment_status_id']] = $payments_status['affiliate_payment_status_name'];
  		}
  		$affiliate_address['firstname'] = $payments['affiliate_firstname'];
		$affiliate_address['lastname'] = $payments['affiliate_lastname'];
		$affiliate_address['street_address'] = $payments['affiliate_street_address'];
		$affiliate_address['suburb'] = $payments['affiliate_suburb'];
		$affiliate_address['city'] = $payments['affiliate_city'];
		$affiliate_address['state'] = $payments['affiliate_state'];
		$affiliate_address['country'] = $payments['affiliate_country'];
		$affiliate_address['postcode'] = $payments['affiliate_postcode'];?>
		
		<tr>
        <td><table width="100%" border="0" cellspacing="0" cellpadding="2">
          <tr>
            <td colspan="2"><?php echo tep_draw_separator(); ?></td>
          </tr>
          <tr>
            <td valign="top"><table border="0" cellspacing="0" cellpadding="2">
              <tr>
                <td class="main" valign="top"><b><?php echo TEXT_AFFILIATE; ?></b></td>
                <td class="main"><?php echo tep_address_format($payments['affiliate_address_format_id'], $affiliate_address, 1, '&nbsp;', '<br>'); ?></td>
              </tr>
              <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
              </tr>
              <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
              </tr>
              <tr>
                <td class="main"><b><?php echo TEXT_AFFILIATE_PAYMENT; ?></b></td>
                <td class="main">&nbsp;<?php echo $currencies->format($payments['affiliate_payment_total']); ?></td>
              </tr>
              <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
              </tr>
              <tr>
                <td class="main"><b><?php echo TEXT_AFFILIATE_BILLED; ?></b></td>
                <td class="main">&nbsp;<?php echo format_date($payments['affiliate_payment_date']); ?></td>
              </tr>
              <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
              </tr>
              <tr>
                <td class="main" valign="top"><b><?php echo TEXT_AFFILIATE_PAYING_POSSIBILITIES; ?></b></td>
                <td class="main"><table border="1" cellspacing="0" cellpadding="5">
                  <tr>
<?php
  if (AFFILIATE_USE_BANK == 'true') {
?>
                    <td class="main"  valign="top"><?php echo '<b>' . TEXT_AFFILIATE_PAYMENT_BANK_TRANSFER . '</b><br><br>' . TEXT_AFFILIATE_PAYMENT_BANK_NAME . ' ' . $payments['affiliate_payment_bank_name'] . '<br>' . TEXT_AFFILIATE_PAYMENT_BANK_BRANCH_NUMBER . ' ' . $payments['affiliate_payment_bank_branch_number'] . '<br>' . TEXT_AFFILIATE_PAYMENT_BANK_SWIFT_CODE . ' ' . $payments['affiliate_payment_bank_swift_code'] . '<br>' . TEXT_AFFILIATE_PAYMENT_BANK_ACCOUNT_NAME . ' ' . $payments['affiliate_payment_bank_account_name'] . '<br>' . TEXT_AFFILIATE_PAYMENT_BANK_ACCOUNT_NUMBER . ' ' . $payments['affiliate_payment_bank_account_number'] . '<br>'; ?></td>
<?php
  }
  if (AFFILIATE_USE_PAYPAL == 'true') {
?>
                    <td class="main"  valign="top"><?php echo '<b>' . TEXT_AFFILIATE_PAYMENT_PAYPAL . '</b><br><br>' . TEXT_AFFILIATE_PAYMENT_PAYPAL_EMAIL . '<br>' . $payments['affiliate_payment_paypal'] . '<br>'; ?></td>
<?php
  }
  if (AFFILIATE_USE_CHECK == 'true') {
?>
                    <td class="main"  valign="top"><?php echo '<b>' . TEXT_AFFILIATE_PAYMENT_CHECK . '</b><br><br>' . TEXT_AFFILIATE_PAYMENT_CHECK_PAYEE . '<br>' . $payments['affiliate_payment_check'] . '<br>'; ?></td>
<?php
  }
?>
                  </tr>
                </table></td>
              </tr>
            </table></td>
          </tr>
        </table></td>
      </tr>
	  <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr>

        <td><?php //echo tep_draw_form('statuss', FILENAME_AFFILIATE_PAYMENT, tep_get_all_get_params(array('action')) . 'action=update_payment'); ?>
		<table border="0" cellspacing="0" cellpadding="2">
          <tr>
            <td><table border="0" cellspacing="0" cellpadding="2">
              <tr>
                <td class="main"><b><?php echo PAYMENT_STATUS; ?></b> <?php echo tep_draw_pull_down_menu('statuss', $payments_statuses, $payments['affiliate_payment_status'],'id=statuss'); ?></td>
              </tr>
              <tr>
                <td class="main"><b><?php echo PAYMENT_NOTIFY_AFFILIATE; ?></b><?php echo tep_draw_checkbox_field('notify', '', true,'id="notify"'); ?></td>
              </tr>
            </table></td>
            <td valign="top"><?php echo tep_image_button('button_update.gif', IMAGE_UPDATE,'onclick="javscript:change_status('.$pID.');"'); ?></td>
          </tr>
        </table>
		</td></tr>

      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr>
        <td class="main"><table border="1" cellspacing="0" cellpadding="5">
          <tr>
            <td class="smallText" align="center"><b><?php echo TABLE_HEADING_NEW_VALUE; ?></b></td>
            <td class="smallText" align="center"><b><?php echo TABLE_HEADING_OLD_VALUE; ?></b></td>
            <td class="smallText" align="center"><b><?php echo TABLE_HEADING_DATE_ADDED; ?></b></td>
            <td class="smallText" align="center"><b><?php echo TABLE_HEADING_AFFILIATE_NOTIFIED; ?></b></td>
          </tr>
<?php
    $affiliate_history_query = tep_db_query("select affiliate_new_value, affiliate_old_value, affiliate_date_added, affiliate_notified from " . TABLE_AFFILIATE_PAYMENT_STATUS_HISTORY . " where affiliate_payment_id = '" . tep_db_input($pID) . "' order by affiliate_status_history_id desc");
    if (tep_db_num_rows($affiliate_history_query)) {
      while ($affiliate_history = tep_db_fetch_array($affiliate_history_query)) {
        echo '          <tr>' . "\n" .
             '            <td class="smallText">' . $payments_status_array[$affiliate_history['affiliate_new_value']] . '</td>' . "\n" .
             '            <td class="smallText">' . (tep_not_null($affiliate_history['affiliate_old_value']) ? $payments_status_array[$affiliate_history['affiliate_old_value']] : '&nbsp;') . '</td>' . "\n" .
             '            <td class="smallText" align="center">' . format_date($affiliate_history['affiliate_date_added']) . '</td>' . "\n" .
             '            <td class="smallText" align="center">';
        if ($affiliate_history['affiliate_notified'] == '1') {
          echo tep_image(DIR_WS_ICONS . 'tick.gif', ICON_TICK);
        } else {
          echo tep_image(DIR_WS_ICONS . 'cross.gif', ICON_CROSS);
        }
        echo '          </tr>' . "\n";
      }
    } else {
        echo '          <tr>' . "\n" .
             '            <td class="smallText" colspan="4">' . TEXT_NO_PAYMENT_HISTORY . '</td>' . "\n" .
             '          </tr>' . "\n";
    }
?>
        </table></td>
      </tr>
      <tr>
        <td colspan="2" align="right"><?php echo '<a href="' . tep_href_link(FILENAME_AFFILIATE_INVOICE, 'pID=' . $pID) . '" TARGET="_blank">' . tep_image_button('button_invoices.gif', IMAGE_ORDERS_INVOICE) . '</a>';?></td>
      </tr>
	  <tr>
	  		<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif',50,20); ?></td>
	  </tr>
	<?php 	
 
  }else if($command=='status'){
  		$pID = isset($HTTP_GET_VARS['id'])?(int)$HTTP_GET_VARS['id']:'';
     	$status = isset($HTTP_GET_VARS['st_name'])?tep_db_prepare_input($HTTP_GET_VARS['st_name']):'';
		$notify = isset($HTTP_GET_VARS['notify'])?tep_db_prepare_input($HTTP_GET_VARS['notify']):'';

      $payment_updated = false;
      $check_status_query = tep_db_query("select af.affiliate_email_address, ap.affiliate_lastname, ap.affiliate_firstname, ap.affiliate_payment_status, ap.affiliate_payment_date, ap.affiliate_payment_date from " . TABLE_AFFILIATE_PAYMENT . " ap, " . TABLE_AFFILIATE . " af where affiliate_payment_id = '" . tep_db_input($pID) . "' and af.affiliate_id = ap.affiliate_id ");
      $check_status = tep_db_fetch_array($check_status_query);
      if ($check_status['affiliate_payment_status'] != $status) {
        tep_db_query("update " . TABLE_AFFILIATE_PAYMENT . " set affiliate_payment_status = '" . tep_db_input($status) . "', affiliate_last_modified = now() where affiliate_payment_id = '" . tep_db_input($pID) . "'");
        $affiliate_notified = '0';
// Notify Affiliate
        if ($notify == 'on') {
          $email = STORE_NAME . "\n" . EMAIL_SEPARATOR . "\n" . EMAIL_TEXT_AFFILIATE_PAYMENT_NUMBER . ' ' . $pID . "\n" . EMAIL_TEXT_INVOICE_URL . ' ' . tep_catalog_href_link(FILENAME_CATALOG_AFFILIATE_PAYMENT_INFO, 'payment_id=' . $pID, 'SSL') . "\n" . EMAIL_TEXT_PAYMENT_BILLED . ' ' . tep_date_long($check_status['affiliate_payment_date']) . "\n\n" . sprintf(EMAIL_TEXT_STATUS_UPDATE, $payments_status_array[$status]);
          tep_mail($check_status['affiliate_firstname'] . ' ' . $check_status['affiliate_lastname'], $check_status['affiliate_email_address'], EMAIL_TEXT_SUBJECT, nl2br($email), STORE_OWNER, AFFILIATE_EMAIL_ADDRESS);
          $affiliate_notified = '1';
        }

        tep_db_query("insert into " . TABLE_AFFILIATE_PAYMENT_STATUS_HISTORY . " (affiliate_payment_id, affiliate_new_value, affiliate_old_value, affiliate_date_added, affiliate_notified) values ('" . tep_db_input($pID) . "', '" . tep_db_input($status) . "', '" . $check_status['affiliate_payment_status'] . "', now(), '" . $affiliate_notified . "')");
        $order_updated = true;
      }

      if ($order_updated) {
       $messageStack->add_session(SUCCESS_PAYMENT_UPDATED, 'success');
      }
 	echo 'status'.$pID;
  }else if(($command=='page_navigation') || ($command=='st_change')){
  		$status=isset($HTTP_GET_VARS['status'])?tep_db_prepare_input($HTTP_GET_VARS['status']):'';
		$where="";
  	  // Search only payment_id by now
     if ($HTTP_GET_VARS['sID'])
	 	$where .= " and p.affiliate_payment_id = '" . tep_db_input($sID) . "' ";
	if(is_numeric($status))
		$where.= " and s.affiliate_payment_status_id = '" . tep_db_input($status) . "'";
		
	 
	 /*if ($HTTP_GET_VARS['sID']) {
	 	// $sID = tep_db_prepare_input($HTTP_GET_VARS['sID']);
		
      	$payments_query_raw = "select p.* , s.affiliate_payment_status_name from " . TABLE_AFFILIATE_PAYMENT . " p , " . TABLE_AFFILIATE_PAYMENT_STATUS . " s where p.affiliate_payment_id = '" . tep_db_input($sID) . "' and p.affiliate_payment_status = s.affiliate_payment_status_id and s.affiliate_language_id = '" . $languages_id . "' order by p.affiliate_payment_id DESC";
		echo $payments_query_raw;
    } elseif (is_numeric($status)) {
	
      	$payments_query_raw = "select p.* , s.affiliate_payment_status_name from " . TABLE_AFFILIATE_PAYMENT . " p , " . TABLE_AFFILIATE_PAYMENT_STATUS . " s where s.affiliate_payment_status_id = '" . tep_db_input($status) . "' and p.affiliate_payment_status = s.affiliate_payment_status_id and s.affiliate_language_id = '" . $languages_id . "' order by p.affiliate_payment_id DESC";
		echo $payments_query_raw; 
    } else {*/
		
		if($HTTP_GET_VARS['acID']>0) $where.= " and  p.affiliate_id='". (int)$HTTP_GET_VARS['acID'] . "'";
		
  	 	$payments_query_raw = "select p.* , s.affiliate_payment_status_name from " . TABLE_AFFILIATE_PAYMENT . " p , " . TABLE_AFFILIATE_PAYMENT_STATUS . " s where p.affiliate_payment_status = s.affiliate_payment_status_id and s.affiliate_language_id = '" . (int)$languages_id . "' " . $where . " order by p.affiliate_payment_id DESC";
		//echo $payments_query_raw;
	//	} 
		  $payments_split = new splitPageResultsEvent($HTTP_GET_VARS['page'], MAX_DISPLAY_SEARCH_RESULTS, $payments_query_raw, $payments_query_numrows);
		   $payments_query = tep_db_query($payments_query_raw);
		 if($command=='st_change') echo 'st_change'; else  echo 'page_navigation';?>
		
		 <table border="0" cellpadding="0" cellspacing="0" width="100%">
		 
 <?php   if(tep_db_num_rows($payments_query)>0){
   while ($payments = tep_db_fetch_array($payments_query)) {
      if (((!$HTTP_GET_VARS['pID']) || ($HTTP_GET_VARS['pID'] == $payments['affiliate_payment_id'])) && (!$pInfo)) {
        $pInfo = new objectInfo($payments);
      }
	  echo '  <tr  class="dataTableRow" onmouseover="this.className=\'dataTableRowOver\';this.style.cursor=\'hand\'" onmouseout="this.className=\'dataTableRow\'" onclick="javascript:if(document.getElementById(\'head_'.$payments['affiliate_payment_id'].'\').style.display==\'none\') expand_category(' . $payments['affiliate_payment_id'] . ',3)">' . "\n";
	  ?>
	  <td >
		<div id='<?php echo 'data_' . $payments['affiliate_payment_id'];  ?>'>
			<table border="0" cellpadding="4" cellspacing="0" width="100%">
				<tr>
                <td class="dataTableContent" width="30%"><?php echo $payments['affiliate_firstname'] . ' ' . $payments['affiliate_lastname']; ?></td>
                <td class="dataTableContent" width="20%" align="right"><?php echo $currencies->format(strip_tags($payments['affiliate_payment'])); ?></td>
                <td class="dataTableContent" width="20%" align="right"><?php echo $currencies->format(strip_tags($payments['affiliate_payment'] + $payments['affiliate_payment_tax'])); ?></td>
                <td class="dataTableContent"  width="20%" align="center"><?php echo format_date($payments['affiliate_payment_date']); ?></td>
                <td class="dataTableContent"  width="30%" align="right"><?php echo $payments['affiliate_payment_status_name']; ?></td>
              </tr>
			  
			  </table>
		   </div>	
		   <div id='<?php echo 'head_' . $payments['affiliate_payment_id'];  ?>' style="display:none" class="openContent" ></div>
	 </td>
	 </tr>
<?php
    } //end of while
	} else if($HTTP_GET_VARS['sID']==''){ ?>
	<tr>
		<td class="dataTableContent" align="center"><?php TEXT_PAYMENTS_FOUND;?></td>
	</tr>
	<?php } if($HTTP_GET_VARS['sID']!='') { 
			if(tep_db_num_rows($payments_query)<=0){
	?>
	<tr>
		<td class="dataTableContent" align="center"><?php echo TEXT_NO_RESULTS_FOUND;?></td>
	</tr>
	<?php }?>
	<tr>
		<td><?php echo tep_draw_separator('pixel_trans.gif','10','10');?></td>
	</tr>
	<tr>
		<td align="right"><a href="javascript:expand_category(1,'1','reset')"><?php echo tep_image_button('button_reset.gif', IMAGE_RESET);?></a></td>
	</tr>		
	
<?php 	}
	?>
	<tr> <td>
				<table border="0" width="100%" cellspacing="0" cellpadding="2">
                  <tr>
                    <td class="smallText" valign="top"><?php echo $payments_split->display_count($payments_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $HTTP_GET_VARS['page'], TEXT_DISPLAY_NUMBER_OF_PAYMENTS); ?></td>
                    <td class="smallText" align="right"><?php echo $payments_split->display_script_links($payments_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $HTTP_GET_VARS['page'],1,'expand_category(\'\'','\'\''); ?></td>
			      </tr>

 <?php }
  exit;
  }

  $payments_statuses = array();
  $payments_status_array = array();
  $payments_status_query = tep_db_query("select affiliate_payment_status_id, affiliate_payment_status_name from " . TABLE_AFFILIATE_PAYMENT_STATUS . " where affiliate_language_id = '" . (int)$languages_id . "'");
  while ($payments_status = tep_db_fetch_array($payments_status_query)) {
    $payments_statuses[] = array('id' => $payments_status['affiliate_payment_status_id'],
                                 'text' => $payments_status['affiliate_payment_status_name']);
    $payments_status_array[$payments_status['affiliate_payment_status_id']] = $payments_status['affiliate_payment_status_name'];
  }


  switch ($HTTP_GET_VARS['action']) {
    case 'start_billing':
		$where="";
		if($HTTP_GET_VARS['acID']>0) $where = " and a.affiliate_id='" . (int)$HTTP_GET_VARS['acID'] ."'";
// Billing can be a lengthy process
      tep_set_time_limit(0);
// We are only billing orders which are AFFILIATE_BILLING_TIME days old
      $time = mktime(1, 1, 1, date("m"), date("d") - AFFILIATE_BILLING_TIME, date("Y"));
      $oldday = date("Y-m-d", $time);
// Select all affiliates who earned enough money since last payment
      $sql="
        SELECT a.affiliate_id, sum(a.affiliate_payment) 
          FROM " . TABLE_AFFILIATE_SALES . " a, " . TABLE_ORDERS . " o 
          WHERE a.affiliate_billing_status != 1 and a.affiliate_orders_id = o.orders_id and o.orders_status >= " . AFFILIATE_PAYMENT_ORDER_MIN_STATUS . " and date_format(a.affiliate_date,'%Y-%m-%d') <= '" . $oldday . "' " . $where . "
          GROUP by a.affiliate_id 
          having sum(a.affiliate_payment) >= '" . AFFILIATE_THRESHOLD . "'
        ";
      $affiliate_payment_query = tep_db_query($sql);

// Start Billing:
      while ($affiliate_payment = tep_db_fetch_array($affiliate_payment_query)) {

// mysql does not support joins in update (planned in 4.x)

// Get all orders which are AFFILIATE_BILLING_TIME days old
        $sql="
        SELECT a.affiliate_orders_id 
          FROM " . TABLE_AFFILIATE_SALES . " a, " . TABLE_ORDERS . " o 
          WHERE a.affiliate_billing_status!=1 and a.affiliate_orders_id=o.orders_id and o.orders_status>=" . AFFILIATE_PAYMENT_ORDER_MIN_STATUS . " and a.affiliate_id='" . (int)$affiliate_payment['affiliate_id'] . "' and date_format(a.affiliate_date,'%Y-%m-%d') <= '" . $oldday . "'
        ";
        $affiliate_orders_query=tep_db_query ($sql);
        $orders_id ="(";
        while ($affiliate_orders = tep_db_fetch_array($affiliate_orders_query)) {
          $orders_id .= $affiliate_orders['affiliate_orders_id'] . ",";
        }
        $orders_id = substr($orders_id, 0, -1) .")";

// Set the Sales to Temp State (it may happen that an order happend while billing)
        $sql="UPDATE " . TABLE_AFFILIATE_SALES . " 
        set affiliate_billing_status=99 
          where affiliate_id='" .  (int)$affiliate_payment['affiliate_id'] . "' 
          and affiliate_orders_id in " . $orders_id . " 
        ";
        tep_db_query ($sql);

// Get Sum of payment (Could have changed since last selects);
        $sql="
        SELECT sum(affiliate_payment) as affiliate_payment
          FROM " . TABLE_AFFILIATE_SALES . " 
          WHERE affiliate_id='" .  (int)$affiliate_payment['affiliate_id'] . "' and  affiliate_billing_status=99 
        ";
        $affiliate_billing_query = tep_db_query ($sql);
        $affiliate_billing = tep_db_fetch_array($affiliate_billing_query);
// Get affiliate Informations
        $sql="
        SELECT a.*, c.countries_id, c.countries_name, c.countries_iso_code_2, c.countries_iso_code_3, c.address_format_id 
          from " . TABLE_AFFILIATE . " a 
          left join " . TABLE_ZONES . " z on (a.affiliate_zone_id  = z.zone_id) 
          left join " . TABLE_COUNTRIES . " c on (a.affiliate_country_id = c.countries_id)
          WHERE affiliate_id = '" . (int)$affiliate_payment['affiliate_id'] . "' 
        ";
        $affiliate_query=tep_db_query ($sql);
        $affiliate = tep_db_fetch_array($affiliate_query);

// Get need tax informations for the affiliate
        $affiliate_tax_rate = tep_get_affiliate_tax_rate(AFFILIATE_TAX_ID, $affiliate['affiliate_country_id'], $affiliate['affiliate_zone_id']);
        $affiliate_tax = tep_round(($affiliate_billing['affiliate_payment'] * $affiliate_tax_rate / 100), 2); // Netto-Provision
        $affiliate_payment_total = $affiliate_billing['affiliate_payment'] + $affiliate_tax;
// Bill the order
        $affiliate['affiliate_state'] = tep_get_zone_code($affiliate['affiliate_country_id'], $affiliate['affiliate_zone_id'], $affiliate['affiliate_state']);
        $sql_data_array = array('affiliate_id' => $affiliate_payment['affiliate_id'],
                                'affiliate_payment' => $affiliate_billing['affiliate_payment'],
                                'affiliate_payment_tax' => $affiliate_tax,
                                'affiliate_payment_total' => $affiliate_payment_total,
                                'affiliate_payment_date' => 'now()',
                                'affiliate_payment_status' => '0',
                                'affiliate_firstname' => $affiliate['affiliate_firstname'],
                                'affiliate_lastname' => $affiliate['affiliate_lastname'],
                                'affiliate_street_address' => $affiliate['affiliate_street_address'],
                                'affiliate_suburb' => $affiliate['affiliate_suburb'],
                                'affiliate_city' => $affiliate['affiliate_city'],
                                'affiliate_country' => $affiliate['countries_name'],
                                'affiliate_postcode' => $affiliate['affiliate_postcode'],
                                'affiliate_company' => $affiliate['affiliate_company'],
                                'affiliate_state' => $affiliate['affiliate_state'],
                                'affiliate_address_format_id' => $affiliate['address_format_id']);
        tep_db_perform(TABLE_AFFILIATE_PAYMENT, $sql_data_array);
        $insert_id = tep_db_insert_id(); 
// Set the Sales to Final State 
        tep_db_query("update " . TABLE_AFFILIATE_SALES . " set affiliate_payment_id = '" . (int)$insert_id . "', affiliate_billing_status = 1, affiliate_payment_date = now() where affiliate_id = '" . (int)$affiliate_payment['affiliate_id'] . "' and affiliate_billing_status = 99");

// Notify Affiliate
        if (AFFILIATE_NOTIFY_AFTER_BILLING == 'true') {
          $check_status_query = tep_db_query("select af.affiliate_email_address, ap.affiliate_lastname, ap.affiliate_firstname, ap.affiliate_payment_status, ap.affiliate_payment_date, ap.affiliate_payment_date from " . TABLE_AFFILIATE_PAYMENT . " ap, " . TABLE_AFFILIATE . " af where affiliate_payment_id  = '" . $insert_id . "' and af.affiliate_id = ap.affiliate_id ");
          $check_status = tep_db_fetch_array($check_status_query);
          $email = STORE_NAME . "\n" . EMAIL_SEPARATOR . "\n" . EMAIL_TEXT_AFFILIATE_PAYMENT_NUMBER . ' ' . $insert_id . "\n" . EMAIL_TEXT_INVOICE_URL . ' ' . tep_catalog_href_link(FILENAME_CATALOG_AFFILIATE_PAYMENT_INFO, 'payment_id=' . $insert_id, 'SSL') . "\n" . EMAIL_TEXT_PAYMENT_BILLED . ' ' . tep_date_long($check_status['affiliate_payment_date']) . "\n\n" . EMAIL_TEXT_NEW_PAYMENT;
          tep_mail($check_status['affiliate_firstname'] . ' ' . $check_status['affiliate_lastname'], $check_status['affiliate_email_address'], EMAIL_TEXT_SUBJECT, nl2br($email), STORE_OWNER, AFFILIATE_EMAIL_ADDRESS);
        }
      }
      $messageStack->add_session(SUCCESS_BILLING, 'success');

      tep_redirect(tep_href_link(FILENAME_AFFILIATE_PAYMENT, tep_get_all_get_params(array('action')) . 'action=edit'));
      break;
    case 'update_payment':
      $pID = tep_db_prepare_input($HTTP_GET_VARS['pID']);
      $status = tep_db_prepare_input($HTTP_POST_VARS['status']);

      $payment_updated = false;
      $check_status_query = tep_db_query("select af.affiliate_email_address, ap.affiliate_lastname, ap.affiliate_firstname, ap.affiliate_payment_status, ap.affiliate_payment_date, ap.affiliate_payment_date from " . TABLE_AFFILIATE_PAYMENT . " ap, " . TABLE_AFFILIATE . " af where affiliate_payment_id = '" . tep_db_input($pID) . "' and af.affiliate_id = ap.affiliate_id ");
      $check_status = tep_db_fetch_array($check_status_query);
      if ($check_status['affiliate_payment_status'] != $status) {
        tep_db_query("update " . TABLE_AFFILIATE_PAYMENT . " set affiliate_payment_status = '" . tep_db_input($status) . "', affiliate_last_modified = now() where affiliate_payment_id = '" . tep_db_input($pID) . "'");
        $affiliate_notified = '0';
// Notify Affiliate
        if ($HTTP_POST_VARS['notify'] == 'on') {
          $email = STORE_NAME . "\n" . EMAIL_SEPARATOR . "\n" . EMAIL_TEXT_AFFILIATE_PAYMENT_NUMBER . ' ' . $pID . "\n" . EMAIL_TEXT_INVOICE_URL . ' ' . tep_catalog_href_link(FILENAME_CATALOG_AFFILIATE_PAYMENT_INFO, 'payment_id=' . $pID, 'SSL') . "\n" . EMAIL_TEXT_PAYMENT_BILLED . ' ' . tep_date_long($check_status['affiliate_payment_date']) . "\n\n" . sprintf(EMAIL_TEXT_STATUS_UPDATE, $payments_status_array[$status]);
          tep_mail($check_status['affiliate_firstname'] . ' ' . $check_status['affiliate_lastname'], $check_status['affiliate_email_address'], EMAIL_TEXT_SUBJECT, nl2br($email), STORE_OWNER, AFFILIATE_EMAIL_ADDRESS);
          $affiliate_notified = '1';
        }

        tep_db_query("insert into " . TABLE_AFFILIATE_PAYMENT_STATUS_HISTORY . " (affiliate_payment_id, affiliate_new_value, affiliate_old_value, affiliate_date_added, affiliate_notified) values ('" . tep_db_input($pID) . "', '" . tep_db_input($status) . "', '" . $check_status['affiliate_payment_status'] . "', now(), '" . $affiliate_notified . "')");
        $order_updated = true;
      }

      if ($order_updated) {
       $messageStack->add_session(SUCCESS_PAYMENT_UPDATED, 'success');
      }

      tep_redirect(tep_href_link(FILENAME_AFFILIATE_PAYMENT, tep_get_all_get_params(array('action')) . 'action=edit'));
      break;
    case 'deleteconfirm':
      $pID = tep_db_prepare_input($HTTP_GET_VARS['pID']);

      tep_db_query("delete from " . TABLE_AFFILIATE_PAYMENT . " where affiliate_payment_id = '" . tep_db_input($pID) . "'");
      tep_db_query("delete from " . TABLE_AFFILIATE_PAYMENT_STATUS_HISTORY . " where affiliate_payment_id = '" . tep_db_input($pID) . "'");

      tep_redirect(tep_href_link(FILENAME_AFFILIATE_PAYMENT, tep_get_all_get_params(array('pID', 'action'))));
      break;
  }

  if ( ($HTTP_GET_VARS['action'] == 'edit') && tep_not_null($HTTP_GET_VARS['pID']) ) {
    $pID = tep_db_prepare_input($HTTP_GET_VARS['pID']);
    $payments_query = tep_db_query("select p.*,  a.affiliate_payment_check, a.affiliate_payment_paypal, a.affiliate_payment_bank_name, a.affiliate_payment_bank_branch_number, a.affiliate_payment_bank_swift_code, a.affiliate_payment_bank_account_name, a.affiliate_payment_bank_account_number from " .  TABLE_AFFILIATE_PAYMENT . " p, " . TABLE_AFFILIATE . " a where affiliate_payment_id = '" . tep_db_input($pID) . "' and a.affiliate_id = p.affiliate_id");
    $payments_exists = true;
    if (!$payments = tep_db_fetch_array($payments_query)) {
      $payments_exists = false;
      $messageStack->add(sprintf(ERROR_PAYMENT_DOES_NOT_EXIST, $pID), 'error');
    }
  }
?>
<!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/http.js"></script>
<script language="javascript">
var img_src="<?php echo HTTP_SERVER.DIR_WS_ADMIN.'images/';?>";



function display_payments(){
	var command="";
	var status = document.getElementById('display_status').value;
	command="<?php echo tep_href_link(FILENAME_AFFILIATE_PAYMENT,'command=st_change');?>&status="+status;	
	do_get_command(command);
}
function check_form(){ 
	  var command="";
	  var frm = document.new_affiliate_payment;
	  var id = frm.affiliate_payment_id.value;
	  var span_details=document.getElementById("span_details");
	  var span_edit=document.getElementById("span_edit");				
	  var span_delete=document.getElementById("span_delete");
	  if(span_details && span_details.style.display=="") return false;
	 	 else if(span_delete.style.display==""){					
			span_details.innerHTML=span_edit.innerHTML="";
			command="<?php echo tep_href_link(FILENAME_AFFILIATE_PAYMENT,'command=delete_confirm');?>&id="+id;			
			do_get_command(command);
			//do_post_command('affiliate',command);
			if(span_delete) span_delete.style.display="none";
			return false;
		}
	}
function change_status(id){
	var obj = document.getElementById('statuss');
	var notify_obj = document.getElementById('notify');
	var st_name = obj.value;
	//var st_name = obj.options[obj.selectedIndex].text;
	if(notify_obj.checked) 
	notify = notify_obj.value;
	else notify = "";
	command = '<?php echo tep_href_link(FILENAME_AFFILIATE_PAYMENT,'command=status');  ?>&id='+id+'&st_name='+st_name+'&notify='+notify;
	do_get_command(command);
}
function expand_category(id,category,s,sta){ 	
		if(!sta) sta="";
		if(category=='1' && document.getElementById("page_''") && sta!=""){
 	 		id=parseInt(document.getElementById("page_''").options[document.getElementById("page_''").selectedIndex].value);
 	 		if(sta=='next' && document.getElementById("page_''")) {
 	 			if(document.getElementById("page_''").length==document.getElementById("page_''").selectedIndex+1) return;
 	 			id+=1;
 	 		}else if(sta=='prev') {
 	 			if(document.getElementById("page_''").selectedIndex==0) return;
 	 			id-=1;
 	 		}
 	 		if(id<=1) id=1;
 	 	}else if(category=='1' && sta=="") id="";
 	 	var res_display=document.getElementById("res_display"); 	 	 	 	 	 	 	 	 	 		 	  	 	
 	 	if(res_display) res_display.style.display="none"; 	 		 	 	
 	 	 if(document.new_affiliate_payment && document.new_affiliate_payment.affiliate_payment_id && document.new_affiliate_payment.affiliate_payment_id.value>0)  { 
			var old_ids=document.new_affiliate_payment.affiliate_payment_id.value;			
			var pre_data_id=document.getElementById("data_"+old_ids);
			var pre_head=document.getElementById("head_"+old_ids);			
			var loadings=document.getElementById("loading");
			if(loadings) loadings.style.display="none";
			if(category==1 && pre_head && pre_head.innerHTML!='') pre_head.innerHTML="";									
		 }
		if(category!=1 && document.new_affiliate_payment && document.new_affiliate_payment.affiliate_payment_id && document.new_affiliate_payment.affiliate_payment_id.value=="") document.new_affiliate_payment.affiliate_payment_id.value=id; 	 	
			if(id>0 && document.new_affiliate_payment.affiliate_payment_id.value>0 && document.new_affiliate_payment.affiliate_payment_id.value!=id){			   
			   if(pre_head && pre_head.style.display=="") pre_head.style.display="none";
			   if(pre_data_id && pre_data_id.style.display=="none") pre_data_id.style.display="";
			   document.new_affiliate_payment.affiliate_payment_id.value=id;			   
			}								 								 	 	 	 
 	 	if(category){
 	 		var span_details=document.getElementById("span_details");
 	 		var span_edit=document.getElementById("span_edit");			
			var span_delete=document.getElementById("span_delete");								
			var data_id=document.getElementById("data_"+id);	
			var head=document.getElementById("head_"+id);
			var img_save=document.getElementById("img_save");			
			var command="";																		 
 	 		switch(category){ 	 			
 	 			case '1': 
 	 			       var ser="";
 	 			       var page="";
					   if(document.getElementById('display_status')){
					   		var status = document.getElementById('display_status').value;
					   		status = '&status='+status;
					   }
 	 			     if(document.getElementById("sID") && document.getElementById("sID").value!="" && sta=="" && s=='search') {
					   	ser="&sID="+document.getElementById("sID").value;
						id="";
					   }else if(document.getElementById("sID") && document.getElementById("sID").value!="" && sta=="" && s=='reset'){
					  	 document.getElementById("sID").value="";
					   }else if(document.getElementById("sID") && document.getElementById("sID").value=="" && sta=="" && s=='search'){
						return;
 					   }else if(document.getElementById("sID") && document.getElementById("sID").value=="" && sta=="" && s=='reset') id=1;					   
						
 	 			       if(document.getElementById('fst_nav')) document.getElementById('fst_nav').style.display='none';
					   if(id!="") page="&page="+id; 
					   
 	 			       command="<?php echo tep_href_link(FILENAME_AFFILIATE_PAYMENT,'command=page_navigation');?>"+page+ser+status;
					   if(document.getElementById("ajax_details")) document.getElementById("ajax_details").innerHTML="<img src='images/24-1.gif' alt='Loading...' title='Loading...'>"; 	 			   					   
 	 			break;
 	 			case 3:
 	 			   if(head && head.style.display==""){
 	 			   	  if(span_details.style.display==""){
 	 			   	    head.style.display="none"; 	 			   	    
 	 			   	  	data_id.style.display=""; 	 			   	  	
 	 			   	  }else { 
 	 			   	  	if(span_edit.style.display=="") span_edit.style.display="none";
 	 			   	  	if(span_delete.style.display=="") span_delete.style.display="none"; 	 			   	  	 	 			   	  	
 	 			   	  	span_details.style.display="";
 	 			   	  	if(img_save) img_save.style.display="none"; 
 	 			   	  	if(document.getElementById("img_ids")){
 	 			   	  		var tr_obj=document.getElementById("img_ids");
 	 			   	  		if(tr_obj.childNodes[1].childNodes[0].src==img_src+'template/img_edit_hover.gif')	
 	 			   	  			tr_obj.childNodes[1].childNodes[0].src=img_src+'template/img_edit.gif';
 	 			   	  		if(tr_obj.childNodes[2].childNodes[0].src==img_src+'template/img_trash_hover.gif')	
 	 			   	  			tr_obj.childNodes[2].childNodes[0].src=img_src+'template/img_trash.gif';	 	 			   	  			
 	 			   	  	}	 			   	  	  	 			   	  	 	 			   	  	
 	 			   	  }	 	 			    			   	   	 			   
 	 			 }else {  		 	 	 			 	
 	 			 	if(data_id && data_id.style.display=="") data_id.style.display="none";
 	 			 	if(pre_head && pre_head.style.display=="") pre_head.style.display="none"; 	 			 	
 	 			 	if(head) {
 	 			 	 	head.style.display=""; 	 			 	 	 	 			 	  	 			
 	 			 		head.innerHTML="<img src='images/24-1.gif' alt='Loading...' title='Loading...'>";	 			 		
 	 			 	}
 	 			 	command="<?php echo tep_href_link(FILENAME_AFFILIATE_PAYMENT,'command=get_details');?>&id="+id; 	 			 	
 	 			 } 	 			  	 			  	 			
 	 			break;
 	 			case 4:
						span_edit.style.display=""; 	 					
 	 					span_details.style.display=span_delete.style.display="none";
						
 	 					span_edit.innerHTML="<img src='images/24-1.gif' alt='Loading...' title='Loading...'>"; 	 					
 	 					command="<?php echo tep_href_link(FILENAME_AFFILIATE_PAYMENT,'command=edit');?>&id="+id; 	 		 	 					 	 					 	 				 	 		
 	 			break; 	 			
 	 			case 5:	 	 				 	 				 	 		 	 				
 	 					span_delete.style.display="";
 	 					span_edit.style.display=span_details.style.display="none";
 	 					span_delete.innerHTML="<img src='images/24-1.gif' alt='Loading...' title='Loading...'>";
 	 					command="<?php echo tep_href_link(FILENAME_AFFILIATE_PAYMENT,'command=delete');?>&id="+id; 	 	 	 					 	 					 	 			
 	 			break; 
				case 6:
						//location.href='<?php echo  tep_href_link(FILENAME_AFFILIATE_INVOICE, 'pID=')?>'+id;
						var width=1200;
						var height=1300;
						var left=(screen.availWidth/2)-(width/2);
						var top=(screen.availHeight/2)-(height/2);
						var features="width="+width+",height="+height+",left="+left+",top="+top+",location=no,menubar=no,toobar=no,scrollbar=yes,resizable=yes,status=yes";
						window.open('<?php echo  tep_href_link(FILENAME_AFFILIATE_INVOICE, 'pID=');?>'+id,"Affiliate Payment",features);
				break;	
								
 	 		}
 	 		if(command!="") do_get_command(command);
			
 	 	}		
 	 }
 function do_result(result){  //alert(result);
	
			if(result.substr(0,11)=='get_details') print_details('get_details',result.substr(11));
			 else  if(result.substr(0,4)=='edit') print_details('edit',result.substr(4));
			 else  if(result.substr(0,6)=='delete') print_details('delete',result.substr(6));
			  else  if(result.substr(0,6)=='status') expand_category(result.substr(6),4);
			  else if(result.substr(0,9)=='st_change') print_details('st_change',result.substr(9));
			 else  if(result.substr(0,14)=='success_delete')  print_details('delete_success',result.substr(0,14));
			 else  if(result.substr(0,4)=='mail') print_details('mail',result.substr(4));
			 else  if(result.substr(0,15)=='page_navigation') print_details('page_navigation',result.substr(15));
			 else  if(result.substr(0,6)=='update') print_details('update',result.substr(6));
 }
 function print_details(category,result){ 		
 	 	var no_result=""; 	 	
 	 	var include="";
		
		if(result.substr(0,15)=='No Record Found'){
 	 		no_result="No Record Found";
 	 	}else if(result!='' && result.length>0 && category!='edit' && result.substr(0,15)!='No Record Found'){
			var splt_result = result.split("^");
			var payment_id="";
			var affiliate_name="";
			var payment_date="";
			var affiliate_last_modified="";
			var affiliate_total_payment="";
			var status="";
				
			 payment_id = splt_result[0];
			 affiliate_name = splt_result[1]+'&nbsp;'+splt_result[2];
			 payment_date = splt_result[3];
			 affiliate_last_modified = splt_result[4];
			 affiliate_total_payment = splt_result[5];
			 status = splt_result[6];
		}
		if(document.new_affiliate_payment && document.new_affiliate_payment.affiliate_payment_id) var id=document.new_affiliate_payment.affiliate_payment_id.value; 		
 		var span_details=document.getElementById("span_details");	
 		var span_edit=document.getElementById("span_edit"); 	 		 	 		
 		var span_delete=document.getElementById("span_delete");				 	
 		var head=document.getElementById("head_"+id); 		 
 		var data_id=document.getElementById("data_"+id);
 		var res_display=document.getElementById("res_display");
 		var img_save=document.getElementById("img_save");
		if(img_save && category!='get_details') img_save.style.display=""; 
 	 	switch(category){ 	 		 	 	 	 		
 	 		case 'get_details': 
			 if(head  && no_result!="")
				head.innerHTML=no_result; 	 			 	 		
 	 		else if(head && no_result==""){  
	 	 		head.innerHTML ="<table width='100%' border='0' cellspacing='0' cellpadding='0' style='padding-top:4px;'>"+
				"<tr heigth='40'>"+
				  "<td width='10'></td>"+				  							  
				  "<td align='left' valign='middle' width='350' class='main'>"+affiliate_name+"</td>"+
				  "<td width='470' align='left' class='main' valign='top'>"+
				  	"<table border='0' cellspacing='0' cellpadding='0' style='width:100;height:30px;display:inline;position:relative;'>"+
				  	  "<tr class='img_move' id='img_ids'>"+
				  	  	"<td class='cell_bg_popm_left' width='10'>&nbsp;</td>"+
				  	  	
				  	    "<td width='10%' onclick='javascript:change_class(this,1);expand_category("+payment_id+",4);' onmouseover='javascript:change_class(this,2);' onmouseout='javascript:change_class(this,3);' ><img alt='Edit' title='Edit' src='images/template/img_edit.gif'></td>"+
				  	    "<td width='10%' onclick='javascript:change_class(this,1);expand_category("+payment_id+",5);' onmouseover='javascript:change_class(this,2);' onmouseout='javascript:change_class(this,3);' ><img alt='Delete' title='Delete' src='images/template/img_trash.gif'></td>"+				  	    
				  	    "<td width='10%' onclick='javascript:change_class(this,1);expand_category("+payment_id+",6);' onmouseover='javascript:change_class(this,2);' onmouseout='javascript:change_class(this,3);' ><img alt='Email' title='Email' src='images/template/img_sms.gif'></td>"+
						"<td class='cell_bg_popm_right' width='10'>&nbsp;</td>"+
				  	  "</tr>"+
				  	"</table>"+
				  "</td>"+
				 "<td width='240' align='right' id='img_save' style='display:none'><input type='image' src='images/template/img_savel.gif' alt='save' title='save' id='img_sav' border='0'>&nbsp;&nbsp;<a href='javascript:expand_category("+payment_id+",3);'><img src='images/template/img_closel.gif' alt='close' title='close' border='0'></a>&nbsp;&nbsp;</td>"+
				"</tr>"+
				"<tr style='height:10'><td id='loading' style='display:none' colspan='5'><img src='images/24-1.gif' alt='Loading...' title='Loading...'></td></tr>"+				
				"<tr>"+
				  "<td colspan='8' class='main'><span style='display:none' id='span_edit'></span><span style='display:none' id='span_delete'></span><span id='span_details'>"+	"<table width='100%' border='0' cellspacing='0' cellpadding='0'>"+
				  		"<tr>"+
				  			"<td width='100'>&nbsp;</td>"+				  							  			
							"<td width='700'>"+				  							  			
				  				"<table width='100%' border='0' cellspacing='5' cellpadding='0' class='main'>"+
				  					"<tr>"+
				  						"<td align='left' width='100' valign='top'><?php echo TEXT_PAYMENT_DATE;?></td>"+
				  						"<td align='left' width='100' valign='top'>"+payment_date+"</td>"+
										"<td align='left' width='100'><?php echo TEXT_TOTAL_PAYMENT ;?></td>"+
										"<td align='left' width='250'>"+affiliate_total_payment+"</td>"+
				  					"</tr>"+			  					
				  												
									"<tr>"+
										"<td align='left' width='100'><?php echo TEXT_PAYMENT_LAST_MODIFIED ;?></td>"+
										"<td align='left' width='100'>"+affiliate_last_modified+"</td>"+
										"<td align='left' width='100'><?php echo TEXT_STATUS ;?></td>"+
										"<td align='left' width='250'>"+status+"</td>"+
									"</tr>"+
									
									"<tr>"+
										'<td colspan="4"><?php echo tep_draw_separator('pixel_trans.gif',50,10); ?></td>'+
									"</tr>"+
									
				  			  "</table></td></tr>"+
					"</span>"+
				  "</td>"+
				"</tr>"+
			"</table>";
 	 		}		 	 		
 	 	break;
 	 	case 'edit': 	 		 	 		 	 		 	 		
			if(span_edit) span_edit.style.display="";			
 	 		if(span_edit && span_details){ 	 			 	 			
 	 			span_edit.innerHTML=result; 	 			
 	 			span_details.style.display='none';
				document.getElementById('img_sav').style.display='none';
 	 		}
			
			
 	 	break;
 	 	case 'delete': 	 				 	 	
 	 		if(span_delete) span_delete.style.display="";
			if(document.getElementById('img_sav')) document.getElementById('img_sav').style.display='';
 	 		if(span_delete){ 	 			
 	 			span_delete.innerHTML=result; 	 			
 	 			span_details.style.display=span_edit.style.display='none';
 	 		} 	 								   			   
 	 	break; 	 	
 	 	case 'delete_success':			 
 	    	 if(head && data_id) {
			 	var tbl_obj=head.parentNode.parentNode.parentNode.parentNode;			 				 				 	
			 	if(tbl_obj) tbl_obj.deleteRow(head.parentNode.parentNode.rowIndex);
			 	data_id.innerHTML=head.innerHTML="";
			 	data_id.style.display=head.style.display="none";			 				 			
			 }
 	    	 if(res_display){ 
	    	 	res_display.innerHTML="<?php echo TEXT_DELETE_SUCCESS;?>";
 	    	 	res_display.style.display=""; 	    	 	
 	    	 } 	    	 			 			 	
			 var obj=document.getElementById("page_''");
 	    	 	if(obj){  	    	 		
 	    	 		var len=obj.options[obj.selectedIndex].value;
 	    	 		command="<?php echo tep_href_link(FILENAME_AFFILIATE,'command=page_navigation');?>&page="+len;
					do_get_command(command);
 	    	 	} 		
 	 	break;	
 	 	case 'page_navigation':
 	 		if(document.getElementById("ajax_details"))
 	 		 document.getElementById("ajax_details").innerHTML=result;
 	 	break;	
		
		case 'st_change': 
				if(document.getElementById("fst_nav"))  document.getElementById("fst_nav").style.display='none';
				if(document.getElementById("ajax_details"))
 	 				 document.getElementById("ajax_details").innerHTML=result;
		break;
 	 	}
		
 	 	if(res_display.style.display=="") setTimeout("res_display.style.display='none'",2000); 	 	 	 	
 	 	clearTimeout();
	
 }
 function change_class(obj,eve){ 		 		 	 	
		 var span_details=document.getElementById("span_details"); 
		 var span_edit=document.getElementById("span_edit");
		 var span_delete=document.getElementById("span_delete");				 		 			 
		 if(obj){		   		    		    
		    var img_get_src=obj.childNodes[0];
			if(img_get_src){
			    if(img_get_src.src==img_src+'template/img_copy.gif') {
					if(span_edit && span_edit.style.display=="" && eve==1 || eve==2){
					 	img_get_src.src=img_src+'template/img_copy_hover.gif';
					}  		   		 	 			 	 			
			    }else if(eve==3 && img_get_src.src==img_src+'template/img_copy_hover.gif' && span_edit && span_edit.style.display=="none") img_get_src.src=img_src+'template/img_copy.gif';
			    if(img_get_src.src==img_src+'template/img_edit.gif') {
					if(span_edit && span_edit.style.display=="" && eve==1 || eve==2){
					 	img_get_src.src=img_src+'template/img_edit_hover.gif';
					}  		   		 	 			 	 			
			    }else if(eve==3 && img_get_src.src==img_src+'template/img_edit_hover.gif' && span_edit && span_edit.style.display=="none") img_get_src.src=img_src+'template/img_edit.gif';
			  
			   if(img_get_src.src==img_src+'template/img_sms.gif') {
					if(span_edit && span_edit.style.display=="" && eve==1 || eve==2){
					 	img_get_src.src=img_src+'template/img_sms_hover.gif';
					}  		   		 	 			 	 			
			    }else if(eve==3 && img_get_src.src==img_src+'template/img_sms_hover.gif' && span_edit )	 img_get_src.src=img_src+'template/img_sms.gif';
			  
			    if(img_get_src.src==img_src+'template/img_trash.gif') {
					if(span_delete && span_delete.style.display=="" && eve==1 || eve==2){
					 	img_get_src.src=img_src+'template/img_trash_hover.gif';
					}  		   		 	 			 	 			
			    }else if(eve==3 && img_get_src.src==img_src+'template/img_trash_hover.gif' && span_delete && span_delete.style.display=="none") img_get_src.src=img_src+'template/img_trash.gif';		    		    		     		   	
		  }
		 }		 
	  }	
	     function check_key(e){
     var kc=0;
   	  if (window.event)
	    kc=window.event.keyCode;
	  else if (e)
	 	kc=e.which;
	  else
		kc=0;
	  if(kc==13) expand_category(1,'1','search');
   }

</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> 
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
<?php
  if ( ($HTTP_GET_VARS['action'] == 'edit') && ($payments_exists) ) {
    $affiliate_address['firstname'] = $payments['affiliate_firstname'];
    $affiliate_address['lastname'] = $payments['affiliate_lastname'];
    $affiliate_address['street_address'] = $payments['affiliate_street_address'];
    $affiliate_address['suburb'] = $payments['affiliate_suburb'];
    $affiliate_address['city'] = $payments['affiliate_city'];
    $affiliate_address['state'] = $payments['affiliate_state'];
    $affiliate_address['country'] = $payments['affiliate_country'];
    $affiliate_address['postcode'] = $payments['affiliate_postcode'];
?>
      <tr>
        <td width="100%"><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', 1, HEADING_IMAGE_HEIGHT); ?></td>
            <td class="pageHeading" align="right"><?php echo '<a href="' . tep_href_link(FILENAME_AFFILIATE_PAYMENT, tep_get_all_get_params(array('action'))) . '">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>'; ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><table width="100%" border="0" cellspacing="0" cellpadding="2">
          <tr>
            <td colspan="2"><?php echo tep_draw_separator(); ?></td>
          </tr>
          <tr>
            <td valign="top"><table border="0" cellspacing="0" cellpadding="2">
              <tr>
                <td class="main" valign="top"><b><?php echo TEXT_AFFILIATE; ?></b></td>
                <td class="main"><?php echo tep_address_format($payments['affiliate_address_format_id'], $affiliate_address, 1, '&nbsp;', '<br>'); ?></td>
              </tr>
              <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
              </tr>
              <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
              </tr>
              <tr>
                <td class="main"><b><?php echo TEXT_AFFILIATE_PAYMENT; ?></b></td>
                <td class="main">&nbsp;<?php echo $currencies->format($payments['affiliate_payment_total']); ?></td>
              </tr>
              <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
              </tr>
              <tr>
                <td class="main"><b><?php echo TEXT_AFFILIATE_BILLED; ?></b></td>
                <td class="main">&nbsp;<?php echo format_date($payments['affiliate_payment_date']); ?></td>
              </tr>
              <tr>
                <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
              </tr>
              <tr>
                <td class="main" valign="top"><b><?php echo TEXT_AFFILIATE_PAYING_POSSIBILITIES; ?></b></td>
                <td class="main"><table border="1" cellspacing="0" cellpadding="5">
                  <tr>
<?php
  if (AFFILIATE_USE_BANK == 'true') {
?>
                    <td class="main"  valign="top"><?php echo '<b>' . TEXT_AFFILIATE_PAYMENT_BANK_TRANSFER . '</b><br><br>' . TEXT_AFFILIATE_PAYMENT_BANK_NAME . ' ' . $payments['affiliate_payment_bank_name'] . '<br>' . TEXT_AFFILIATE_PAYMENT_BANK_BRANCH_NUMBER . ' ' . $payments['affiliate_payment_bank_branch_number'] . '<br>' . TEXT_AFFILIATE_PAYMENT_BANK_SWIFT_CODE . ' ' . $payments['affiliate_payment_bank_swift_code'] . '<br>' . TEXT_AFFILIATE_PAYMENT_BANK_ACCOUNT_NAME . ' ' . $payments['affiliate_payment_bank_account_name'] . '<br>' . TEXT_AFFILIATE_PAYMENT_BANK_ACCOUNT_NUMBER . ' ' . $payments['affiliate_payment_bank_account_number'] . '<br>'; ?></td>
<?php
  }
  if (AFFILIATE_USE_PAYPAL == 'true') {
?>
                    <td class="main"  valign="top"><?php echo '<b>' . TEXT_AFFILIATE_PAYMENT_PAYPAL . '</b><br><br>' . TEXT_AFFILIATE_PAYMENT_PAYPAL_EMAIL . '<br>' . $payments['affiliate_payment_paypal'] . '<br>'; ?></td>
<?php
  }
  if (AFFILIATE_USE_CHECK == 'true') {
?>
                    <td class="main"  valign="top"><?php echo '<b>' . TEXT_AFFILIATE_PAYMENT_CHECK . '</b><br><br>' . TEXT_AFFILIATE_PAYMENT_CHECK_PAYEE . '<br>' . $payments['affiliate_payment_check'] . '<br>'; ?></td>
<?php
  }
?>
                  </tr>
                </table></td>
              </tr>
            </table></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr>
<?php echo tep_draw_form('status', FILENAME_AFFILIATE_PAYMENT, tep_get_all_get_params(array('action')) . 'action=update_payment'); ?>
        <td><table border="0" cellspacing="0" cellpadding="2">
          <tr>
            <td><table border="0" cellspacing="0" cellpadding="2">
              <tr>
                <td class="main"><b><?php echo PAYMENT_STATUS; ?></b> <?php echo tep_draw_pull_down_menu('status', $payments_statuses, $payments['affiliate_payment_status']); ?></td>
              </tr>
              <tr>
                <td class="main"><b><?php echo PAYMENT_NOTIFY_AFFILIATE; ?></b><?php echo tep_draw_checkbox_field('notify', '', true); ?></td>
              </tr>
            </table></td>
            <td valign="top"><?php echo tep_image_submit('button_update.gif', IMAGE_UPDATE); ?></td>
          </tr>
        </table></td>
      </form></tr>

      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr>
        <td class="main"><table border="1" cellspacing="0" cellpadding="5">
          <tr>
            <td class="smallText" align="center"><b><?php echo TABLE_HEADING_NEW_VALUE; ?></b></td>
            <td class="smallText" align="center"><b><?php echo TABLE_HEADING_OLD_VALUE; ?></b></td>
            <td class="smallText" align="center"><b><?php echo TABLE_HEADING_DATE_ADDED; ?></b></td>
            <td class="smallText" align="center"><b><?php echo TABLE_HEADING_AFFILIATE_NOTIFIED; ?></b></td>
          </tr>
<?php
    $affiliate_history_query = tep_db_query("select affiliate_new_value, affiliate_old_value, affiliate_date_added, affiliate_notified from " . TABLE_AFFILIATE_PAYMENT_STATUS_HISTORY . " where affiliate_payment_id = '" . tep_db_input($pID) . "' order by affiliate_status_history_id desc");
    if (tep_db_num_rows($affiliate_history_query)) {
      while ($affiliate_history = tep_db_fetch_array($affiliate_history_query)) {
        echo '          <tr>' . "\n" .
             '            <td class="smallText">' . $payments_status_array[$affiliate_history['affiliate_new_value']] . '</td>' . "\n" .
             '            <td class="smallText">' . (tep_not_null($affiliate_history['affiliate_old_value']) ? $payments_status_array[$affiliate_history['affiliate_old_value']] : '&nbsp;') . '</td>' . "\n" .
             '            <td class="smallText" align="center">' . format_date($affiliate_history['affiliate_date_added']) . '</td>' . "\n" .
             '            <td class="smallText" align="center">';
        if ($affiliate_history['affiliate_notified'] == '1') {
          echo tep_image(DIR_WS_ICONS . 'tick.gif', ICON_TICK);
        } else {
          echo tep_image(DIR_WS_ICONS . 'cross.gif', ICON_CROSS);
        }
        echo '          </tr>' . "\n";
      }
    } else {
        echo '          <tr>' . "\n" .
             '            <td class="smallText" colspan="4">' . TEXT_NO_PAYMENT_HISTORY . '</td>' . "\n" .
             '          </tr>' . "\n";
    }
?>
        </table></td>
      </tr>
      <tr>
        <td colspan="2" align="right"><?php echo '<a href="' . tep_href_link(FILENAME_AFFILIATE_INVOICE, 'pID=' . $HTTP_GET_VARS['pID']) . '" TARGET="_blank">' . tep_image_button('button_invoices.gif', IMAGE_ORDERS_INVOICE) . '</a> <a href="' . tep_href_link(FILENAME_AFFILIATE_PAYMENT, tep_get_all_get_params(array('action'))) . '">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>'; ?></td>
      </tr>
<?php
  } else {
?>
      <tr>
        <td width="100%"><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', 1, HEADING_IMAGE_HEIGHT); ?></td>
            <td class="pageHeading"><?php echo '<a href="' . tep_href_link(FILENAME_AFFILIATE_PAYMENT, 'pID=' . $pInfo->affiliate_payment_id. '&action=start_billing' . (isset($HTTP_GET_VARS['acID'])?'&acID='.(int)$HTTP_GET_VARS['acID']:'') ) . '">' . tep_image_button('button_affiliate_billing.gif', IMAGE_AFFILIATE_BILLING) . '</a>'; 
			//echo  tep_image_button('button_affiliate_billing.gif', IMAGE_AFFILIATE_BILLING,'onclick="javascript:billing();"'); ?></td>
            <td align="right"><table border="0" width="100%" cellspacing="0" cellpadding="0">
              <tr><?php echo tep_draw_form('status', FILENAME_AFFILIATE_PAYMENT, '', 'get'); ?>
                <td class="dataTableHeadingContent" align="right"><?php echo HEADING_TITLE_STATUS . ' ' . tep_draw_pull_down_menu('display_status', array_merge(array(array('id' => '', 'text' => TEXT_ALL_PAYMENTS)), $payments_statuses), '', 'onChange="display_payments()"'); ?></td>
              </form></tr>
            </table></td>
          </tr>-->
		  <tr class="dataTableHeadingRow">
				<td class="dataTableHeadingContent"><?php echo '<a href="' . tep_href_link(FILENAME_AFFILIATE_PAYMENT, 'pID=' . $pInfo->affiliate_payment_id. '&action=start_billing' . (isset($HTTP_GET_VARS['acID'])?'&acID='.(int)$HTTP_GET_VARS['acID']:'') ) . '">' . tep_image_button('button_affiliate_billing.gif', IMAGE_AFFILIATE_BILLING) . '</a>'; ?></td>			<?php echo tep_draw_form('status', FILENAME_AFFILIATE_PAYMENT, '', 'get'); ?>
                <td class="dataTableHeadingContent" align="right"><?php echo HEADING_TITLE_STATUS . ' ' . tep_draw_pull_down_menu('display_status', array_merge(array(array('id' => '', 'text' => TEXT_ALL_PAYMENTS)), $payments_statuses), '', 'onChange="display_payments()"'); ?></td>
              </form>
			<td class="dataTableHeadingContent" align="right" colspan="3"><?php echo HEADING_TITLE_SEARCH . ' ' . tep_draw_input_field('sID', '', 'onkeyup="javascript:check_key(event);"') . tep_draw_hidden_field('action', 'edit') . '&nbsp;'; ?></td>
			<td align="left" valign="absmiddle" width="25"><a alt="Search" title="Search" href="javascript:expand_category(1,'1','search');"><?php echo tep_image('images/template/img_search.gif');?></a></td>	
		  </tr>
        </table></td>
      </tr>
      <tr>
        <td><?php  echo tep_draw_form('payment',FILENAME_AFFILIATE_PAYMENT,'','post','onSubmit="javascript:return check_form();"');?>
		 <table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr id="result_id">
            <td valign="top">
			
			
		   <table border="0" width="100%" cellspacing="0" cellpadding="0">
             <tr>
               <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="dataTableHeadingRow">
                <td class="dataTableHeadingContent" width="30%"><?php echo TABLE_HEADING_AFILIATE_NAME; ?></td>
                <td class="dataTableHeadingContent" width="20%" align="right"><?php echo TABLE_HEADING_NET_PAYMENT; ?></td>
                <td class="dataTableHeadingContent" width="20%" align="right"><?php echo TABLE_HEADING_PAYMENT; ?></td>
                <td class="dataTableHeadingContent"  width="20%" align="center"><?php echo TABLE_HEADING_DATE_BILLED; ?></td>
                <td class="dataTableHeadingContent" width="30%" align="right"><?php echo TABLE_HEADING_STATUS; ?></td>
                <!--<td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_ACTION; ?>&nbsp;</td>-->
              </tr>
			  
			   <tr><td colspan="5" class="main" id="res_display" style="display:none"></td></tr>
			  <tr> <td id="ajax_details" colspan="6">
			  
			       <table border="0" cellpadding="0" cellspacing="0" width="100%">
<?php
    if ($HTTP_GET_VARS['sID']) {
      // Search only payment_id by now
      $sID = tep_db_prepare_input($HTTP_GET_VARS['sID']);
      $payments_query_raw = "select p.* , s.affiliate_payment_status_name from " . TABLE_AFFILIATE_PAYMENT . " p , " . TABLE_AFFILIATE_PAYMENT_STATUS . " s where p.affiliate_payment_id = '" . tep_db_input($sID) . "' and p.affiliate_payment_status = s.affiliate_payment_status_id and s.affiliate_language_id = '" . (int)$languages_id . "' order by p.affiliate_payment_id DESC";
    } elseif (is_numeric($HTTP_GET_VARS['status'])) {
      $status = tep_db_prepare_input($HTTP_GET_VARS['status']);
      $payments_query_raw = "select p.* , s.affiliate_payment_status_name from " . TABLE_AFFILIATE_PAYMENT . " p , " . TABLE_AFFILIATE_PAYMENT_STATUS . " s where s.affiliate_payment_status_id = '" . tep_db_input($status) . "' and p.affiliate_payment_status = s.affiliate_payment_status_id and s.affiliate_language_id = '" . (int)$languages_id . "' order by p.affiliate_payment_id DESC";
    } else {
		$where="";
		if($HTTP_GET_VARS['acID']>0) $where= " and  p.affiliate_id='". (int)$HTTP_GET_VARS['acID'] . "'";
      $payments_query_raw = "select p.* , s.affiliate_payment_status_name from " . TABLE_AFFILIATE_PAYMENT . " p , " . TABLE_AFFILIATE_PAYMENT_STATUS . " s where p.affiliate_payment_status = s.affiliate_payment_status_id and s.affiliate_language_id = '" . (int)$languages_id . "' " . $where . " order by p.affiliate_payment_id DESC";
	} 
    $payments_split = new splitPageResultsEvent($HTTP_GET_VARS['page'], MAX_DISPLAY_SEARCH_RESULTS, $payments_query_raw, $payments_query_numrows);
    $payments_query = tep_db_query($payments_query_raw);
	if(tep_db_num_rows($payments_query)>0){
    while ($payments = tep_db_fetch_array($payments_query)) {
      if (((!$HTTP_GET_VARS['pID']) || ($HTTP_GET_VARS['pID'] == $payments['affiliate_payment_id'])) && (!$pInfo)) {
        $pInfo = new objectInfo($payments);
      }
	echo '  <tr  class="dataTableRow" onmouseover="this.className=\'dataTableRowOver\';this.style.cursor=\'hand\'" onmouseout="this.className=\'dataTableRow\'" onclick="javascript:if(document.getElementById(\'head_'.$payments['affiliate_payment_id'].'\').style.display==\'none\') expand_category(' . $payments['affiliate_payment_id'] . ',3)">' . "\n";
	  ?>
	  <td >
		<div id='<?php echo 'data_' . $payments['affiliate_payment_id'];  ?>'>
			<table border="0" cellpadding="4" cellspacing="0" width="100%">
			
                <td class="dataTableContent" width="30%"><?php echo  $payments['affiliate_firstname'] . ' ' . $payments['affiliate_lastname']; ?></td>
                <td class="dataTableContent" width="20%" align="right"><?php echo $currencies->format(strip_tags($payments['affiliate_payment'])); ?></td>
                <td class="dataTableContent" width="20%" align="right"><?php echo $currencies->format(strip_tags($payments['affiliate_payment'] + $payments['affiliate_payment_tax'])); ?></td>
                <td class="dataTableContent"  width="20%" align="center"><?php echo format_date($payments['affiliate_payment_date']); ?></td>
                <td class="dataTableContent"  width="30%" align="right"><?php echo $payments['affiliate_payment_status_name']; ?></td>
<!--                <td class="dataTableContent" align="right"><?php if ( (is_object($pInfo)) && ( $payments['affiliate_payment_id'] == $pInfo->affiliate_payment_id) ) { echo tep_image(DIR_WS_IMAGES . 'icon_arrow_right.gif', ''); } else { echo '<a href="' . tep_href_link(FILENAME_AFFILIATE_PAYMENT, tep_get_all_get_params(array('pID')) . 'pID=' . $payments['affiliate_payment_id']) . '">' . tep_image(DIR_WS_IMAGES . 'icon_info.gif', IMAGE_ICON_INFO) . '</a>'; } ?>&nbsp;</td>
-->              </tr>
			  
			  </table>
		   </div>	
		   <div id='<?php echo 'head_' . $payments['affiliate_payment_id'];  ?>' style="display:none" class="openContent" ></div>
	 </td>
	 </tr>
<?php
    } 
	} else {?>
	<tr>
		<td class="dataTableContent" align="center"><?php echo TEXT_PAYMENTS_FOUND;?></td>
	</tr>	
		
<?php	} echo '</form>';
?>
</table></td>
	 </tr>
              <tr>
                <td colspan="6">
				<form name="new_affiliate_payment" action="<?php echo FILENAME_AFFILIATE; ?>" method="post">
				<input type="hidden" id="affiliate_payment_id">
				</form>
				<table border="0" width="100%" cellspacing="0" cellpadding="2">
                  <tr id="fst_nav">
                    <td class="smallText" valign="top"><?php echo $payments_split->display_count($payments_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $HTTP_GET_VARS['page'], TEXT_DISPLAY_NUMBER_OF_PAYMENTS); ?></td>
                    <td class="smallText" align="right"><?php echo $payments_split->display_script_links($payments_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $HTTP_GET_VARS['page'],1,'expand_category(\'\'','\'\''); ?></td>
                  </tr>
                </table></td>
              </tr>
            </table></td>
<?php
  /*$heading = array();
  $contents = array();
  switch ($HTTP_GET_VARS['action']) {
    case 'delete':
      $heading[] = array('text' => '<b>' . TEXT_INFO_HEADING_DELETE_PAYMENT . '</b>');

      $contents = array('form' => tep_draw_form('payment', FILENAME_AFFILIATE_PAYMENT, tep_get_all_get_params(array('pID', 'action')) . 'pID=' . $pInfo->affiliate_payment_id. '&action=deleteconfirm'));
      $contents[] = array('text' => TEXT_INFO_DELETE_INTRO . '<br>');
      $contents[] = array('align' => 'center', 'text' => '<br>' . tep_image_submit('button_delete.gif', IMAGE_DELETE) . ' <a href="' . tep_href_link(FILENAME_AFFILIATE_PAYMENT, tep_get_all_get_params(array('pID', 'action')) . 'pID=' . $pInfo->affiliate_payment_id) . '">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL) . '</a>');
      break;
    default:
      if (is_object($pInfo)) {
        $heading[] = array('text' => '<b>[' . $pInfo->affiliate_payment_id . ']&nbsp;&nbsp;' . tep_datetime_short($pInfo->affiliate_payment_date) . '</b>');

        $contents[] = array('align' => 'center', 'text' => '<a href="' . tep_href_link(FILENAME_AFFILIATE_PAYMENT, tep_get_all_get_params(array('pID', 'action')) . 'pID=' . $pInfo->affiliate_payment_id . '&action=edit') . '">' . tep_image_button('button_edit.gif', IMAGE_EDIT) . '</a> <a href="' . tep_href_link(FILENAME_AFFILIATE_PAYMENT, tep_get_all_get_params(array('pID', 'action')) . 'pID=' . $pInfo->affiliate_payment_id  . '&action=delete') . '">' . tep_image_button('button_delete.gif', IMAGE_DELETE) . '</a>');
        $contents[] = array('align' => 'center', 'text' => '<a href="' . tep_href_link(FILENAME_AFFILIATE_INVOICE, 'pID=' . $pInfo->affiliate_payment_id ) . '" TARGET="_blank">' . tep_image_button('button_invoices.gif', IMAGE_ORDERS_INVOICE) . '</a> ');
      }
      break;
  }

  if ( (tep_not_null($heading)) && (tep_not_null($contents)) ) {
    echo '            <td  width="25%" valign="top">' . "\n";

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

    echo '            </td>' . "\n";
  }*/
?>
          </tr>
        </table></td>
      </tr>
<?php
  }
?>
    </table></td>
<!-- body_text_eof //-->
  </tr>
</table>
<?php 
function get_default_details($id){
	global $languages_id;
	$details_query=tep_db_query("select p.affiliate_payment_id,p.affiliate_firstname,p.affiliate_lastname,p.affiliate_payment_date,p.affiliate_payment_last_modified,p.affiliate_payment_total, s.affiliate_payment_status_name from " . TABLE_AFFILIATE_PAYMENT . " p , " . TABLE_AFFILIATE_PAYMENT_STATUS . " s where p.affiliate_payment_status = s.affiliate_payment_status_id and s.affiliate_language_id = '" . (int)$languages_id . "' and p.affiliate_payment_id  = $id  order by p.affiliate_payment_id DESC");   
	if(tep_db_num_rows($details_query)>0){
		 	$affiliate_payment = tep_db_fetch_array($details_query);
			while(list($key,$vals)=each($affiliate_payment)){						
			 	if(($key=='affiliate_payment_date') || ($key=='affiliate_payemnt_last_modified') )
					$vals=format_date($vals);													
				
				$str_vals.=$vals."^";	
			}
		 }else $str_vals = 'No Record Found';
		  echo 'get_details'.$str_vals;
}
?>
<!-- body_eof //-->

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