<?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(DIR_WS_LANGUAGES . $language . '/' . FILENAME_CHECKOUT_CONFIRMATION);
  require(DIR_WS_CLASSES . 'order_total.php');

  require(DIR_WS_CLASSES . 'order.php');
 // if($HTTP_GET_VARS['load'] && $HTTP_GET_VARS['load'] == 'load'){
	//  require('checkout_payment_process.php');
	//  return;
 // }

  require(DIR_WS_CLASSES . 'payment.php');
 
 //if (isset($HTTP_POST_VARS['coupon'])) $cc = $HTTP_POST_VARS['coupon'];
 //tep_session_unregister['couponcode'];
 if($HTTP_GET_VARS['do_action']=='get_counpon_details'){
 	$HTTP_POST_VARS['coupon']=tep_db_prepare_input($HTTP_GET_VARS['coupon']);
 	$HTTP_POST_VARS['gv_redeem_code']=tep_db_prepare_input($HTTP_GET_VARS['gv_redeem_code']);
	$HTTP_POST_VARS['cot_gv']=tep_db_prepare_input($HTTP_GET_VARS['cot_gv']);
	if ($HTTP_POST_VARS['cot_gv']) $_SESSION['cot_gv'] = tep_db_prepare_input($HTTP_POST_VARS['cot_gv']);
 	if ($credit_covers) $payment=''; //ICW added for CREDIT CLASS
    $payment_modules = new payment($payment);
  //ICW ADDED FOR CREDIT CLASS SYSTEM
   $order = new order;
    
  //$payment_modules->update_status();
  //ICW ADDED FOR CREDIT CLASS SYSTEM
   $order_total_modules = new order_total;
  //ICW ADDED FOR CREDIT CLASS SYSTEM

  $order_total_modules->collect_posts_forajax();
  
  //ICW ADDED FOR CREDIT CLASS SYSTEM
 	$order_total_modules->process();
	//print_r($GLOBALS);
	
  	if(sizeof($GLOBALS['ot_coupon'])){
  		$coupon=$GLOBALS['ot_coupon'];
  		$error="";
  		$error=$HTTP_SESSION_VARS['coupon_error'];
  		if($coupon->code=='ot_coupon' && $error=="" && $coupon->output[0]['text']){
   			echo '<table border="0" width="90%" cellspacing="3" cellpadding="3" class="formArea" align="center">' .
   					'<tr><td></td>' .
   					'   <td class="eventTitle">'.TEXT_REDEEM_CODE . (($HTTP_SESSION_VARS['email_redeem_code'])?$HTTP_SESSION_VARS['email_redeem_code']: $coupon->coupon_code).'</td>' .
   					'   <td class="eventTitle">'.TEXT_VALUE. $coupon->output[0]['text'].'</td>' .
   					'</tr>' . 
   					'<tr><td></td>' .
   					'   <td class="eventTitle">'.TEXT_VALID_UTIL. $coupon->expire_date.'</td>' .
   					'   <td class="eventTitle">'.(($coupon->uses_per_user!=0)?TEXT_USES_REMAINING .$coupon->uses_per_user:'').'</td>' .
   					'<td rowspan="2" valign="middle" align="right">'.tep_template_image_button('button_dont_redeem.gif','','style="cursor:pointer;cursor:hand" onclick="javascript:submitFunction(1);"').'</td>'.
   					'</tr>' .   					
   					'</table>';
	    } else {
	    	echo '<table border="0" width="90%" align="center" cellspacing="0" cellpadding="0"><tr align="center"><td align="left" class="smalltext"><span class="errortext">'.$error.'</span></td></tr></table>';
	    }
  	} 
	exit;
  }

// if the customer is not logged on, redirect them to the login page
  if (!tep_session_is_registered('customer_id')) {
    $navigation->set_snapshot(array('mode' => 'SSL', 'page' => FILENAME_CHECKOUT_PAYMENT));
    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($HTTP_POST_VARS['comments_chkbox']){
  	
  }else {  
  	$HTTP_POST_VARS['comments']="";
  	tep_session_unregister('comments');
  }
//for coupon  
  if(isset($HTTP_POST_VARS['coupon'])){
  	if(!tep_session_is_registered('coupon'))
		tep_session_register('coupon');
	else
		tep_session_unregister('coupon');
	$coupon = true;
  } 
  if(isset($HTTP_POST_VARS['gv_redeem_code'])){
  	if(!tep_session_is_registered('gv_redeem_code'))
		tep_session_register('gv_redeem_code');
	else
	 	tep_session_unregister('gv_redeem_code');
	$gv_redeem_code = tep_db_prepare_input($HTTP_POST_VARS['gv_redeem_code']);
  }
 // if(tep_session_is_registered('email_redeem_code'))
  //	tep_session_unregister('email_redeem_code');
	
// 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 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'));
  }

  if (!tep_session_is_registered('payment')) tep_session_register('payment');
  if (isset($HTTP_POST_VARS['payment'])) $payment = tep_db_prepare_input($HTTP_POST_VARS['payment']);
   
	if(tep_session_is_registered('comments')) {
		tep_session_unregister('comments');
	}	

	$comments=(isset($HTTP_POST_VARS['comments'])?tep_db_prepare_input($HTTP_POST_VARS['comments']):'');
	if (tep_not_null($comments)) {
	  if (!tep_session_is_registered('comments')) tep_session_register('comments');
  	}
  

// load the selected payment module
  
  if ($credit_covers) $payment=''; //ICW added for CREDIT CLASS
  $payment_modules = new payment($payment);
//ICW ADDED FOR CREDIT CLASS SYSTEM
    $order = new order;
 tep_check_country_differ();
  $payment_modules->update_status();
//ICW ADDED FOR CREDIT CLASS SYSTEM
  $order_total_modules = new order_total;
//ICW ADDED FOR CREDIT CLASS SYSTEM

 $order_total_modules->collect_posts();
  
  
//ICW ADDED FOR CREDIT CLASS SYSTEM
  $order_total_modules->pre_confirmation_check();


// ICW CREDIT CLASS Amended Line
//  if ( ( is_array($payment_modules->modules) && (sizeof($payment_modules->modules) > 1) && !is_object($$payment) ) || (is_object($$payment) && ($$payment->enabled == false)) ) {
  if ( (is_array($payment_modules->modules)) && (sizeof($payment_modules->modules) > 1) && (!is_object($$payment)) && (!$credit_covers) ) {
    tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_NO_PAYMENT_MODULE_SELECTED), 'SSL'));
  }
  if (is_array($payment_modules->modules)) {
    $credit_covers=false;
    $payment_modules->pre_confirmation_check();
  }

// load the selected shipping module
  require(DIR_WS_CLASSES . 'shipping.php');
  $shipping_modules = new shipping($shipping);
//ICW Credit class amendment Lines below repositioned
//  require(DIR_WS_CLASSES . 'order_total.php');
//  $order_total_modules = new order_total;
// Stock Check
  $any_out_of_stock = false;
  if (STOCK_CHECK == 'true') {
    for ($i=0, $n=sizeof($order->products); $i<$n; $i++) {
     if ($order->products[$i]['element_type']=='P'){
      if (tep_check_stock($order->products[$i]['id'], $order->products[$i]['qty'])) {
        $any_out_of_stock = true;
      }
     } 
	 if(isset($order->products[$i]['attributes'])){
	 	$attribute_stock_check="";
	reset($order->products[$i]['attributes']);
	while (list($option, $value) = each($order->products[$i]['attributes'])) {
		$attrb_ids.=$value["option_id"] . "{".$value["value_id"]."}-";
	}
	$attrb_ids=substr($attrb_ids,0,-1);
	reset($order->products[$i]['attributes']);
			$attribute_stock_check=tep_check_attribute_stock($order->products[$i]['id'],$attrb_ids,$order->products[$i]['qty']);
			if(tep_not_null($attribute_stock_check)){
				$any_out_of_stock=true;
			}
		}	
    }	
		  if(tep_session_is_registered('coupon_exist')) tep_session_unregister('coupon_exist'); 
		  if(tep_session_is_registered('coupon_exist_equal_amount')) tep_session_unregister('coupon_exist_equal_amount');
    // Out of Stock
    if ( (STOCK_ALLOW_CHECKOUT != 'true') && ($any_out_of_stock == true) ) {
      tep_redirect(tep_href_link(FILENAME_SHOPPING_CART));
    }
  }
  $messageStack->add('security', HEADING_TRANSACTION_SECURITY . '&nbsp;<br>&nbsp;&nbsp;&nbsp;'.HEADING_TRANSACTION_ENSURE . $_SERVER['REMOTE_ADDR'], 'warning');

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

  $content = CONTENT_CHECKOUT_CONFIRMATION;

  //require('includes/http.js');
  require(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/' . TEMPLATENAME_MAIN_PAGE);

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