<?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_CLASSES . 'order.php');	
	
	
// if the customer is not logged on, redirect them to the shopping cart page
  if (!tep_session_is_registered('customer_id')) {
    tep_redirect(tep_href_link(FILENAME_SHOPPING_CART));
  }
	$order_id = isset($HTTP_GET_VARS['order_id'])?(tep_db_prepare_input($HTTP_GET_VARS['order_id'])):0;
	
	if(!is_numeric($order_id) || $order_id == 0){
		tep_redirect(tep_href_link(FILENAME_DEFAULT));
	}
	$customer_number_query = tep_db_query("select customers_id from " . TABLE_ORDERS . " where orders_id = '". tep_db_input(tep_db_prepare_input($order_id)) . "'");
	$customer_number = tep_db_fetch_array($customer_number_query);

	if ($customer_number['customers_id'] != $customer_id) {
		tep_redirect(tep_href_link(FILENAME_DEFAULT));
	}
	
  if (isset($HTTP_GET_VARS['action']) && ($HTTP_GET_VARS['action'] == 'update')) {
    $notify_string = 'action=notify&';
    $notify = tep_db_prepare_input($HTTP_POST_VARS['notify']);
    if (!is_array($notify)) $notify = array($notify);
    for ($i=0, $n=sizeof($notify); $i<$n; $i++) {
      $notify_string .= 'notify[]=' . $notify[$i] . '&';
    }
    if (strlen($notify_string) > 0) $notify_string = substr($notify_string, 0, -1);

    tep_redirect(tep_href_link(FILENAME_DEFAULT, $notify_string));
  }

  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_CHECKOUT_SUCCESS);

  $breadcrumb->add(NAVBAR_TITLE_1);
  $paypalipn_query = tep_db_query("select o.orders_status,p.* from " . TABLE_ORDERS . " o LEFT JOIN " . TABLE_PAYPALIPN_TXN . " p on p.item_number = o.orders_id AND o.customers_id = '" . (int)$customer_id . "' order by o.date_purchased desc limit 1");
  $paypalipn = tep_db_fetch_array($paypalipn_query);

  if ($paypalipn['ipn_result']=='VERIFIED') {
    if ($paypalipn['payment_status']=='Completed') {
      $NAVBAR_TITLE_2 = PAYPAL_NAVBAR_TITLE_2_OK;
      $HEADING_TITLE = PAYPAL_HEADING_TITLE_OK; 
      $TEXT_SUCCESS = PAYPAL_TEXT_SUCCESS_OK; 
    } else if ($paypalipn['payment_status']=='Pending') { 
      $NAVBAR_TITLE_2 = PAYPAL_NAVBAR_TITLE_2_PENDING;
      $HEADING_TITLE = PAYPAL_HEADING_TITLE_PENDING; 
      $TEXT_SUCCESS = PAYPAL_TEXT_SUCCESS_PENDING; 
    }; 
    $cart->reset(TRUE); 
  } else if ($paypalipn['ipn_result']=='INVALID') { 
    $NAVBAR_TITLE_2 = PAYPAL_NAVBAR_TITLE_2_FAILED; 
    $HEADING_TITLE = PAYPAL_HEADING_TITLE_FAILED; 
    $TEXT_SUCCESS = PAYPAL_TEXT_SUCCESS_FAILED; 
  } else if ($paypalipn['orders_status']==99999) { 
      $NAVBAR_TITLE_2 = PAYPAL_NAVBAR_TITLE_2_PENDING; 
      $HEADING_TITLE = PAYPAL_HEADING_TITLE_PENDING; 
      $TEXT_SUCCESS = PAYPAL_TEXT_SUCCESS_PENDING; 
  } else { 
    $NAVBAR_TITLE_2 = NAVBAR_TITLE_2; 
    $HEADING_TITLE = HEADING_TITLE; 
    $TEXT_SUCCESS = TEXT_SUCCESS; 
  };
  $breadcrumb->add($NAVBAR_TITLE_2);
	
	 $payment=(isset($HTTP_GET_VARS["payment"])?(tep_db_prepare_input($HTTP_GET_VARS["payment"])):'');
  $global_query = tep_db_query("select global_product_notifications from " . TABLE_CUSTOMERS_INFO . " where customers_info_id = '" . (int)$customer_id . "'");
  $global = tep_db_fetch_array($global_query);

	$orders_query = tep_db_query("select orders_id,payment_return1,payment_return2,payment_method from " . TABLE_ORDERS . " where customers_id = '" . (int)$customer_id . "' order by date_purchased desc limit 1");
	$orders = tep_db_fetch_array($orders_query);
	$orders_result=$orders;

	if ($global['global_product_notifications'] != '1') {
		$orders_query = tep_db_query("select orders_id from " . TABLE_ORDERS . " where customers_id = '" . (int)$customer_id . "' order by date_purchased desc limit 1");
		$orders = tep_db_fetch_array($orders_query);
		
		$products_array = array();
		$products_query = tep_db_query("select products_id, products_name from " . TABLE_ORDERS_PRODUCTS . " where orders_id = '" . (int)$orders['orders_id'] . "' and products_type='P' order by products_name");
		while ($products = tep_db_fetch_array($products_query)) {
			$products_array[] = array(	'id' => $products['products_id'],
										'text' => $products['products_name']
										);
		}
		
		$subscription_array = array();
		$subscription_query = tep_db_query("select products_id, products_name from " . TABLE_ORDERS_PRODUCTS . " where orders_id = '" . (int)$orders['orders_id'] . "' and products_type='S' order by products_name");
		while ($products = tep_db_fetch_array($subscription_query)) {
			$subscription_array[] = array(	'id' => $products['products_id'],
										'text' => $products['products_name']
										);
		}

		$events_query = tep_db_query("select s.start_date,s.events_id,op.products_id, op.products_name from " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_EVENTS_SESSIONS . " s where orders_id = '" . (int)$orders['orders_id'] . "' and products_type='E' and s.sessions_id=op.products_id order by s.events_id,s.start_date");
		$prev_events_id=0;
		$events_array=array();
		if($cart->mode=='E'){
		 while ($events = tep_db_fetch_array($events_query)) {
			if ($prev_events_id!=$events['events_id']){
				$events_array[$events['events_id']] = array('id' => $events['events_id'],
															'text' =>$events['products_name'],
															'dates'=>'');
				$prev_events_id=$events['events_id'];
			}
			$events_array[$events['events_id']]['dates'].="<br>" . date(EVENTS_DATE_FORMAT,strtotime($events['start_date']));
		}
	  }
	} 
	
  $content = CONTENT_CHECKOUT_SUCCESS;
  $javascript = 'popup_window.js';
  require(DIR_WS_TEMPLATES . TEMPLATE_NAME . '/' . TEMPLATENAME_MAIN_PAGE);

  require(DIR_WS_INCLUDES . 'application_bottom.php');
  

?>
