<?php
// Copyright 1999-2024. WebPros International GmbH. All rights reserved.
?>
<div class="form-row">
    <div class="single-row">
        <?php echo $this->elementPart($this->element)->content() ?>
        <a href="#" id="<?php echo $this->escape($this->element->getId()) ?>-toggler" class="toggler">
            <?php echo $this->element->getLabel() ?>
        </a>
    </div>
</div>
<script type="text/javascript">
//<![CDATA[
Jsw.onReady(function () {
    var hiddenInput = document.getElementById('<?php echo $this->escape($this->element->getId()) ?>');
    var fileInput = document.getElementById('<?php echo $this->escape($this->form->getElement('ruleSetCustomFile')->getId()) ?>');
    var updateControls = function () {
        var showReplace = false;
        var showUpload = false;
        if (document.getElementById('<?php echo $this->escape($this->form->getElement('ruleSet')->getId()) ?>-custom').checked) {
            showReplace = 'true' !== hiddenInput.value;
            showUpload = !showReplace;
        }
        hiddenInput.closest('.form-row').style.display = showReplace ? '' : 'none';
        fileInput.closest('.form-row').style.display = showUpload ? '' : 'none';

        if (showUpload) {
            fileInput.closest('form').setAttribute('enctype', 'multipart/form-data');
        } else {
            fileInput.closest('form').setAttribute('enctype', 'application/x-www-form-urlencoded');
        }
    };
    updateControls();

    <?php foreach (array_keys($this->form->getElement('ruleSet')->getMultiOptions()) as $option): ?>
        document.getElementById('<?php echo $this->escape($this->form->getElement('ruleSet')->getId() . '-' . $option) ?>').addEventListener('click', updateControls);
    <?php endforeach ?>

    document.getElementById('<?php echo $this->escape($this->element->getId()) ?>-toggler').addEventListener('click', function(event) {
        event.preventDefault();
        hiddenInput.value = 'true';
        updateControls();
    });

    document.getElementById('<?php echo $this->escape($this->form->getElement('webServer')->getId()) ?>').addEventListener('change', function(event) {
        hiddenInput.value = 'true';
        updateControls();
    });
});
//]]>
</script>
