<?php
/**
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the GNU General Public License (GPL 3)
 * that is bundled with this package in the file LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Payone_Core to newer
 * versions in the future. If you wish to customize Payone_Core for your
 * needs please refer to http://www.payone.de for more information.
 *
 * @category        Payone
 * @package         design_frontend_base_default
 * @subpackage      template
 * @copyright       Copyright (c) 2015 <kontakt@fatchip.de> - www.fatchip.com,  Copyright (c) 2017 <support@e3n.de> - www.e3n.de
 * @author          Robert M�ller <robert.mueller@fatchip.de>, Tim Rein <tim.rein@e3n.de>
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
 * @link            http://www.fatchip.com, http://www.e3n.de
 */

/** @var $this Payone_Core_Block_Payment_Method_Form_Payolution */
$code = $this->getMethodCode();
$configId = $this->getPaymentConfig()->getId();
$getPayolutionTyp = $this->payolutionTypeMapping();

?>
<fieldset class="form-list">
    <ul id="payment_form_<?php echo $code ?>" style="display:none">
        <li class="fields">
            <div class="field">
                <input type="hidden" id="<?php echo $code ?>_type_select" name="payment[payone_payolution_type]"
                       value="<?php echo $getPayolutionTyp[$code] ?>">
            </div>
            <div id="<?php echo $code ?>_main_block">
                <div class="field" id="<?php echo $code; ?>_installment_wrap2"></div>
                <?php if ($this->isB2BMode() && $getPayolutionTyp[$code] === Payone_Api_Enum_PayolutionType::PYV) { ?>
                    <div id="<?php echo $code; ?>_b2b_wrap" class="field">
                        <div class="input-box">
                            <label
                                for="<?php echo $code ?>_vat_id"><?php echo $this->__('VAT ID number') ?>
                            </label>
                            <br class="clear"/>
                            <input type="text" id="<?php echo $code ?>_vat_id"
                                   name="payment[payone_vat_id]"
                                   value="" title="" class="input-text no-submit"/>
                        </div>
                    </div>
                <?php } ?>
                <?php if ($this->isTelephoneRequired()) : ?>
                    <div id="<?php echo $code; ?>_telephone_wrap" class="field">
                        <div class="input-box">
                            <label for="<?php echo $code ?>_additional_fields_customer_telephone" class="required">
                                <em>*</em><?php echo $this->__('Telephone') ?>
                            </label>
                            <br class="clear"/>
                            <input type="text" name="payment[payone_customer_telephone]"
                                   id="<?php echo $code ?>_additional_fields_customer_telephone"
                                   class="input-text required-entry"/>
                        </div>
                    </div>
                <?php endif;?>
                <?php if ($this->showBirthdayFields($getPayolutionTyp[$code])) { ?>
                    <div id="<?php echo $code; ?>_birthday_wrap" class="field">
                        <label for="<?php echo $code ?>_additional_fields_customer_dob_month"
                               class="required"><em>*</em><?php echo $this->__('Date of birth (DD MM YYYY)') ?>
                        </label>
                        <br class="clear"/>
                        <select id="<?php echo $code ?>_additional_fields_customer_dob_day" class="required-entry"
                                style="width: 60px;" onchange="payoneCustomerDobInput('<?php echo $code ?>')">
                            <option value="">--</option>
                            <?php for ($x = 1; $x <= 31; $x++): ?>
                                <option value="<?php echo sprintf('%02d', $x); ?>">
                                    <?php echo sprintf('%02d', $x); ?>
                                </option>
                            <?php endfor; ?>
                        </select>
                        <select id="<?php echo $code ?>_additional_fields_customer_dob_month" class="required-entry"
                                style="width: 60px;" onchange="payoneCustomerDobInput('<?php echo $code ?>')">
                            <option value="">--</option>
                            <?php for ($x = 1; $x <= 12; $x++) : ?>
                                <option value="<?php echo sprintf('%02d', $x); ?>">
                                    <?php echo sprintf('%02d', $x); ?>
                                </option>
                            <?php endfor; ?>
                        </select>
                        <select id="<?php echo $code ?>_additional_fields_customer_dob_year" class="required-entry"
                                style="width: 60px;" onchange="payoneCustomerDobInput('<?php echo $code ?>')">
                            <option value="">----</option>
                            <?php for ($x = date('Y'); $x > (date('Y') - 100); $x--) : ?>
                                <option value="<?php echo $x; ?>">
                                    <?php echo $x; ?>
                                </option>
                            <?php endfor; ?>
                        </select>

                        <input type="hidden" id="<?php echo $code ?>_additional_fields_customer_dob_full"
                               name="payment[payone_customer_dob]" class="validate-18-years">
                    </div>
                <?php } ?>
                <div id="<?php echo $code; ?>_debit_wrap" class="field" style="display:none;">
                    <?php if ($this->isSepaDataRequired()) { ?>
                        <div id="<?php echo $code; ?>_debit_subwrap" style="display:none;"><br>
                            <strong><?php echo $this->__('Account Information'); ?></strong><br>
                            <div class="input-box">
                                <label for="<?php echo $code ?>_payolution_accountholder">
                                    <?php echo $this->__('Accountholder') ?><span class="required">*</span>
                                </label>
                                <br class="clear"/>
                                <input type="text" id="<?php echo $code ?>_payolution_accountholder"
                                       title="<?php echo $this->__('Accountholder') ?>"
                                       class="required-entry input-text"
                                       value=""/>
                            </div>
                        </div>
                        <div class="input-box">
                            <label for="<?php echo $code ?>_sepa_iban_xxx">
                                <?php echo $this->__('IBAN') ?><span class="required">*</span>
                            </label>
                            <br class="clear"/>
                            <input type="text" id="<?php echo $code ?>_sepa_iban_xxx"
                                   title="<?php echo $this->__('IBAN') ?>"
                                   class="validate-sepa-iban required-entry input-text"
                                   value=""
                                   autocomplete="off" maxlength="34"
                                   onchange="inputToUppaerCaseAndNumbers(this); payonePayolutionCopyDebitPaymentSepaIban('<?php echo $code ?>');"
                                   oninput="inputToUppaerCaseAndNumbers(this); payonePayolutionCopyDebitPaymentSepaIban('<?php echo $code ?>');"/>
                            <input type="hidden" id="<?php echo $code ?>_sepa_iban" name="payment[payone_payolution_iban]"
                                   value="">
                        </div>
                        <div class="input-box">
                            <label for="<?php echo $code ?>_payolution_bic">
                                <?php echo $this->__('BIC') ?><span class="required">*</span>
                            </label>
                            <br class="clear"/>
                            <input type="text" id="<?php echo $code ?>_payolution_bic" name="payment[payone_payolution_bic]"
                                   title="<?php echo $this->__('BIC') ?>"
                                   class="validate-sepa-bic required-entry input-text"
                                   value=""
                                   autocomplete="off" maxlength="11"
                                   onchange="inputToUppaerCaseAndNumbers(this);"
                                   oninput="inputToUppaerCaseAndNumbers(this);"/>
                        </div>
                    <?php } else { ?>
                        <span id="<?php echo $code; ?>_debit_subwrap"></span>
                    <?php } ?>
                </div>
                <div id="<?php echo $code; ?>_acceptance_wrap" class="field" style="position:relative;">
                    <input type="checkbox" id="<?php echo $code ?>_additional_fields_agreement_checkbox"
                           class="checkbox required-entry"/>
                    <label for="<?php echo $code ?>_additional_fields_agreement_checkbox" class="required"
                           style="float:none;">
                        <?php echo $this->__('PAYOLUTION_ACCEPTANCE_1'); ?><br>
                        <?php echo $this->__('PAYOLUTION_ACCEPTANCE_2'); ?>
                        <a href="#" style="float:none; margin:0;"
                           onclick="displayPayolutionOverlay('<?php echo $code ?>');return false;"><?php echo $this->__('acceptance'); ?></a>
                        <?php echo $this->__('PAYOLUTION_ACCEPTANCE_3'); ?>
                        <em style="float:none; position:relative;">*</em>
                    </label>
                    <input type="hidden" name="payment[payone_config_payment_method_id]"
                           value="<?php echo $configId; ?>" id="<?php echo $code ?>_payment_method_id"/>
                    <input type="hidden" name="payment[payone_isb2b]" value="<?php echo (int)$this->isB2BMode(); ?>"/>
                    <input type="hidden" name="payment[payone_payolution_installment_duration]" value=""
                           id="<?php echo $code ?>_selected_installmentplan"/>

                    <div id="<?php echo $code ?>_overlay" style="display:none;" class="payolution_overlay">
                        <a href="#" onclick="removePayolutionOverlay('<?php echo $code ?>');return false;"
                           style="float:right;font-weight:bold;"><?php echo $this->__('Close window'); ?></a><br><br>
                        <?php echo $this->getPayolutionAcceptanceText(); ?>
                    </div>
                    <div id="<?php echo $code; ?>_debit_wrap2" class="field" style="display:none;">
                        <input type="checkbox" id="<?php echo $code ?>_payolution_sepa_mandate" class="checkbox required-entry"/>
                        <label for="<?php echo $code ?>_payolution_sepa_mandate" class="required" style="float:none;">
                            <?php echo $this->__('Hereby I grant the'); ?>
                            <a href="https://payment.payolution.com/payolution-payment/infoport/sepa/mandate.pdf" target="_blank" style="float:none; margin:0;"><?php echo $this->__('SEPA-Mandate'); ?></a>.
                            <em style="float:none; position:relative;">*</em>
                        </label>
                    </div>
                    <div id="<?php echo $code; ?>_installment_wrap1" class="field">
                        <br><button
                            onclick="handleInstallment('<?php echo $code ?>', '<?php echo $this->getHandleInstallmentUrl(); ?>');"
                            class="button" type="button">
                        <span>
                            <span><?php echo $this->__('Check installment availability'); ?></span>
                        </span>
                        </button>
                    </div>
                </div>
            </div>
        </li>
    </ul>
</fieldset>

<script type="text/javascript">
    var code = '<?php echo $code ?>';
    payoneSwitchPayolution($(code + '_type_select'), code);

    Event.observe(document, "dom:loaded", function () {
        payoneSwitchPayolution($(code + '_type_select'), code);
    });
    Event.observe(document, "dom:ready", function () {
        payoneSwitchPayolution($(code + '_type_select'), code);
    });
    // we need to call the switch method after refreshing payment section via ajax
    // unfortunately there is no specific property to identify the needed ajax request
    Ajax.Responders.register({
        onComplete: function (transport, element) {

            var typeSelect = $(code + '_type_select');

            if (typeSelect == undefined) {
                return;
            }
            var url = element.request.url;

            if (url.indexOf('checkout/onepage/saveShippingMethod') !== -1 || url.indexOf('checkout/onepage/progress') !== 1) {
                payoneSwitchPayolution(typeSelect, code);
            }
        }
    });
    //    Add validation messages translation
    Translator.add('You have to be at least 18 years old to use this payment type!', '<?php echo $this->__('You have to be at least 18 years old to use this payment type!'); ?>');
    Translator.add('The installment calculation failed. Please choose another payment type.', '<?php echo $this->__('The installment calculation failed. Please choose another payment type.'); ?>');
</script>
