<?php

/*

  osCommRes, Services Online
  http://www.oscommres.com

  Copyright (c) 2005 osCommRes

  Released under the GNU General Public License
  
  Freeway eCommerce from ZacWare
http://www.zac-ware.com/freeway

Copyright 2007 ZacWare Pty. Ltd
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );

  require('includes/application_top.php');
  require(DIR_WS_CLASSES . 'currencies.php');
  require(DIR_WS_CLASSES . 'payment.php');
  $currencies = new currencies();

  include(DIR_WS_CLASSES . 'order.php');

  // New "Status History" table has different format.
  $OldNewStatusValues = (tep_field_exists(TABLE_ORDERS_STATUS_HISTORY, "old_value") && tep_field_exists(TABLE_ORDERS_STATUS_HISTORY, "new_value"));
  $CommentsWithStatus = tep_field_exists(TABLE_ORDERS_STATUS_HISTORY, "comments");
  $SeparateBillingFields = tep_field_exists(TABLE_ORDERS, "billing_name");
  
  // Optional Tax Rate/Percent
  $AddShippingTax = "0.0"; // e.g. shipping tax of 17.5% is "17.5"

  // store all the status flags in an array
  $orders_statuses = array();
  $orders_status_array = array();
  $orders_statuses=tep_get_orders_status();
  $orders_status_query = tep_db_query("select orders_status_id, orders_status_name from " . TABLE_ORDERS_STATUS . " where language_id = '" . (int)$languages_id . "'");
  while ($orders_status = tep_db_fetch_array($orders_status_query)) {
  //  $orders_statuses[] = array('id' => $orders_status['orders_status_id'],
  //                             'text' => $orders_status['orders_status_name']);
    $orders_status_array[$orders_status['orders_status_id']] = $orders_status['orders_status_name'];
  }
  $oID=(isset($HTTP_GET_VARS['oID'])?(int)$HTTP_GET_VARS['oID']:0);
  $action=(isset($HTTP_GET_VARS['action'])?$HTTP_GET_VARS['action']:'');
  $step=(isset($HTTP_GET_VARS['step'])?(int)$HTTP_GET_VARS['step']:1);
  if ((int)$oID<=0)
    tep_redirect(tep_href_link(FILENAME_ORDERS));

  if (!($step>=1 && $step<=4)){
    tep_redirect(tep_href_link(FILENAME_ORDERS));
  }

  // confirm order entry in the table before procedding
  $orders_query = tep_db_query("select orders_id,customers_id from " . TABLE_ORDERS . " where orders_id = '" . (int)$oID . "'");
  $order_exists = true;
  if (!tep_db_num_rows($orders_query)) {
      $order_exists = false;
      $action="";
      //$messageStack->add_Session('orders',sprintf(ERROR_ORDER_DOES_NOT_EXIST, $oID), 'error');
      tep_redirect(tep_href_link(FILENAME_ORDERS));
  }
  $order_result=tep_db_fetch_array($orders_query);
  
  // check the order id with element in temporart edit cart and recreate the edit cart
  if (!tep_session_is_registered('edit_order') || $edit_order!=$oID){
               tep_session_unregister('edit_order');
	       $edit_cart=new editOrderCart($oID);
	       $edit_cart->restore_contents();
	       tep_session_register('edit_order');
	       $edit_order=$oID;
  } else {
     if (!tep_session_is_registered('edit_order'))
	   tep_redirect(tep_href_link(FILENAME_ORDERS));
  }

  if (tep_not_null($action)) {
    switch ($action) {
		case 'delete_item':
			// delete the single item from edit cart 
			$id=(isset($HTTP_GET_VARS['id'])?(int)$HTTP_GET_VARS['id']:0);
			tep_db_query("DELETE FROM " . TABLE_ORDERS_EDIT_PRODUCTS . " where orders_products_id='" . $id . "'");
			tep_db_query("DELETE FROM " . TABLE_ORDERS_EDIT_PRODUCTS_ATTRIBUTES . " where orders_products_id='" . $id ."'");
			tep_redirect(tep_href_link(FILENAME_EDIT_ORDERS,'oID=' . $oID . '&step=2'));
		case 'delete_group': 
			// needed to delete the event entries as a group
			$eID=(isset($HTTP_GET_VARS['eID'])?(int)$HTTP_GET_VARS['eID']:0);
			$fetch_query=tep_db_query("SELECT op.orders_products_id from " . TABLE_ORDERS_EDIT_PRODUCTS . " op, " . TABLE_EVENTS_SESSIONS . " s where op.orders_id='" . $oID . "' and s.events_id='" . $eID ."' and op.products_id=s.sessions_id and op.products_type='E' and op.events_type='W'");
			$delete_string="";
			while($fetch_result=tep_db_fetch_array($fetch_query))
				$delete_string.=$fetch_result["orders_products_id"] . ",";
			$delete_string=substr($delete_string,0,-1);
			if ($delete_string!="") {
				tep_db_query("DELETE FROM " . TABLE_ORDERS_EDIT_PRODUCTS . " where orders_products_id in(" . $delete_string . ")");

			}
			tep_redirect(tep_href_link(FILENAME_EDIT_ORDERS,'oID=' . $oID . '&step=2'));
	case 'update_order':
		// update the order contents
		$order = new order($oID);
		$status = tep_db_prepare_input($HTTP_POST_VARS['status']);
		// Update Order Info
		if ($step==1){
		    $UpdateOrders = "update " . TABLE_ORDERS . " set ";
			$UpdateOrders .="customers_name = '" . tep_db_input(stripslashes($update_customer_name)) . "',
			customers_company = '" . tep_db_input(stripslashes($update_customer_company)) . "',
			customers_street_address = '" . tep_db_input(stripslashes($update_customer_street_address)) . "',
			customers_suburb = '" . tep_db_input(stripslashes($update_customer_suburb)) . "',
			customers_city = '" . tep_db_input(stripslashes($update_customer_city)) . "',
			customers_state = '" . tep_db_input(stripslashes($update_customer_state)) . "',
			customers_postcode = '" . tep_db_input(stripslashes($update_customer_postcode)) . "',
			customers_country = '" . tep_db_input(stripslashes($update_customer_country)) . "'";
		}
		// update billing and shipping details
		if ($step==3){
		  $UpdateOrders = "update " . TABLE_ORDERS . " set ";
		  if($SeparateBillingFields)
	   	   {
		      $UpdateOrders .= "billing_name = '" . tep_db_input(stripslashes($update_billing_name)) . "',
			billing_company = '" . tep_db_input(stripslashes($update_billing_company)) . "',
			billing_street_address = '" . tep_db_input(stripslashes($update_billing_street_address)) . "',
			billing_suburb = '" . tep_db_input(stripslashes($update_billing_suburb)) . "',
			billing_city = '" . tep_db_input(stripslashes($update_billing_city)) . "',
			billing_state = '" . tep_db_input(stripslashes($update_billing_state)) . "',
			billing_postcode = '" . tep_db_input(stripslashes($update_billing_postcode)) . "',
			billing_country = '" . tep_db_input(stripslashes($update_billing_country)) . "',";
		   }
		     $UpdateOrders .= " delivery_name = '" . tep_db_input(stripslashes($update_delivery_name)) . "',
			delivery_company = '" . tep_db_input(stripslashes($update_delivery_company)) . "',
			delivery_street_address = '" . tep_db_input(stripslashes($update_delivery_street_address)) . "',
			delivery_suburb = '" . tep_db_input(stripslashes($update_delivery_suburb)) . "',
			delivery_city = '" . tep_db_input(stripslashes($update_delivery_city)) . "',
			delivery_state = '" . tep_db_input(stripslashes($update_delivery_state)) . "',
			delivery_postcode = '" . tep_db_input(stripslashes($update_delivery_postcode)) . "',
			delivery_country = '" . tep_db_input(stripslashes($update_delivery_country)) . "',
			customers_telephone = '" . tep_db_input(stripslashes($update_customer_telephone)) . "',
		        customers_second_telephone = '" . tep_db_input(stripslashes($update_customer_second_telephone_number)) . "',
			customers_fax ='" . tep_db_input(stripslashes($update_customer_fax)) . "',
		        customers_second_email_address = '" . tep_db_input(stripslashes($update_customer_second_email_address)) . "',
			customers_email_address = '" . tep_db_input(stripslashes($update_customer_email_address)) . "'";
                }
		
                if ($UpdateOrders!=''){
 		   $UpdateOrders .= " where orders_id = '" . tep_db_prepare_input($oID) . "';";
   		   tep_db_query($UpdateOrders);
		}
		if ($step==2 && sizeof($update_products)>0){
		// Update Products and event items
		   $product_type=isset($HTTP_POST_VARS['products_type'])?$HTTP_POST_VARS['products_type']:'';
		   foreach($update_products as $orders_products_id => $products_details)
		   {
			// Update orders_products Table
			if($products_details["qty"] > 0)
			{
				$Query = "update " . TABLE_ORDERS_EDIT_PRODUCTS . " set
					products_model = '" . tep_db_input(stripslashes($products_details["model"])) . "',
					products_name = '" . tep_db_input(stripslashes($products_details["name"])) . "',
					final_price = '" . tep_db_input($products_details["final_price"]) . "',
					products_tax = '" . tep_db_input($products_details["tax"]) . "',
					products_quantity = '" . tep_db_input($products_details["qty"]) . "'
					where orders_products_id = '$orders_products_id';";
				tep_db_query($Query);
                       	
				// Update Any Attributes
				if(IsSet($products_details[attributes]))
				{
					foreach($products_details["attributes"] as $orders_products_attributes_id => $attributes_details)
					{
						$Query = "update " . TABLE_ORDERS_EDIT_PRODUCTS_ATTRIBUTES . " set
							products_options = '" . tep_db_input(stripslashes($attributes_details["option"])) . "',
							products_options_values = '" . tep_db_input($attributes_details["value"]) . "'
							where orders_products_attributes_id = '$orders_products_attributes_id';";
						tep_db_query($Query);
					}
				}
			}
			else
			{
				// 0 Quantity = Delete
				$Query = "delete from " . TABLE_ORDERS_EDIT_PRODUCTS . " where orders_products_id = '$orders_products_id';";
				tep_db_query($Query);
				$Query = "delete from " . TABLE_ORDERS_EDIT_PRODUCTS_ATTRIBUTES . " where orders_products_id = '$orders_products_id';";
				tep_db_query($Query);
			}
		   }


		 }

                if ($step>=4){
		   tep_redirect(tep_href_link("orders.php", 'oID=' . $oID . '&page=1&action=edit'));}
		else
		{
		  if ($step==2){

		    if ($product_type=="E") tep_redirect(tep_href_link("edit_orders.php",'oID=' . $oID . '&step=4&type=' . $product_type));
		  }
		  $step=$step+1;
		  tep_redirect(tep_href_link("edit_orders.php", 'oID=' . $oID . '&step=' . $step . '&type=' . $product_type));
		}  
	break;

	// Add a Product
	case 'insert_product':
			// Get Order Info
			$oID = tep_db_prepare_input($HTTP_GET_VARS['oID']);
			$order = new order($oID);
            $mode=(isset($HTTP_GET_VARS['mode'])?$HTTP_GET_VARS['mode']:'');
			$id=(isset($HTTP_GET_VARS['id'])?(int)$HTTP_GET_VARS['id']:0);
			$AddedOptionsPrice = 0;
			// Get Product Attribute Info
			if(IsSet($add_product_options))
			{
				foreach($add_product_options as $option_id => $option_value_id)
				{
					$result = tep_db_query("SELECT * FROM " . TABLE_PRODUCTS_ATTRIBUTES . " pa LEFT JOIN " . TABLE_PRODUCTS_OPTIONS . " po ON po.products_options_id=pa.options_id LEFT JOIN " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov ON pov.products_options_values_id=pa.options_values_id WHERE products_id='$add_product_products_id' and options_id=$option_id and options_values_id=$option_value_id");
					$row = tep_db_fetch_array($result);
					extract($row, EXTR_PREFIX_ALL, "opt");
					$AddedOptionsPrice += $opt_options_values_price;
					$option_value_details[$option_id][$option_value_id] = array ("options_values_price" => $opt_options_values_price);
					$option_names[$option_id] = $opt_products_options_name;
					$option_values_names[$option_value_id] = $opt_products_options_values_name;
				}
			}
			// Get Product Info
			$InfoQuery = "select p.products_model,p.products_price,pd.products_name,p.products_tax_class_id from " . TABLE_PRODUCTS . " p left join " . TABLE_PRODUCTS_DESCRIPTION . " pd on pd.products_id=p.products_id where p.products_id='$add_product_products_id'";
			$result = tep_db_query($InfoQuery);
			$row = tep_db_fetch_array($result);
			if(tep_db_num_rows($result)=="0"){
				tep_redirect(tep_href_link(FILENAME_EDIT_ORDERS,"oID=$oID&action=add_product&step=1&mode=add"));
			}else {
			extract($row, EXTR_PREFIX_ALL, "p");
			}
			// Following functions are defined at the bottom of this file
			$CountryID = tep_get_country_id($order->billing["country"]);
			$ZoneID = tep_get_zone_id($CountryID, $order->billing["state"]);
			$ProductsTax = tep_get_tax_rate($p_products_tax_class_id, $CountryID, $ZoneID);
			$sql_array=array('orders_id'=>$oID,
				'products_id' => $add_product_products_id,
				'products_model' =>$p_products_model,
				'products_name' =>str_replace("'", "&#39;", $p_products_name),
				'products_price' =>$p_products_price,
				'final_price' => ($p_products_price + $AddedOptionsPrice),
				'products_tax' => $ProductsTax,
				'products_quantity' =>$add_product_quantity,
				'products_type'=>'P');
			// if the current operation is editing or inserting a new entry
			if ($mode=='edit'){
			   tep_db_perform(TABLE_ORDERS_EDIT_PRODUCTS,$sql_array,'update',"orders_products_id='" . (int)$id  . "'");
			   $new_product_id=$id;
                           tep_db_query("DELETE from " . TABLE_ORDERS_EDIT_PRODUCTS_ATTRIBUTES . " where orders_products_id='" . $id . "'");
			}
			else{
			   tep_db_perform(TABLE_ORDERS_EDIT_PRODUCTS,$sql_array);
			   $new_product_id = tep_db_insert_id();
			}
			if(IsSet($add_product_options))
			{
				foreach($add_product_options as $option_id => $option_value_id)
				{
					$Query = "insert into " . TABLE_ORDERS_EDIT_PRODUCTS_ATTRIBUTES . " set
						orders_id = $oID,
						orders_products_id = $new_product_id,
						products_options = '" . $option_names[$option_id] . "',
						products_options_values = '" . $option_values_names[$option_value_id] . "',
						options_values_price = '" . $option_value_details[$option_id][$option_value_id]["options_values_price"] . "',
						price_prefix = '+';";
					tep_db_query($Query);
				}
			}

		tep_redirect(tep_href_link(FILENAME_EDIT_ORDERS, 'oID=' . $oID . '&step=2'));
	break;
	case 'insert_subscription':
			// Get Order Info
			$oID = tep_db_prepare_input($HTTP_GET_VARS['oID']);
			$order = new order($oID);
                        $mode=(isset($HTTP_GET_VARS['mode'])?$HTTP_GET_VARS['mode']:'');
			$id=(isset($HTTP_GET_VARS['id'])?(int)$HTTP_GET_VARS['id']:0);
			$AddedOptionsPrice = 0;

			$InfoQuery = "select su.subscription_costs,sud.subscription_name,su.subscription_tax_class_id from " . TABLE_SUBSCRIPTIONS . " su left join " . TABLE_SUBSCRIPTIONS_DESCRIPTION . " sud on sud.subscription_id=su.subscription_id where su.subscription_id='$add_subscription_subscriptions_id'";
			$result = tep_db_query($InfoQuery);
			$row = tep_db_fetch_array($result);
			if(tep_db_num_rows($result)=="0"){
				tep_redirect(tep_href_link(FILENAME_EDIT_ORDERS,"oID=$oID&action=add_subscription&step=1&mode=add"));
			}else {
			extract($row, EXTR_PREFIX_ALL, "su");
			}
			// Following functions are defined at the bottom of this file
			$CountryID = tep_get_country_id($order->billing["country"]);
			$ZoneID = tep_get_zone_id($CountryID, $order->billing["state"]);
			
			$ProductsTax = tep_get_tax_rate($su_subscription_tax_class_id, $CountryID, $ZoneID);
			
			$sql_array=array('orders_id'=>$oID,
					'products_id' => $add_subscription_subscriptions_id,
					'products_name' =>  str_replace("'", "&#39;", $su_subscription_name),
					'products_price' => $su_subscription_costs,
					'final_price' => ($su_subscription_costs + $AddedOptionsPrice),
					'products_tax' => $ProductsTax,
					'products_quantity' => 1,
					'products_type'=>'S');
			// if the current operation is editing or inserting a new entry
			   tep_db_perform(TABLE_ORDERS_EDIT_PRODUCTS,$sql_array);
			   $new_subscription_id = tep_db_insert_id();
		tep_redirect(tep_href_link(FILENAME_EDIT_ORDERS, 'oID=' . $oID . '&step=2'));
	break;
    }
  }
?>
<!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> 
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
<?php
  if ($order_exists == true && $action=='') {
    $order_detail = new order($oID);
?>
<script language=javascript>      
   function validateForm(){
      var error_result="";
      var select_payment=0;

<?php if($step==1) { ?>
      if (document.edit_order.update_customer_name.value=="")
         error_result="<?php echo tep_db_input(ERR_CUSTOMER . ERR_NAME); ?>";
      else if (document.edit_order.update_customer_street_address.value=="")
         error_result="<?php echo tep_db_input(ERR_CUSTOMER . ERR_ADDRESS); ?>";
      else if (document.edit_order.update_customer_suburb.value=="")
         error_result="<?php echo tep_db_input(ERR_CUSTOMER . ERR_SUBURB); ?>";
      else if (document.edit_order.update_customer_city.value=="")
         error_result="<?php echo tep_db_input(ERR_CUSTOMER . ERR_CITY); ?>";
      else if (document.edit_order.update_customer_state.value=="")
         error_result="<?php echo tep_db_input(ERR_CUSTOMER . ERR_STATE); ?>";
      else if (document.edit_order.update_customer_postcode.value=="")
         error_result="<?php echo tep_db_input(ERR_CUSTOMER . ERR_POSTCODE); ?>";
      else if (document.edit_order.update_customer_country.value=="")
         error_result="<?php echo tep_db_input(ERR_CUSTOMER . ERR_COUNTRY); ?>";
<?php } 
      if($step==12) {?>
        if (document.edit_order.products_check.value!="valid")
         error_result="<?php echo tep_db_input(ERR_SELECT_PRODUCTS); ?>";
<?php } 
  if ($step==3){
   if($SeparateBillingFields) { ?>
      if (document.edit_order.update_billing_name.value=="")
         error_result="<?php echo tep_db_input(ERR_BILLING . ERR_NAME); ?>";
      else if (document.edit_order.update_billing_street_address.value=="")
         error_result="<?php echo tep_db_input(ERR_BILLING . ERR_ADDRESS); ?>";
      else if (document.edit_order.update_billing_suburb.value=="")
         error_result="<?php echo tep_db_input(ERR_BILLING . ERR_SUBURB); ?>";
      else if (document.edit_order.update_billing_city.value=="")
         error_result="<?php echo tep_db_input(ERR_BILLING . ERR_CITY); ?>";
      else if (document.edit_order.update_billing_state.value=="")
         error_result="<?php echo tep_db_input(ERR_BILLING . ERR_STATE); ?>";
      else if (document.edit_order.update_billing_postcode.value=="")
         error_result="<?php echo tep_db_input(ERR_BILLING . ERR_POSTCODE); ?>";
      else if (document.edit_order.update_billing_country.value=="")
         error_result="<?php echo tep_db_input(ERR_BILLING . ERR_COUNTRY); ?>";
<?php } ?>
     if (error_result==""){
      if (document.edit_order.update_delivery_name.value=="")
         error_result="<?php echo tep_db_input(ERR_DELIVERY . ERR_NAME); ?>";
      else if (document.edit_order.update_delivery_street_address.value=="")
         error_result="<?php echo tep_db_input(ERR_DELIVERY . ERR_ADDRESS); ?>";
      else if (document.edit_order.update_delivery_suburb.value=="")
         error_result="<?php echo tep_db_input(ERR_DELIVERY . ERR_SUBURB); ?>";
      else if (document.edit_order.update_delivery_city.value=="")
         error_result="<?php echo tep_db_input(ERR_DELIVERY . ERR_CITY); ?>";
      else if (document.edit_order.update_delivery_state.value=="")
         error_result="<?php echo tep_db_input(ERR_DELIVERY . ERR_STATE); ?>";
      else if (document.edit_order.update_delivery_postcode.value=="")
         error_result="<?php echo tep_db_input(ERR_DELIVERY . ERR_POSTCODE); ?>";
      else if (document.edit_order.update_delivery_country.value=="")
         error_result="<?php echo tep_db_input(ERR_DELIVERY . ERR_COUNTRY); ?>";
      else if (document.edit_order.update_customer_telephone.value=="")
         error_result="<?php echo tep_db_input(ERR_TELEPHONE_NUMBER); ?>";
      else if (document.edit_order.update_customer_email_address.value.indexOf("@")==-1 || document.edit_order.update_customer_email_address.value.indexOf(".")==-1)
         error_result="<?php echo tep_db_input(ERR_EMAIL_ADDRESS); ?>";
     }
<?php } ?>
<?php if ($step==4){ ?>
      select_payment='';
      if (error_result==""){
      if (document.edit_order.check_payment[0]){
      for (icnt=0;icnt<=2;icnt++){
         if (document.edit_order.update_info_payment_method[icnt].checked==true){ select_payment=check_payment[icnt].value;break;}
      }} else {
        if (document.edit_order.check_payment) select_payment=document.edit_order.check_payment.value;
      }
      if (select_payment=='') error_result="<?php echo tep_db_input(ERR_SELECT_PAYMENT); ?>";
      else if (select_payment=="Credit Card"){
         if (document.edit_order.update_info_cc_owner.value=="")
	    error_result="<?php echo tep_db_input(ERR_CC_OWNER); ?>";
	 else if (document.edit_order.update_info_cc_type.value=="")
	    error_result="<?php echo tep_db_input(ERR_CC_TYPE); ?>";
	 else if (document.edit_order.update_info_cc_number.value=="")
	    error_result="<?php echo tep_db_input(ERR_CC_NUMBER); ?>";
	 else if (document.edit_order.update_info_cc_cvv.value=="")
	    error_result="<?php echo tep_db_input(ERR_CC_CVV); ?>";
         }}
<?php } ?>
      if (error_result!=""){
         alert(error_result); 
	 return false;
	 }
      return true;
   }
</script>
      <tr>
        <td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo HEADING_TITLE; ?> #<?php echo $oID; ?></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_ORDERS, 'oID=' . $oID . '&action=edit') . '">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL) . '</a>'; ?></td>
          </tr>
        </table></td>
      </tr>
<!-- Begin Addresses Block -->
  <?php 
  // set action depend on step value
  if ($step==4) {?>
      <tr><?php echo tep_draw_form('edit_order', FILENAME_EDIT_ORDERS_CONFIRM, 'oID=' . $oID,'post','SSL'); ?>
  <?php } else {?>
      <tr><?php echo tep_draw_form('edit_order', FILENAME_EDIT_ORDERS, 'oID=' . $oID  . '&action=update_order&step=' . $step,'post','onSubmit="return validateForm()"'); ?>  
  <?php } ?>
	<td valign=top><table width="100%" border="0" cellspacing="0" cellpadding="2">
<?php if ($step==1){ ?>
	  <tr>
	    <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif','1','3'); ?></td>
	  </tr>
	  <tr>
	    <td valign="top">
		<table border="0" cellspacing="2" cellpadding="2" class=formArea width=60%>
		<tr>
		    <td class="main" width=30%><?php echo TEXT_NAME; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_customer_name',tep_html_quotes($order_detail->customer['name']),'size=37',true); ?></td>
		</tr>
		<tr>
		    <td class="main" width=30%><?php echo TEXT_COMPANY; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_customer_company',tep_html_quotes($order_detail->customer['company']),'size=37'); ?></td>
		</tr>
		<tr>
		    <td class="main"><?php echo TEXT_ADDRESS; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_customer_street_address',tep_html_quotes($order_detail->customer['street_address']),'size=37',true); ?></td>
		</tr>
		<tr>
		    <td class="main"><?php echo TEXT_SUBURB; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_customer_suburb',tep_html_quotes($order_detail->customer['suburb']),'size=37',true); ?></td>
		</tr>
		<tr>
		    <td class="main"><?php echo TEXT_POSTCODE; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_customer_postcode',tep_html_quotes($order_detail->customer['postcode']),'size=37',true); ?></td>
		</tr>
		<tr>
		    <td class="main"><?php echo TEXT_CITY; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_customer_city',tep_html_quotes($order_detail->customer['city']),'size=37',true); ?></td>
		</tr>
		<tr>
		    <td class="main"><?php echo TEXT_STATE; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_customer_state',tep_html_quotes($order_detail->customer['state']),'size=37',true); ?></td>
		</tr>
		<tr>
		    <td class="main"><?php echo TEXT_COUNTRY; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_customer_country',tep_html_quotes($order_detail->customer['country']),'size=37',true); ?></td>
		</tr>
		</table>
	    </td>
	    </tr>
	  <tr>
	    <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif','1','5'); ?></td>
	  </tr>
<?php } ?>
<?php if ($step==3){ ?>
       <?php if($SeparateBillingFields) { ?>
	  <tr>
	    <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif','1','5'); ?></td>
	  </tr>
	  <tr>
	    <td colspan="2" class="main" valign="top"><b><?php echo ENTRY_BILLING_ADDRESS; ?></b></td>
	  </tr>
	  <tr>
	    <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif','1','3'); ?></td>
	  </tr>
	  <tr>
	    <td valign="top">
	    <!-- Billing Address Block -->
		<table border="0" cellspacing="2  " cellpadding="2" class="formArea" width=60%>
		<tr>
		    <td class="main" width=30%><?php echo TEXT_NAME; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_billing_name',tep_html_quotes($order_detail->billing['name']),'size=37',true); ?></td>
		</tr>
		<tr>
		    <td class="main"><?php echo TEXT_COMPANY; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_billing_company',tep_html_quotes($order_detail->billing['company']),'size=37'); ?></td>
		</tr>
		<tr>
		    <td class="main"><?php echo TEXT_ADDRESS; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_billing_street_address',tep_html_quotes($order_detail->billing['street_address']),'size=37',true); ?></td>
		</tr>
		<tr>
		    <td class="main"><?php echo TEXT_SUBURB; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_billing_suburb',tep_html_quotes($order_detail->billing['suburb']),'size=37',true); ?></td>
		</tr>
		<tr>
		    <td class="main"><?php echo TEXT_CITY; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_billing_city',tep_html_quotes($order_detail->billing['city']),'size=37',true); ?></td>
		</tr>
		<tr>
		    <td class="main"><?php echo TEXT_STATE; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_billing_state',tep_html_quotes($order_detail->billing['state']),'size=37',true); ?></td>
		</tr>
		<tr>
		    <td class="main"><?php echo TEXT_POSTCODE; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_billing_postcode',tep_html_quotes($order_detail->billing['postcode']),'size=37',true); ?></td>
		</tr>
		<tr>
		    <td class="main"><?php echo TEXT_COUNTRY; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_billing_country',tep_html_quotes($order_detail->billing['country']),'size=37',true); ?></td>
		</tr>
		</table>
	    </td>
	    </tr>
<?php } ?>
	  <tr>
	    <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif','1','5'); ?></td>
	  </tr>
	  <tr>
	    <td colspan="2" class="main" valign="top"><b><?php echo ENTRY_SHIPPING_ADDRESS; ?></b></td>
	  </tr>
	  <tr>
	    <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif','1','3'); ?></td>
	  </tr>
	  <tr>
	    <td valign="top">
	    <!-- Shipping Address Block -->
		<table border="0" cellspacing="2" cellpadding="2" class="formArea" width=60%>
		<tr>
		    <td class="main" width=30%><?php echo TEXT_NAME; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_delivery_name',tep_html_quotes($order_detail->delivery['name']),'size=37',true); ?></td>
		</tr>
		<tr>
		    <td class="main"><?php echo TEXT_COMPANY; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_delivery_company',tep_html_quotes($order_detail->delivery['company']),'size=37'); ?></td>
		</tr>
		<tr>
		    <td class="main"><?php echo TEXT_ADDRESS; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_delivery_street_address',tep_html_quotes($order_detail->delivery['street_address']),'size=37',true); ?></td>
		</tr>
		<tr>
		    <td class="main"><?php echo TEXT_SUBURB; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_delivery_suburb',tep_html_quotes($order_detail->delivery['suburb']),'size=37',true); ?></td>
		</tr>
		<tr>
		    <td class="main"><?php echo TEXT_CITY; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_delivery_city',tep_html_quotes($order_detail->delivery['city']),'size=37',true); ?></td>
		</tr>
		<tr>
		    <td class="main"><?php echo TEXT_STATE; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_delivery_state',tep_html_quotes($order_detail->delivery['state']),'size=37',true); ?></td>
		</tr>
		<tr>
		    <td class="main"><?php echo TEXT_POSTCODE; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_delivery_postcode',tep_html_quotes($order_detail->delivery['postcode']),'size=37',true); ?></td>
		</tr>
		<tr>
		    <td class="main"><?php echo TEXT_COUNTRY; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_delivery_country',tep_html_quotes($order_detail->delivery['country']),'size=37',true); ?></td>
		</tr>
		</table>
	    </td>
	  </tr>
	  <tr>
	    <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif','1','10'); ?></td>
	  </tr>
          <tr>
          <td><table border="0" cellspacing="2" cellpadding="2" class="formArea" width=60%>
      		<tr>
      		  <td class="main" width="30%"><?php echo ENTRY_TELEPHONE_NUMBER; ?></td>
     		  <td class="main"><?php echo tep_draw_input_field('update_customer_telephone',$order_detail->customer['telephone'],'size=15',true); ?></td>
      		</tr>
              <?php if (ACCOUNT_SECOND_PHONE=='true') { ?>
		<tr>
		    <td class="main"><?php echo TEXT_SECOND_TELEPHONE; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_customer_second_telephone_number',tep_html_quotes($order_detail->customer['second_telephone']),'size=15'); ?></td>
		</tr>
	      <?php } ?>
		<tr>
		    <td class="main"><?php echo TEXT_MOBILE; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_customer_fax',tep_html_quotes($order_detail->customer['fax']),'size=15'); ?></td>
		</tr>
      		<tr>
      		  <td class="main"><?php echo ENTRY_EMAIL_ADDRESS; ?></td>
      		  <td class="main"><?php echo tep_draw_input_field('update_customer_email_address',$order_detail->customer['email_address'],'size=37',true); ?></td>
      		</tr>
               <?php if (ACCOUNT_SECOND_EMAIL=='true') { ?>
		<tr>
		    <td class="main"><?php echo TEXT_SECOND_EMAIL; ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_customer_second_email_address',tep_html_quotes($order_detail->customer['second_email_address']),'size=37'); ?></td>
		</tr>
	       <?php } ?>
      	      </table></td>
          </tr>
	</table></td>
      </tr>
<!-- End Addresses Block -->
<?php } ?>
<?php if ($step==4) {
    tep_session_unregister('payment');
                $tax=0.0;
	        $updated_subtotal=0.0;
		$updated_tax=0.0;
		$updated_total=0.0;
		$present_total=0.0;
		$net_amount=0.0;
		$orders_products_query = tep_db_query("select * from " . TABLE_ORDERS_EDIT_PRODUCTS . " where orders_id = '" . (int)$oID . "'");
		while ($orders_products = tep_db_fetch_array($orders_products_query)) {
		  $tax = (($orders_products['products_tax'] / 100) * ($orders_products['products_quantity'] * $orders_products['final_price']));
  		  $updated_subtotal += (($orders_products['products_quantity'] * $orders_products['final_price'])+$tax);
		  $updated_tax+=$tax;
                }
		$updated_total=$updated_subtotal;

		$totals_query = tep_db_query("select value from " . TABLE_ORDERS_TOTAL . " where orders_id = '" . (int)$oID . "' and class='ot_total'");
                $totals=tep_db_fetch_array($totals_query);
		if (isset($totals['value']))
		  $present_total=(float)$totals['value'];
		
		$payment_query=tep_db_query("select payment_method from " . TABLE_ORDERS . " where orders_id= '" . (int)$oID . "'");
		$payment_result=tep_db_fetch_array($payment_query);
		$payment_method='';
		$net_amount=0;
		if (isset($payment_result['payment_method']))
		  $payment_method=$payment_result['payment_method'];
		if ($payment_method=='Cash' || $payment_method=='Credit Card'){
		  if ($present_total>$updated_total){
		   $pay_type='refund';
		   $display_title='admin';
		   $net_amount=$present_total-$updated_total;}
		  else if ($present_total<$updated_total){
		   $display_title='customer';
		   $pay_type='getamount';
		   $net_amount=$updated_total-$present_total;}
		} else if ($payment_method=='change'){
                   $net_amount=$present_total;
		   $display_title='customer';
		} else {
		  if ($updated_total<>$present_total){
		    if ($updated_total>0){
		       $net_amount=$updated_total;
		       $display_title='customer';
		    }
		    else
		    {
		       $display_title='admin';
		       $net_amount=$present_total;
		    }
		  }
		}
		if ($net_amount==0) 
		   $pay_type='Nil';
		else
	  	   $pay_type='getamount';
   $payment_modules=new payment();
   	$net_amount=tep_get_rounded_amount($net_amount);
	
	$update_details=false;
	if ($updated_total>0 && strval($updated_total)==strval($present_total)) $update_details=true;
?>
<?php  if (isset($HTTP_GET_VARS['error_message'])){ ?>
          <tr><td colspan="2" class='main'><font color=red><?php echo $HTTP_GET_VARS['error_message']; ?></font></td></tr>
       <tr>
	<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
      </tr>
<?php } 
      if (isset($HTTP_GET_VARS['payment_error']) && is_object(${$HTTP_GET_VARS['payment_error']}) && ($error = ${$HTTP_GET_VARS['payment_error']}->get_error())) {?>
        <tr><td>
 	  <table border="0" width="100%" cellspacing="0" cellpadding="0">
	    <tr><td align=left class='pageHeading' colspan='2'><?php echo tep_output_string_protected($error['title']); ?></td></tr>
	  </table></td></tr>
      <tr>
        <td><table border="0" width="100%" cellspacing="1" cellpadding="2" bgcolor="#ff6c6c">
          <tr class="infoBoxNoticeContents">
            <td><table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                <td class="main" width="100%" valign="top"><font color=white><?php echo $error['error']; ?></font></td>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
              </tr>
            </table></td>
          </tr>
        </table></td>
      </tr>
      <tr>
	<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
      </tr>
<?php } ?>
      <tr>
	<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
      </tr>
	<script language="JavaScript">
		function updateDetail(){
			document.edit_order.detail_only.value=1;
			document.edit_order.submit();
		}
	</script>
	<tr>
	   <td class="main"><b><?php echo TEXT_PAYMENT_AMOUNT; ?></b></td>
	</tr>
      <tr>
	<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif','1','3'); ?></td>
      </tr>
      <tr>
	    <td align=left>
	    	<table border="0" cellspacing="2" cellpadding="2" width="60%" class=formArea>
		<tr>
	    	<td align='right'>
	    	<table border="0" cellspacing="2" cellpadding="2" align=left>
		    <tr><td class=main align=right><?php echo TEXT_UPDATED_TOTAL ?></td>
		        <td class=smallText align=right><b><?php echo $currencies->format($net_amount); ?></b>
			 	<input type=hidden name='amount' value='<?php echo $net_amount; ?>'>
			 	<input type=hidden name='detail_only' value='0'>
			 </td>
		    </tr>
	    	</table>
	    	</td>
	    	</tr></table></td></tr>
<?php if ($net_amount!=0){ ?>
      <tr>
	<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif','1','5'); ?></td>
      </tr>
	<tr>
	   <td class="main">
	                 <?php if ($display_title=='customer')
			          echo '<b>' . TEXT_CUSTOMER_PAYMENT_METHOD . '&nbsp;</b>' . TEXT_CUSTOMER_INFO;
			       else
			          echo '<b>' . TEXT_ADMIN_PAYMENT_METHOD . '&nbsp;</b>' . TEXT_ADMIN_INFO;
			 ?></td>
	</tr>
      <tr>
	<td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif','1','3'); ?></td>
      </tr>
<!-- Begin Payment Block -->
      <tr>
        <td><table border="0" width="60%" cellspacing="1" cellpadding="2" class="formArea">
          <tr class="infoBoxContents">
            <td><table border="0" width="100%" cellspacing="0" cellpadding="2">
<?php
  $selection = $payment_modules->selection();
  if (sizeof($selection) > 1) {
?>
              <tr>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                <td class="main" width="50%" valign="top"><b><?php //echo TEXT_SELECT_PAYMENT_METHOD; ?></b></td>
                <td class="main" width="50%" valign="top" align="right"><b><?php echo TITLE_PLEASE_SELECT; ?></b><br><?php echo tep_image(DIR_WS_IMAGES . 'arrow_east_south.gif'); ?></td>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
              </tr>
<?php
  } else {
?>
              <tr>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                <td class="main" width="100%" colspan="2"><?php echo TEXT_ENTER_PAYMENT_INFORMATION; ?></td>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
              </tr>
<?php
  }

  $radio_buttons = 0;
  $cnt=1;
  for ($i=0, $n=sizeof($selection); $i<$n; $i++) {
?>
              <tr>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                <td colspan="2"><table border="0" width="100%" cellspacing="0" cellpadding="2">
<?php
    if ( ($selection[$i]['id'] == $payment) || ($n == 1) ) {
      echo '                  <tr id="defaultRow" class="moduleRowSelected" onmouseover="rowOverEffect2(this)" onmouseout="rowOutEffect2(this)" onclick="selectRowEffect2(this, ' . $radio_buttons . ')">' . "\n";
    } else {
      echo '                  <tr class="moduleRow" onmouseover="rowOverEffect2(this)" onmouseout="rowOutEffect2(this)" onclick="selectRowEffect2(this, ' . $radio_buttons . ')">' . "\n";
	}
?>
                    <td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                    <td class="main" colspan="3"><b><?php echo $selection[$i]['module']; ?></b></td>
                    <td class="main" align="right">
<?php
    if (sizeof($selection) > 1) {
      echo tep_draw_radio_field('payment', $selection[$i]['id']);
    } else {
      echo tep_draw_hidden_field('payment', $selection[$i]['id']);
    }
    echo "<input type=hidden name=check_payment value='" . $selection[$i]['module'] . "'>";
?>
                    </td>
                    <td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                  </tr>
<?php
    if (isset($selection[$i]['error'])) {
?>
                  <tr>
                    <td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                    <td class="main" colspan="4"><?php echo $selection[$i]['error']; ?></td>
                    <td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                  </tr>
<?php
    } elseif (isset($selection[$i]['fields']) && is_array($selection[$i]['fields'])) {
?>
                  <tr>
                    <td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                    <td colspan="4"><table border="0" cellspacing="0" cellpadding="2">
<?php
      for ($j=0, $n2=sizeof($selection[$i]['fields']); $j<$n2; $j++) {
?>
                      <tr>
                        <td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                        <td class="main"><?php echo $selection[$i]['fields'][$j]['title']; ?></td>
                        <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                        <td class="main"><?php echo $selection[$i]['fields'][$j]['field']; ?></td>
                        <td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                      </tr>
<?php
   }
                  if ($selection[$i]['module']=='Test') {
?>
		  <tr><td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
		    <td class="main"><?php echo ENTRY_CREDIT_CARD_CVV; ?></td>
		    <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
		    <td class="main"><?php echo tep_draw_input_field('update_info_cc_cvv','','size=10');?></td>
		    <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
	           </tr>
<?php } ?>
                    </table></td></tr>
                    
                    <tr><td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                  </tr>
<?php
    }
?>
                </table></td>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
              </tr>
<?php
    $radio_buttons++;
    $cnt++;
  }
?>
            </table></td>
          </tr>
        </table></td>
<!-- End Payment Block -->
<?php } ?>
      </tr></table></td></tr>
<?php } ?>
<?php if ($step==2) {?>
      <tr>
	<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr>
	<td class=main><b><?php echo TEXT_HEADING_PRODUCTS; ?></b></td>
      </tr>
      <tr>
	<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '3'); ?></td>
      </tr>
<!-- Begin Products Listing Block -->
      <tr>
	<td><table border="0" width="100%" cellspacing="2" cellpadding="2" class="formArea">
	  <tr class="attributes-odd" height=30>
	    <td class="main" width=40><b><?php echo TABLE_HEADING_ACTION; ?></b></td>
	    <td class="main" colspan="2" align=center><b><?php echo TABLE_HEADING_PRODUCTS; ?></b></td>
	    <td class="main"><b><?php echo TABLE_HEADING_PRODUCTS_MODEL; ?></b></td>
	    <td class="main" align="center"><b><?php echo TABLE_HEADING_TAX; ?></b></td>
	    <td class="main" align="right"><b><?php echo TABLE_HEADING_UNIT_PRICE; ?></b></td>
	    <td class="main" align="right"><b><?php echo TABLE_HEADING_TOTAL_PRICE; ?></b></td>
	  </tr>

	<!-- Begin Products Listings Block -->
	<?
      	// Override order.php Class's Field Limitations
	    $RunningSubTotal=0.0;
		$Tax=0.0;
		$RunningTax=0.0;
		$Total=0.0;
		$index = 0;
		$products_check=0;
		$order_detail->products = array();
		$orders_products_query = tep_db_query("select * from " . TABLE_ORDERS_EDIT_PRODUCTS . " where orders_id = '" . (int)$oID . "' and products_type='P'");
		while ($orders_products = tep_db_fetch_array($orders_products_query)) {
		$order_detail->products[$index] = array('qty' => $orders_products['products_quantity'],
                                        'name' => str_replace("'", "&#39;", $orders_products['products_name']),
                                        'model' => $orders_products['products_model'],
                                        'tax' => $orders_products['products_tax'],
                                        'price' => $orders_products['products_price'],
                                        'final_price' => $orders_products['final_price'],
										'products_type' => $orders_products['products_type'],
                                        'orders_products_id' => $orders_products['orders_products_id']);
		$subindex = 0;
		$attributes_query_string = "select * from " . TABLE_ORDERS_EDIT_PRODUCTS_ATTRIBUTES . " where orders_id = '" . (int)$oID . "' and orders_products_id = '" . (int)$orders_products['orders_products_id'] . "'";
		$attributes_query = tep_db_query($attributes_query_string);

		if (tep_db_num_rows($attributes_query)) {
		while ($attributes = tep_db_fetch_array($attributes_query)) {
		  $order_detail->products[$index]['attributes'][$subindex] = array('option' => $attributes['products_options'],
		                                                           'value' => $attributes['products_options_values'],
		                                                           'prefix' => $attributes['price_prefix'],
		                                                           'price' => $attributes['options_values_price'],
		                                                           'orders_products_attributes_id' => $attributes['orders_products_attributes_id']);
		$subindex++;
		}
		}
		$Tax=(($order_detail->products[$index]['tax'] / 100) * ($order_detail->products[$index]['qty'] * $order_detail->products[$index]['final_price']));

		$RunningSubTotal += ((($order_detail->products[$index]['qty'] * $order_detail->products[$index]['final_price']))+$Tax);
		$RunningTax+=$Tax;
		$index++;
		}
		$Total=$RunningSubTotal;
		$product_type="";
	for ($i=0; $i<sizeof($order_detail->products); $i++) {
		$product_type="P";
		$orders_products_id = $order_detail->products[$i]['orders_products_id'];
		$producttype=$order_detail->products[$i]['products_type'];
		$readonly="";
		if($producttype=='E'){
			$readonly="readonly";
		}
		else{
			$readonly="";
		}
		$RowStyle = "dataTableContent";
		
		echo '	  <tr class="' .  (floor($i/2) == ($i/2) ? 'attributes-even' : 'attributes-odd') . '">' . "\n" .
		   '        <td class="' . $RowStyle . '" valign="top" align="right">' . '<a href="' . tep_href_link(FILENAME_EDIT_ORDERS,"oID=$oID&action=add_product&mode=edit&id=$orders_products_id") . '">' . tep_image(DIR_WS_ICONS . '/edit.gif',IMAGE_EDIT_PRODUCT) . '</a><a href="' . tep_href_link(FILENAME_EDIT_ORDERS,'oID=' . $oID . "&action=delete_item&id=$orders_products_id") . '">' . tep_image(DIR_WS_ICONS . '/delete_cross.gif',IMAGE_DELETE_PRODUCT) . '</a></td>' .
		   '	    <td class="' . $RowStyle . '" valign="top" align="right">' . "<input name='update_products[$orders_products_id][qty]'  size='2' value='" . $order_detail->products[$i]['qty'] . "' " . $readonly . ">&nbsp;x</td>\n" . 
		   '	    <td class="' . $RowStyle . '" valign="top">' . "<input name='update_products[$orders_products_id][name]' size='40' value='" . $order_detail->products[$i]['name'] . "'>";
		
		// Has Attributes?
		if (sizeof($order_detail->products[$i]['attributes']) > 0) {
			for ($j=0; $j<sizeof($order_detail->products[$i]['attributes']); $j++) {
				$orders_products_attributes_id = $order_detail->products[$i]['attributes'][$j]['orders_products_attributes_id'];
				echo '<br><nobr><small>&nbsp;<i> - ' . "<input name='update_products[$orders_products_id][attributes][$orders_products_attributes_id][option]' size='6' value='" . $order_detail->products[$i]['attributes'][$j]['option'] . "'>" . ': ' . "<input name='update_products[$orders_products_id][attributes][$orders_products_attributes_id][value]' size='10' value='" . $order_detail->products[$i]['attributes'][$j]['value'] . "'>";
				echo '</i></small></nobr>';
			}
		}
		
		echo '	    </td>' . "\n" .
		     '	    <td class="' . $RowStyle . '" valign="top">' . "<input name='update_products[$orders_products_id][model]' size='12' value='" . $order_detail->products[$i]['model'] . "'>" . '</td>' . "\n" .
		     '	    <td class="' . $RowStyle . '" align="center" valign="top">' . "<input name='update_products[$orders_products_id][tax]' size='3' value='" . tep_display_tax_value($order_detail->products[$i]['tax']) . "'>" . '%</td>' . "\n" .
		     '	    <td class="' . $RowStyle . '" align="right" valign="top">' . "<input name='update_products[$orders_products_id][final_price]' size='5' value='" . number_format($order_detail->products[$i]['final_price'], 2, '.', '') . "'>" . '</td>' . "\n" . 
		     '	    <td class="' . $RowStyle . '" align="right" valign="top">' . $currencies->format($order_detail->products[$i]['final_price'] * $order_detail->products[$i]['qty'], true, $order_detail->info['currency'], $order_detail->info['currency_value']) . '</td>' . "\n" . 
		     '	  </tr>' . "\n";
		$products_check++;
	}
	?>
	<!-- End Products Listings Block -->
	<tr class="<?php echo (floor($i/2) == ($i/2) ? 'attributes-even' : 'attributes-odd');?>">
	    <td colspan=7><?php echo '<a href="' . tep_href_link(FILENAME_EDIT_ORDERS,"oID=$oID&action=add_product&step=1&mode=add") . '">' . tep_image_button('button_add.gif',IMAGE_ADD_PRODUCT,'align=absmiddle') . '</a>';?>
	<tr>
	</table><td></tr>

      <tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td></tr>
      <tr>
	<td class=main><b><?php echo TEXT_HEADING_EVENTS; ?></b></td>
      </tr>
      <tr>
	<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '3'); ?></td>
      </tr>
<!-- Begin Events Listing Block -->
      <tr>
	<td><table border="0" width="100%" cellspacing="2" cellpadding="2" class="formArea">
	  <tr class="attributes-odd" height=30>
	    <td class="main" width=40><b><?php echo TABLE_HEADING_ACTION; ?></b></td>
	    <td class="main" colspan="2" align="center"><b><?php echo TABLE_HEADING_EVENTS; ?></b></td>
	    <td class="main"><b><?php echo TABLE_HEADING_EVENTS_SESSION; ?></b></td>
	    <td class="main" align="center"><b><?php echo TABLE_HEADING_TAX; ?></b></td>
	    <td class="main" align="right"><b><?php echo TABLE_HEADING_UNIT_PRICE; ?></b></td>
	    <td class="main" align="right"><b><?php echo TABLE_HEADING_TOTAL_PRICE; ?></b></td>
	  </tr>

	<!-- Begin Products Listings Block -->
	<?
      	// show the event details from the order
		$index = 0;
		$order_detail->products = array();
		$orders_products_query = tep_db_query("select s.start_date,s.events_id,op.orders_products_id,op.products_quantity,op.products_name,op.products_model,op.products_tax,op.products_price,op.final_price,op.products_type,op.events_type,op.waitlist_type from " . TABLE_ORDERS_EDIT_PRODUCTS . " op, " . TABLE_EVENTS_SESSIONS . " s where op.orders_id = '" . (int)$oID . "' and op.products_type='E' and op.products_id=s.sessions_id order by op.products_name,s.start_date");
		$events_style_waitlist=array();
		$prev_event=0;
		while ($orders_products = tep_db_fetch_array($orders_products_query)) {
		// fetch every event waiting list style into an array
		if ($prev_event!=$orders_products["events_id"]){
			$prev_event=$orders_products["events_id"];
			$events_style_waitlist[$prev_event]=0;
			$event_query=tep_db_query("SELECT session_style from " . TABLE_EVENTS . " where events_id='" . $prev_event ."'");
			$events_style_result=tep_db_fetch_array($event_query);
			if ($events_style_result["session_style"]=='E') $events_style_waitlist[$prev_event]=1;
		}
		// fetch the event details
		$order_detail->products[$index] = array('qty' => $orders_products['products_quantity'],
                                        'name' => str_replace("'", "&#39;", $orders_products['products_name']),
                                        'model' => format_date($orders_products['start_date']),
                                        'tax' => $orders_products['products_tax'],
                                        'price' => $orders_products['products_price'],
                                        'final_price' => $orders_products['final_price'],
										'products_type' => $orders_products['products_type'],
										'events_type'=>$orders_products['events_type'],
										'events_id'=>$orders_products['events_id'],
                                        'orders_products_id' => $orders_products['orders_products_id']);
		$subindex = 0;
		$attributes_query_string = "select * from " . TABLE_ORDERS_EDIT_PRODUCTS_ATTRIBUTES . " where orders_id = '" . (int)$oID . "' and orders_products_id = '" . (int)$orders_products['orders_products_id'] . "'";
		$attributes_query = tep_db_query($attributes_query_string);

		if (tep_db_num_rows($attributes_query)) {
		while ($attributes = tep_db_fetch_array($attributes_query)) {
		  $order_detail->products[$index]['attributes'][$subindex] = array('option' => $attributes['products_options'],
		                                                           'value' => $attributes['products_options_values'],
		                                                           'prefix' => $attributes['price_prefix'],
		                                                           'price' => $attributes['options_values_price'],
		                                                           'orders_products_attributes_id' => $attributes['orders_products_attributes_id']);
		$subindex++;
		}
		}
		$Tax=(($order_detail->products[$index]['tax'] / 100) * ($order_detail->products[$index]['qty'] * $order_detail->products[$index]['final_price']));
		$RunningSubTotal += ((($order_detail->products[$index]['qty'] * $order_detail->products[$index]['final_price']))+$Tax);
		$RunningTax+=$Tax;
		$index++;
		}
		$Total=$RunningSubTotal;
		$prev_event=0;		
	for ($i=0; $i<sizeof($order_detail->products); $i++) {
	    if ($product_type=="P") $product_type="M";
		else if ($product_type!="M") $product_type="E";
		
		$orders_products_id = $order_detail->products[$i]['orders_products_id'];
		$producttype=$order_detail->products[$i]['products_type'];
		$readonly="";
		if($producttype=='E'){
			$readonly="readonly";
		} else{
			$readonly="";
		}
		if ($order_detail->products[$i]['events_type']=='R') $color="#000000";
		else $color="#FFA715";
		$RowStyle = "dataTableContent";
		//check for events style waitlist
		if ($order_detail->products[$i]["events_type"]=='W' && $events_style_waitlist[$order_detail->products[$i]["events_id"]]==1){
			//  check the session item amount for none
			if ($order_detail->products[$i]['final_price']!=0){
				$prev_event=$order_detail->products[$i]["events_id"];
				echo '	  <tr height=30 class="' .  (floor($i/2) == ($i/2) ? 'attributes-even' : 'attributes-odd') . '">' . "\n" .
					 '        <td class="' . $RowStyle . '" valign="middle" align="right">' . '<a href="' . tep_href_link(FILENAME_EDIT_ORDERS,'oID=' . $oID . "&action=delete_group&eID=" . $prev_event) . '">' . tep_image(DIR_WS_ICONS . '/delete_cross.gif',IMAGE_DELETE_EVENT) . '</a></td>' .
					 '	    <td></td><td class="' . $RowStyle . '" valign="middle">' . $order_detail->products[$i]['name'] . "</td>\n" . 
					 '	    <td class="main" valign="middle"><font color="' . $color . '">' . TEXT_WAITLIST . "</font></td>\n" . 
					 '	    <td class="' . $RowStyle . '" valign="middle">' . tep_display_tax_value($order_detail->products[$i]['tax']) . "%</td>\n" . 
 					 '	    <td class="' . $RowStyle . '" valign="middle" align="right">' . number_format($order_detail->products[$i]['final_price'], 2, '.', '') . "</td>\n" . 
					 '	    <td class="' . $RowStyle . '" valign="middle" align="right">' . $currencies->format($order_detail->products[$i]['final_price'] * $order_detail->products[$i]['qty'], true, $order_detail->info['currency'], $order_detail->info['currency_value']) . "</td>\n";
			}
			echo	"<input type='hidden' name='update_products[$orders_products_id][qty]'   value='" . $order_detail->products[$i]['qty'] . "' " . $readonly . ">" . 
					"<input type='hidden' name='update_products[$orders_products_id][name]'  value='" . $order_detail->products[$i]['name'] . "'>" . 
		 			"<input type='hidden' name='update_products[$orders_products_id][model]' value='" . $order_detail->products[$i]['model'] . "'>" . 
					"<input type='hidden' name='update_products[$orders_products_id][tax]' value='" . tep_display_tax_value($order_detail->products[$i]['tax']) . "'>" . 
					"<input type='hidden' name='update_products[$orders_products_id][final_price]' value='" . number_format($order_detail->products[$i]['final_price'], 2, '.', '') . "'>";
		} else {
			echo '	  <tr height=30 class="' .  (floor($i/2) == ($i/2) ? 'attributes-even' : 'attributes-odd') . '">' . "\n" .
			   '        <td class="' . $RowStyle . '" valign="top" align="right">' . '<a href="' . tep_href_link(FILENAME_ADD_EVENTS,"oID=$oID&mode=edit&eId=" . $order_detail->products[$i]["events_id"] . "&step=0") . '">' . tep_image(DIR_WS_ICONS . '/edit.gif',IMAGE_EDIT_EVENT) . '</a><a href="' . tep_href_link(FILENAME_EDIT_ORDERS,'oID=' . $oID . "&action=delete_item&id=$orders_products_id") . '">' . tep_image(DIR_WS_ICONS . '/delete_cross.gif',IMAGE_DELETE_EVENT) . '</a></td>' .
			   '	    <td class="' . $RowStyle . '" valign="top" align="right">' . "<input name='update_products[$orders_products_id][qty]'  size='2' value='" . $order_detail->products[$i]['qty'] . "' " . $readonly . ">&nbsp;x</td>\n" . 
			   '	    <td class="' . $RowStyle . '" valign="top" align="left">' . "<input name='update_products[$orders_products_id][name]' size='40' value='" . $order_detail->products[$i]['name'] . "'>";
	
			// Has Attributes?
			if (sizeof($order_detail->products[$i]['attributes']) > 0) {
				for ($j=0; $j<sizeof($order_detail->products[$i]['attributes']); $j++) {
					$orders_products_attributes_id = $order_detail->products[$i]['attributes'][$j]['orders_products_attributes_id'];
					echo '<br><nobr><small>&nbsp;<i> - ' . "<input name='update_products[$orders_products_id][attributes][$orders_products_attributes_id][option]' size='6' value='" . $order_detail->products[$i]['attributes'][$j]['option'] . "'>" . ': ' . "<input name='update_products[$orders_products_id][attributes][$orders_products_attributes_id][value]' size='10' value='" . $order_detail->products[$i]['attributes'][$j]['value'] . "'>";
					echo '</i></small></nobr>';
				}
			}
			
			echo '	    </td>' . "\n" .
				 '	    <td class="' . $RowStyle . '" valign="top">' . "<input name='update_products[$orders_products_id][model]' size='12' readonly=true value='" . $order_detail->products[$i]['model'] . "' style='color:" . $color . "'>" . '</td>' . "\n" .
				 '	    <td class="' . $RowStyle . '" align="center" valign="top">' . "<input name='update_products[$orders_products_id][tax]' size='3' value='" . tep_display_tax_value($order_detail->products[$i]['tax']) . "'>" . '%</td>' . "\n" .
				 '	    <td class="' . $RowStyle . '" align="right" valign="top">' . "<input name='update_products[$orders_products_id][final_price]' size='5' value='" . number_format($order_detail->products[$i]['final_price'], 2, '.', '') . "'>" . '</td>' . "\n" . 
				 '	    <td class="' . $RowStyle . '" align="right" valign="top">' . $currencies->format($order_detail->products[$i]['final_price'] * $order_detail->products[$i]['qty'], true, $order_detail->info['currency'], $order_detail->info['currency_value']) . '</td>' . "\n" . 
				 '	  </tr>' . "\n";
		}
		$products_check++;
	}
	?>
	<!-- End Events Listings Block -->
	<tr class="<?php echo (floor($i/2) == ($i/2) ? 'attributes-even' : 'attributes-odd');?>">
	    <td colspan=7><?php echo '<a href="' . tep_href_link(FILENAME_ADD_EVENTS,"oID=$oID&step=1&mode=add") . '">' . tep_image_button('button_add.gif',IMAGE_ADD_EVENT) . '</a>';?>
	</tr></table><td></tr>
      <tr>
	<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
	<td class=main><b><?php echo TEXT_HEADING_SUBSCRIPTIONS; ?></b></td>
      </tr>
      <tr>
	<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '3'); ?></td>
      </tr>
	<!-- Begin subscription Listing Block -->
      <tr>
	<td><table border="0" width="100%" cellspacing="2" cellpadding="2" class="formArea">
	  <tr class="attributes-odd" height=30>
	    <td class="main" width=40><b><?php echo TABLE_HEADING_ACTION; ?></b></td>
	    <td class="main" colspan="2" align=center><b><?php echo TABLE_HEADING_SUBSCRIPTIONS; ?></b></td>
		<td class="main"></td>
	    <td class="main" align="center"><b><?php echo TABLE_HEADING_TAX; ?></b></td>
	    <td class="main" align="right"><b><?php echo TABLE_HEADING_UNIT_PRICE; ?></b></td>
	    <td class="main" align="right"><b><?php echo TABLE_HEADING_TOTAL_PRICE; ?></b></td>
	  </tr>

	<!-- Begin Subscription Listings Block -->
	<?
      	// Override order.php Class's Field Limitations
		$index = 0;
		$order_detail->products = array();
		$orders_products_query = tep_db_query("select * from " . TABLE_ORDERS_EDIT_PRODUCTS . " where orders_id = '" . (int)$oID . "' and products_type='S'");
		while ($orders_products = tep_db_fetch_array($orders_products_query)) {
		$order_detail->products[$index] = array('qty' => $orders_products['products_quantity'],
                                        'name' => str_replace("'", "&#39;", $orders_products['products_name']),
                                        'tax' => $orders_products['products_tax'],
                                        'price' => $orders_products['products_price'],
                                        'final_price' => $orders_products['final_price'],
										'products_type' => $orders_products['products_type'],
                                        'orders_products_id' => $orders_products['orders_products_id']);
		$subindex = 0;
		$attributes_query_string = "select * from " . TABLE_ORDERS_EDIT_PRODUCTS_ATTRIBUTES . " where orders_id = '" . (int)$oID . "' and orders_products_id = '" . (int)$orders_products['orders_products_id'] . "'";
		$attributes_query = tep_db_query($attributes_query_string);

		//if (tep_db_num_rows($attributes_query)) {
		//while ($attributes = tep_db_fetch_array($attributes_query)) {
		 // $order_detail->products[$index]['attributes'][$subindex] = array('option' => $attributes['products_options'],
		  //                                                         'value' => $attributes['products_options_values'],
		  //                                                         'prefix' => $attributes['price_prefix'],
		   //                                                        'price' => $attributes['options_values_price'],
		    //                                                       'orders_products_attributes_id' => $attributes['orders_products_attributes_id']);
		//$subindex++;
		//}
		//}
		$Tax=(($order_detail->products[$index]['tax'] / 100) * ($order_detail->products[$index]['qty'] * $order_detail->products[$index]['final_price']));

		$RunningSubTotal += ((($order_detail->products[$index]['qty'] * $order_detail->products[$index]['final_price']))+$Tax);
		$RunningTax+=$Tax;
		$index++;
		}
		$Total=$RunningSubTotal;
		$product_type="";
	for ($i=0; $i<sizeof($order_detail->products); $i++) {
		$product_type="S";
		$orders_products_id = $order_detail->products[$i]['orders_products_id'];
		$producttype=$order_detail->products[$i]['products_type'];
		$readonly="";
		if($producttype=='S'){
			$readonly="readonly";
		}
		else{
			$readonly="";
		}
		$RowStyle = "dataTableContent";
		
		echo '	  <tr class="' .  (floor($i/2) == ($i/2) ? 'attributes-even' : 'attributes-odd') . '">' . "\n" .
		   '        <td class="' . $RowStyle . '" valign="top" align="right">' .  '<a href="' . tep_href_link(FILENAME_EDIT_ORDERS,'oID=' . $oID . "&action=delete_item&id=$orders_products_id") . '">' . tep_image(DIR_WS_ICONS . '/delete_cross.gif',IMAGE_DELETE_SUBSCRIPTIONS) . '</a></td>' .
		   '	    <td class="' . $RowStyle . '" valign="top" align="right">' . "<input name='update_products[$orders_products_id][qty]'  size='2' value='" . $order_detail->products[$i]['qty'] . "' " . $readonly . ">&nbsp;x</td>\n" . 
		   '	    <td class="' . $RowStyle . '" valign="top">' . "<input name='update_products[$orders_products_id][name]' size='40' value='" . $order_detail->products[$i]['name'] . "'>";
		
		// Has Attributes?
		if (sizeof($order_detail->products[$i]['attributes']) > 0) {
			for ($j=0; $j<sizeof($order_detail->products[$i]['attributes']); $j++) {
				$orders_products_attributes_id = $order_detail->products[$i]['attributes'][$j]['orders_products_attributes_id'];
				echo '<br><nobr><small>&nbsp;<i> - ' . "<input name='update_products[$orders_products_id][attributes][$orders_products_attributes_id][option]' size='6' value='" . $order_detail->products[$i]['attributes'][$j]['option'] . "'>" . ': ' . "<input name='update_products[$orders_products_id][attributes][$orders_products_attributes_id][value]' size='10' value='" . $order_detail->products[$i]['attributes'][$j]['value'] . "'>";
				echo '</i></small></nobr>';
			}
		}
		
		echo '	    </td>' . "\n" .
		     '	    <td class="' . $RowStyle . '" valign="top">' .'</td>' . "\n" .
		     '	    <td class="' . $RowStyle . '" align="center" valign="top">' . "<input name='update_products[$orders_products_id][tax]' size='3' value='" . tep_display_tax_value($order_detail->products[$i]['tax']) . "'>" . '%</td>' . "\n" .
		     '	    <td class="' . $RowStyle . '" align="right" valign="top">' . "<input name='update_products[$orders_products_id][final_price]' size='5' value='" . number_format($order_detail->products[$i]['final_price'], 2, '.', '') . "'>" . '</td>' . "\n" . 
		     '	    <td class="' . $RowStyle . '" align="right" valign="top">' . $currencies->format($order_detail->products[$i]['final_price'] * $order_detail->products[$i]['qty'], true, $order_detail->info['currency'], $order_detail->info['currency_value']) . '</td>' . "\n" . 
		     '	  </tr>' . "\n";
		$products_check++;
	}
	?>
	<!-- End subscription Listings Block -->
	<tr class="<?php echo (floor($i/2) == ($i/2) ? 'attributes-even' : 'attributes-odd');?>">
	    <td colspan=7><?php echo '<a href="' . tep_href_link(FILENAME_EDIT_ORDERS,"oID=$oID&action=add_subscription&step=1&mode=add") . '">' . tep_image_button('button_add.gif',IMAGE_ADD_SUBSCRIPTIONS,'align=absmiddle') . '</a>';?>
	<tr>
	</table><td></tr>
	<tr>
		<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '3'); ?></td>
    </tr>
	<!-- end of Subscription  Block-->
	<!-- Begin Order Total Block -->
	  <tr>
	    <td align=right>
	    	<table border="0" cellspacing="2" cellpadding="2" width=30% class=formArea>
		<tr>
	    	<td align='right'>
	    	<table border="0" cellspacing="2" cellpadding="2" align=right width=100%>
<?php
                $order_query=tep_db_query("SELECT currency,currency_value from " . TABLE_ORDERS . " where orders_id='" . $oID . "'");
                $order_info=tep_db_fetch_array($order_query);

		$totals_query = tep_db_query("select * from " . TABLE_ORDERS_TOTAL . " where orders_id = '" . (int)$oID . "' order by sort_order");
		$order_detail->totals = array();
                $TotalsArray=array();
                while($totals=tep_db_fetch_array($totals_query)){
		   if ($totals['class']=='ot_subtotal'){
		      $TotalsArray[]=array('title'=>TEXT_PRESENT_SUBTOTAL,'value'=>$currencies->format($totals['value'], true,$order_info['currency'], $order_info['currency_value']));
		      $TotalsArray[]=array('title'=>TEXT_UPDATED_SUBTOTAL,'value'=>'<b>' . $currencies->format($RunningSubTotal, true,$order_info['currency'], $order_info['currency_value']) . '</b>');
		   }
		   else if ($totals['class']=='ot_tax'){
		      $TotalsArray[]=array('title'=>TEXT_PRESENT_TAX,'value'=>$currencies->format($totals['value'], true,$order_info['currency'], $order_info['currency_value']));
		      $TotalsArray[]=array('title'=>TEXT_UPDATED_TAX,'value'=>'<b>' . $currencies->format($RunningTax, true,$order_info['currency'], $order_info['currency_value']) . '</b>');
		   }
		   else if ($totals['class']=='ot_shipping'){
		      $TotalsArray[]=array('title'=>TEXT_PRESENT_SHIPPING,'value'=>$currencies->format($totals['value'], true,$order_info['currency'], $order_info['currency_value']));
		      $TotalsArray[]=array('title'=>TEXT_UPDATED_SHIPPING,'value'=>'<b>' . $currencies->format($totals['value'], true,$order_info['currency'], $order_info['currency_value']) . '</b>');
		   }
		   else if ($totals['class']=='ot_total'){
		      $TotalsArray[]=array('title'=>TEXT_PRESENT_TOTAL,'value'=>$currencies->format($totals['value'], true,$order_info['currency'], $order_info['currency_value']));
		      $TotalsArray[]=array('title'=>TEXT_UPDATED_TOTAL,'value'=>'<b>' . $currencies->format($Total, true,$order_info['currency'], $order_info['currency_value'])  . '</b>');
		   }
		}   
		for($icnt=0;$icnt<sizeof($TotalsArray);$icnt++){
		?>
		    <tr><td class=main align=right><?php echo $TotalsArray[$icnt]['title'];?></td>
		        <td class=smallText align=right><?php echo $TotalsArray[$icnt]['value'];?></td>
		    </tr>
                    <tr><td><?php if ($icnt%2==1) echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td></tr>
		<?php 
		}
		echo tep_draw_hidden_field('products_type',$product_type);
		if ($products_check>0)
		  echo tep_draw_hidden_field('products_check','valid');
		else
		  echo tep_draw_hidden_field('products_check','invalid');
?>
	    	</table>
	    	</td>
	    	</tr>
	    	</table>
	    </td>
	  </tr>
	<!-- End Order Total Block -->
<?php } ?>
<?php if ($step==5) { ?>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
      </tr>
      <tr><td class="main"><table border="0" cellspacing="2" cellpadding="2" class="formarea" width=60%><tr>
	<tr bgcolor=white>
	   <td class="main"><?php echo '<b>' . TEXT_CURRENT_STATUS . '</b>' ;?></td></tr>
	<tr><td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td></tr>
        <tr>
        <td class="main"><table border="1" cellspacing="0" cellpadding="5" width=100%>
          <tr>
            <td class="smallText" align="center"><b><?php echo TABLE_HEADING_DATE_ADDED; ?></b></td>
            <td class="smallText" align="center"><b><?php echo TABLE_HEADING_CUSTOMER_NOTIFIED; ?></b></td>
            <td class="smallText" align="center"><b><?php echo TABLE_HEADING_STATUS; ?></b></td>
            <?php if($CommentsWithStatus) { ?>
            <td class="smallText" align="center"><b><?php echo TABLE_HEADING_COMMENTS; ?></b></td>
            <?php } ?>
          </tr>
<?php
    $orders_history_query = tep_db_query("select * from " . TABLE_ORDERS_STATUS_HISTORY . " where orders_id = '" . tep_db_prepare_input($oID) . "' order by date_added");
    if (tep_db_num_rows($orders_history_query)) {
      while ($orders_history = tep_db_fetch_array($orders_history_query)) {
        echo '          <tr>' . "\n" .
             '            <td class="smallText" align="center">' . format_date($orders_history['date_added']) . '</td>' . "\n" .
             '            <td class="smallText" align="center">';
        if ($orders_history['customer_notified'] == '1') {
          echo tep_image(DIR_WS_ICONS . 'tick.gif', ICON_TICK) . "</td>\n";
        } else {
          echo tep_image(DIR_WS_ICONS . 'cross.gif', ICON_CROSS) . "</td>\n";
        }
        echo '            <td class="smallText">' . $orders_status_array[$orders_history['orders_status_id']] . '</td>' . "\n";
        
        if($CommentsWithStatus) {
        echo '            <td class="smallText">' . nl2br(tep_db_output($orders_history['comments'])) . '&nbsp;</td>' . "\n";
        }
        
        echo '          </tr>' . "\n";
      }
    } else {
        echo '          <tr>' . "\n" .
             '            <td class="smallText" colspan="5">' . TEXT_NO_ORDER_HISTORY . '</td>' . "\n" .
             '          </tr>' . "\n";
    }
?>
        </table></td>
      </tr>
      <tr>
	<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
      </tr>
      <tr bgcolor=white>
	<td class="main"><b><?php echo TEXT_MODIFY_STATUS; ?></b></td>
      </tr>
      <tr>
	<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '3'); ?></td>
      </tr>
      <tr><td class="main"><table border="1" cellspacing="0" cellpadding="5" width=100%>
        <tr><td class="main"><table border="0" cellspacing="0" cellpadding="5" width=100%><tr>
           <td class="main"><b><?php echo ENTRY_STATUS; ?></b> 
	                    <?php echo tep_draw_pull_down_menu('status', $orders_statuses, $order_detail->info['orders_status']); ?>
                            <b><?php echo ENTRY_NOTIFY_CUSTOMER; ?></b> <?php echo tep_draw_checkbox_field('notify', '', true); ?>
          <?php if($CommentsWithStatus) { ?>
                <b><?php echo ENTRY_NOTIFY_COMMENTS; ?></b> <?php echo tep_draw_checkbox_field('notify_comments', '', true); ?>
          <?php } ?></td>
	</tr>
	<tr>
	<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
	</tr>
      <tr>
        <td class="main"><b><?php echo TABLE_HEADING_COMMENTS; ?></b></td>
      </tr>

	<tr>
	<td class="main">
	<?
	if($CommentsWithStatus) {
		echo tep_draw_textarea_field('comments', 'soft', '60', '5');
	}
	else
	{
		echo tep_draw_textarea_field('comments', 'soft', '60', '5', $order_detail->info['comments']);
	}
	?>
	</td>
	</tr>
        </table></td></tr></table></td>
      </tr></table></td></tr>
	<tr>
	<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
	</tr>
  <?php } ?>
	<?php if ($step==4){
		if (sizeof($selection)==0 && $net_amount!=0)
		  echo '<tr><td class="main">' . TEXT_NO_PAYMENT_MODULE . '</td></tr>';
		else if ($net_amount==0)
		  echo '<tr><td class="main">' . TEXT_NO_UPDATION . '</td></tr>'; ?>
	<tr>
	<td><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?></td>
	</tr>
	<?php } ?>
    <tr>
	<td align='left' valign="top" class="main">
	   <?php  
	     $type=isset($HTTP_GET_VARS['type'])?$HTTP_GET_VARS['type']:'';
	   if ($step>1) echo '<a href="' . tep_href_link(FILENAME_EDIT_ORDERS,'oID=' . $oID . '&step=' . ($type=='E'?2:$step-1)) . '">' . tep_image_button('button_back.gif',IMAGE_BACK) . '</a>';
	      if ($step==4){
	        if (sizeof($selection)>0 && ($pay_type=='refund' || $pay_type=='getamount')) {
	          echo '&nbsp' . tep_image_submit('button_update.gif',IMAGE_UPDATE);
			} else if ($update_details) {
				echo '&nbsp<a href="javascript:updateDetail()">' . tep_image_button("button_update.gif",IMAGE_UPDATE_CHANGES) . '</a>';
			}
	      } else {
	          echo '&nbsp' . tep_image_submit('button_continue.gif',IMAGE_CONTINUE); 
	      }
	   ?>
	</td>
      </tr><table>
      </form>
<?php
  }
  
if($action == "add_product")
{
  $mode=(isset($HTTP_GET_VARS['mode'])?$HTTP_GET_VARS['mode']:'');
  $id=(isset($HTTP_GET_VARS['id'])?(int)$HTTP_GET_VARS['id']:'');
  $add_step=(isset($HTTP_POST_VARS['add_step'])?(int)$HTTP_POST_VARS['add_step']:0);
  if ((int)$add_step==0 && $mode=='edit'){
     $data_query=tep_db_query("SELECT op.products_id,p2c.categories_id,op.products_quantity from " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where op.orders_products_id='" . $id . "' and op.products_id=p2c.products_id");
     $data_result=tep_db_fetch_array($data_query);
     $add_product_categories_id=$data_result['categories_id'];
     $add_product_products_id=$data_result['products_id'];
     $add_product_quantity=$data_result['products_quantity'];
     $step=3;
  }
?>
      <tr>
        <td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo ADDING_TITLE; ?> #<?php echo $oID; ?></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_EDIT_ORDERS, 'oID=' . $oID . '&step=2') . '">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>'; ?></td>
          </tr>
        </table></td>
      </tr>

<?
	// ############################################################################
	//   Get List of All Products
	// ############################################################################

		$result = tep_db_query("SELECT products_name, p.products_id, categories_name, ptc.categories_id FROM " . TABLE_PRODUCTS . " p LEFT JOIN " . TABLE_PRODUCTS_DESCRIPTION . " pd ON pd.products_id=p.products_id LEFT JOIN " . TABLE_PRODUCTS_TO_CATEGORIES . " ptc ON ptc.products_id=p.products_id LEFT JOIN " . TABLE_CATEGORIES_DESCRIPTION . " cd ON cd.categories_id=ptc.categories_id ORDER BY categories_name");
		while($row = tep_db_fetch_array($result))
		{
			extract($row,EXTR_PREFIX_ALL,"db");
			$ProductList[$db_categories_id][$db_products_id] = $db_products_name;
			$CategoryList[$db_categories_id] = $db_categories_name;
			$LastCategory = $db_categories_name;
		}
		
		// ksort($ProductList);
		
		$LastOptionTag = "";
		$ProductSelectOptions = "<option value='0'>Don't Add New Product" . $LastOptionTag . "\n";
		$ProductSelectOptions .= "<option value='0'>&nbsp;" . $LastOptionTag . "\n";
		foreach($ProductList as $Category => $Products)
		{
			$ProductSelectOptions .= "<option value='0'>$Category" . $LastOptionTag . "\n";
			$ProductSelectOptions .= "<option value='0'>---------------------------" . $LastOptionTag . "\n";
			asort($Products);
			foreach($Products as $Product_ID => $Product_Name)
			{
				$ProductSelectOptions .= "<option value='$Product_ID'> &nbsp; $Product_Name" . $LastOptionTag . "\n";
			}
			
			if($Category != $LastCategory)
			{
				$ProductSelectOptions .= "<option value='0'>&nbsp;" . $LastOptionTag . "\n";
				$ProductSelectOptions .= "<option value='0'>&nbsp;" . $LastOptionTag . "\n";
			}
		}
	
	
	// ############################################################################
	//   Add Products Steps
	// ############################################################################
	

		print "<tr><td><table border='0'>\n";
		
		// Set Defaults
			if(!IsSet($add_product_categories_id))
			$add_product_categories_id = 0;

			if(!IsSet($add_product_products_id))
			$add_product_products_id = 0;
		
		// Step 1: Choose Product
			print "<tr class=\"dataTableRow\"><form action='$PHP_SELF?oID=$oID&action=$action&mode=$mode&id=$id' method='POST'>\n";
			print "<td class='dataTableContent' align='right'><b>STEP 1:</b></td><td class='dataTableContent' valign='top'>";
			echo tep_draw_pull_down_menu('add_product_products_id',tep_get_products_display_tree(),$add_product_products_id);
			print "<td class='dataTableContent' align='center'><input type='submit' value='Select This Product'>";
			print "<input type='hidden' name='add_product_categories_id' value='$add_product_categories_id'>";
			print "<input type='hidden' name='add_step' value='3'>";
			print "</td>\n";
			print "</form></tr>\n";

			print "<tr><td colspan='3'>&nbsp;</td></tr>\n";
		// Step 3: Choose Options
		if(($add_step > 2) && ($add_product_products_id > 0))
		{
			// Get Options for Products
			$result = tep_db_query("SELECT * FROM " . TABLE_PRODUCTS_ATTRIBUTES . " pa LEFT JOIN " . TABLE_PRODUCTS_OPTIONS . " po ON po.products_options_id=pa.options_id LEFT JOIN " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov ON pov.products_options_values_id=pa.options_values_id WHERE products_id='$add_product_products_id'");
			
			// Skip to Step 4 if no Options
			if(tep_db_num_rows($result) == 0)
			{
				print "<tr class=\"dataTableRow\">\n";
				print "<td class='dataTableContent' align='right'><b>STEP 3:</b></td><td class='dataTableContent' valign='top' colspan='2'><i>No Options - Skipped...</i></td>";
				print "</tr>\n";
				$add_step = 4;
			}
			else
			{
				while($row = tep_db_fetch_array($result))
				{
					extract($row,EXTR_PREFIX_ALL,"db");
					$Options[$db_products_options_id] = $db_products_options_name;
					$ProductOptionValues[$db_products_options_id][$db_products_options_values_id] = $db_products_options_values_name;
				}
			
				print "<tr class=\"dataTableRow\"><form action='$PHP_SELF?oID=$oID&action=$action&mode=$mode&id=$id' method='POST'>\n";
				print "<td class='dataTableContent' align='right'><b>STEP 3:</b></td><td class='dataTableContent' valign='top'>";
				foreach($ProductOptionValues as $OptionID => $OptionValues)
				{
					$OptionOption = "<b>" . $Options[$OptionID] . "</b> - <select name='add_product_options[$OptionID]'>";
					foreach($OptionValues as $OptionValueID => $OptionValueName)
					{
					$OptionOption .= "<option value='$OptionValueID'> $OptionValueName\n";
					}
					$OptionOption .= "</select><br>\n";
					
					if(IsSet($add_product_options))
					$OptionOption = str_replace("value='" . $add_product_options[$OptionID] . "'","value='" . $add_product_options[$OptionID] . "' selected",$OptionOption);
					
					print $OptionOption;
				}		
				print "</td>";
				print "<td class='dataTableContent' align='center'><input type='submit' value='Select These Options'>";
				print "<input type='hidden' name='add_product_categories_id' value='$add_product_categories_id'>";
				print "<input type='hidden' name='add_product_products_id' value='$add_product_products_id'>";
				print "<input type='hidden' name='add_step' value='4'>";
				print "</td>\n";
				print "</form></tr>\n";
			}

			print "<tr><td colspan='3'>&nbsp;</td></tr>\n";
		}

		// Step 4: Confirm
		if($add_step > 3)
		{
			print "<tr class=\"dataTableRow\"><form action='$PHP_SELF?oID=$oID&action=insert_product&mode=$mode&id=$id' method='POST'>\n";
			print "<td class='dataTableContent' align='right'><b>STEP 4:</b></td>";
			print "<td class='dataTableContent' valign='top'><input name='add_product_quantity' size='2' value='" . $add_product_quantity . "'> Quantity</td>";
			print "<td class='dataTableContent' align='center'><input type='submit' value='Add Now!'>";

			if(IsSet($add_product_options))
			{
				foreach($add_product_options as $option_id => $option_value_id)
				{
					print "<input type='hidden' name='add_product_options[$option_id]' value='$option_value_id'>";
				}
			}
			print "<input type='hidden' name='add_product_categories_id' value='$add_product_categories_id'>";
			print "<input type='hidden' name='add_product_products_id' value='$add_product_products_id'>";
			print "<input type='hidden' name='add_step' value='5'>";
			print "</td>\n";
			print "</form></tr>\n";
		}
		
		print "</table></td></tr>\n";
}  
// Add Subscriptions
if($action == "add_subscription")
{
  $mode=(isset($HTTP_GET_VARS['mode'])?$HTTP_GET_VARS['mode']:'');
  $id=(isset($HTTP_GET_VARS['id'])?(int)$HTTP_GET_VARS['id']:'');
  $add_step=(isset($HTTP_POST_VARS['add_step'])?(int)$HTTP_POST_VARS['add_step']:0);
  if ((int)$add_step==0 && $mode=='edit'){
     $data_query=tep_db_query("SELECT op.products_id,p2c.categories_id,op.products_quantity from " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where op.orders_products_id='" . $id . "' and op.products_id=p2c.products_id");
     $data_result=tep_db_fetch_array($data_query);
     $add_subscription_categories_id=$data_result['categories_id'];
     $add_subscription_products_id=$data_result['products_id'];
     $add_subscription_quantity=$data_result['products_quantity'];
     $step=3;
  }
?>
      <tr>
        <td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo ADDING_CATEGORIES_TITLE; ?> #<?php echo $oID; ?></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_EDIT_ORDERS, 'oID=' . $oID . '&step=2') . '">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>'; ?></td>
          </tr>
        </table></td>
      </tr>

<?
	// ############################################################################
	//   Get List of All Products
	// ############################################################################

		$result = tep_db_query("SELECT subscription_name, su.subscription_id, subscription_categories_name, suTsuc.subscription_categories_id FROM " . TABLE_SUBSCRIPTIONS . " su LEFT JOIN " . TABLE_SUBSCRIPTIONS_DESCRIPTION . " sud ON sud.subscription_id=su.subscription_id LEFT JOIN " . TABLE_SUBSCRIPTION_TO_SUBSCRIPTION_CATEGORIES . " suTsuc ON suTsuc.subscription_id=su.subscription_id LEFT JOIN " . TABLE_SUBSCRIPTION_CATEGORIES_DESCRIPTION . " scd ON scd.subscription_categories_id=suTsuc.subscription_categories_id ORDER BY subscription_categories_name");
		while($row = tep_db_fetch_array($result))
		{
			extract($row,EXTR_PREFIX_ALL,"db");
			$SubscriptionList[$db_subscription_categories_id][$db_subscription_id] = $db_subscription_name;
			$Subscription_CategoryList[$db_subscription_id] = $db_subscription_categories_name;
			$LastCategory = $db_subscription_categories_name;
		}
		// ksort($ProductList);
		$LastOptionTag = "";
		$SubscriptionSelectOptions = "<option value='0'>Don't Add New Subscription" . $LastOptionTag . "\n";
		$SubscriptionSelectOptions .= "<option value='0'>&nbsp;" . $LastOptionTag . "\n";
		foreach($SubscriptionList as $subscription_Category =>$subscription)
		{
			$SubscriptionSelectOptions .= "<option value='0'>$subscription_Category" . $LastOptionTag . "\n";
			$SubscriptionSelectOptions .= "<option value='0'>---------------------------" . $LastOptionTag . "\n";
			asort($subscription);
			foreach($subscription as $subscription_ID => $subscription_Name)
			{
				$SubscriptionSelectOptions .= "<option value='$subscription_id'> &nbsp; $subscription_Name" . $LastOptionTag . "\n";
			}
			
			if($subscription_Category != $LastCategory)
			{
				$SubscriptionSelectOptions .= "<option value='0'>&nbsp;" . $LastOptionTag . "\n";
				$SubscriptionSelectOptions .= "<option value='0'>&nbsp;" . $LastOptionTag . "\n";
			}
		}
	// ############################################################################
	//   Add Products Steps
	// ############################################################################
		print "<tr><td><table border='0'>\n";
		// Set Defaults
			if(!IsSet($add_subscription_categories_id))
			$add_subscription_categories_id = 0;

			if(!IsSet($add_subscription_subscriptions_id))
			$add_subscription_subscriptions_id = 0;
		// Step 1: Choose Product
			print "<tr class=\"dataTableRow\"><form action='$PHP_SELF?oID=$oID&action=$action&mode=$mode&id=$id' method='POST'>\n";
			print "<td class='dataTableContent' align='right'><b>Select Subscription:</b></td><td class='dataTableContent' valign='top'>";
			echo tep_draw_pull_down_menu('add_subscription_subscriptions_id',tep_get_subscription_display_tree(),$add_subscription_subscriptions_id);
			//print_r(tep_get_subscription_display_tree());
			print "<td class='dataTableContent' align='center'><input type='submit' value='Select'>";
			print "<input type='hidden' name='add_subscription_categories_id' value='$add_subscription_categories_id'>";
			print "<input type='hidden' name='add_step' value='3'>";
			print "</td>\n";
			print "</form></tr>\n";
		// Step 3: Choose Options
		if(($add_step > 2) && ($add_subscription_subscriptions_id > 0))
		{
				$add_step = 4;
			}
				print "</td>";
				print "</form></tr>\n";
			print "<tr><td colspan='3'>&nbsp;</td></tr>\n";
		// Step 4: Confirm
		if($add_step > 3)
		{
			print "<tr class=\"dataTableRow\"><form action='$PHP_SELF?oID=$oID&action=insert_subscription&mode=$mode&id=$id' method='POST'>\n";
			// get the costs and type for selected option from list
			$cos_and_type=tep_db_query("select * from " . TABLE_SUBSCRIPTIONS . " where subscription_id='" .$add_subscription_subscriptions_id . "'");
			$cos_and_type_value=tep_db_fetch_array($cos_and_type);
			if(isset($cos_and_type_value['subscription_type']) && tep_not_null($cos_and_type_value['subscription_type']) && isset($cos_and_type_value['subscription_costs']) && $cos_and_type_value['subscription_costs']!=''){
				if($cos_and_type_value['subscription_type']=='T'){
					$type="Tangible";
				}else {
					$type="Digital";
				}
				if($cos_and_type_value['subscription_costs']){
					$costs=$cos_and_type_value['subscription_costs']; 
				}
			}
			print "<td class='dataTableContent' align='left'><b>Subscription Costs:<br><br>Subscription Type:</td>"; 
			print "<td class='dataTableContent' align='left'>" . $currencies->format($costs) . "<br><Br>$type</td>";
			print  "<td class='dataTableContent' align='left' vailgn='middle'><input type='submit' value='Add Now!'></td>";
			//print "";
			 if(IsSet($add_subscription_options))
			{
				foreach($add_subscription_options as $option_id => $option_value_id)
				{
					print "<input type='hidden' name='add_subscription_options[$option_id]' value='$option_value_id'>";
				}
			}
			print "<input type='hidden' name='add_subscription_categories_id' value='$add_subscription_categories_id'>";
			print "<input type='hidden' name='add_subscription_subscriptions_id' value='$add_subscription_subscriptions_id'>";
			print "<input type='hidden' name='add_step' value='5'>";
			//print "</td>\n";
			print "</form></tr>\n";
		}
		
		print "</table></td></tr>\n";
} ?>
    </table></td>
<!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?
  ////////////////////////////////////////////////////////////////////////////////////////////////
  //
  // Function    : tep_get_country_id
  //
  // Arguments   : country_name		country name string
  //
  // Return      : country_id
  //
  // Description : Function to retrieve the country_id based on the country's name
  //
  ////////////////////////////////////////////////////////////////////////////////////////////////
  function tep_get_country_id($country_name) {

    $country_id_query = tep_db_query("select * from " . TABLE_COUNTRIES . " where countries_name = '" . $country_name . "'");

    if (!tep_db_num_rows($country_id_query)) {
      return 0;
    }
    else {
      $country_id_row = tep_db_fetch_array($country_id_query);
      return $country_id_row['countries_id'];
    }
  }

  ////////////////////////////////////////////////////////////////////////////////////////////////
  //
  // Function    : tep_get_country_iso_code_2
  //
  // Arguments   : country_id		country id number
  //
  // Return      : country_iso_code_2
  //
  // Description : Function to retrieve the country_iso_code_2 based on the country's id
  //
  ////////////////////////////////////////////////////////////////////////////////////////////////
  function tep_get_country_iso_code_2($country_id) {

    $country_iso_query = tep_db_query("select * from " . TABLE_COUNTRIES . " where countries_id = '" . $country_id . "'");

    if (!tep_db_num_rows($country_iso_query)) {
      return 0;
    }
    else {
      $country_iso_row = tep_db_fetch_array($country_iso_query);
      return $country_iso_row['countries_iso_code_2'];
    }
  }

  ////////////////////////////////////////////////////////////////////////////////////////////////
  //
  // Function    : tep_get_zone_id
  //
  // Arguments   : country_id		country id string
  //               zone_name		state/province name
  //
  // Return      : zone_id
  //
  // Description : Function to retrieve the zone_id based on the zone's name
  //
  ////////////////////////////////////////////////////////////////////////////////////////////////
  function tep_get_zone_id($country_id, $zone_name) {

    $zone_id_query = tep_db_query("select * from " . TABLE_ZONES . " where zone_country_id = '" . $country_id . "' and zone_name = '" . $zone_name . "'");

    if (!tep_db_num_rows($zone_id_query)) {
      return 0;
    }
    else {
      $zone_id_row = tep_db_fetch_array($zone_id_query);
      return $zone_id_row['zone_id'];
    }
  }
  
  ////////////////////////////////////////////////////////////////////////////////////////////////
  //
  // Function    : tep_field_exists
  //
  // Arguments   : table	table name
  //               field	field name
  //
  // Return      : true/false
  //
  // Description : Function to check the existence of a database field
  //
  ////////////////////////////////////////////////////////////////////////////////////////////////
  function tep_field_exists($table,$field) {

    $describe_query = tep_db_query("describe $table");
    while($d_row = tep_db_fetch_array($describe_query))
    {
      if ($d_row["Field"] == "$field")
      return true;
    }

    return false;
  }

  ////////////////////////////////////////////////////////////////////////////////////////////////
  //
  // Function    : tep_html_quotes
  //
  // Arguments   : string	any string
  //
  // Return      : string with single quotes converted to html equivalent
  //
  // Description : Function to change quotes to HTML equivalents for form inputs.
  //
  ////////////////////////////////////////////////////////////////////////////////////////////////
  function tep_html_quotes($string) {
    return str_replace("'", "&#39;", $string);
  }

  ////////////////////////////////////////////////////////////////////////////////////////////////
  //
  // Function    : tep_html_unquote
  //
  // Arguments   : string	any string
  //
  // Return      : string with html equivalent converted back to single quotes
  //
  // Description : Function to change HTML equivalents back to quotes
  //
  ////////////////////////////////////////////////////////////////////////////////////////////////
  function tep_html_unquote($string) {
    return str_replace("&#39;", "'", $string);
  }

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