<?php
// Copyright 1999-2017. Parallels IP Holdings 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 updateChilds() {
            enableWhen.each(function (element) {
                var enable = element.value == element.parentEl.value;

                element.children.each(function (child) {
                    if (enable) {
                        var defaultText = $(child + '-default-text').value;

                        if ($(child).value) {
                            if (!$(child + '-real').value) {
                                $(child + '-real').value = $(child).value.replace(defaultText, '');
                            }
                        } else {
                            $(child + '-real').value = $(child + '-default').value;
                            $(child).value = $(child + '-default').value + $(child + '-default-text').value;
                        }
                        if (-1 < $(child).value.indexOf(defaultText)) {
                            $(child + '-reset-to-default').hide();
                        } else {
                            $(child + '-reset-to-default').show();
                        }
                        $(child).enable();
                    } else {
                        $(child).disable();
                        $(child + '-real').value = null;
                        $(child + '-reset-to-default').hide();
                    }
                });
            });
        }

        enableWhen.each(function (element) {
            element.parentEl = $(element.parent);
            element.parentEl.observe('change', updateChilds);
        });

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