<?php /** @@var $this Mage_Checkout_Block_Onepage_Shipping_Method_Available */ ?>
<?php $_shippingRateGroups = $this->getShippingRates(); ?>
<?php if (!$_shippingRateGroups): ?>
    <?php echo Mage::helper('designer/messages')->notice($this->__('Sorry, no quotes are available for this order at this time.')) ?>
<?php else: ?>
    <dl class="sp-methods">
    <?php $shippingCodePrice = array(); ?>
    <?php $_sole = count($_shippingRateGroups) == 1; foreach ($_shippingRateGroups as $code => $_rates): ?>
        <dt><?php echo $this->escapeHtml($this->getCarrierName($code)) ?></dt>
        <dd>
            <ul>
            <?php $_sole = $_sole && count($_rates) == 1; foreach ($_rates as $_rate): ?>
                <?php $shippingCodePrice[] = "'".$_rate->getCode()."':".(float)$_rate->getPrice(); ?>
                <li>
                   <?php if ($_rate->getErrorMessage()): ?>
                       <?php echo Mage::helper('designer/messages')->warning($_rate->getErrorMessage()) ?>
                   <?php else: ?>
                        <?php if ($_sole) : ?>
                        <span class="no-display"><input name="shipping_method" type="radio" value="<?php echo $_rate->getCode() ?>" id="s_method_<?php echo $_rate->getCode() ?>" checked="checked" /></span>
                        <?php else: ?>
                        <input name="shipping_method" type="radio" value="<?php echo $_rate->getCode() ?>" id="s_method_<?php echo $_rate->getCode() ?>"<?php if($_rate->getCode()===$this->getAddressShippingMethod()) echo ' checked="checked"' ?> class="radio"/>

                        <?php if ($_rate->getCode() === $this->getAddressShippingMethod()): ?>
                        <script type="text/javascript">
                            //<![CDATA[
                                lastPrice = <?php echo (float)$_rate->getPrice(); ?>;
                            //]]>
                        </script>
                        <?php endif; ?>

                        <?php endif; ?>
                        <label for="s_method_<?php echo $_rate->getCode() ?>"><?php echo $this->escapeHtml($_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; ?>
                        </label>
                   <?php endif ?>
                </li>
            <?php endforeach; ?>
            </ul>
        </dd>
    <?php endforeach; ?>
    </dl>
<script type="text/javascript">
//<![CDATA[
    <?php if (!empty($shippingCodePrice)): ?>
        var shippingCodePrice = {<?php echo implode(',',$shippingCodePrice); ?>};
    <?php endif; ?>

    $$('input[type="radio"][name="shipping_method"]').each(function(el){
        Event.observe(el, 'click', function(){
            if (el.checked == true) {
                var getShippingCode = el.getValue();
                <?php if (!empty($shippingCodePrice)): ?>
                    var newPrice = shippingCodePrice[getShippingCode];
                    if (!lastPrice) {
                        lastPrice = newPrice;
                        quoteBaseGrandTotal += newPrice;
                    }
                    if (newPrice != lastPrice) {
                        quoteBaseGrandTotal += (newPrice-lastPrice);
                        lastPrice = newPrice;
                    }
                <?php endif; ?>
                checkQuoteBaseGrandTotal = quoteBaseGrandTotal;
                return false;
            }
       });
    });
//]]>
</script>
<?php endif; ?>
