<?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');

	// modified for wallet payment -start
	if (tep_session_is_registered("payment_page") && $payment_page=="wallet" && (isset($HTTP_GET_VARS["error"]) || isset($HTTP_GET_VARS["payment_error"]))){
		tep_redirect(tep_href_link(FILENAME_WALLET_CHECKOUT_PAYMENT,tep_get_all_input_params() .'&validID=' . $checkID,'SSL'));
	}
	// modified for wallet payment -end

	//error count start
	//if($HTTP_POST_VARS['comments'] && tep_session_is_registered('comments')) 
	if(isset($HTTP_GET_VARS["error"]) || isset($HTTP_GET_VARS["payment_error"])){
		$error_count=(int)$HTTP_SESSION_VARS['error_count'];		
		tep_set_session_value('error_count',$error_count+1);
		$exp_count=3;		
   	if(defined('CHECKOUT_FAIL_RETRY')) 	$exp_count=(int)CHECKOUT_FAIL_RETRY; 
         
   	if($HTTP_SESSION_VARS['error_count']>$exp_count){
	   	tep_session_recreate();
	   	tep_session_unregister('customer_id');
	   	tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
   	}
   } 
   //error count end 
 
// if the customer is not logged on, redirect them to the login page
  if (!tep_session_is_registered('customer_id')) {
    $navigation->set_snapshot();
    tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  }

// if there is nothing in the customers cart, redirect them to the shopping cart page
  if ($cart->count_contents() < 1) {
    tep_redirect(tep_href_link(FILENAME_SHOPPING_CART));
  }

// if no shipping method has been selected, redirect the customer to the shipping method selection page
  if (!tep_session_is_registered('shipping')) {
    tep_redirect(tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));
  }

// avoid hack attempts during the checkout procedure by checking the internal cartID
  if (isset($cart->cartID) && tep_session_is_registered('cartID')) {
    if ($cart->cartID != $cartID) {
      tep_redirect(tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));
    }
  }

   if (!tep_session_is_registered('cartID')) tep_session_register('cartID');
   $cartID = $cart->cartID;   
   if (NO_CHECKOUT_ZERO_PRICE=="1" && $cart->is_free_checkout()) tep_redirect(tep_href_link(FILENAME_CHECKOUT_PROCESS_FREE, '', 'SSL'));   

// Stock Check
  if ( (STOCK_CHECK == 'true') && (STOCK_ALLOW_CHECKOUT != 'true') ) {
    $products = $cart->get_products();
    for ($i=0, $n=sizeof($products); $i<$n; $i++) {
     if ($products[$i]['elementType']=='P'){
      if (tep_check_stock($products[$i]['id'], $products[$i]['quantity'])) {
        tep_redirect(tep_href_link(FILENAME_SHOPPING_CART));
        break;
      }
     }
    }
  }
  
	// modified for wallet payment -start
	if (!tep_session_is_registered("payment_page")){
		tep_session_register("payment_page");
	}
	$payment_page="default";
	// modified for wallet payment -end
// if no billing destination address was selected, use the customers own address as default
  if (!tep_session_is_registered('billto')) {
    tep_session_register('billto');
    $billto = $customer_default_address_id;
  } else {
// verify the selected billing address
    $check_address_query = tep_db_query("select count(*) as total from " . TABLE_ADDRESS_BOOK . " where customers_id = '" . (int)$customer_id . "' and address_book_id = '" . (int)$billto . "'");
    $check_address = tep_db_fetch_array($check_address_query);

    if ($check_address['total'] != '1') {
      $billto = $customer_default_address_id;
      if (tep_session_is_registered('payment')) tep_session_unregister('payment');
    }
  }

	if ($shipping==false){
		$sendto=$billto;
	}
  require(DIR_WS_CLASSES . 'order.php');
  $order = new order;
  tep_check_country_differ(true);
  if(tep_session_is_registered('comments'))
  	tep_session_unregister('comments');

	if(tep_not_null($HTTP_POST_VARS['comments']))
		tep_session_register('comments');

	
 // if (!tep_session_is_registered('order_timestamp')) tep_session_register('order_timestamp');	
  $order_timestamp=time();
 $referenceID= substr(strtolower($order->customer['firstname']),0,3) . substr(strtolower($order->customer['lastname']),0,3) . $order_timestamp ;
 if (!tep_session_is_registered('referenceID')) tep_session_register('referenceID');	
 
 
  $total_weight = $cart->show_weight();
  $total_count = $cart->count_contents();
  $total_count = $cart->count_contents_virtual(); //ICW ADDED FOR CREDIT CLASS SYSTEM

// load all enabled payment modules
  require(DIR_WS_CLASSES . 'payment.php');
  $payment_modules = new payment;
  
  require(DIR_WS_CLASSES . 'order_total.php');//ICW ADDED FOR CREDIT CLASS SYSTEM
  $order_total_modules = new order_total;//ICW ADDED FOR CREDIT CLASS SYSTEM
  
  
  
  
  
  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_CHECKOUT_PAYMENT);

  $breadcrumb->add(NAVBAR_TITLE_1, tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));
  $breadcrumb->add(NAVBAR_TITLE_2, tep_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL'));

  $content = CONTENT_CHECKOUT_PAYMENT;
  $javascript ='checkout_payment.js.php';

  require(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/' . TEMPLATENAME_MAIN_PAGE);

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