{% extends request.ajax ? "layout_blank.phtml" : "layout_default.phtml" %}

{% import "macro_functions.phtml" as mf %}

{% block meta_title %}{{ 'Register new domain'|trans }}{% endblock %}

{% block content_before %}
    {% include "partial_steps.phtml" with {'selected': 2} %}
{% endblock %}

{% block content %}
    {% set periods = guest.system_periods %}
    {% set pricing = product.pricing %}

<form method="post" action="" class="api_form" data-api-url="guest/cart/add_item" data-api-redirect="{{ 'cart'|link }}">
    <div class="h-block">
        <div class="h-block-header">
            <div class="icon"><span class="number">2</span></div>
            <h2>{{ 'Configure'|trans }}</h2>
            <p>{{ 'The product/service you have chosen has the following configuration options for you to choose from.'|trans }}</p>
        </div>
        <div class="block conversation">
    
            <div class="widget simpleTabs tabsRight first">
                <div class="head">
                    {#<h2 class="dark-icon i-services">{{ 'Domain configuration'|trans }}</h2>#}
                </div>

                <ul class="tabs">
                    <li><a href="#register" rel="register"><span class="dark-icon"></span>{{ 'Register new domain'|trans }}</a></li>
                    <li><a href="#transfer" rel="transfer"><span class="dark-icon"></span>{{ 'Transfer my domain'|trans }}</a></li>
                </ul>

                <div class="tabs_container" style="margin-top: 20px">

                    <div class="tab_content" id="register">

                        <fieldset>
                            <legend>{{ 'Check domain availability'|trans }}</legend>
                            <p>
                                <input type="text" name="register_sld" value="{{ request.register_sld }}" style="width: 583px" placeholder="{{ 'Enter new domain name to register'|trans }}">
                                {{ mf.selectboxtld('register_tld', guest.serviceDomain_tlds({ "allow_register": 1 })) }}
                            </p>
                            <button class="bb-button bb-button-big" id="domain-check" style="float: right;">{{ 'Check'|trans }}</button>
                            <button class="bb-button bb-button-submit bb-button-big order-button" type="submit" style="float: right; display: none;">{% if product.addons|length > 0 %}{{ 'Continue'|trans }}{% else %}{{ 'Order now'|trans }}{% endif %}</button>

                            <div class="onAfterDomainCheck" style="display:none;">
                                <label>
                                    <select name="register_years" id="registration-years"></select>
                                </label>
                                <br/>
                                <label><input type="checkbox" onclick="$('#nameservers').slideToggle();">{{ 'I want to use my nameservers'|trans }}</label>
                                <div id="nameservers" style="display:none; margin: 10px 0;">
                                    <p><input type="text" name="ns1" value="" placeholder="{{ 'Nameserver 1'|trans }}"></p>
                                    <p><input type="text" name="ns2" value="" placeholder="{{ 'Nameserver 2'|trans }}"></p>
                                    <p><input type="text" name="ns3" value="" placeholder="{{ 'Nameserver 3'|trans }}"></p>
                                    <p><input type="text" name="ns4" value="" placeholder="{{ 'Nameserver 4'|trans }}"></p>
                                </div>

                            </div>

                        </fieldset>
                    </div>

                    <div class="tab_content" id="transfer">
                        <fieldset>
                            <p>
                                <input type="text" name="transfer_sld" value="{{ request.transfer_sld }}"  style="width: 583px" placeholder="{{ 'Enter your domain name to transfer'|trans }}">
                                {{ mf.selectboxtld('transfer_tld', guest.serviceDomain_tlds({ "allow_transfer": 1 })) }}
                            </p>

                            <button class="bb-button bb-button-big" type="button" id="transfer-check" style="float: right;">{{ 'Check'|trans }}</button>
                            <button class="bb-button bb-button-submit bb-button-big order-button" type="submit" style="float: right; display: none;">{% if product.addons|length > 0 %}{{ 'Continue'|trans }}{% else %}{{ 'Order now'|trans }}{% endif %}</button>

                            <div id="domain-transfer-config" style="display:none;">
                                <p>{{ 'Transfer price'|trans }}: <span id="transfer-price"></span></p>
                                <input type="text" name="transfer_code" value="{{ request.transfer_code }}" style="width: 200px" placeholder="{{ 'Enter domain transfer code'|trans }}">
                            </div>
                        </fieldset>
                    </div>
                </div>
                <div class="clear"></div>
            </div>
        </div>
        <div class="clear"></div>
    </div>
    <div class="clear"></div>
    {% include 'partial_addons.phtml' with { 'product': product } %}
    
    <input type="hidden" name="id" value="{{ product.id }}" />
    <input type="hidden" name="action" value="register" id="domain-action"/>
</form>
{% endblock %}

{% block sidebar2 %}
    {% include 'partial_currency.phtml' %}
{% endblock %}

{% block js %}
{% set currency = guest.cart_get_currency %}
<script type="text/javascript">
$(function() {
    $('ul.tabs li a').bind('click', function() {
        $('#domain-action').val($(this).attr('rel'));

        $(this).parents('ul').find('span.dark-icon')
            .removeClass('i-check i-uncheck')
            .addClass('i-uncheck');

        $(this).find('span')
            .removeClass('i-uncheck')
            .addClass('i-check');
    });

    $('ul.tabs li a:first').click();

    if($(".addons").length) {
        $('.order-button').one('click', function() {
            $(this).hide();
            $('.addons').slideDown('fast');

            return false;
        });
    }

    $('.addon-period-selector').change(function() {
        var r = $(this).attr('rel');

        $('#' + r + ' span').hide();
        $('#' + r + ' span.' + $(this).val()).fadeIn();
    }).trigger('change');

    $('#transfer-check').bind('click', function(event) {
        var sld = $('input[name="transfer_sld"]').val();
        var tld = $('select[name="transfer_tld"]').val();
        var domain = sld + tld;

        bb.post(
            'guest/servicedomain/can_be_transferred',
            { sld: sld, tld: tld },
            function(result) {
                setTransferPricing(tld);
                $('#domain-name').text(domain);
                $('#domain-transfer-config').fadeIn('fast');
                $('.onAfterDomainCheck').fadeIn('fast');
                $('#transfer-check').hide();
                $('#transfer .order-button').show();
            }
        );
        
        return false;
    });

    $('#domain-check').bind('click', function(event) {
        var sld = $('input[name="register_sld"]').val();
        var tld = $('select[name="register_tld"]').val();
        var domain = sld + tld;

        bb.post(
            'guest/servicedomain/check',
            { sld: sld, tld: tld },
            function(result) {
                setPricing(tld);
                $('#domain-name').text(domain);
                $('.onAfterDomainCheck').fadeIn('fast');
                $('#domain-check').hide();
                $('#register .order-button').show();
            }
        );

        return false;
    });

    function setPricing(tld) {
        bb.post(
            'guest/servicedomain/pricing',
            { tld: tld },
            function(result) {
                var s = $("#registration-years");
                s.find('option').remove();
                for (i=1;i<6;i++) {
                    var price = bb.currency(result.price_registration, {{ currency.conversion_rate }}, "{{ currency.code }}", i);
                    s.append(new Option(i + "{{ ' Year/s @ '|trans }}" + price, i));
                }
            }
        );
    }

    function setTransferPricing(tld) {
        bb.post(
            'guest/servicedomain/pricing',
            { tld: tld },
            function(result) {
                var price = bb.currency(result.price_transfer, {{ currency.conversion_rate }}, "{{ currency.code }}");
                $('#transfer-price').text(price);
            }
        );
    }
});
</script>
{% endblock %}
