<?php
/*
  www.oscGold.com (C) Kamelia-Net Jacek Krysiak
  modul oscGold wer 4.0
*/
?>


<?php if (($cart->count_contents() > 0)) { ?>

<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
  <tr >
    <td>

      <!-- shipping_estimator //-->
      <script language="JavaScript">
        function shipincart_submit(sid){
          if(sid){
            document.estimator.sid.value=sid;
          }
          document.estimator.submit();
          return false;
        }
      </script>

      <?php
      require(DIR_WS_LANGUAGES . $language . '/modules/' . FILENAME_SHIPPING_ESTIMATOR);


        // shipping cost
        require('includes/classes/http_client.php'); // shipping in basket

        //if($cart->get_content_type() !== 'virtual') {
        if (tep_session_is_registered('customer_id')) {
          // user is logged in
          if (isset($HTTP_POST_VARS['address_id'])){
            // user changed address
            $sendto = $HTTP_POST_VARS['address_id'];
          } elseif (tep_session_is_registered('customer_default_address_id')) {
            // user once changed address
            $sendto = $customer_default_address_id;
          } else {
            // first timer
            $sendto = $customer_default_address_id;
          }
          // set session now
          $cart_address_id = $sendto;
          tep_session_register('cart_address_id');
          // set shipping to null ! multipickup changes address to store address...
          $shipping='';
          // include the order class (uses the sendto !)
          require(DIR_WS_CLASSES . 'order.php');
          $order = new order;
        } else {
          // user not logged in !
          if (isset($HTTP_POST_VARS['country_id'])){
            // country is selected
            $country_info = tep_get_countries($HTTP_POST_VARS['country_id'],true);
            $cache_state_prov_values = tep_db_fetch_array(tep_db_query("select zone_code from " . TABLE_ZONES . " where zone_country_id = '" . $HTTP_POST_VARS['country_id'] . "' and zone_id = '" . $HTTP_POST_VARS['state'] . "'"));
            $cache_state_prov_code = $cache_state_prov_values['zone_code'];
            $order->delivery = array('postcode' => $HTTP_POST_VARS['zip_code'],
                                 'state' => $cache_state_prov_code,
                                 'country' => array('id' => $HTTP_POST_VARS['country_id'], 'title' => $country_info['countries_name'], 'iso_code_2' => $country_info['countries_iso_code_2'], 'iso_code_3' =>  $country_info['countries_iso_code_3']),
                                 'country_id' => $HTTP_POST_VARS['country_id'],
                                 'zone_id' => $HTTP_POST_VARS['state'],
                                 'format_id' => tep_get_address_format_id($HTTP_POST_VARS['country_id']));
            $cart_country_id = $HTTP_POST_VARS['country_id'];
            tep_session_register('cart_country_id');
            //add state zone_id
            $cart_zone = $HTTP_POST_VARS['zone_id'];
            tep_session_register('cart_zone');
            $cart_zip_code = $HTTP_POST_VARS['zip_code'];
            tep_session_register('cart_zip_code');
          } elseif (tep_session_is_registered('cart_country_id')) {
            // session is available
            $country_info = tep_get_countries($cart_country_id,true);
            $order->delivery = array('postcode' => $cart_zip_code,
                                 'country' => array('id' => $cart_country_id, 'title' => $country_info['countries_name'], 'iso_code_2' => $country_info['countries_iso_code_2'], 'iso_code_3' =>  $country_info['countries_iso_code_3']),
                                 'country_id' => $cart_country_id,
                                 'format_id' => tep_get_address_format_id($cart_country_id));
          } else {
            // first timer
            $cart_country_id = STORE_COUNTRY;
            tep_session_register('cart_country_id');
            $country_info = tep_get_countries(STORE_COUNTRY,true);
            tep_session_register('cart_zip_code');
            $order->delivery = array(//'postcode' => '',
                                 'country' => array('id' => STORE_COUNTRY, 'title' => $country_info['countries_name'], 'iso_code_2' => $country_info['countries_iso_code_2'], 'iso_code_3' =>  $country_info['countries_iso_code_3']),
                                 'country_id' => STORE_COUNTRY,
                                 'format_id' => tep_get_address_format_id($HTTP_POST_VARS['country_id']));
          }
          // set the cost to be able to calculate free shipping
          $order->info = array('total' => $cart->show_total(), // TAX ????
                           'currency' => $currency,
                           'currency_value'=> $currencies->currencies[$currency]['value']);
        }
        // weight and count needed for shipping
        $total_weight = $cart->show_weight();
        $total_count = $cart->count_contents();
        require(DIR_WS_CLASSES . 'shipping.php');
        $shipping_modules = new shipping;
        $quotes = $shipping_modules->quote();
        $order->info['subtotal'] = $cart->total;

        // set selections for displaying
        $selected_country = $order->delivery['country']['id'];
        $selected_address = $sendto;
        // eof shipping cost

        // check free shipping based on order total
        if ( defined('MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING') && (MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING == 'true')) {
          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;
            default:
              $pass = false; 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;
        }

        // begin shipping cost
        if(!$free_shipping && $cart->get_content_type() !== 'virtual'){
          if (tep_not_null($HTTP_POST_VARS['sid'])) {
            list($module, $method) = explode('_', $HTTP_POST_VARS['sid']);
            $cart_sid = $HTTP_POST_VARS['sid'];
            tep_session_register('cart_sid');
          } elseif (tep_session_is_registered('cart_sid')) {
            list($module, $method) = explode('_', $cart_sid);
          } else {
            $module="";
            $method="";
          }
          if (tep_not_null($module)){
            $selected_quote = $shipping_modules->quote($method, $module);
            if($selected_quote[0]['error'] || !tep_not_null($selected_quote[0]['methods'][0]['cost'])){
              $selected_shipping = $shipping_modules->cheapest();
              $order->info['shipping_method'] = $selected_shipping['title'];
              $order->info['shipping_cost'] = $selected_shipping['cost'];
              $order->info['total']+= $selected_shipping['cost'];
            }else{
              $order->info['shipping_method'] = $selected_quote[0]['module'].' ('.$selected_quote[0]['methods'][0]['title'].')';
              $order->info['shipping_cost'] = $selected_quote[0]['methods'][0]['cost'];
              $order->info['total']+= $selected_quote[0]['methods'][0]['cost'];
              $selected_shipping['title'] = $order->info['shipping_method'];
              $selected_shipping['cost'] = $order->info['shipping_cost'];
              $selected_shipping['id'] = $selected_quote[0]['id'].'_'.$selected_quote[0]['methods'][0]['id'];
            }
          } else {
            $selected_shipping = $shipping_modules->cheapest();
            $order->info['shipping_method'] = $selected_shipping['title'];
            $order->info['shipping_cost'] = $selected_shipping['cost'];
            $order->info['total']+= $selected_shipping['cost'];
          }
        }

        // virtual products use free shipping
        if($cart->get_content_type() == 'virtual') {
          $order->info['shipping_method'] = CART_SHIPPING_METHOD_FREE_TEXT . ' ' . CART_SHIPPING_METHOD_ALL_DOWNLOADS;
          $order->info['shipping_cost'] = 0;
        }
        if($free_shipping) {
          $order->info['shipping_method'] = MODULE_ORDER_TOTAL_SHIPPING_TITLE;
          $order->info['shipping_cost'] = 0;
        }
        $shipping=$selected_shipping;
        // end of shipping cost
        // end free shipping based on order total

        echo '<table align="center" border="0" cellpadding="4" cellspacing="0" width="100%" class="productListing">';
        echo '  <tr >';
        echo '    <td class="productListing-heading">';
        echo CART_SHIPPING_OPTIONS;
        echo '    </td>';
        echo '  </tr>';
        echo '</table>';
        ?>
      </td>
    </tr>
    <tr>
      <td>
        <?php

        echo tep_draw_form('estimator', tep_href_link(FILENAME_SHOPPING_CART, '', 'NONSSL'), 'post'); //'onSubmit="return check_form();"'
        echo tep_draw_hidden_field('sid', $selected_shipping['id']);
        echo '<table border="0" cellpadding="4" cellspacing="0" width="100%" class="TableFrameShipping">';
        if(sizeof($quotes)) {
          if (tep_session_is_registered('customer_id')) {
            // logged in

            if (CARTSHIP_SHOWWT == 'true') {
              $showweight = '&nbsp;(' . $total_weight . '&nbsp;' . CARTSHIP_WTUNIT . ')';
            } else {
              $showweight = '';
            }

            $addresses_query = tep_db_query("select address_book_id, entry_city as city, entry_postcode as postcode, entry_state as state, entry_zone_id as zone_id, entry_country_id as country_id from " . TABLE_ADDRESS_BOOK . " where customers_id = '" . $customer_id . "'");
            // only display addresses if more than 1
            if (tep_db_num_rows($addresses_query) > 1){
              while ($addresses = tep_db_fetch_array($addresses_query)) {
                $addresses_array[] = array('id' => $addresses['address_book_id'], 'text' => tep_address_format(tep_get_address_format_id($addresses['country_id']), $addresses, 0, ' ', ' '));
              }
              echo '<tr><td colspan="3" class="main" nowrap>' .
                  CART_SHIPPING_METHOD_ADDRESS .'&nbsp;'. tep_draw_pull_down_menu('address_id', $addresses_array, $selected_address, 'onchange="return shipincart_submit(\'\');"').'</td></tr>';
            }
            echo '<tr valign="top"><td class="main"><table border="0" cellpadding="0" cellspacing="0"><tr valign="top"><td class="main"><b>' . CART_SHIPPING_METHOD_TO .'</b>&nbsp;</td><td colspan="2" class="main">'. tep_address_format($order->delivery['format_id'], $order->delivery, 1, ' ', '<br>') . '</td></tr></table></td></tr>';
          } else {
            // not logged in
              echo '<tr><td class="main" colspan="3">' . CART_SHIPPING_OPTIONS_LOGIN . '</td></tr>';

            if($cart->get_content_type() != 'virtual'){

              if(CARTSHIP_SHOWCDD == 'true'){
                echo '<tr><td colspan="3" class="main" nowrap>' .
                  ENTRY_COUNTRY .'&nbsp;'. tep_get_country_list('country_id', $selected_country,'style="width=200" onChange="this.form.submit();"').'<br>';
              }

              //add state zone_id
              $state_array[] = array('id' => '', 'text' => 'Please Select');
              $state_query = tep_db_query("select zone_name, zone_id from " . TABLE_ZONES . " where zone_country_id = '$selected_country' order by zone_country_id DESC, zone_name");
              while ($state_values = tep_db_fetch_array($state_query)) {
                $state_array[] = array('id' => $state_values['zone_id'],
                                       'text' => $state_values['zone_name']);
              }
            }
          }
          if($cart->get_content_type() == 'virtual'){
            // virtual product-download
            echo '<tr><td class="main" colspan="3">&nbsp;</td></tr><tr><td class="main" colspan="3"><i>' . CART_SHIPPING_METHOD_FREE_TEXT . ' ' . CART_SHIPPING_METHOD_ALL_DOWNLOADS . '</i></td></tr>';
          } elseif ($free_shipping==1) {
            // order $total is free
            echo '<tr><td class="main" colspan="3">&nbsp;</td></tr><tr><td class="main" colspan="3"><i>' . sprintf(FREE_SHIPPING_DESCRIPTION, $currencies->format(MODULE_ORDER_TOTAL_SHIPPING_FREE_SHIPPING_OVER)) . '</i></td><td>&nbsp;</td></tr>';
          } else {
            // shipping display
            if ( empty($quotes[0]['error']) || (!empty($quotes[1])&&empty($quotes[1]['error'])) ) {
              echo '<tr><td class="productListing-heading" align="left"><b>' . CART_SHIPPING_METHOD_TEXT . '</b></td><td  class="productListing-heading" align="right"><b>' . CART_SHIPPING_METHOD_RATES . '</b></td></tr>';
            } else {
              echo '';
            }
            for ($i=0, $n=sizeof($quotes); $i<$n; $i++) {
              if (sizeof($quotes[$i]['methods'])==1) {
                // simple shipping method
                $thisquoteid = $quotes[$i]['id'].'_'.$quotes[$i]['methods'][0]['id'];
                echo '<tr class="'.$extra.'">';
                if ($quotes[$i]['error']) {
                  echo '<td colspan="2" class="main">'.$quotes[$i]['module'].'&nbsp;';
                  echo $quotes[$i]['error'].'</td></tr>';
                } else {
                  if ($selected_shipping['id'] == $thisquoteid){
                    echo '<td class="main"><a title="' . CART_SELECT_THIS_METHOD .'" href="_"  onclick="return shipincart_submit(\''.$thisquoteid.'\');"><b>'.$quotes[$i]['module'].'&nbsp;';
                    echo '<br>'.$quotes[$i]['methods'][0]['title'].'</b></a>&nbsp;&nbsp;&nbsp;</td><td align="right" class="main" nowrap><b>'.$currencies->format(tep_add_tax($quotes[$i]['methods'][0]['cost'], $quotes[$i]['tax'])).'</b></td></tr>';
                  } else {
                    echo '<td class="main"><a title="' . CART_SELECT_THIS_METHOD .'" href="_" onclick="return shipincart_submit(\''.$thisquoteid.'\');">'.$quotes[$i]['module'].'&nbsp;';
                    echo '<br>'.$quotes[$i]['methods'][0]['title'].'</a>&nbsp;&nbsp;&nbsp;</td><td align="right" class="main" nowrap>'.$currencies->format(tep_add_tax($quotes[$i]['methods'][0]['cost'], $quotes[$i]['tax'])).'</td></tr>';
                  }
                }
              } else {
                // shipping method with sub methods (multipickup)
                /*
                for ($j=0, $n2=sizeof($quotes[$i]['methods']); $j<$n2; $j++) {
                  $thisquoteid = $quotes[$i]['id'].'_'.$quotes[$i]['methods'][$j]['id'];
                  echo '<tr class="'.$extra.'">';
                  //echo '<td class="main">'.$quotes[$i]['icon'].'&nbsp;&nbsp;&nbsp;</td>';
                  if($quotes[$i]['error']){
                    echo '<td colspan="2" class="main">'.$quotes[$i]['module'].'&nbsp;';
                    echo '('.$quotes[$i]['error'].')</td></tr>';
                  }else{
                    if($selected_shipping['id'] == $thisquoteid){
                      echo '<td class="main"><a title="' . CART_SELECT_THIS_METHOD .'" href="_" onclick="return shipincart_submit(\''.$thisquoteid.'\');"><b>'.$quotes[$i]['module'];
                      echo trim($quotes[$i]['methods'][$j]['title']).'</b></a>&nbsp;&nbsp;&nbsp;</td><td align="right" class="main"><b>'.$currencies->format(tep_add_tax($quotes[$i]['methods'][$j]['cost'], $quotes[$i]['tax'])).'</b></td></tr>';
                    } else {
                      echo '<td class="main"><a title="' . CART_SELECT_THIS_METHOD .'" href="_" onclick="return shipincart_submit(\''.$thisquoteid.'\');">'.$quotes[$i]['module'];
                      echo trim($quotes[$i]['methods'][$j]['title']).'</a>&nbsp;&nbsp;&nbsp;</td><td align="right" class="main">'.$currencies->format(tep_add_tax($quotes[$i]['methods'][$j]['cost'], $quotes[$i]['tax'])).'</td></tr>';
                    }
                  }
                }
                */
                // zmiana do paczkomatow
                for ($j=0, $n2=1; $j<$n2; $j++) {
                  $thisquoteid = $quotes[$i]['id'].'_'.$quotes[$i]['methods'][$j]['id'];
                  echo '<tr class="'.$extra.'">';
                  //echo '<td class="main">'.$quotes[$i]['icon'].'&nbsp;&nbsp;&nbsp;</td>';
                  if($quotes[$i]['error']){
                    echo '<td colspan="2" class="main">'.$quotes[$i]['module'].'&nbsp;';
                    echo '<br>'.$quotes[$i]['error'].'</td></tr>';
                  }else{
                    if($selected_shipping['id'] == $thisquoteid){
                      echo '<td class="main"><a title="' . CART_SELECT_THIS_METHOD .'" href="_" onclick="return shipincart_submit(\''.$thisquoteid.'\');"><b>'.str_replace('<br>','',$quotes[$i]['module']);
                      echo '</b></a>&nbsp;&nbsp;&nbsp;</td><td align="right" class="main"><b>'.$currencies->format(tep_add_tax($quotes[$i]['methods'][$j]['cost'], $quotes[$i]['tax'])).'</b></td></tr>';
                    } else {
                      echo '<td class="main"><a title="' . CART_SELECT_THIS_METHOD .'" href="_" onclick="return shipincart_submit(\''.$thisquoteid.'\');">'.str_replace('<br>','',$quotes[$i]['module']);
                      echo '</a>&nbsp;&nbsp;&nbsp;</td><td align="right" class="main">'.$currencies->format(tep_add_tax($quotes[$i]['methods'][$j]['cost'], $quotes[$i]['tax'])).'</td></tr>';
                    }
                  }
                }
              }
            }
          }
        }
        echo '</table></form>';
        ?>
      </td>
    </tr>
    <tr>
      <td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '3'); ?></td>
    </tr>
    <tr>
      <td>
        <?php

        if (CARTSHIP_SHOWOT == 'true'){
          // BOF get taxes if not logged in
          if (!tep_session_is_registered('customer_id')){
            $products = $cart->get_products();
            for ($i=0, $n=sizeof($products); $i<$n; $i++) {
              $products_tax = tep_get_tax_rate($products[$i]['tax_class_id'], $order->delivery['country_id'],$order->delivery['zone_id']);
              $products_tax_description = tep_get_tax_description($products[$i]['tax_class_id'], $order->delivery['country_id'], $order->delivery['zone_id']);
              if (DISPLAY_PRICE_WITH_TAX == 'true') {
                //Modified by Strider 42 to correct the tax calculation when a customer is not logged in
                // $tax_val = ($products[$i]['final_price']-(($products[$i]['final_price']*100)/(100+$products_tax)))*$products[$i]['quantity'];
                $tax_val = (($products[$i]['final_price']/100)*$products_tax)*$products[$i]['quantity'];
              } else {
                $tax_val = (($products[$i]['final_price']*$products_tax)/100)*$products[$i]['quantity'];
              }
              $order->info['tax'] += $tax_val;
              $order->info['tax_groups']["$products_tax_description"] += $tax_val;
              // Modified by Strider 42 to correct the order total figure when shop displays prices with tax
              if (DISPLAY_PRICE_WITH_TAX == 'true') {
                $order->info['total'];
              } else {
                $order->info['total']+=$tax_val;
              }
            }
          }
          // EOF get taxes if not logged in (seems like less code than in order class)
          require(DIR_WS_CLASSES . 'order_total.php');
          $order_total_modules = new order_total;
          // order total code
          $order_total_modules->process();

          echo '<table align="center" border="0" cellpadding="4" cellspacing="0" width="100%" class="productListing">';
          echo '  <tr >';
          echo '    <td class="productListing-heading">';
          echo CART_OT;
          echo '    </td>';
          echo '  </tr>';
          echo '</table>';
          ?>
      </td>
    </tr>
    <tr>
      <td>
          <?php
          echo '<table align="right" border="0" cellpadding="4" cellspacing="0" width="100%" class="TableFrameShipping">';
          echo $order_total_modules->output().'</table>';

        }
?>
      </td>
    </tr>
  </table>

<?php } // Use only when cart_contents > 0 ?>