<?php
/*
  $Id: checkout_shipping.php,v 1.2 2003/09/24 15:34:25 wilt Exp $
  $Id: checkout_shipping.php,v 1.2 2003/09/24 15:34:25 wilt Exp $
  $Id: checkout_shipping.php,v 1.2 2003/09/24 15:34:25 wilt Exp $

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

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
  Shoppe Enhancement Controller - Copyright (c) 2003 WebMakers.com
  Linda McGrath - osCommerce@WebMakers.com
*/
// Set flag that this is a parent file
	defined('_FEXEC') or die();// BOF: WebMakers.com Added: Downloads Controller - Free Shipping
// Reset $shipping if free shipping is on and weight is not 0
  require('includes/classes/http_client.php');
  $disable_shipping=isset($HTTP_GET_VARS['disable_shipping'])?tep_db_prepare_input($HTTP_GET_VARS['disable_shipping']):'';

if (tep_get_configuration_key_value('MODULE_SHIPPING_FREESHIPPER_STATUS') and $cart->show_weight()!=0) {
  tep_session_unregister('shipping');
}

// EOF: WebMakers.com Added: Downloads Controller - Free Shipping
// if the customer is not logged on, redirect them to the login page
// if there is nothing in the customers cart, redirect them to the shopping cart page
// BOF: WebMakers.com Added: Attributes Sorter and Copier and Quantity Controller
// Validate Cart for checkout
  $valid_to_checkout= true;
  $cart->get_products(true);

// EOF: WebMakers.com Added: Attributes Sorter and Copier and Quantity Controller

// if no shipping destination address was selected, use the customers own address as default
  if (!tep_session_is_registered('sendto')) {
    tep_session_register('sendto');
    $sendto = $customer_default_address_id;
  } else {
// verify the selected shipping address
    $check_address_query = tep_db_query("select count(*) as total from " . TABLE_ADDRESS_BOOK . " where customers_id = '" . (int)$customer_id . "' and address_book_id = '" . (int)$sendto . "'");
    $check_address = tep_db_fetch_array($check_address_query);

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

  if (!tep_session_is_registered('cartID')) tep_session_register('cartID');
  $cartID = $cart->cartID;
  
	if (NO_CHECKOUT_ZERO_PRICE=="1" && $cart->is_free_checkout()){
	//for free event checkout
		 echo "redirect_page||" . tep_href_link('checkout_process_new.php', '', 'SSL');
		 exit;
	}
  require(DIR_WS_CLASSES . 'order.php');
  $order = new order;

  require(DIR_WS_CLASSES . 'shipping.php');
  $shipping_modules = new shipping;
// register a random ID in the session to check throughout the checkout procedure
// against alterations in the shopping cart contents

// if the order contains only virtual products, forward the customer to the billing page as
// a shipping address is not needed
// ICW CREDIT CLASS GV AMENDE LINE BELOW
//  if ($order->content_type == 'virtual') {
//exit;

  if (($order->content_type == 'virtual') || ($order->content_type == 'virtual_weight') || $disable_shipping==1 || tep_count_shipping_modules()<=0) {
    if (!tep_session_is_registered('shipping')) {
		tep_session_register('shipping');
	}
    $shipping = $_SESSION["shipping"]=false;
    $sendto = $_SESSION["sendto"]=false;
	 echo "step4||";
	 tep_session_close();
	 exit;
  }

  $total_weight = $cart->show_weight();
  $total_count = $cart->count_contents();

// load all enabled shipping modules

  if ( defined('MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING') && (MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING == 'true') ) {
    $pass = false;

    switch (MODULE_ORDER_TOTAL_SHIPPING_DESTINATION) {
      case 'national':
        if ($order->delivery['country_id'] == STORE_COUNTRY) {
          $pass = true;
        }
        break;
      case 'international':
        if ($order->delivery['country_id'] != STORE_COUNTRY) {
          $pass = true;
        }
        break;
      case 'both':
        $pass = true;
        break;
    }

    $free_shipping = false;
    if ( ($pass == true) && ($order->info['total'] >= MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING_OVER) ) {
      $free_shipping = true;

      include(DIR_WS_LANGUAGES . $language . '/modules/order_total/ot_shipping.php');
    }
  } else {
    $free_shipping = false;
  }

// process the selected shipping method
  if ( isset($HTTP_POST_VARS['action']) && ($HTTP_POST_VARS['action'] == 'process') ) {
    if (!tep_session_is_registered('comments')) tep_session_register('comments');
    if (tep_not_null($HTTP_POST_VARS['comments'])) {
      $comments = tep_db_prepare_input($HTTP_POST_VARS['comments']);
    }else {
    	tep_session_unregister('comments');
    }

    if (!tep_session_is_registered('shipping')) tep_session_register('shipping');

    if ( (tep_count_shipping_modules() > 0) || ($free_shipping == true) ) {
      if ( (isset($HTTP_POST_VARS['shipping'])) && (strpos(tep_db_prepare_input($HTTP_POST_VARS['shipping']), '_')) ) {
        $shipping = tep_db_prepare_input($HTTP_POST_VARS['shipping']);

        list($module, $method) = explode('_', $shipping);
        if ( is_object($$module) || ($shipping == 'free_free') ) {
          if ($shipping == 'free_free') {
            $quote[0]['methods'][0]['title'] = FREE_SHIPPING_TITLE;
            $quote[0]['methods'][0]['cost'] = '0';
          } else {
            $quote = $shipping_modules->quote($method, $module);
          }
          if (isset($quote['error'])) {
            tep_session_unregister('shipping');
          } else {
            if ( (isset($quote[0]['methods'][0]['title'])) && (isset($quote[0]['methods'][0]['cost'])) ) {
              $shipping = array('id' => $shipping,
                                'title' => (($free_shipping == true) ?  $quote[0]['methods'][0]['title'] : $quote[0]['module'] . ' (' . $quote[0]['methods'][0]['title'] . ')'),
                                'cost' => $quote[0]['methods'][0]['cost']);

      			 echo "step5||";
				 tep_session_close();
				 exit;
            }
          }
        } else {
          tep_session_unregister('shipping');
        }
      }
    } else {
      $shipping = false;
       if($free_shipping == true)
	       echo "step5||";
		else
	       echo "step4||";
    }    
  }

// get all available shipping quotes
  $quotes = $shipping_modules->quote();
// if no shipping method has been selected, automatically select the cheapest method.
// if the modules status was changed when none were available, to save on implementing
// a javascript force-selection method, also automatically select the cheapest shipping
// method if more than one module is now enabled
  if ( !tep_session_is_registered('shipping') || ( tep_session_is_registered('shipping') && ($shipping == false) && (tep_count_shipping_modules() > 1) ) ) $shipping = $shipping_modules->cheapest();

?>
<table border="0" width="100%" cellspacing="2" cellpadding="1">
<tr>
		<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
	</tr>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="5">
          <tr>
			<td class="infoBoxHeading" valign="middle"><font color="#003366"><b><?php echo TEXT_NAME;?></b></font>
           <?php echo tep_customers_name($customer_id); ?>&nbsp;<span  id="img_process4"></span></td>
          </tr>
        </table></td>
      </tr>
	  <tr>
		<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
	</tr>

	<tr>
	<td  valign="top" width="100%">
<?php echo tep_draw_form('checkout_shipping', FILENAME_CHECKOUT_SHIPPING) . tep_draw_hidden_field('action', 'process'); ?><table border="0" width="100%" cellspacing="0" cellpadding="<?php echo CELLPADDING_SUB; ?>">
      <tr>
        <td><table border="0" width="100%" cellspacing="1" cellpadding="2" class="attributes-odd" >
<?php 
  if (tep_count_shipping_modules() > 0) {
?><?php tep_content_title_top( TABLE_HEADING_SHIPPING_METHOD); ?>
          <tr class="main">
            <td><table border="0" width="100%" cellspacing="0" cellpadding="2">
<?php 
    if (sizeof($quotes) > 1 && sizeof($quotes[0]) > 1) { 
?>
              <tr>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                <td class="main" width="50%" valign="top"><?php echo TEXT_CHOOSE_SHIPPING_METHOD; ?></td>
                <td class="main" width="50%" valign="top" align="right"><?php echo '<b>' . TITLE_PLEASE_SELECT . '</b><br>' . tep_image(DIR_WS_IMAGES . 'arrow_east_south.gif'); ?></td>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
              </tr>
<?php
    } elseif ($free_shipping == false) {
?>
              <tr>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                <td class="main" width="100%" colspan="2"><?php echo TEXT_ENTER_SHIPPING_INFORMATION; ?></td>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
              </tr>
<?php
    }

    if ($free_shipping == true) {
?>
              <tr>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                <td colspan="2" width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="2">
                  <tr>
                    <td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                    <td class="main" colspan="3"><b><?php echo FREE_SHIPPING_TITLE; ?></b>&nbsp;<?php echo $quotes[$i]['icon']; ?></td>
                    <td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                  </tr>
                  <tr id="defaultSelected" class="moduleRowSelected" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="selectRowEffect(this, 0)">
                    <td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                    <td class="main" width="100%"><?php echo sprintf(FREE_SHIPPING_DESCRIPTION, $currencies->format(MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING_OVER)) . tep_draw_hidden_field('shipping', 'free_free'); ?></td>
                    <td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                  </tr>
                </table></td>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td> 
              </tr>
<?php
    } else {
      $radio_buttons = 0; 
      for ($i=0, $n=sizeof($quotes); $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">
                  <tr>
                    <td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                    <td class="main" colspan="3"><b><?php echo $quotes[$i]['module']; ?></b>&nbsp;<?php if (isset($quotes[$i]['icon']) && tep_not_null($quotes[$i]['icon'])) { echo $quotes[$i]['icon']; } ?></td>
                    <td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                  </tr>
<?php  
        if (isset($quotes[$i]['error'])) { 
?>
                  <tr>
                    <td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                    <td class="main" colspan="3"><?php echo $quotes[$i]['error']; ?></td>
                    <td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                  </tr>
<?php
        } else {
          for ($j=0, $n2=sizeof($quotes[$i]['methods']); $j<$n2; $j++) { 
// set the radio button to be checked if it is the method chosen
            $checked = (($quotes[$i]['id'] . '_' . $quotes[$i]['methods'][$j]['id'] == $shipping['id']) ? true : false);

            if ( ($checked == true) || ($n == 1 && $n2 == 1) ) {
              echo '                  <tr id="defaultSelected"  onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" >' . "\n";
            } else {
              echo '                  <tr class="moduleRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)"  >' . "\n";
            }
?>
                    <td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                    <td class="main" width="75%"><?php echo $quotes[$i]['methods'][$j]['title']; ?></td>
<?php
            if ( ($n > 1) || ($n2 > 1) ) {
?>
                    <td class="main"><?php echo $currencies->format(tep_add_tax($quotes[$i]['methods'][$j]['cost'], (isset($quotes[$i]['tax']) ? $quotes[$i]['tax'] : 0))); ?></td>
                    <td class="main" align="right"><?php echo tep_draw_radio_field('shipping', $quotes[$i]['id'] . '_' . $quotes[$i]['methods'][$j]['id'], $checked); ?></td>
<?php
            } else {
?>
                    <td class="main" align="right" colspan="2"><?php echo $currencies->format(tep_add_tax($quotes[$i]['methods'][$j]['cost'], $quotes[$i]['tax'])) . tep_draw_hidden_field('shipping', $quotes[$i]['id'] . '_' . $quotes[$i]['methods'][$j]['id']); ?></td>
<?php
            }
?>
                    <td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                  </tr>
<?php
            $radio_buttons++;
          }
        }
?>
                </table></td>
                <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td> 
              </tr>
<?php
      }
    }
?>
            </table></td>
          </tr><?php tep_content_title_bottom();?>
        </table></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
      </tr>
<?php
  }
?>
	  <Tr><td><?php echo tep_draw_separator('pixel_trans.gif', '10', '5'); ?></td></tr>
 	  <tr>
         <td class="main">
		        <table border="0" width="100%" cellspacing="1" cellpadding="2" class="attributes-odd" >
				<tr><td class="main"> 
					<b><?php echo TABLE_HEADING_COMMENTS .tep_draw_checkbox_field('comments', '0',($comments)?true:false,'','id="comments" onclick=javascript:showComment(this)'); ?></a></b></td>
			      </tr>
		      <tr id="show_comments" style="display:none;">
        		<td><table border="0" width="100%" cellspacing="1" cellpadding="2" >
	          <tr class="main">
    	        <td>
        	      <table border="0" width="100%" cellspacing="0" cellpadding="2">
               <tr>
                 <td><?php echo '&nbsp;&nbsp;&nbsp;'.tep_draw_textarea_field('comments', 'soft', '90', '5','','id="comments"'); ?></td>
               </tr>
              </table>
            </td>
          </tr>
        </table></td>
      </tr>
        </table></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
      </tr>
      <tr>
	          	<td>
			<table border="0" width="100%" cellspacing="1" cellpadding="2" >
          		<tr class="infoBoxContents">
            		<td>
					<table border="0" width="100%" cellspacing="0" cellpadding="2">
					  	<tr>
							<td width="50%"><?php echo '<a href="javascript:show_step_number(3);">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>'. tep_draw_separator('pixel_trans.gif', '10', '1');?></td>
							<td width="50%" class="main" align="right" nowrap><?php echo '<a href="javascript:do_page_fetch(\'submit_shipping_details\');">' . tep_image_button('button_continue.gif', IMAGE_CONTINUE,'name="button_confirm4" id="button_confirm4"') . '</a>'; ?></td>
					  	</tr>
            		</table> 
					</td>
         		</tr>
			</table>
			</td>
      </tr>
        </table>
		</td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
      </tr>
	<!--	<tr>
			<td>
			</td>
		</tr> -->
</table>
	<table border="0" width="100%" cellspacing="0" cellpadding="0">
	 <tr>
		<td colspan="4"><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
	</tr>
	<tr>
	<td width="40%">
	<table border="0" width="100%" cellspacing="0" cellpadding="0">
		<tr>
		<td width="10%" align="right"><?php echo tep_draw_separator('pixel_black.gif', '1', '5'); ?></td>
		<td width="40%"><?php echo tep_draw_separator('pixel_black.gif', '100%', '1'); ?></td>
		<td><?php echo tep_image(DIR_WS_IMAGES . 'checkout_bullet.gif'); ?></td>
		<td width="50%"><?php echo tep_draw_separator('pixel_black.gif', '100%', '1'); ?></td>
		</tr>
	</table>
	</td>
	<td width="25%"><?php echo tep_draw_separator('pixel_black.gif', '100%', '1'); ?></td>
	<td width="25%"><?php echo tep_draw_separator('pixel_black.gif', '100%', '1'); ?></td>
	<td width="25%">
	<table border="0" width="100%" cellspacing="0" cellpadding="0">
	    <tr>
			<td width="50%"><?php echo tep_draw_separator('pixel_black.gif', '100%', '1'); ?></td>
			<td width="50%"><?php echo tep_draw_separator('pixel_black.gif', '1', '5'); ?></td>
		</tr>
	</table>
	</td>
	</tr>
	<tr>
	<td align="center" width="25%" class="checkoutBarCurrent"><?php echo CHECKOUT_BAR_DELIVERY; ?></td>
	<td align="center" width="25%" class="checkoutBarTo"><?php echo CHECKOUT_BAR_PAYMENT; ?></td>
	<td align="center" width="25%" class="checkoutBarTo"><?php echo CHECKOUT_BAR_CONFIRMATION; ?></td>
	<td align="center" width="25%" class="checkoutBarTo"><?php echo CHECKOUT_BAR_FINISHED; ?></td>
	</tr>
	</table>
	</form></td></tr>
</table>