<?php
// Copyright 1999-2024. WebPros International GmbH. All rights reserved.
?>

<?php echo $this->form; ?>
<script type="text/javascript">
//<![CDATA[

(function () {
    // show/hide primitives
    function hideHosting() {
        document.getElementById('hostingSettings-root').disabled = true;
        document.getElementById('hostingSettings').style.display = 'none';
    }

    function showHosting() {
        document.getElementById('hostingSettings-root').disabled = false;
        document.getElementById('hostingSettings').style.display = '';
    }

    function hideForwarding() {
        document.getElementById('forwardingSettings-forwardTo').disabled = true;
        document.getElementById('forwardingSettings').style.display = 'none';
    }

    function showForwarding() {
        document.getElementById('forwardingSettings-forwardTo').disabled = false;
        document.getElementById('forwardingSettings').style.display = '';
    }

    // handlers
    function hostingActive() {
        hideForwarding();
        showHosting();
    }

    function forwardingActive() {
        hideHosting();
        showForwarding();
    }

    function noneActive() {
        hideHosting();
        hideForwarding();
    }

    function onHostingTypeChange() {
        var hostingType = document.querySelector('select[name="hostingType[hosting]"]');
        if (!hostingType) {
            return;
        }
        switch (hostingType.value) {
            case 'hosting':
                hostingActive();
                break;
            case 'forwarding':
                forwardingActive();
                break;
            case 'none':
                noneActive();
                break;
        }
    }

    document.querySelector('select[name="hostingType[hosting]"]').addEventListener('change', onHostingTypeChange);
    onHostingTypeChange();

    if (!document.getElementById('domainInfo')) {
        return;
    }

    var newWebspaceTargetControls = ['domainInfo'];
    var existingWebspaceTargetControls = ['hostingType', 'hostingSettings', 'forwardingSettings'];
    if (document.getElementById('dnsSettings')) {
        existingWebspaceTargetControls.push('dnsSettings');
    }
    if (document.getElementById('mailSettings')) {
        existingWebspaceTargetControls.push('mailSettings');
    }

    function showNewWebspaceOptions() {
        newWebspaceTargetControls.forEach(function (id) {
            document.getElementById(id).style.display = '';
        });
        existingWebspaceTargetControls.forEach(function (id) {
            document.getElementById(id).style.display = 'none';
        });
    }

    function showExistingWebspaceOptions() {
        newWebspaceTargetControls.forEach(function (id) {
            document.getElementById(id).style.display = 'none';
        });
        existingWebspaceTargetControls.forEach(function (id) {
            document.getElementById(id).style.display = '';
        });
        onHostingTypeChange();
        if (document.getElementById('hostingSettings-root')) {
            Jsw.fireCustomEvent(document.getElementById('hostingSettings-root'), 'value:change');
        }
    }

    if (document.getElementById('domainName-targetWebspace')) {
        showNewWebspaceOptions();
    } else if (Jsw.getComponent('domainName-webspace')){
        function onWebspaceChange() {
            var subscriptionLookUp = Jsw.getComponent('domainName-webspace');
            if (subscriptionLookUp.getValue() === 'new') {
                showNewWebspaceOptions();
            } else {
                showExistingWebspaceOptions();
            }
        }
        // Temporary workaround for custom prototype's event 'component:change'
        Jsw.getComponent('domainName-webspace').addEventObserver('dataavailable', onWebspaceChange);
        onWebspaceChange();
    }

    if (document.getElementById('domainName-assignToCustomer')) {
        function onCustomerChange() {
            if (document.getElementById('customerInfo')) {
                document.getElementById('customerInfo').style.display = (document.getElementById('domainName-assignToCustomer').checked
                    && (!Jsw.getComponent('domainName-customer') || Jsw.getComponent('domainName-customer').getValue() === 'new')) ? '' : 'none';
            }
        }

        function onAssignToCustomerChange() {
            var assignToCustomer = document.getElementById('domainName-assignToCustomer').checked;
            if (document.getElementById('domainName-customer-form-row')) {
                document.getElementById('domainName-customer-form-row').style.display = assignToCustomer ? '' : 'none';
            }
            if (document.getElementById('domainName-webspace-form-row')) {
                document.getElementById('domainName-webspace-form-row').style.display = assignToCustomer ? 'none' : '';
            }
            if (assignToCustomer) {
                showNewWebspaceOptions();
            } else if (Jsw.getComponent('domainName-webspace')) {
                onWebspaceChange();
            }
            onCustomerChange();
        }

        if (Jsw.getComponent('domainName-customer')) {
            // Temporary workaround for custom prototype's event 'component:change'
            Jsw.getComponent('domainName-customer').addEventObserver('dataavailable', onCustomerChange);
        }
        document.getElementById('domainName-assignToCustomer').addEventListener('click', onAssignToCustomerChange);
        onAssignToCustomerChange();
    }
})();
//]]>
</script>
