<?php
/**
 * Avalanche for Magento 1.8+
 * Designed by Fast Division (http://fastdivision.com)
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the EULA
 * that is bundled with this package in the file license.txt.
 * It is also available through the world-wide-web at this URL:
 * http://fastdivision.com/legal/license.txt
 *
 * @author     Fast Division
 * @version    1.5.0
 * @copyright  Copyright 2013 Fast Division
 * @license    http://fastdivision.com/legal/license.txt
 */
?>
<div class="shipping white-box">
    <h3><?php echo $this->__('Shipping &amp; Tax') ?></h3>
    <div class="shipping-form white-box-inner">
       <form action="<?php echo $this->getUrl('checkout/cart/estimatePost') ?>" method="post" id="shipping-zip-form">
            <ul class="form-list">
                <li>
                    <label for="country" class="required"><?php echo $this->__('Country') ?><em>*</em></label>
                    <div class="input-box">
                        <?php echo Mage::getBlockSingleton('directory/data')->getCountryHtmlSelect($this->getEstimateCountryId()) ?>
                    </div>
                </li>
            <?php //if($this->getStateActive()): ?>
                <li>
                    <label for="region_id"<?php if ($this->isStateProvinceRequired()) echo ' class="required"' ?>><?php echo $this->__('State / Province') ?><?php if ($this->isStateProvinceRequired()) echo '<em>*</em>' ?></label>
                    <div class="input-box">
                        <select id="region_id" name="region_id" title="<?php echo $this->__('State/Province') ?>" style="display:none;"<?php echo ($this->isStateProvinceRequired() ? ' class="validate-select"' : '') ?>>
                            <option value=""><?php echo $this->__('Please select region, state or province') ?></option>
                        </select>

                       <script type="text/javascript">
                       //<![CDATA[
                           $('region_id').setAttribute('defaultValue',  "<?php echo $this->getEstimateRegionId() ?>");
                       //]]>
                       </script>

                       <input type="text" id="region" name="region" value="<?php echo $this->htmlEscape($this->getEstimateRegion()) ?>"  title="<?php echo $this->__('State/Province') ?>" class="input-text" style="display:none;" />
                   </div>
                </li>
            <?php //endif; ?>
            <?php if($this->getCityActive()): ?>
                <li>
                    <label for="city"<?php if ($this->isCityRequired()) echo ' class="required"' ?>><?php echo $this->__('City') ?><?php if ($this->isStateProvinceRequired()) echo '<em>*</em>' ?></label>
                    <div class="input-box">
                        <input class="input-text<?php if ($this->isCityRequired()):?> required-entry<?php endif;?>" id="city" type="text" name="estimate_city" value="<?php echo $this->htmlEscape($this->getEstimateCity()) ?>" />
                    </div>
                </li>
            <?php endif; ?>
                <li>
                    <label for="postcode" class="required"><?php echo $this->__('Zip / Postal Code') ?><em>*</em></label>
                    <div class="input-box">
                        <input class="input-text validate-postcode<?php if ($this->isZipCodeRequired()):?> required-entry<?php endif;?>" type="text" id="postcode" name="estimate_postcode" value="<?php echo $this->htmlEscape($this->getEstimatePostcode()) ?>" />
                    </div>
                </li>
            </ul>
            <div class="buttons-set">
                <button type="button" onclick="coShippingMethodForm.submit()" class="light-button"><span><span><?php echo $this->__('Get Estimate') ?></span></span></button>
            </div>
        </form>
        <script type="text/javascript">
        //<![CDATA[
            new RegionUpdater('country', 'region', 'region_id', <?php echo $this->helper('directory')->getRegionJson() ?>);
        //]]>
        </script>

        <?php if (($_shippingRateGroups = $this->getEstimateRates())): ?>
        <form id="co-shipping-method-form" action="<?php echo $this->getUrl('checkout/cart/estimateUpdatePost') ?>">
            <dl class="sp-methods">
                <dt>Shipping Method</dt>
                <dd>
                    <select name="estimate_method" style="width: 100%">
                        <?php foreach($_shippingRateGroups as $code => $_rates): ?>
                            <optgroup label="<?php echo $this->getCarrierName($code) ?>">
                                <?php foreach ($_rates as $_rate): ?>
                                    <option value="<?php echo $this->htmlEscape($_rate->getCode()) ?>"<?php if($_rate->getCode()===$this->getAddressShippingMethod()) echo ' selected="selected"' ?>>
                                        <?php echo $_rate->getMethodTitle() ?>
                                        <?php $_excl = $this->getShippingPrice($_rate->getPrice(), $this->helper('tax')->displayShippingPriceIncludingTax()); ?>
                                        <?php $_incl = $this->getShippingPrice($_rate->getPrice(), true); ?>
                                        <?php echo $_excl; ?>
                                        <?php if ($this->helper('tax')->displayShippingBothPrices() && $_incl != $_excl): ?>
                                            (<?php echo $this->__('Incl. Tax'); ?> <?php echo $_incl; ?>)
                                        <?php endif; ?>
                                    </option>
                                <?php endforeach; ?>
                            </optgroup>
                        <?php endforeach; ?>
                    </select>
                </dd>
            </dl>
            <div class="buttons-set">
                <button type="submit" title="<?php echo $this->__('Update Total') ?>" class="button" name="do" value="<?php echo $this->__('Update Total') ?>"><span><span><?php echo $this->__('Update Total') ?></span></span></button>
            </div>
        </form>
        <?php endif; ?>
        <script type="text/javascript">
        //<![CDATA[
            var coShippingMethodForm = new VarienForm('shipping-zip-form');
            var countriesWithOptionalZip = <?php echo $this->helper('directory')->getCountriesWithOptionalZip(true) ?>;

            coShippingMethodForm.submit = function () {
                var country = $F('country');
                var optionalZip = false;

                for (i=0; i < countriesWithOptionalZip.length; i++) {
                    if (countriesWithOptionalZip[i] == country) {
                        optionalZip = true;
                    }
                }
                if (optionalZip) {
                    $('postcode').removeClassName('required-entry');
                }
                else {
                    $('postcode').addClassName('required-entry');
                }
                return VarienForm.prototype.submit.bind(coShippingMethodForm)();
            }
        //]]>
        </script>
    </div>
</div>