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

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

{% block meta_title %}{% trans 'Register new domain' %}{% 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>{% trans 'Configure' %}</h2>
            <p>{% trans 'The product/service you have chosen has the following configuration options for you to choose from.' %}</p>
        </div>
        <div class="block conversation">
    
            <div class="widget simpleTabs tabsRight first">
                <div class="head">
                    {#<h2 class="dark-icon i-services">{% trans 'Domain configuration' %}</h2>#}
                </div>

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

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

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

                        <fieldset>
                            <legend>{% trans 'Check domain availability' %}</legend>
                            <p>
                                <input type="text" name="register_sld" value="{{ request.register_sld }}" style="width: 583px" placeholder="{% trans 'Enter new domain name to register' %}">
                                {{ mf.selectboxtld('register_tld', guest.serviceDomain_tlds({"allow_register":1})) }}
                            </p>
                            <button class="bb-button bb-button-big" id="domain-check" style="float: right;">{% trans 'Check' %}</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 %}{% trans 'Continue' %}{% else %}{% trans 'Order now' %}{% 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();">{% trans 'I want to use my nameservers' %}</label>
                                <div id="nameservers" style="display:none; margin: 10px 0;">
                                    <p><input type="text" name="ns1" value="" placeholder="{% trans 'Nameserver 1' %}"></p>
                                    <p><input type="text" name="ns2" value="" placeholder="{% trans 'Nameserver 2' %}"></p>
                                    <p><input type="text" name="ns3" value="" placeholder="{% trans 'Nameserver 3' %}"></p>
                                    <p><input type="text" name="ns4" value="" placeholder="{% trans 'Nameserver 4' %}"></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="{% trans 'Enter your domain name to transfer' %}">
                                {{ 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;">{% trans 'Check' %}</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 %}{% trans 'Continue' %}{% else %}{% trans 'Order now' %}{% endif %}</button>

                            <div id="domain-transfer-config" style="display:none;">
                                <p>{% trans 'Transfer price'%}: <span id="transfer-price"></span></p>
                                <input type="text" name="transfer_code" value="{{ request.transfer_code }}" style="width: 200px" placeholder="{% trans 'Enter domain transfer code' %}">
                            </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 + "{% trans ' Year/s @ ' %}" + 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 %}