<?php
/*
  $Id: checkout_shipping.php,v 1.16 2003/06/09 23:03:53 hpdl Exp $

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

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/

  require('includes/application_top.php');
  require('includes/classes/http_client.php');

// if the customer is not logged on, redirect them to the login page
  if (!tep_session_is_registered('customer_id')) {
    $navigation->set_snapshot();
    tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
  }

// if there is nothing in the customers cart, redirect them to the shopping cart page
  if ($cart->count_contents() < 1) {
    tep_redirect(tep_href_link(FILENAME_SHOPPING_CART));
  }
else
{
	$product_id_list=$cart->get_product_id_list();
	$check_products_status = tep_db_query("select products_status from products where (products_status=0 or products_quantity<1) and products_id in (".$product_id_list.")");
	if (tep_db_num_rows($check_products_status)>0)
		{
		    tep_redirect(tep_href_link('shopping_cart.php'));
		}
}

// 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');
    }
  }

  require(DIR_WS_CLASSES . 'order.php');
  $order = new order;

// register a random ID in the session to check throughout the checkout procedure
// against alterations in the shopping cart contents
  if (!tep_session_is_registered('cartID')) tep_session_register('cartID');
  $cartID = $cart->cartID;

// if the order contains only virtual products, forward the customer to the billing page as
// a shipping address is not needed
  if ($order->content_type == 'virtual') {
    if (!tep_session_is_registered('shipping')) tep_session_register('shipping');
    $shipping = false;
    $sendto = false;
    tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL'));
  }

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

// load all enabled shipping modules
  require(DIR_WS_CLASSES . 'shipping.php');
  $shipping_modules = new shipping;

  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') ) {
	$o_type=0;
	if ($HTTP_POST_VARS['shipping']==2)//select the DHL 
		{
              $o_type=1;
			  $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'])) ) {
					if (!tep_session_is_registered('o_type')) tep_session_register('o_type');  
					if (!tep_session_is_registered('shipping')) tep_session_register('shipping');  

					$addfee=17;
					if ($total_count<6)
					{
						$addfee=17;
					}
					else if ($total_count<11)
					{
						$addfee=15;
					}
					else if($total_count<25)
					{
						$addfee=12;
					}

					$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']+$addfee);
				    tep_redirect(tep_href_link('checkout_payment.php', '', 'SSL'));
				}
			  }
		}
	else
	if ($HTTP_POST_VARS['shipping']==1)//select the DHL 
		{
              $o_type=0;
			  $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'])) ) {
					if (!tep_session_is_registered('o_type')) tep_session_register('o_type');  
					if (!tep_session_is_registered('shipping')) tep_session_register('shipping');  

					$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']);
				    tep_redirect(tep_href_link('checkout_payment.php', '', 'SSL'));
				}
			  }
		}


    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']);
    }

    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($HTTP_POST_VARS['shipping'], '_')) ) {
        $shipping = $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'])) ) {
			  $o_type=1;
			  if (!tep_session_is_registered('o_type')) tep_session_register('o_type');  
              $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']);

              tep_redirect(tep_href_link('checkout_payment.php', '', 'SSL'));
            }
          }
        } else {
          tep_session_unregister('shipping');
        }
      }
    } else {
      $shipping = false;
                
      tep_redirect(tep_href_link('checkout_payment.php', '', 'SSL'));
    }    
  }

// 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();

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

  $breadcrumb->add(NAVBAR_TITLE_1, tep_href_link('checkout_shipping.php', '', 'SSL'));
  $breadcrumb->add(NAVBAR_TITLE_2, tep_href_link('checkout_shipping.php', '', 'SSL'));
?>
<!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>
<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<link rel="stylesheet" type="text/css" href="style.css">
<script language="javascript"><!--
var selected;

function selectRowEffect(object, buttonSelect) {
  if (!selected) {
    if (document.getElementById) {
      selected = document.getElementById('defaultSelected');
    } else {
      selected = document.all['defaultSelected'];
    }
  }

  if (selected) selected.className = 'moduleRow';
  object.className = 'moduleRowSelected';
  selected = object;

// one button is not an array
  if (document.checkout_address.shipping[0]) {
    document.checkout_address.shipping[buttonSelect].checked=true;
  } else {
    document.checkout_address.shipping.checked=true;
  }
}

function rowOverEffect(object) {
  if (object.className == 'moduleRow') object.className = 'moduleRowOver';
}

function rowOutEffect(object) {
  if (object.className == 'moduleRowOver') object.className = 'moduleRow';
}
function check_form() {
  var error = 0;
  var error_message = "";
  var payment_value = null;

  if (document.checkout_address.shipping.length) {
    for (var i=0; i<document.checkout_address.shipping.length; i++) {
      if (document.checkout_address.shipping[i].checked) {
        payment_value = document.checkout_address.shipping[i].value;
      }
    }
  } else if (document.checkout_address.shipping.checked) {
    payment_value = document.checkout_address.shipping.value;
  } else if (document.checkout_address.shipping.value) {
    payment_value = document.checkout_address.shipping.value;
  }


  if (payment_value == null) {
    error_message = "<?php echo MODULE_SHIPPING_MUST_SELECT;?>";
    error = 1;
  }

  if (error == 1) {
    alert(error_message);
    return false;
  } else {
    return true;
  }
}
//--></script>
</head>
<body>
<div id="pg">

<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<div id="bdmain" class="layout">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
<!-- body_text //-->
	<div id="bdcenter"><?php echo tep_draw_form('checkout_address', tep_href_link('checkout_shipping.php', '', 'SSL'),'post','onsubmit="return check_form();"') . tep_draw_hidden_field('action', 'process'); ?>
	 <p class="pageHeading"><?php echo HEADING_TITLE; ?></p>
	  <p style="font-weight:bold; text-align:left;"><?php echo TABLE_HEADING_SHIPPING_ADDRESS; ?></p>
	<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="50%" valign="top"><?php echo TEXT_CHOOSE_SHIPPING_DESTINATION . '<br><br><a href="' . tep_href_link('checkout_shipping_address.php', '', 'SSL') . '">' . tep_image_button('button_change_address.gif', IMAGE_BUTTON_CHANGE_ADDRESS) . '</a>'; ?></td>
                <td align="right" width="50%" valign="top"><table border="0" cellspacing="0" cellpadding="2">
                  <tr>
                    <td class="main" align="center" valign="top"><?php echo '<b>' . TITLE_SHIPPING_ADDRESS . '</b><br>' . tep_image(DIR_WS_IMAGES . 'arrow_south_east.gif'); ?></td>
                    <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td> 
                    <td class="main" valign="top"><?php echo tep_address_label($customer_id, $sendto, true, ' ', '<br>'); ?></td>
                    <td><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td> 
                  </tr>
                </table></td>
              </tr>
      </table>
			
			
			
<?php
//echo(tep_count_shipping_modules().'<br>');
  if (tep_count_shipping_modules() > 0) {
?><p style="font-weight:bold; text-align:left;"><?php echo TABLE_HEADING_SHIPPING_METHOD; ?></p>

   
<?php
    if ($cart->count_contents()<25) {
?><table border="0" width="625" cellspacing="0" cellpadding="2">
              <tr>
                <td class="main" width="75%" valign="top" height="15"><?php echo TEXT_ENTER_SHIPPING_INFORMATION_1."<br>".TEXT_ENTER_SHIPPING_INFORMATION_2; ?></td>
                <td class="main" width="25%" valign="top" align="center"><?php echo '<b>' . TITLE_PLEASE_SELECT . '</b><br>' . tep_image(DIR_WS_IMAGES . 'arrow_east_south.gif'); ?></td>
              </tr>
</table>
<?php
    } elseif ($free_shipping == false) {
?><table border="0" width="625" cellspacing="0" cellpadding="2">
              <tr>
                <td class="main" width="75%" valign="top" align="left"><?php echo TEXT_ENTER_SHIPPING_INFORMATION_3."";?></td>
                <td class="main" width="25%" valign="top" align="center"><?php echo '<b>' . TITLE_FEE . '</b>'?></td>
              </tr>
</table>
<?php
    }

    if ($free_shipping == true) {
?>
<table border="0" width="625" cellspacing="0" cellpadding="2">
                  <tr>
                    <td class="main"><b><?php echo FREE_SHIPPING_TITLE; ?></b>&nbsp;<?php echo $quotes[$i]['icon']; ?></td>
      </tr></table>
				  <table border="0" width="625" cellspacing="0" cellpadding="2">
                  <tr id="defaultSelected" class="moduleRowSelected" onMouseOver="this.style.backgroundColor='#F8F5EE'" onMouseOut="this.style.backgroundColor='#fff'" onClick="selectRowEffect(this, 0)">
                    <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>
                    </tr>
      </table>
<?php
    } else {
      $radio_buttons = 0;
      for ($i=0, $n=sizeof($quotes); $i<$n; $i++) {
?>
<?php
        if (isset($quotes[$i]['error'])) {
?><p class="main" style="text-align:left;"><?php echo $quotes[$i]['error']; ?></p>
<?php
        } else {
            if ( $cart->count_contents()<25 ) 
			{
?>
		      		<table width="625" border="0" align="center" cellpadding="2" cellspacing="0">
					<tr id="defaultSelected"  class="moduleRow" onMouseOver="this.style.backgroundColor='#F8F5EE'" onMouseOut="this.style.backgroundColor='#fff'" onClick="selectRowEffect(this,0)" >
                    <td class="main" width="450"><?php echo MODULE_SHIPPING_ITEM_TEXT_WAY_1.MODULE_SHIPPING_ITEM_TEXT_WAY_11; ?></td>
                    <td width="100" align="right"  valign="top"><?php echo $currencies->format(tep_add_tax($quotes[$i]['methods'][0]['cost'], (isset($quotes[$i]['tax']) ? $quotes[$i]['tax'] : 0))); ?></td>
                    <td class="main" align="left"  valign="top"><?php echo tep_draw_radio_field('shipping',1, 0); ?></td>
                    </tr>
	  </table>
					
					<table width="625" border="0" align="center" cellpadding="2" cellspacing="0">
					<tr class="moduleRow"  onmouseover="this.style.backgroundColor='#F8F5EE'" onMouseOut="this.style.backgroundColor='#fff'" onClick="selectRowEffect(this,1)">
            <?php
if ( $cart->count_contents()<6 )
{
?>
                    <td class="main" width="450" valign="top"><?php echo MODULE_SHIPPING_ITEM_TEXT_WAY_2;?>
					<select name="dhlfeelist">
                      <option value="1" selected>1&nbsp;&nbsp;&nbsp;—&nbsp;&nbsp;&nbsp;5&nbsp;pcs&nbsp;&nbsp;&nbsp;&nbsp;+ <?=$currencies->display_price(17,0)?></option>
                      <option value="2">6&nbsp;&nbsp;&nbsp;—&nbsp;&nbsp;10&nbsp;pcs&nbsp;&nbsp;&nbsp;+ <?=$currencies->display_price(15,0)?></option>
                      <option value="3">11&nbsp;—&nbsp;&nbsp;24&nbsp;pcs&nbsp;&nbsp;&nbsp;+ <?=$currencies->display_price(12,0)?></option>
                      <option value="4">25&nbsp;pcs&nbsp;—&nbsp;&nbsp;--&nbsp;&nbsp;&nbsp;&nbsp;+ <?=$currencies->display_price(0,0)?></option>
                    </select>					</td>
                    <td width="100" align="right"  valign="top"><?php echo $currencies->format(tep_add_tax($quotes[$i]['methods'][0]['cost']+17, $quotes[$i]['tax'])); ?></td>
<?php
}else if ( $cart->count_contents()<11 ){
?>
                    <td class="main" width="450" valign="top"><?php echo MODULE_SHIPPING_ITEM_TEXT_WAY_2;?>
					<select name="dhlfeelist">
                      <option value="1">1&nbsp;&nbsp;&nbsp;—&nbsp;&nbsp;&nbsp;5&nbsp;pcs&nbsp;&nbsp;&nbsp;&nbsp;+ <?=$currencies->display_price(17,0)?></option>
                      <option value="2" selected>6&nbsp;&nbsp;&nbsp;—&nbsp;&nbsp;10&nbsp;pcs&nbsp;&nbsp;&nbsp;+ <?=$currencies->display_price(15,0)?></option>
                      <option value="3">11&nbsp;—&nbsp;&nbsp;24&nbsp;pcs&nbsp;&nbsp;&nbsp;+ <?=$currencies->display_price(12,0)?></option>
                      <option value="4">25&nbsp;pcs&nbsp;—&nbsp;&nbsp;--&nbsp;&nbsp;&nbsp;&nbsp;+ <?=$currencies->display_price(0,0)?></option>
                    </select>					</td>
                    <td width="100" align="right"  valign="top"><?php echo $currencies->format(tep_add_tax($quotes[$i]['methods'][0]['cost']+15, $quotes[$i]['tax'])); ?></td>
<?
}else if ( $cart->count_contents()<25 ){
?>
                    <td width="450" align="left" valign="top" class="main"><?php echo MODULE_SHIPPING_ITEM_TEXT_WAY_2;?>
					<select name="dhlfeelist">
                      <option value="1">1&nbsp;&nbsp;&nbsp;—&nbsp;&nbsp;&nbsp;5&nbsp;pcs&nbsp;&nbsp;&nbsp;&nbsp;+ <?=$currencies->display_price(17,0)?></option>
                      <option value="2">6&nbsp;&nbsp;&nbsp;—&nbsp;&nbsp;10&nbsp;pcs&nbsp;&nbsp;&nbsp;+ <?=$currencies->display_price(15,0)?></option>
                      <option value="3" selected>11&nbsp;—&nbsp;&nbsp;24&nbsp;pcs&nbsp;&nbsp;&nbsp;+ <?=$currencies->display_price(12,0)?></option>
                      <option value="4">25&nbsp;pcs&nbsp;—&nbsp;&nbsp;--&nbsp;&nbsp;&nbsp;&nbsp;+ <?=$currencies->display_price(0,0)?></option>
                    </select>					</td>
                    <td width="100" align="right"  valign="top"><?php echo $currencies->format(tep_add_tax($quotes[$i]['methods'][0]['cost']+12, $quotes[$i]['tax'])); ?></td>
<?
}
?>
                    <td class="main" align="left"  valign="top"><?php echo tep_draw_radio_field('shipping', 2, 0); ?></td>
                    </tr>
	  </table>
					
					<table border="0" width="625" cellspacing="0" cellpadding="2">
				    <tr>
                    <td colspan="4" class="smallText" width="100%" valign="top" align="left"><?php echo MODULE_SHIPPING_ITEM_TEXT_WAY_4;?></td>
                    </tr>
					</table>
<?
			}
			else
			{
?>					
	<table border="0" width="100%" cellspacing="0" cellpadding="2">
					<tr id="defaultSelected" class="moduleRowSelected">
                    <td width="10" valign="top"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
                    <td class="main" width="90%" valign="top" colspan="2"><?php echo MODULE_SHIPPING_ITEM_TEXT_WAY_3;?></td>
					<td class="main" align="right"><?php echo $currencies->format($quotes[$i]['methods'][0]['cost']);?><input type="hidden" name="shipping" value="item_item"></td>
					<td width="10"  valign="top"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td></tr></table>
<?
			}

        }
?>

<?php
      }
    }
  }
?>

	 
	 
	 
	 
	 
	 <p style="font-weight:bold; text-align:left;"><?php echo TABLE_HEADING_COMMENTS; ?></p>
	 <p style="text-align:left;"><?php echo tep_draw_textarea_field('comments', 'soft', '60', '5'); ?></p>
	 <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"><?php echo '<b>' . TITLE_CONTINUE_CHECKOUT_PROCEDURE . '</b><br>' . TEXT_CONTINUE_CHECKOUT_PROCEDURE; ?></td>
                <td class="main" align="right"><?php echo tep_image_submit('button_continue.gif', IMAGE_BUTTON_CONTINUE); ?></td>
                <td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
              </tr>
      </table>
	 <table width="100%" border="0" cellspacing="0" cellpadding="2">
      <tr>
        <td><span style="font-weight:bold; color:red"><?php echo CHECKOUT_BAR_DELIVERY; ?></span></td>
        <td width="18%" align="left"><span class="checkoutBarCurrent"><?php echo tep_draw_separator('pixel_silver.gif', '100%', '1'); ?></span></td>
        <td width="8"><img src="images/arr_to.gif" width="8" height="8" /></td>
        <td><?php echo CHECKOUT_BAR_PAYMENT; ?></td>
        <td width="18%" align="left"><span class="checkoutBarCurrent"><?php echo tep_draw_separator('pixel_silver.gif', '100%', '1'); ?></span></td>
        <td width="8"><img src="images/arr_to.gif" width="8" height="8" /></td>
        <td><span><?php echo CHECKOUT_BAR_CONFIRMATION; ?></span></td>
        <td width="18%" align="left"><span class="checkoutBarCurrent"><?php echo tep_draw_separator('pixel_silver.gif', '100%', '1'); ?></span></td>
        <td width="8"><img src="images/arr_to.gif" width="8" height="8" /></td>
        <td><span><?php echo CHECKOUT_BAR_FINISHED; ?></span></td>
      </tr>
    </table>
    </form>
	</div>
<!-- body_text_eof //-->

<!-- right_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_right.php'); ?>
<!-- right_navigation_eof //-->

</div>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</div>
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
