<?php
// Copyright 1999-2024. WebPros International GmbH. All rights reserved.
?>
<div id="<?php echo $this->id ?>-container">
<?php
    echo $this->partial('partials/forms/radio-controls.phtml', array(
        'id' => $this->id,
        'radio' => $this->radio,
        'indentControls' => $this->indentControls,
        'element' => $this->element,
        'hideIndentControls' => true,
    ));
?>
    <p id="<?php echo $this->id ?>-hint" style="display: none;">
        <?php echo $this->lmsg('smb.components.forms.database.user.remoteAccessHint') ?>
    </p>
</div>
<script type="text/javascript">
//<![CDATA[
Jsw.onReady(function () {
    var supportedDatabases = <?php echo Zend_Json::encode($this->supportedDatabases) ?>;
    var supportedServers = <?php echo Zend_Json::encode($this->supportedServers) ?>;
    var databaseElement = <?php echo $this->databaseControl ? 'document.getElementById("' . $this->databaseControl->getId() . '")' : 'null' ?>;
    var serverElement = <?php echo $this->serverControl ? 'document.getElementById("' . $this->serverControl->getId() . '")' : 'null' ?>;
    var createUserElement = <?php echo $this->createUserControl ? 'document.getElementById("' . $this->createUserControl->getId() . '")' : 'null' ?>;
    var databaseServerId = <?php echo $this->databaseServerId ? $this->databaseServerId : 'null' ?>;

    var getServerId = function() {
        var serverId;
        if ('select' === serverElement.tagName.toLowerCase()) {
            serverId = serverElement.value;
        } else {
            serverId = serverElement.dataset.value;
        }
        return parseInt(serverId);
    };

    var updateControlsState = function () {
        var isSupported = false;
        var database = databaseElement ? parseInt(databaseElement.value) : 0;
        if (database) {
            isSupported = -1 < supportedDatabases.indexOf(database);
        } else if (databaseServerId || serverElement) {
            var server = databaseServerId || getServerId();
            isSupported = -1 < supportedServers.indexOf(server);
        }
        document.getElementById('<?php echo $this->id ?>-container').style.display = isSupported && (null === createUserElement || createUserElement.checked) ? '' : 'none';
    };

    Jsw.Observer.appendAndCall(updateControlsState, 'dbServer');
});
//]]>
</script>
