<?php
// Copyright 1999-2023. Plesk International GmbH. All rights reserved.

$enableWhen = [];
foreach ($this->enableWhen as $parent => $item) {
    $enableWhen[] = [
        'parent' => $this->element->getElement($parent)->getId(),
        'value' => $item['value'],
        'children' => array_map(function ($child) {
            return $this->element->getElement(str_replace('.', '', $child))->getId();
        }, $item['children']),
    ];
}
?>
<script type="text/javascript">
//<![CDATA[
Jsw.onReady(function () {
    var enableWhen = <?=Zend_Json::encode($enableWhen)?>;

    function updateChildren() {
        enableWhen.forEach(function (element) {
            var enable = element.value == element.parentEl.value;

            element.children.forEach(function (child) {
                if (document.getElementById(child).tagName.toLowerCase() === 'select') {
                    toggleExtendedSelect(child, enable)
                } else {
                    toggleEditableSelect(child, enable);
                }
            });
        });
    }

    function toggleEditableSelect(child, enable) {
        if (enable) {
            var defaultText = document.getElementById(child + '-default-text').value;

            if (document.getElementById(child).value) {
                if (!document.getElementById(child + '-real').value) {
                    document.getElementById(child + '-real').value = document.getElementById(child).value.replace(defaultText, '');
                }
            } else {
                document.getElementById(child + '-real').value = document.getElementById(child + '-default').value;
                document.getElementById(child).value = document.getElementById(child + '-default').value + document.getElementById(child + '-default-text').value;
            }
            if (-1 < document.getElementById(child).value.indexOf(defaultText)) {
                document.getElementById(child + '-reset-to-default').style.display = 'none';
            } else {
                document.getElementById(child + '-reset-to-default').style.display = '';
            }
            document.getElementById(child).disabled = false;
        } else {
            document.getElementById(child).disabled = true;
            document.getElementById(child + '-real').value = document.getElementById(child + '-default').value;
            document.getElementById(child + '-reset-to-default').style.display = 'none';
        }
    }

    function toggleExtendedSelect(child, enable) {
        if (enable) {
            document.getElementById(child).disabled = false;
            if (document.getElementById(child + '-default').value === document.getElementById(child).value) {
                document.getElementById(child + '-reset-to-default').style.display = 'none';
            } else {
                document.getElementById(child + '-reset-to-default').style.display = '';
            }
        } else {
            document.getElementById(child).disabled = true;
            document.getElementById(child).value = document.getElementById(child + '-default').value;
            document.getElementById(child + '-reset-to-default').style.display = 'none';
        }
    }

    enableWhen.forEach(function (element) {
        element.parentEl = document.getElementById(element.parent);
        element.parentEl.addEventListener('change', updateChildren);
    });

    updateChildren();
});
//]]>
</script>
