{include file="./index_top.tpl"}
{if $tariff_limits.mailing_off}
<div align=center><br>{$lang.Not_supported_cur_tariff}</div>
{else}
<script>
    // значения для системной подсказки
    var help_section = 'Settings';
    var help_sub_section = 'Mailer';
    var public_domains = ['gmail.com', 'outlook.com', 'yandex.ru', 'rambler.ru', 'mail.ru', 'inbox.ru', 'list.ru', 'bk.ru'];
    function set_domain(email) {
        var domain = email.split('@')[1];
        if (domain == 'clientbase.ru') {
            domain = 'mail.ru';
        }
        document.getElementById('username').value = email;
        document.getElementById('smtp_host').value = domain ? 'smtp.' + domain : '';
        document.getElementById('imap_host').value = domain ? 'imap.' + domain : '';
        document.getElementById('security' ).value = domain ? 'ssl' : '';
        document.getElementById('smtp_port').value = domain ? '465' : '25';
        document.getElementById('imap_port').value = domain ? '993' : '143';
        if (public_domains.indexOf(domain) == '-1') {
            document.getElementById('sender_spoofing').style.display = '';
        } else {
            document.getElementById('sender_spoofing').style.display = 'none';
        }
    }
    function set_smtp_port(sec) {
        if (sec == '' && document.getElementById('smtp_port').value == '465') {
            document.getElementById('smtp_port').value = '25';
        } else if (sec == 'ssl' && document.getElementById('smtp_port').value == '25') {
            document.getElementById('smtp_port').value = '465';
        }
    }
    function set_imap_port(sec) {
        if (sec == '' && document.getElementById('imap_port').value == '993') {
            document.getElementById('imap_port').value = '143';
        } else if (sec == 'ssl' && document.getElementById('imap_port').value == '143') {
            document.getElementById('imap_port').value = '993';
        }
    }
    function display_sender_spoofing() {
        var domain = document.getElementById('name').value.split('@')[1];
        if (domain == 'clientbase.ru') {
            domain = 'mail.ru';
        }
        if (public_domains.indexOf(domain) == '-1') {
            document.getElementById('sender_spoofing').style.display = '';
        } else {
            document.getElementById('sender_spoofing').style.display = 'none';
        }
    }
</script>
<div align="center">
    <br>{if $smtp_id eq -1}{$lang.create_smtp_connection}{else}{$lang.edit_smtp_connection}{/if}<br><br>
    <form action="edit_smtp.php?id={$smtp_id}" method="post">
        <table width=700 border=0 cellpadding=3 cellspacing=0>
            <input type="hidden" name="csrf" value="{$csrf}">
            {if $message}
                <tr>
                    <td align=center colspan=2 style="color:green;padding-bottom:15px">{$message}</td>
                </tr>
            {/if}
            <tr>
                <td align="right" width="45%">E-mail:</td>
                <td><input class="form-control form-control-160" type="text" name="name" id="name" value="{$name}" onchange="set_domain(this.value)" autocomplete="new-username"></td>
            </tr>
            <tr>
                <td align="right">{$lang.Password}:</td>
                <td><input class="form-control form-control-160" type="password" name="password" value="{$password}" autocomplete="new-password"></td>
            </tr>
            <tr>
                <td></td>
                <td><a href="" onclick="$('#ext_smtp').toggle(); {if $inv_check && !$invalid_imap}$('#imap_settings').toggle();{/if} return false;">{$lang.Additional}</a></td>
            </tr>
            <tbody id="ext_smtp" style="display:none">
            <tr>
                <td align="right">{$lang.Login}:</td>
                <td><input class="form-control form-control-160" type="text" name="username" id="username" value="{$username}"></td>
            </tr>
            <tr>
                <td align="right">SMTP {$lang.server}:</td>
                <td><input class="form-control form-control-160" type="text" name="smtp_host" id="smtp_host" value="{$smtp_host}" placeholder="localhost"></td>
            </tr>
            <tr>
                <td align="right">SMTP {$lang.port}:</td>
                <td><input class="form-control form-control-160" type="text" name="smtp_port" id="smtp_port" value="{$smtp_port}"></td>
            </tr>
            <tr>
                <td align="right">SMTP {$lang.Security}:</td>
                <td>
                    {if $invalid_opennssl}
                        <div>
                            {$lang.invalid_opennssl}
                        </div>
                    {else}
                        <select class="form-control form-control-160" name="security" id="security" onchange="set_smtp_port(this.value)">
                            <option value="">{$lang.disabled1}</option>
                            <option value="ssl" {if $security eq "ssl"}selected{/if}>ssl</option>
                            <option value="tls" {if $security eq "tls"}selected{/if}>tls</option>
                        </select>
                    {/if}

                </td>
            </tr>
            <tr id="sender_spoofing" style="display:none">
                <td align="right">{$lang.allow_spoofing_sender}<span class="help_bt" h_id="sender_spoofing"></span>:</td>
                <td>
                    <input type="checkbox" name="spoof_sender" {if $spoof_sender}checked{/if}>
                    <script>display_sender_spoofing()</script>
                </td>
            </tr>
            <tr {if $name|substr:-14 == '.clientbase.ru'}style="display:none;"{/if}>
                <td align="right">{$lang.check_invalid_emails}<span class="help_bt" h_id="check_invalid_emails"></span>:</td>
                <td>
                    {if $invalid_imap}
                        {$lang.invalid_imap}
                    {else}
                        <input type="checkbox" name="inv_check" {if $inv_check}checked{/if} onclick="$('#imap_settings').toggle()">
                    {/if}
                </td>
            </tr>
            </tbody>
            <tbody id="imap_settings" style="display:none">
            <tr>
                <td align="right">IMAP {$lang.server}:</td>
                <td><input class="form-control form-control-160" type="text" name="imap_host" id="imap_host" value="{$imap_host}" placeholder="localhost"></td>
            </tr>
            <tr>
                <td align="right">IMAP {$lang.port}:</td>
                <td><input class="form-control form-control-160" type="text" name="imap_port" id="imap_port" value="{$imap_port}"></td>
            </tr>
            <tr>
                <td align="right">IMAP {$lang.Security}:</td>
                <td>
                    {if $invalid_opennssl}
                        <div>
                            {$lang.invalid_opennssl}
                        </div>
                    {else}
                        <select class="form-control form-control-160" name="imap_sec" onchange="set_imap_port(this.value)">
                            <option value="">{$lang.disabled1}</option>
                            <option value="ssl" {if $imap_sec eq "ssl"}selected{/if}>ssl</option>
                            <option value="tls" {if $imap_sec eq "tls"}selected{/if}>tls</option>
                        </select>
                    {/if}

                </td>
            </tr>
            </tbody>
            <tr>
                <td></td>
                <td><br></td>
            </tr>
            <tr>
                <td align="right">{$lang.max_letters_per_hour}
                    <span class="help_bt" h_id="max_letters_per_hour"></span>:
                </td>
                <td>
                    <input class="form-control form-control-35" type="text" name="mph" value="{if $mph}{$mph}{/if}" size=3>
                </td>
            </tr>
            <tr>
                <td align="right">{$lang.max_letters_per_day}
                    <span class="help_bt" h_id="max_letters_per_day"></span>:
                </td>
                <td>
                    <input class="form-control form-control-35" type="text" name="mpd" value="{if $mpd}{$mpd}{/if}" size=3>
                </td>
            </tr>
            <tr>
                <td colspan=2 align="center">
                    <br>
                    <input class="btn btn-default btn-sm" type="submit" name="save" value="{$lang.Save}"
                           onclick="if (!document.getElementById('name').value) { jalert('{$lang.alert_smtp_name}'); return false; }">
                </td>
            <tr>
        </table>
        <br><a href="edit_smtp.php">{$lang.Return}</a>
    </form>
</div><br>
{/if}