<?php

$step_counter = 1;
$helper = Mage::helper('onestepcheckout/checkout');

?>
<?php if(!$this->canCheckout() || !$this->validateMinimumAmount()): ?>
    <?php if($this->settings['checkout_title']): ?>
        <h1 class="onestepcheckout-title"><?php echo $this->settings['checkout_title']; ?></h1>
    <?php endif; ?>

    <?php if($this->canCheckout() && !$this->validateMinimumAmount()): ?>
    <p><?php echo Mage::getStoreConfig('sales/minimum_order/description'); ?></p>
    <p><a href="<?php echo $this->getUrl(''); ?>"><?php echo $this->__('Back to homepage'); ?></a></p>
    <?php else: ?>
    <p><?php echo $this->__('You need to have products in your cart to checkout, and your cart is empty.'); ?></p>
    <p><a href="<?php echo $this->getUrl(''); ?>"><?php echo $this->__('Back to homepage'); ?></a></p>
    <?php endif; ?>
<?php else: ?>

<form id="onestepcheckout-form" method="post" action="<?php echo $this->getUrl('onestepcheckout', array('_secure'=>true)); ?>">
<fieldset class="group-select" style="margin: 0;">

<?php if($this->settings['checkout_title']): ?>
    <h1 class="onestepcheckout-title"><?php echo $this->settings['checkout_title']; ?></h1>
<?php endif; ?>

<?php if($this->settings['checkout_description']): ?>
    <p class="onestepcheckout-description"><?php echo $this->settings['checkout_description']; ?></p>
<?php endif; ?>

<?php if(!$this->isCustomerLoggedIn() && $helper->showLoginLink()): ?>
    <p class="onestepcheckout-login-link">
        <a id="onestepcheckout-login-link" href="<?php echo $this->getUrl('customer/account/login'); ?>"><?php echo $this->__('Already registered? Click here to login.'); ?></a>
    </p>
<?php endif; ?>

<?php if(isset($this->formErrors['unknown_source_error'])): ?>
<div class="onestepcheckout-error">
    <?php echo $this->formErrors['unknown_source_error']; ?>
</div>
<?php endif; ?>

<div class="onestepcheckout-threecolumns checkoutcontainer onestepcheckout-skin-<?php echo $this->settings['skin']; ?> <?php if(Mage::helper('onestepcheckout')->isEnterprise()): ?>onestepcheckout-enterprise<?php endif; ?>">
    <div class="onestepcheckout-column-left">
        <div id="billing_address">
            <script type="text/javascript">
            var billing = new Billing();
            </script>
            <ul>
                <li>
                    <p class="onestepcheckout-numbers onestepcheckout-numbers-<?php echo $step_counter++; ?>"><?php echo $this->__('Billing address'); ?></p>
                        <?php if(isset($this->formErrors['billing_error']) && count($this->formErrors['billing_errors']) > 0): ?>
                        <div class="onestepcheckout-error">
                            <?php echo $this->__('Please check red fields below and try again.'); ?>
                        </div>
                        <?php endif; ?>
                </li>
                <?php if ($this->customerHasAddresses()): ?>
                <li>
                    <label for="billing-address-select"><?php echo $this->__('Select a billing address from your address book or enter a new address.') ?></label>
                    <div class="input-box">
                        <?php echo $this->getAddressesHtmlSelect('billing') ?>
                    </div>
                </li>
                <?php endif; ?>
                <li>
                    <div>
                        <ul id="billing_address_list" <?php echo (($this->customerHasAddresses() && !$this->getNewAddressSelectValueOnError('billing')) ? 'style = "display:none"' : false ); ?>>
                            <?php echo $this->getChildHtml('billing_address');?>
                        </ul>
                    </div>
                </li>
                <li>
                    <?php $uncheck = (!empty($_POST['billing']) && empty($_POST['billing']['use_for_shipping']));?>
                    <?php if($this->differentShippingAvailable()): ?>
                        <div class="input-box input-different-shipping">
                            <input type="checkbox" name="billing[use_for_shipping]" id="billing:use_for_shipping_yes" value="1" <?php echo (($this->sameAsBilling() && !$uncheck) ? 'checked="checked" ':'')?>/>
                            <label for="billing:use_for_shipping_yes"><?php echo $this->__('Ship to the same address')?></label>
                        </div>
                    <?php else: ?>
                        <input type="hidden" name="billing[use_for_shipping]" id="billing:use_for_shipping_yes" value="1" />
                    <?php endif; ?>
                </li>
            </ul>
        </div>
    <?php if($this->differentShippingAvailable()): ?>
        <div id="shipping_address" <?php  echo (($this->sameAsBilling() && !$uncheck) ? 'style="display: none"': false);?>>
            <script type="text/javascript">
                var shipping = new Shipping();
            </script>
            <ul>
                <li class="shipping-address-title">
                    <?php echo $this->__('Shipping address'); ?>
                </li>
                <?php if ($this->customerHasAddresses()): ?>
                   <li class="form-alt">
                       <label for="shipping-address-select"><?php echo $this->__('Select a shipping address from your address book or enter a new address.') ?></label>
                       <div class="input-box"><?php echo $this->getAddressesHtmlSelect('shipping') ?></div>
                   </li>
                <?php endif ?>
                <li id="shipping_address_list" <?php if($this->customerHasAddresses() && !$this->getNewAddressSelectValueOnError('shipping')) { echo '  style="display: none;" '; } ?>>
                    <div id="">
                        <ul>
                            <?php echo $this->getChildHtml('shipping_address');?>
                        </ul>
                        <input type="hidden" name="shipping[address_id]" value="<?php echo $this->getQuote()->getShippingAddress()->getId() ?>" id="shipping:address_id" />
                        <!-- END LIST OF SHIPPIING FIELDS -->
                    </div>
                </li>
            </ul>
        </div>
    <?php endif; ?>
</div>

    <div class="onestepcheckout-column-middle">


        <?php if(!$this->isVirtual()): ?>
            <?php if(Mage::getStoreConfig('onestepcheckout/general/hide_shipping_method')):?>
                <?php if(isset($this->formErrors['shipping_method']) && $this->formErrors['shipping_method']): ?>
                    <div class="onestepcheckout-error onestepcheckout-shipment-method-error">
                        <?php echo $this->__('Please choose a shipping method.'); ?>
                    </div>
                <?php endif; ?>
                <?php echo $this->getChildHtml('choose-shipping-method'); ?>
            <?php else:?>
                <div class="onestepcheckout-shipping-method">
                    <p class="onestepcheckout-numbers onestepcheckout-numbers-<?php echo $step_counter++; ?>"><?php echo $this->__('Shipping method'); ?></p>

                    <?php if(isset($this->formErrors['shipping_method']) && $this->formErrors['shipping_method']): ?>
                    <div class="onestepcheckout-error onestepcheckout-shipment-method-error">
                        <?php echo $this->__('Please choose a shipping method.'); ?>
                    </div>
                    <?php endif; ?>

                    <div class="onestepcheckout-shipping-method-block">
                        <?php echo $this->getChildHtml('choose-shipping-method'); ?>
                    </div>
                </div>
            <?php endif; ?>
        <?php endif; ?>

        <?php if(Mage::getStoreConfig('onestepcheckout/general/hide_payment_method')):?>
            <?php if(!empty($this->formErrors['payment_method'])): ?>
            <div class="onestepcheckout-error onestepcheckout-payment-method-error">
                <?php echo $this->__('Please choose a payment method.'); ?>
            </div>
            <?php endif; ?>
            <?php if(!empty($this->formErrors['payment_method_error'])): ?>
                <div class="onestepcheckout-error onestepcheckout-payment-method-error">
                    <?php echo $this->__('Please enter valid details below.'); ?>
                </div>
            <?php endif; ?>
            <?php echo $this->getChildHtml('choose-payment-method'); ?>
        <?php else: ?>
            <p class="onestepcheckout-numbers onestepcheckout-numbers-<?php echo $step_counter++; ?>"><?php echo $this->__('Payment method'); ?></p>
            <?php if(isset($this->formErrors['payment_method']) && $this->formErrors['payment_method']): ?>
                <div class="onestepcheckout-error onestepcheckout-payment-method-error">
                    <?php echo $this->__('Please choose a payment method.'); ?>
                </div>
            <?php else: ?>
                <?php if(isset($this->formErrors['payment_method_error'])): ?>
                    <div class="onestepcheckout-error onestepcheckout-payment-method-error">
                        <?php echo $this->__('Please enter valid details below.'); ?>
                    </div>
                <?php endif; ?>
            <?php endif; ?>

            <?php echo $this->getChildHtml('choose-payment-method'); ?>

            <div class="tool-tip" id="payment-tool-tip" style="display:none;">
                <div class="btn-close">
                    <a href="#" id="payment-tool-tip-close"><img src="<?php echo $this->getSkinUrl('images/btn_window_close.gif') ?>" alt="<?php echo $this->__('Close') ?>" /></a>
                </div>
                <div class="block-content">
                <img src="<?php echo $this->getSkinUrl('images/cvv.gif') ?>" alt="<?php echo $this->__('Card Verification Number Visual Reference') ?>" />
                </div>
            </div>
        <?php endif; ?>
    </div>

    <div class="onestepcheckout-column-right">

        <p class="onestepcheckout-numbers onestepcheckout-numbers-4"><?php echo $this->__('Review your order'); ?></p>

        <div class="onestepcheckout-summary">
            <?php echo $this->getChildHtml('summary'); ?>
        </div>

        <?php if($this->settings['enable_discount']): ?>
        <div class="onestepcheckout-coupons">
            <div id="coupon-notice" style="display: none;"></div>
            <?php $_couponcode = $this->getQuote()->getCouponCode(); ?>
            <label for="id_couponcode"><?php echo $this->__('Coupon code:'); ?></label><br/>
            <input class="input-text" type="text" name="onestepcheckout-couponcode" id="id_couponcode" value="<?php echo $_couponcode; ?>" />
            <br/>
            <button id="onestepcheckout-coupon-add" class="form-button-alt" type="button"><span><?php echo $this->__('Apply Coupon'); ?></span></button>
            <button id="onestepcheckout-coupon-remove" class="form-button-alt" type="button" style="<?php if($_couponcode == '') { echo 'display: none;'; } ?>"><span><?php echo $this->__('Cancel Coupon'); ?></span></button>
            <script>
            document.observe('dom:loaded', function() {
                $('onestepcheckout-coupon-add').observe('click', function(e)    {
                    var coupon = $('id_couponcode').getValue();
                    var couponNotice = $('coupon-notice');
                    couponNotice.hide();
                    if(coupon == '')    {
                        alert('<?php echo $this->__('Please enter a valid coupon code.'); ?>');
                        return;
                    }

                    var url = '<?php echo $this->getUrl('onestepcheckout/ajax/add_coupon', array('_secure'=>true)); ?>';
                    var parameters = {code: coupon};
                    var summary = $$('div.onestepcheckout-summary').first();

                    summary.update('<div class="loading-ajax">&nbsp;</div>');

                    new Ajax.Request(url, {
                        method: 'post',
                        parameters: parameters,
                        onSuccess: function(transport) {
                            if(transport.status == 200) {

                                var response = transport.responseText.evalJSON();

                                if(response.success) {
                                    summary.update(response.summary);
                                    couponNotice.update(response.message);
                                    couponNotice.removeClassName('error-msg');
                                    couponNotice.addClassName('success-msg');
                                    couponNotice.show();
                                    /* Show remove button */
                                    $('onestepcheckout-coupon-remove').show();
                                }
                                else    {
                                    summary.update(response.summary);
                                    couponNotice.update(response.message);
                                    couponNotice.removeClassName('success-msg');
                                    couponNotice.addClassName('error-msg');
                                    couponNotice.show();
                                    /* Hide remove button */
                                    $('onestepcheckout-coupon-remove').hide();
                                }
                            }
                        }
                    });
                });

                $('onestepcheckout-coupon-remove').observe('click', function(e) {
                    var coupon = $('id_couponcode').getValue();
                    var couponNotice = $('coupon-notice');
                    couponNotice.hide();
                    var url = '<?php echo $this->getUrl('onestepcheckout/ajax/add_coupon', array('_secure'=>true)); ?>';
                    var parameters = {code: coupon, remove: '1'};
                    var summary = $$('div.onestepcheckout-summary').first();

                    summary.update('<div class="loading-ajax">&nbsp;</div>');

                    new Ajax.Request(url, {
                        method: 'post',
                        parameters: parameters,
                        onSuccess: function(transport)  {
                            if(transport.status == 200) {
                                var response = transport.responseText.evalJSON();

                                if(response.success){
                                    $('id_couponcode').setValue('')
                                    $('onestepcheckout-coupon-remove').hide();
                                }
                                var summary = $$('div.onestepcheckout-summary').first();

                                summary.hide();
                                summary.update(response.summary);
                                summary.show();

                                couponNotice.hide();
                                couponNotice.update(response.message);
                                couponNotice.removeClassName('error-msg');
                                couponNotice.addClassName('success-msg');
                                couponNotice.show();
                            }
                        }
                    });
                });
            });
            </script>
        </div>
        <?php endif; ?>


        <?php if($this->settings['enable_comments']): ?>
            <div class="onestepcheckout-comments">
                <label for="id_comments"><?php echo $this->__('Comments'); ?></label><br/>
                <textarea id="id_comments" name="onestepcheckout_comments"><?php if(isset($_POST['onestepcheckout_comments'])) { echo $_POST['onestepcheckout_comments']; } ?></textarea>
            </div>
        <?php endif; ?>

        <?php if($this->settings['enable_gift_messages']): ?>
            <div id="onestepcheckout-giftmessages">
            <div class="onestepcheckout-giftmessagecontainer">
            <?php echo $this->helper('onestepcheckout/message')->getInline('onepage_checkout', $this->getQuote(), $this->getDontDisplayContainer()) ?>
            </div>
            </div>
        <?php endif; ?>

        <?php if($this->settings['enable_newsletter'] && $this->settings['enable_terms']): ?>
            <div class="onestepcheckout-enable-newsletter">
                <input type="checkbox" id="id_subscribe_newsletter" name="subscribe_newsletter" value="1" <?php if($this->settings['newsletter_default_checked']): ?>checked="checked"<?php endif; ?> />
                <label for="id_subscribe_newsletter"><?php echo $this->__('Subscribe to our newsletter'); ?></label>
            </div>
        <?php endif; ?>

        <?php if($this->settings['enable_terms']): ?>
            <div class="onestepcheckout-enable-terms">
                <?php

                if(isset($this->formErrors['terms_error']) && $this->formErrors['terms_error']) {
                    $terms_error = true;
                }
                else    {
                    if($_SERVER['REQUEST_METHOD'] == 'POST')    {
                        $terms_error = false;
                    }
                    else    {
                        $terms_error = true;
                    }
                }
                ?>

                <input class="required-entry" type="checkbox" id="id_accept_terms" name="accept_terms" value="1" <?php if(!$terms_error) echo "checked=\"checked\""; ?> />
                <label for="id_accept_terms"><?php echo $this->__('I accept the <a id="onestepcheckout-toc-link" target="_blank" href="#">Terms and Conditions</a>'); ?></label>

                <?php if(isset($this->formErrors['terms_error']) && $this->formErrors['terms_error']): ?>
                <div class="onestepcheckout-error onestepcheckout-terms-error">
                    <?php echo $this->__('You must accept our terms to continue.'); ?>
                </div>
                <?php endif; ?>

            </div>
        <?php endif; ?>




        <?php $_extraProductsHelper = Mage::helper('onestepcheckout/extraproducts'); ?>
        <?php if($_extraProductsHelper->hasExtraProducts()): ?>
            <div class="onestepcheckout-extraproducts">
            <ul>
            <?php foreach($_extraProductsHelper->getExtraProducts() as $product): ?>
                <li>
                    <input type="checkbox" class="onestepcheckout-extra-product" <?php if($_extraProductsHelper->productInCart($product->getId())): ?> checked="checked" <?php endif; ?> name="extra_products_<?php echo $product->getId(); ?>" id="id_extra_product_<?php echo $product->getId(); ?>" />
                    &nbsp;
                    <label for="id_extra_product_<?php echo $product->getId(); ?>">
                        <?php echo $product->getName(); ?>
                        <span><?php echo Mage::helper('checkout')->formatPrice($product->getPrice()); ?></span>
                    </label>
                </li>
            <?php endforeach; ?>
            </ul>
            </div>

            <script>
            document.observe('dom:loaded', function() {
                $$('input.onestepcheckout-extra-product').invoke('observe', 'click', function(e) {
                    var id_temp = e.element().id.split('id_extra_product_');
                    if(id_temp.length == 2) {
                        var product_id = id_temp[1];
                        var url = '<?php echo $this->getUrl('onestepcheckout/ajax/add_extra_product'); ?>';
                        var parameters = {
                            product_id: product_id
                        }

                        if(!e.element().checked) {
                            parameters['remove'] = 1;
                        }

                        var summary = $$('div.onestepcheckout-summary').first();
                        summary.update('<div class="loading-ajax">&nbsp;</div>');

                        new Ajax.Request(url, {
                            method: 'post',
                            parameters: parameters,
                            onSuccess: function(transport)  {
                                summary.update(transport.responseText);
                            }
                        });
                   };
                });
            });


    </script>

        <?php endif; ?>


        <?php
        /**
         * Feedbackdropdown start
         */
        ?>
        <?php if(!empty($this->settings['feedback']['enable_feedback']) && !empty($this->settings['feedback']['feedback_values'])):?>
            <?php
                $selectedFeedBackFields = $this->getRequest()->getPost('onestepcheckout-feedback', false);
                $feedbackValues = unserialize($this->settings['feedback']['feedback_values']);
            ?>
            <div class="onestepcheckout-feedback" id="">
                <label for="id_feedback"><?php echo $this->__('How did you hear about us?'); ?></label><br>
                <select style="" name="onestepcheckout-feedback" id="id_feedback" defaultvalue="">
                    <option value=""><?php echo $this->__('Please choose'); ?></option>
                    <?php foreach($feedbackValues as $value => $label):
                        $selected= (!empty($selectedFeedBackFields) && $selectedFeedBackFields == $value) ? ' selected' : '';
                    ?>
                    <option value="<?php echo $value?>" <?php echo $selected;?>><?php echo $label['value']?></option>
                    <?php endforeach;?>
                    <?php if(!empty($this->settings['feedback']['enable_feedback_freetext'])):
                        $selected= (empty($feedbackValues[$selectedFeedBackFields]) && $selectedFeedBackFields != '') ? ' selected' : '';
                    ?>
                    <option value="freetext" <?php echo $selected;?>><?php echo $this->__('Other'); ?></option>
                    <?php endif;?>
                </select>
            </div>
            <?php if(!empty($this->settings['feedback']['enable_feedback_freetext'])):?>
                <script type="text/javascript">
                    $('id_feedback').observe('change', function (event){
                        if(this.getValue() == 'freetext'){
                            $('id_feedback_freetext_div').show();
                        } else {
                            $('id_feedback_freetext_div').hide();
                        }
                    });
                </script>
                <div id='id_feedback_freetext_div' class="onestepcheckout-feedback-freetext"<?php echo ((!empty($selectedFeedBackFields) && $selectedFeedBackFields == 'freetext') ? '' : ' style="display: none;"'); ?>>
                    <label for="id_feedback_freetext"><?php echo $this->__('Please specify:'); ?></label><br/>
                    <textarea id="id_feedback_freetext" name="onestepcheckout-feedback-freetext"><?php echo $this->getRequest()->getPost('onestepcheckout-feedback-freetext', false);?></textarea>
                </div>
            <?php endif; ?>
        <?php endif; ?>
        <?php
        /**
         * Feedbackdropdown end
         */
        ?>



        <div class="onestepcheckout-place-order">
            <a id="onestepcheckout-place-order" href="#" class="large orange onestepcheckout-button"><?php echo $this->__('Place order now'); ?></a>
        </div>



        <?php if($this->settings['enable_newsletter'] && !$this->settings['enable_terms']): ?>
            <div class="onestepcheckout-enable-newsletter-bottom">
                <input type="checkbox" id="id_subscribe_newsletter" name="subscribe_newsletter" value="1" <?php if($this->settings['newsletter_default_checked']): ?> checked="checked" <?php endif; ?> />
                <label for="id_subscribe_newsletter"><?php echo $this->__('Subscribe to our newsletter'); ?></label>
            </div>
        <?php endif; ?>

    </div>

    <div style="clear: both;">&nbsp;</div>

</div>

</fieldset>



</form>















<?php if(!$this->isCustomerLoggedIn() && $helper->showLoginLink()): ?>
<div id="onestepcheckout-login-popup" style="display: none;">
    <div class="onestepcheckout-popup-wrapper">
    <div class="onestepcheckout-popup-contents">

        <div id="onestepcheckout-login-popup-contents-forgot" style="display: none;">
            <h1><?php echo $this->__('Forgotten your password?'); ?></h1>
            <p><?php echo $this->__('Enter your email below and we\'ll send you a new password by email.'); ?></p>

            <form id="onestepcheckout-forgot-form">

                <div id="onestepcheckout-forgot-loading" style="display: none;" class="loading-ajax">&nbsp;</div>
                <div id="onestepcheckout-forgot-error" class="onestepcheckout-error" style="display: none;">&nbsp;</div>
                <div id="onestepcheckout-forgot-success" style="display: none;">
                    <?php echo $this->__('We have now sent you a new password to your email address. Click the link below to return to login.'); ?>
                </div>

                <table id="onestepcheckout-forgot-table">
                    <tr>
                        <td style="width: 25%;"><label for="id_onestepcheckout_email"><?php echo $this->__('Email address'); ?></label></td>
                        <td style="width: 40%;"><input type="text" class="input-text" name="onestepcheckout_email" id="id_onestepcheckout_email" /></td>
                        <td style="width: 45%; text-align: center;"><button id="onestepcheckout-forgot-button" type="button"><?php echo $this->__('Send password'); ?></button></td>
                    </tr>
                </table>
            </form>

            <p style="margin-top: 10px;"><a href="#" id="onestepcheckout-return-login-link"><?php echo $this->__('Return to login'); ?></a></p>

        </div>

        <div id="onestepcheckout-login-popup-contents-login">

        <h1><?php echo $this->__('Login'); ?></h1>

        <p><?php echo $this->__('Please login with your email address and password.'); ?></p>
        <form id="onestepcheckout-login-form">

        <div id="onestepcheckout-login-loading" style="display: none;" class="loading-ajax">&nbsp;</div>
        <div id="onestepcheckout-login-error" class="onestepcheckout-error" style="display: none;">&nbsp;</div>


        <table id="onestepcheckout-login-table">
            <tr>
                <td style="width: 30%;"><label for="id_onestepcheckout_username"><?php echo $this->__('Email address'); ?></label></td>
                <td style="width: 51%;"><input tabindex="100" type="text" class="input-text" name="onestepcheckout_username" id="id_onestepcheckout_username" /></td>
                <td rowspan="2"  style="width: 19%; vertical-align: middle;"><button tabindex="102" id="onestepcheckout-login-button" type="button"><?php echo $this->__('Login'); ?></button></td>
            </tr>
            <tr>
                <td><label for="id_onestepcheckout_password"><?php echo $this->__('Password'); ?></label></td>
                <td><input type="password" tabindex="101" name="onestepcheckout_password" class="input-text" id="id_onestepcheckout_password" /></td>
            </tr>

        </table>
        </form>

        <p style="margin-top: 10px;"><a href="#" id="onestepcheckout-forgot-password-link"><?php echo $this->__('Forgotten your password?'); ?></a></p>



        </div>

        <p class="close"><a href="#"><?php echo $this->__('Close'); ?></a></p>
    </div>

    </div>
    <div class="onestepcheckout-popup-footer">&nbsp;</div>

</div>
<script>
var login_popup;
Event.observe(window, 'load', function() {

    var button = $('onestepcheckout-login-button');
    var loginButtonFunction = function(e) {

        /* Hide form and display loading */
        var table = $('onestepcheckout-login-table');
        var loading = $('onestepcheckout-login-loading');
        var error = $('onestepcheckout-login-error');

        table.hide();
        error.hide();
        loading.show();


        var form = $('onestepcheckout-login-form');
        var url = '<?php echo $this->getUrl('onestepcheckout/ajax/login', array('_secure'=>true)); ?>';
        new Ajax.Request(url, {
            parameters: form.serialize(true),
            method: 'POST',
            onComplete: function(transport) {
                if(transport.status == 200) {
                    var result = transport.responseText.evalJSON();

                    if(!result.success) {
                        loading.hide();

                        error.update(result.error);
                        error.show();

                        table.show();
                    }
                    else    {
                        // Successfully logged in user, now reload page
                        //window.location.reload(true);
                        window.location=window.location;
                    }
                }
            }
        })
    };

    var onkeypressHandler = function(event) {
        if(event.keyCode == Event.KEY_RETURN)  {
           event.preventDefault();
           loginButtonFunction();
        }
    };

    login_popup = new OneStepCheckout_Popup('onestepcheckout-login-popup','onestepcheckout-login-link', 'div#onestepcheckout-login-popup p.close a', function() {
        /* Callback for closing the popup */
        Event.stopObserving(document, 'keypress', onkeypressHandler);
    }, function() {
        /* Callback for opening the popup */
        Event.observe(document, 'keypress', onkeypressHandler);

        /* Reset error messages and state */
        $('onestepcheckout-login-error').hide();
        $('onestepcheckout-forgot-error').hide();
        $('onestepcheckout-login-popup-contents-forgot').hide();
        $('onestepcheckout-login-popup-contents-login').show();


    });

    button.observe('click', loginButtonFunction);

    $('onestepcheckout-forgot-password-link').observe('click', function(e)  {
        Event.stop(e);
        $('onestepcheckout-login-error').hide();
        $('onestepcheckout-login-popup-contents-login').hide();
        $('onestepcheckout-login-popup-contents-forgot').show();

    });

    $('onestepcheckout-return-login-link').observe('click', function(e) {
        Event.stop(e);
        $('onestepcheckout-forgot-error').hide();
        $('onestepcheckout-login-popup-contents-forgot').hide();
        $('onestepcheckout-login-popup-contents-login').show();

    });

    var forgot_password_button = $('onestepcheckout-forgot-button');

    forgot_password_button.observe('click', function(e) {

        var table = $('onestepcheckout-forgot-table');
        var loading = $('onestepcheckout-forgot-loading');
        var error = $('onestepcheckout-forgot-error');
        var success = $('onestepcheckout-forgot-success');
        var email = $('id_onestepcheckout_email').getValue();

        if(email != '') {

            table.hide();
            error.hide();
            loading.show();

            var url = '<?php echo $this->getUrl('onestepcheckout/ajax/forgotPassword', array('_secure'=>true)); ?>';
            var parameters = { email: email };

            new Ajax.Request(url, {
                method: 'post',
                parameters: parameters,
                onSuccess: function(transport)  {
                    var result = transport.responseText.evalJSON();

                    if(result.success)  {
                         loading.hide();
                         success.show();
                    }
                    else    {
                        error.update('<?php echo $this->__('Please enter a registered email address.'); ?>');
                        error.show();
                        table.show();
                        loading.hide();
                    }
                }
            });

        }
        else    {
            alert('<?php echo $this->__('Please enter a valid email address'); ?>');
        }
    });


});
</script>
<?php endif; ?>

<?php if($helper->isValidateEmail()): ?>
<script>
$('billing:email').observe('blur', function(e)    {
    var url = '<?php echo $this->getUrl('onestepcheckout/ajax/check_email', array('_secure'=>true)); ?>';
    var email = e.element().getValue();
    var parameters = { email: email };

    new Ajax.Request(url, {
        parameters: parameters,
        onComplete: function(response)  {
            if(response.status == 200)  {
                var result = response.responseText.evalJSON().result;
                if(result == 'invalid') {
                    $('onestepcheckout-email-error-message').update('<?php echo $this->__('Invalid email address.'); ?>');
                    $('onestepcheckout-email-error').show();
                }
                else if(result == 'exists') {
                    <?php if($this->settings['registration_order_without_password']): ?>
                    // Remove the password fields if the email exists in database
                    $('onestepcheckout-li-password').hide();
                    <?php else: ?>

                    $('onestepcheckout-email-error-message').update('<?php echo $this->__('Email address already registered. Please <a href="#" onclick="login_popup.show(); return false;">login now</a> or use a different email address.'); ?>');
                    $('onestepcheckout-email-error').show();
                    // Ask the user to login or change email address
                    //login_popup.show();
                    $('id_onestepcheckout_username').value = email;
                    <?php endif; ?>
                }
                else    {
                    $('onestepcheckout-email-error').hide();
                    $('onestepcheckout-li-password').show();
                }
            }
        }
    })

});
</script>
<?php endif; ?>





<?php if($this->settings['enable_terms']): ?>
<div id="onestepcheckout-toc-popup" style="display: none;">

    <div class="onestepcheckout-popup-wrapper">
        <div class="onestepcheckout-popup-wrapper-inner">
            <h1><?php echo $this->settings['terms_title']; ?></h1>

            <div class="onestepcheckout-toc-terms">
                <?php echo $this->settings['terms_contents']; ?>
            </div>

            <p class="close"><a href="#"><?php echo $this->__('Close'); ?></a></p>
        </div>
    </div>
    <div class="onestepcheckout-popup-footer">&nbsp;</div>
</div>
<script>
var popup;
Event.observe(window, 'load', function() {
    popup = new OneStepCheckout_Popup('onestepcheckout-toc-popup','onestepcheckout-toc-link', 'div#onestepcheckout-toc-popup p.close a');
});
</script>
<?php endif; ?>





<script>
<?php if($this->hasFormErrors()): ?>
if($$('div.input-error').length > 0) {
    var input = $$('div.input-error')[0].select('input');
    if(input.length == 1)   {
        input[0].focus();
    }
}
<?php endif; ?>
</script>

<?php if(!$this->settings['exclude_region']): ?>
<script type="text/javascript">countryRegions = <?php echo $this->helper('directory')->getRegionJson() ?></script>
<script type="text/javascript">
//<![CDATA[
    var billingRegionUpdater = new RegionUpdater('billing:country_id', 'billing:region', 'billing:region_id', countryRegions);

    <?php if($this->settings['enable_different_shipping'] && !$this->isVirtual()): ?>
    var shippingRegionUpdater = new RegionUpdater('shipping:country_id', 'shipping:region', 'shipping:region_id', countryRegions);
    <?php endif; ?>
//]]>
</script>
<?php endif; ?>


<script type="text/javascript">

document.observe('dom:loaded', function() {
    if ($$('div.shopping-cart-totals').length == 1) {
    }
    else    {

        var already_placing_order = false;

        /* Fix issue with bad centering due to parent */
        $(document.body).insert($('onestepcheckout-login-popup'));
        $(document.body).insert($('onestepcheckout_popup_overlay'));
        $(document.body).insert($('onestepcheckout-toc-popup'));

        /* Handle place order click event */
        $('onestepcheckout-place-order').observe('click', function(e)   {
            Event.stop(e);

           // First validate the form
            var form = new VarienForm('onestepcheckout-form');

            if(!form.validator.validate())  {
                Event.stop(e);
            }
            else    {

                if(!already_placing_order) {

                    already_placing_order = true;

                    var element = new Element('div').
                        addClassName('onestepcheckout-place-order-loading').
                        update('<img src="<?php echo $this->getSkinUrl('images/opc-ajax-loader.gif'); ?>" />&nbsp;&nbsp;<?php echo $this->__('Please wait, processing your order...'); ?>');

                    this.parentNode.appendChild(element);

                    /* Disable button to avoid multiple clicks */
                    e.element().removeClassName('orange').addClassName('grey');
                    var element = e.element();
                    element.disabled = true;

                    /* Submit the form */
                    $('onestepcheckout-form').submit();
                }
            }
        });


        // This is a separate page
        var url = '<?php echo $this->getUrl('onestepcheckout/ajax/set_methods_separate', array('_secure'=>true)); ?>';
        var update_payments = <?php echo $this->settings['enable_update_payment_on_shipping'] ? 'true' : 'false'; ?>;

        $$('dl.shipment-methods input').invoke('observe', 'click', get_separate_save_methods_function(url, update_payments));
        $$('div.payment-methods input[name^=payment\[method\]]').invoke('observe', 'click', get_separate_save_methods_function(url));

        $$('div.payment-methods input[name^=payment\[method\]]').invoke('observe', 'click', function() {
            $$('div.onestepcheckout-payment-method-error').each(function(item) {
                new Effect.Fade(item);
            });
        });

        $$('dl.shipment-methods input').invoke('observe', 'click', function() {
            $$('div.onestepcheckout-shipment-method-error').each(function(item) {
                new Effect.Fade(item);
            });
        });

        var has_hidden_terms = false;

        if($('id_accept_terms') != null)    {

            $('id_accept_terms').observe('click', function(e)   {
                var element = e.element();

                if(element.checked) {
                    $$('div.onestepcheckout-terms-error').each(function(item) {
                        new Effect.Fade(item);
                        has_hidden_terms = true;
                    });
                }
                else    {
                    if(has_hidden_terms)    {
                        $$('div.onestepcheckout-terms-error').each(function(item) {
                            new Effect.Appear(item);
                            has_hidden_terms = false;
                        });
                    }
                }
            });
        }
    }

    var form = $('onestepcheckout-form');

    /* Highlight selected payment method if one set */
    if($RF(form, 'payment[method]') != null)    {
        try {
            var payment_method = $RF(form, 'payment[method]');
            $('container_payment_method_' + payment_method).show();
            $('payment_form_' + payment_method).show();
        } catch(err)    {

        }
    }

    /* Set default shipping method if not set */
    if($RF(form, 'shipping_method') == null)    {
        try {
            var method = '<?php echo $this->_getDefaultShippingMethod(); ?>';
            if(method != '')    {
                $('s_method_' + method).checked = true;
                get_separate_save_methods_function(url)();
            }
        } catch(err)    {

        }
    }

   get_separate_save_methods_function(url)();

<?php if($this->differentShippingAvailable()): ?>
    $('billing:use_for_shipping_yes').observe('click', function(e)  {
        var element = e.element();
        if(element.checked){
            $('shipping_address').hide();
        } else {
            if($('shipping-address-select') && $('shipping-address-select').value == ''){
                $('shipping_address_list').show()
            }
            $('shipping_address').show();
            <?php if(!$this->isCustomerLoggedIn()):?>
            $('shipping_address_list').show()
            <?php endif;?>
            <?php if($this->isCustomerLoggedIn()):?>
            if(!$('shipping-address-select') && $('shipping_address_list').getStyle('display')=='none'){
                $('shipping_address_list').show()
            }
            <?php endif;?>
        }

        <?php if($this->settings['enable_ajax_save_billing']): ?>
        get_save_billing_function('<?php echo $this->getUrl('onestepcheckout/ajax/save_billing', array('_secure'=>true)); ?>', '<?php echo $this->getUrl('onestepcheckout/ajax/set_methods_separate', array('_secure'=>true)); ?>', <?php echo $this->settings['enable_update_payment_on_shipping'] ? 'true' : 'false'; ?>)();
        <?php endif; ?>

    });
    <?php endif; ?>

    <?php if($this->settings['enable_ajax_save_billing']): ?>

    var url_save_billing = '<?php echo $this->getUrl('onestepcheckout/ajax/save_billing', array('_secure'=>true)); ?>';
    var url_set_methods = '<?php echo $this->getUrl('onestepcheckout/ajax/set_methods_separate', array('_secure'=>true)); ?>';
    var update_payments = <?php echo $this->settings['enable_update_payment_on_shipping'] ? 'true' : 'false'; ?>;
    var update_on_initial = false;

    var shipping_address_select = $('shipping-address-select');
    var billing_address_select = $('billing-address-select');
    var billing_region_id = $('billing:region_id');
    var shipping_region_id = $('shipping:region_id');

    <?php if($this->hasAjaxSaveBillingField('country')): ?>

        $('billing:country_id').observe('change', get_save_billing_function(url_save_billing, url_set_methods,update_payments));

        <?php if($this->differentShippingAvailable()): ?>
        $('shipping:country_id').observe('change', get_save_billing_function(url_save_billing, url_set_methods, update_payments));
            <?php if($this->isCustomerLoggedIn()): ?>
            if(shipping_address_select !== null) {
                Event.stopObserving('shipping-address-select', 'change');
                $('shipping-address-select').observe('change', get_save_billing_function(url_save_billing, url_set_methods,update_payments));
            }
            <?php endif; ?>
        <?php endif; ?>
        <?php if($this->isCustomerLoggedIn()): ?>
        if(billing_address_select !== null) {
            Event.stopObserving('billing-address-select', 'change');
            $('billing-address-select').observe('change', get_save_billing_function(url_save_billing, url_set_methods,update_payments));
        }
        <?php endif; ?>
    <?php endif; ?>


    <?php if($this->hasAjaxSaveBillingField('state/region')): ?>
    if(billing_region_id != null){
        $('billing:region_id').observe('change', get_save_billing_function(url_save_billing, url_set_methods,update_payments));
        $('billing:region').observe('change', get_save_billing_function(url_save_billing, url_set_methods,update_payments));
    }

        <?php if($this->differentShippingAvailable()): ?>
        if(shipping_region_id != null){
            $('shipping:region_id').observe('change', get_save_billing_function(url_save_billing, url_set_methods,update_payments));
            $('shipping:region').observe('change', get_save_billing_function(url_save_billing, url_set_methods,update_payments));
        }
            <?php if($this->isCustomerLoggedIn()): ?>
            if(shipping_address_select !== null) {
                Event.stopObserving('shipping-address-select', 'change');
                $('shipping-address-select').observe('change', get_save_billing_function(url_save_billing, url_set_methods,update_payments));
            }
            <?php endif; ?>
        <?php endif; ?>
        <?php if($this->isCustomerLoggedIn()): ?>
        if(billing_address_select !== null) {
            Event.stopObserving('billing-address-select', 'change');
            $('billing-address-select').observe('change', get_save_billing_function(url_save_billing, url_set_methods,update_payments));
        }
        <?php endif; ?>

    <?php endif; ?>



    <?php if($this->hasAjaxSaveBillingField('postcode')): ?>
    $('billing:postcode').observe('change', get_save_billing_function(url_save_billing, url_set_methods,update_payments));

        <?php if($this->differentShippingAvailable()): ?>
        $('shipping:postcode').observe('change', get_save_billing_function(url_save_billing, url_set_methods,update_payments));
            <?php if($this->isCustomerLoggedIn()): ?>
            if(shipping_address_select !== null) {
                Event.stopObserving('shipping-address-select', 'change');
                $('shipping-address-select').observe('change', get_save_billing_function(url_save_billing, url_set_methods,update_payments));
            }
            <?php endif; ?>
        <?php endif; ?>
        <?php if($this->isCustomerLoggedIn()): ?>
        if(billing_address_select !== null) {
            Event.stopObserving('billing-address-select', 'change');
            $('billing-address-select').observe('change', get_save_billing_function(url_save_billing, url_set_methods,update_payments));
        }
        <?php endif; ?>

    <?php endif; ?>

    <?php if($this->hasAjaxSaveBillingField('city')): ?>
    $('billing:city').observe('change', get_save_billing_function(url_save_billing, url_set_methods,update_payments));

        <?php if($this->differentShippingAvailable()): ?>
        $('shipping:city').observe('change', get_save_billing_function(url_save_billing, url_set_methods,update_payments));
            <?php if($this->isCustomerLoggedIn()): ?>
            if(shipping_address_select !== null) {
                Event.stopObserving('shipping-address-select', 'change');
                $('shipping-address-select').observe('change', get_save_billing_function(url_save_billing, url_set_methods,update_payments));
            }
            <?php endif; ?>
        <?php endif; ?>
        <?php if($this->isCustomerLoggedIn()): ?>
        if(billing_address_select !== null) {
            Event.stopObserving('billing-address-select', 'change');
            $('billing-address-select').observe('change', get_save_billing_function(url_save_billing, url_set_methods,update_payments));
        }
        <?php endif; ?>

    <?php endif; ?>


    <?php endif; ?>
});

if($('payment-tool-tip-close')){
    Event.observe($('payment-tool-tip-close'), 'click', toggleToolTip);
}

</script>

<?php endif; ?>

<div id="onestepcheckout_popup_overlay" style="display: none;">&nbsp;</div>


<div id="loading-process" style="display: none;"></div>
