<?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="">
<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']; ?></h1>
<?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-two-columns checkoutcontainer onestepcheckout-skin-<?php echo $this->settings['skin']; ?>">

    <div class="onestepcheckout-column-left">
        <p class="onestepcheckout-numbers onestepcheckout-numbers-<?php echo $step_counter++; ?>"><?php echo $this->__('Name and 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; ?>

            <ul>
                <li>
                    <div class="input-box input-firstname<?php if(in_array('firstname', $this->formErrors['billing_errors'])) { echo ' input-error'; } ?>">
                        <label for="billing:firstname"><?php echo $this->__('First Name') ?> <span class="required">*</span></label><br />
                        <input class="required-entry input-text" type="text" name="billing[firstname]" id="billing:firstname" value="<?php echo $this->getFirstname(); ?>" />
                    </div>
                    <div class="input-box input-lastname<?php if(in_array('lastname', $this->formErrors['billing_errors'])) { echo ' input-error'; } ?>">
                        <label for="billing:lastname"><?php echo $this->__('Last Name') ?> <span class="required">*</span></label><br />
                        <input class="required-entry input-text" type="text" name="billing[lastname]" id="billing:lastname" value="<?php echo $this->getLastname(); ?>" />
                    </div>
                </li>
                <li>
                    <?php if(!$this->isCustomerLoggedIn()): ?>
                    <div class="input-box input-email<?php if(in_array('email', $this->formErrors['billing_errors'])) { echo ' input-error'; } ?>">
                        <label for="billing:email"><?php echo $this->__('Email Address') ?> <span class="required">*</span></label><br />
                        <input type="text" name="billing[email]" id="billing:email" value="<?php echo $this->htmlEscape($this->getAddress()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="validate-email required-entry input-text" />
                    </div>
                    <?php endif ?>

                    <?php if(!$this->settings['exclude_telephone']):?>
                    <div class="input-box input-telephone<?php if(in_array('telephone', $this->formErrors['billing_errors'])) { echo ' input-error'; } ?>">
                        <label for="billing:telephone"><?php echo $this->__('Telephone') ?> <span class="required">*</span></label><br/>
                        <input type="text" name="billing[telephone]" value="<?php echo $this->htmlEscape($this->getTelephone()) ?>" title="<?php echo $this->__('Telephone') ?>" class="required-entry input-text" id="billing:telephone" />
                    </div>
                    <?php endif; ?>
                </li>

                <?php if(!$this->isCustomerLoggedIn() || in_array('email_registered', $this->formErrors['billing_errors'])): ?>
                <li id="onestepcheckout-email-error" <?php if(!in_array('email_registered', $this->formErrors['billing_errors'])): ?>style="display: none"<?php endif; ?>>
                    <div id="onestepcheckout-email-error-message" class="onestepcheckout-error">
                        <?php if(in_array('email_registered', $this->formErrors['billing_errors'])): ?>
                            <?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.'); ?>
                        <?php else: ?>
                            <?php echo $this->__('Invalid email address.'); ?>
                        <?php endif; ?>
                    </div>
                </li>
                <?php endif; ?>


                <li>
                    <div class="input-box input-address<?php if(in_array('address', $this->formErrors['billing_errors'])) { echo ' input-error'; } ?>">
                        <label for="billing:street1"><?php echo $this->__('Address'); ?> <span class="required">*</span></label><br />
                        <?php for ($_i=1, $_n=$this->helper('customer/address')->getStreetLines(); $_i<=$_n; $_i++): ?>
                            <input type="text" title="<?php echo $this->__('Street Address '.$_i) ?>" name="billing[street][]" id="billing:street<?php echo $_i?>" value="<?php echo $this->htmlEscape($this->getStreet($_i)) ?>" class="<?php if($_i == 1): ?>required-entry <?php endif; ?>input-text onestepcheckout-address-line" />
                            <?php if($_i != $this->helper('customer/address')->getStreetLines()): ?><br/><?php endif; ?>
                        <?php endfor; ?>

                    </div>
                </li>

                <li>
                    <div class="input-box input-country<?php if(in_array('country', $this->formErrors['billing_errors'])) { echo ' input-error'; } ?>">
                        <label for="billing:country_id"><?php echo $this->__('Country') ?> <span class="required">*</span></label><br />
                            <?php echo $this->getCountryHtmlSelect('billing') ?>
                    </div>
                </li>

                <?php if(!$this->settings['exclude_city']):?>
                <li>
                    <div class="input-box input-city<?php if(in_array('city', $this->formErrors['billing_errors'])) { echo ' input-error'; } ?>">
                        <label for="billing:city"><?php echo $this->__('City') ?> <span class="required">*</span></label><br/>
                        <input type="text" name="billing[city]" value="<?php echo $this->htmlEscape($this->getCity()) ?>" title="<?php echo $this->__('City') ?>" class="required-entry input-text" id="billing:city" />
                    </div>
                </li>
                <?php endif; ?>


                <?php if(!($this->settings['exclude_zip'] && $this->settings['exclude_region'])): ?>
                <li>
                    <?php if(!$this->settings['exclude_zip']): ?>
                    <div class="input-box input-postcode<?php if(in_array('postcode', $this->formErrors['billing_errors'])) { echo ' input-error'; } ?>" >
                        <label for="billing:postcode"><?php echo $this->__('Zip/Postal Code') ?> <span class="required">*</span></label><br />
                        <input type="text" title="<?php echo $this->__('Zip/Postal Code') ?>" name="billing[postcode]" id="billing:postcode" value="<?php echo $this->htmlEscape($this->getPostcode()) ?>" class="validate-zip-international required-entry input-text" />
                    </div>
                    <?php endif; ?>


                    <?php if(!$this->settings['exclude_region']): ?>
                    <div class="input-box input-region<?php if(in_array('region', $this->formErrors['billing_errors'])) { echo ' input-error'; } ?>">
                        <label for="billing:region"><?php echo $this->__('State/Province') ?> <span class="required">*</span></label><br/>
                        <select id="billing:region_id" name="billing[region_id]" title="<?php echo $this->__('State/Province') ?>" class="validate-select" style="display:none">
                            <option value=""><?php echo $this->__('Please select region, state or province') ?></option>
                        </select>
                        <script type="text/javascript">
                            $('billing:region_id').setAttribute('defaultValue',  "<?php echo $this->getRegionId() ?>");
                        </script>
                        <input type="text" id="billing:region" name="billing[region]" value="<?php echo $this->htmlEscape($this->getRegion()) ?>"  title="<?php echo $this->__('State/Province') ?>" class="input-text" style="display:none" />
                    </div>
                    <?php endif; ?>
                </li>
                <?php endif; ?>



                <?php if(!($this->settings['exclude_company'] && $this->settings['exclude_fax'])): ?>
                <li>
                    <?php if(!$this->settings['exclude_company']): ?>
                    <div class="input-box input-company<?php if(in_array('company', $this->formErrors['billing_errors'])) { echo ' input-error'; } ?>">
                        <label for="billing:company"><?php echo $this->__('Company') ?></label><br/>
                        <input type="text" name="billing[company]" value="<?php echo $this->htmlEscape($this->getCompany()) ?>" title="<?php echo $this->__('Company') ?>" class="input-text" id="billing:company" />
                    </div>
                    <?php endif; ?>

                    <?php if(!$this->settings['exclude_fax']): ?>
                    <div class="input-box input-fax<?php if(in_array('fax', $this->formErrors['billing_errors'])) { echo ' input-error'; } ?>">
                        <label for="billing:fax"><?php echo $this->__('Fax') ?></label><br/>
                        <input type="text" name="billing[fax]" value="<?php echo $this->htmlEscape($this->getFax()) ?>" title="<?php echo $this->__('Fax') ?>" class="input-text" id="billing:fax" />
                    </div>
                    <?php endif; ?>
                </li>
                <?php endif; ?>

                <?php $_taxvat = $this->getLayout()->createBlock('customer/widget_taxvat'); ?>
                <?php if($_taxvat->isEnabled()): ?>
                <li class="taxvat">
                        <?php echo $_taxvat->setTaxvat($this->getQuote()->getCustomerTaxvat())
                            ->setFieldIdFormat('billing:%s')->setFieldNameFormat('billing[%s]')->toHtml() ?>
                </li>
                <?php endif; ?>


                <?php if(!$this->isCustomerLoggedIn() && $helper->showCreateAccount()): ?>
                <li>
                    <div class="input-box">
                        <input id="id_create_account" type="checkbox" name="create_account" value="1" <?php if(isset($_POST['create_account']) && $_POST['create_account'] == '1') { echo ' checked="checked" ';  } ?> />
                        <label for="id_create_account"><?php echo $this->__('Create an account for later use'); ?></label>
                    </div>

                    <script>
                    document.observe("dom:loaded", function() {
                        $('id_create_account').observe('click', function(e) {
                            var element = e.element();
                            if(element.checked)    {
                                $('onestepcheckout-li-password').show();
                            }
                            else    {
                                $('onestepcheckout-li-password').hide();
                            }
                        });
                    });
                    </script>
                </li>
                <?php endif; ?>

                <?php if($helper->showPasswords() && !$this->isCustomerLoggedIn()): ?>
                <li id="onestepcheckout-li-password" <?php if($helper->hidePasswords()): ?>style="display: none;"<?php endif; ?>>
                    <div class="input-box input-password<?php if(in_array('password', $this->formErrors['billing_errors'])) { echo ' input-error'; } ?>">
                        <label for="billing:customer_password"><?php echo $this->__('Password') ?></label><br/>
                        <input type="password" class="required-entry validate-password input-text" value="<?php if(isset($_POST['billing']['customer_password'])) { echo $_POST['billing']['customer_password']; }; ?>" title="Password" id="billing:customer_password" name="billing[customer_password]"/>
                    </div>
                    <div class="input-box input-password<?php if(in_array('confirm_password', $this->formErrors['billing_errors'])) { echo ' input-error'; } ?>">
                        <label for="billing:confirm_password"><?php echo $this->__('Confirm password') ?></label><br/>
                        <input type="password" class="required-entry validate-password input-text" value="<?php if(isset($_POST['billing']['confirm_password'])) { echo $_POST['billing']['confirm_password']; }; ?>" id="billing:confirm_password" title="Confirm Password" name="billing[confirm_password]"/>
                    </div>
                </li>
                <?php endif; ?>

                <?php if($this->differentShippingAvailable()): ?>
                <li>
                    <div class="input-box input-different-shipping">


                            <input type="checkbox" name="billing[use_for_shipping]" id="billing:use_for_shipping_yes" value="1" <?php if($this->sameAsBilling()) { echo ' checked="checked" '; } ?>  />

                            <label for="billing:use_for_shipping_yes"><?php echo $this->__('Ship to the same address'); ?></label>




                    </div>
                </li>
                <?php else: ?>
                <li>
                    <input type="hidden" name="billing[use_for_shipping]" id="billing:use_for_shipping_yes" value="1" />
                </li>
                <?php endif; ?>
                <li>
                    &nbsp;
                </li>
            </ul>

            <input type="hidden" name="billing[address_id]" value="<?php echo $this->getQuote()->getBillingAddress()->getId() ?>" id="billing:address_id" />


            <?php if($this->differentShippingAvailable()): ?>

            <ul id="shipping_address_list" <?php if($this->sameAsBilling()) { echo '  style="display: none;" '; } ?>>

                    <!-- START LIST OF SHIPPING FIELDS -->
                    <li class="shipping-address-title">
                        <?php echo $this->__('Shipping address'); ?>
                    </li>

                    <li>
                        <div class="input-box input-firstname<?php if(in_array('firstname', $this->formErrors['shipping_errors'])) { echo ' input-error'; } ?>">
                            <label for="shipping:firstname"><?php echo $this->__('First Name') ?> <span class="required">*</span></label><br />
                            <input class="required-entry input-text" type="text" name="shipping[firstname]" id="shipping:firstname" value="<?php echo $this->getShippingFirstname(); ?>" />
                        </div>
                        <div class="input-box input-lastname<?php if(in_array('lastname', $this->formErrors['shipping_errors'])) { echo ' input-error'; } ?>">
                            <label for="shipping:lastname"><?php echo $this->__('Last Name') ?> <span class="required">*</span></label><br />
                            <input class="required-entry input-text" type="text" name="shipping[lastname]" id="shipping:lastname" value="<?php echo $this->getShippingLastname(); ?>" />
                        </div>
                    </li>
                    <li>
                        <div class="input-box input-address<?php if(in_array('address', $this->formErrors['shipping_errors'])) { echo ' input-error'; } ?>">
                            <label for="shipping:street1"><?php echo $this->__('Address'); ?> <span class="required">*</span></label><br />

                            <?php for ($_i=1, $_n=$this->helper('customer/address')->getStreetLines(); $_i<=$_n; $_i++): ?>
                                    <input type="text" title="<?php echo $this->__('Street Address '.$_i) ?>" name="shipping[street][]" id="shipping:street<?php echo $_i?>" value="<?php echo $this->htmlEscape($this->getShippingStreet($_i)) ?>" class="input-text onestepcheckout-address-line" />
                            <?php endfor ?>



                        </div>
                    </li>

                    <li>
                        <div class="input-box input-country<?php if(in_array('country', $this->formErrors['shipping_errors'])) { echo ' input-error'; } ?>">
                            <label for="shipping:country_id"><?php echo $this->__('Country') ?> <span class="required">*</span></label><br />
                                <?php echo $this->getCountryHtmlSelect('shipping') ?>
                        </div>
                    </li>

                    <?php if(!$this->settings['exclude_city']):?>
                    <li>
                        <div class="input-box input-city<?php if(in_array('city', $this->formErrors['shipping_errors'])) { echo ' input-error'; } ?>">
                            <label for="shipping:city"><?php echo $this->__('City') ?> <span class="required">*</span></label><br/>
                            <input type="text" name="shipping[city]" value="<?php echo $this->htmlEscape($this->getShippingCity()) ?>" title="<?php echo $this->__('City') ?>" class="required-entry input-text" id="shipping:city" />
                        </div>
                    </li>
                    <?php endif; ?>


                    <?php if(!($this->settings['exclude_zip'] && $this->settings['exclude_region'])): ?>
                    <li>
                        <?php if(!$this->settings['exclude_zip']): ?>
                        <div class="input-box input-postcode<?php if(in_array('postcode', $this->formErrors['shipping_errors'])) { echo ' input-error'; } ?>" >
                            <label for="shipping:postcode"><?php echo $this->__('Zip/Postal Code') ?> <span class="required">*</span></label><br />
                            <input type="text" title="<?php echo $this->__('Zip/Postal Code') ?>" name="shipping[postcode]" id="shipping:postcode" value="<?php echo $this->htmlEscape($this->getShippingPostcode()) ?>" class="validate-zip-international required-entry input-text" />
                        </div>
                        <?php endif; ?>


                        <?php if(!$this->settings['exclude_region']): ?>
                        <div class="input-box input-region">
                            <label for="shipping:region"><?php echo $this->__('State/Province') ?> <span class="required">*</span></label><br/>
                            <select id="shipping:region_id" name="shipping[region_id]" title="<?php echo $this->__('State/Province') ?>" class="validate-select" style="display:none">
                                <option value=""><?php echo $this->__('Please select region, state or province') ?></option>
                            </select>
                            <script type="text/javascript">
                                $('shipping:region_id').setAttribute('defaultValue',  "<?php echo $this->getShippingRegionId() ?>");
                            </script>
                            <input type="text" id="shipping:region" name="shipping[region]" value="<?php echo $this->htmlEscape($this->getShippingRegion()) ?>"  title="<?php echo $this->__('State/Province') ?>" class="input-text" style="display:none" />
                        </div>
                        <?php endif; ?>
                    </li>
                    <?php endif; ?>

                    <li>
                        <?php if(!$this->settings['exclude_telephone']):?>
                        <div class="input-box input-telephone<?php if(in_array('telephone', $this->formErrors['shipping_errors'])) { echo ' input-error'; } ?>">
                            <label for="shipping:telephone"><?php echo $this->__('Telephone') ?> <span class="required">*</span></label><br/>
                            <input type="text" name="shipping[telephone]" value="<?php echo $this->htmlEscape($this->getShippingTelephone()) ?>" title="<?php echo $this->__('Telephone') ?>" class="required-entry input-text" id="shipping:telephone" />
                        </div>
                        <?php endif; ?>

                    </li>


                    <?php if(!($this->settings['exclude_company'] && $this->settings['exclude_fax'])): ?>
                    <li>
                        <?php if(!$this->settings['exclude_company']): ?>
                        <div class="input-box input-company<?php if(in_array('company', $this->formErrors['shipping_errors'])) { echo ' input-error'; } ?>">
                            <label for="shipping:company"><?php echo $this->__('Company') ?></label><br/>
                            <input type="text" name="shipping[company]" value="<?php echo $this->htmlEscape($this->getShippingCompany()) ?>" title="<?php echo $this->__('Company') ?>" class="input-text" id="shipping:company" />
                        </div>
                        <?php endif; ?>

                        <?php if(!$this->settings['exclude_fax']): ?>
                        <div class="input-box input-fax<?php if(in_array('fax', $this->formErrors['shipping_errors'])) { echo ' input-error'; } ?>">
                            <label for="shipping:fax"><?php echo $this->__('Fax') ?></label><br/>
                            <input type="text" name="shipping[fax]" value="<?php echo $this->htmlEscape($this->getShippingFax()) ?>" title="<?php echo $this->__('Fax') ?>" class="input-text" id="shipping:fax" />
                        </div>
                        <?php endif; ?>
                    </li>
                    <?php endif; ?>


                    <input type="hidden" name="shipping[address_id]" value="<?php echo $this->getQuote()->getShippingAddress()->getId() ?>" id="shipping:address_id" />

                    <!-- END LIST OF SHIPPIING FIELDS -->



            </ul>
            <?php endif; ?>

    </div>

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


        <?php if(!$this->isVirtual()): ?>
            <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; ?>

        <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>

        <div style="margin-bottom: 15px">&nbsp;</div>

        <?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 class="onestepcheckout-giftmessagecontainer">
            <?php echo $this->helper('onestepcheckout/message')->getInline('onepage_checkout', $this->getQuote(), $this->getDontDisplayContainer()) ?>
            </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" checked="checked" />
                <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; ?>

        <div class="onestepcheckout-place-order">
            <input id="onestepcheckout-place-order" type="image" src="<?php echo $this->getSkinUrl('onestepcheckout/images/onestepcheckout-place-order.png') ?>" value="<?php echo $this->__('Place Order') ?>" />
        </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" checked="checked" />
                <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() {
    login_popup = new OneStepCheckout_Popup('onestepcheckout-login-popup','onestepcheckout-login-link', 'div#onestepcheckout-login-popup p.close a');

    $('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'); ?>');
        }
    });


    var button = $('onestepcheckout-login-button');

    button.observe('click', 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;
                    }
                }
            }
        })
    });

});
</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">
function dummy_shipping()
{
    this.variable = 'dummy';
}

dummy_shipping.prototype.setSameAsBilling = function(param)
{
}

var shipping = new dummy_shipping();

document.observe('dom:loaded', function() {


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


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

            if(!form.validator.validate())    {
                Event.stop(e);
            }
            else    {
                /* Disable button to avoid multiple clicks */
                var element = e.element();
                element.disabled = true;

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

        var url = '<?php echo $this->getUrl('onestepcheckout/ajax/set_methods', 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_save_methods_function(url, update_payments));
        $$('div.payment-methods input[name^=payment\[method\]]').invoke('observe', 'click', get_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_save_methods_function(url)();
            }
        } catch(err)    {

        }
    }

    get_save_methods_function(url)();

    <?php if($this->differentShippingAvailable()): ?>

    $('billing:use_for_shipping_yes').observe('click', function(e)    {
        var element = e.element();
        if(element.getValue() == "1")    {
            $('shipping_address_list').hide();
        }
        else    {
            $('shipping_address_list').show();
        }

        <?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', 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)); ?>?original_totals=1';
    var url_set_methods = '<?php echo $this->getUrl('onestepcheckout/ajax/set_methods', array('_secure'=>true)); ?>';
    var update_payments = <?php echo $this->settings['enable_update_payment_on_shipping'] ? 'true' : 'false'; ?>;

    <?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 endif; ?>
    <?php endif; ?>


    <?php if($this->hasAjaxSaveBillingField('state/region')): ?>
    $('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()): ?>
        $('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 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 endif; ?>

    <?php endif; ?>


    <?php endif; ?>



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

    $$('.cvv-what-is-this').each(function(element){
        Event.observe(element, 'click', toggleToolTip);
    });


});

function toggleToolTip(event){
    if($('payment-tool-tip')){
        $('payment-tool-tip').setStyle({
            left: (Event.pointerX(event)-100)+'px',
            top: (Event.pointerY(event)-300)+'px'
        })
        $('payment-tool-tip').toggle();
    }
    Event.stop(event);
}



</script>
<?php endif; ?>

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