{include file="./index_head.tpl"}
<style>
.headname:hover{
 background-color:transparent;
}
</style>
</HEAD>
<BODY>
<div id="mainmenu" style='width:100%;padding: 0 32px;margin: 0px;'>
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tr valign="top">
            <td class="otstup" width="100%"><span class="headname">{$lang.setup_header}</span></td>
        </tr>
    </table>
</div>
<div style="padding: 15px 34px">
    <script language="JavaScript" type="text/javascript">
        function well() {
            if (!test_ip()) return false;
            if (document.getElementById('dump4').checked && !document.getElementById('backup').value) {
                jalert('{$lang.alert_select_backup_file}');
                return false;
            }
            {if $config.type=="WEB"}
            else if (!document.getElementById('check_lic').checked) {
                jalert('{$lang.alert_need_rights}');
                return false;
            }
            {/if}
            document.getElementById('main_form').submit();
            return true;
        }

        var last_ip_value = "";
        var test_ip_res = false;
        var img_test = document.createElement('img');
        var onload_break = 0;
        var already_started = 0;

        img_test.onload = function () {
            already_started = 0;
            if (onload_break) return;
            onload_break = 1;
            if (img_test.width == 1) test_ip_res = true;
            else test_ip_res = false;
            well();
        }
        img_test.onerror = function () {
            already_started = 0;
            if (onload_break) return;
            onload_break = 1;
            test_ip_res = false;
            well();
        }
        img_test_onerror = function () {
            already_started = 0;
            if (onload_break) return;
            onload_break = 1;
            test_ip_res = false;
            well();
        }
        function test_ip() {
            if (already_started) {
                jalert("{$lang.checking_ip}");
                return false;
            }
            var tested_ip = /\b(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])\b/;
            var new_ip = document.getElementById('ip');
            if (!new_ip) return true;
            if (document.getElementById("ip_field").style.display == 'none') return true;
            if (new_ip.style.display == 'none') return true;
            var ip = new_ip.value;
            if (ip == '') {
                jalert("{$lang.please_enter_ip}");
                return false;
            }
            if (ip.search(tested_ip) == -1) {
                jalert("{$lang.alert_invalid_ip}2");
                return false;
            }
            if (last_ip_value == ip) {
                if (!test_ip_res)  jalert("{$lang.alert_invalid_ip}");
                return test_ip_res;
            }
            last_ip_value = ip;
            test_ip_res = false;
            onload_break = 0;
            img_test.src = "http://" + ip + ":7558/images/empty.gif?r=" + Math.random();
            setTimeout(img_test_onerror, 3000) // Таймаут 3 секунды
            already_started = 1;
            return false; // возвращаем отмена, по результату запуститься проверка снова
        }
    </script>
    {if $restore_msg}
    <div style="text-align: center">{$lang.Installation} &quot;{$backup_filename}&quot;<br/><br/></div>
    <div id="backup_status" style="font-size: 13px; text-align: center">
        {if $restore_msg}
            <img src='images/indicator.gif' alt='' style='vertical-align: middle'/>
            {$restore_msg}...
            <br/>
            <br/>
        {/if}
    </div>
    <div id="backup_process" style="width: 200px; text-align: left; border: 1px solid black; margin: 3px auto 40px">
        <div id="backup_bar" style="height: 18px; width: {$part_done}%; background: #85C51F"></div>
        <div id="backup_progress"
             style="position: absolute; margin-top: -18px; text-align: center; width: 200px; font-size: 13px;">{$part_done}
            %
        </div>
    </div>
    <script type="text/javascript">
        var backupError = true;
        $(document).ready(function () {
            if (backupError) {
                document.getElementById("backup_process").style.display = "none";
                document.getElementById("backup_status").style.display = "none";
            }
        });
    </script>
    {else}
    <form action="setup.php?save" method="post" enctype="multipart/form-data" id="main_form">
        {if $error}
            <p style="font-size:14px; color:red; margin-bottom:30px;">
                <b>{$lang.Error}:</b> {$error.message} {if $error.db_error}({$error.db_error}){/if}
            </p>
        {else}
            <br>
            <input type="button" value="{$lang.Check_instal_requirements}" onclick="location.href='check_req.php'" style="font-size:16px">
            <br><br><br>
        {/if}
        <p style="font-size:16px; margin-bottom:30px;">{$lang.setup_for_steps}</p>
        {if $config.type=="WEB" && $config["container"] !== 1}
            <p style="font-size:14px; margin-bottom:25px;"><b>1.</b> {$lang.setup_create_bd}</p>
            <p style="font-size:14px"><b>2.</b> {$lang.setup_enter_bd_params}:</p>
            <table border=0 cellspacing=1 cellpadding=5>
                <tr bgcolor=#EEEEEE>
                    <td>DB host</td>
                    <td><input size=30 type="text" name="dbhost" value="{$config.dbhost}"></td>
                </tr>
                <tr bgcolor=#EEEEEE>
                    <td>DB user</td>
                    <td><input size=30 type="text" name="dbuser" value="{$config.dbuser}"></td>
                </tr>
                <tr bgcolor=#EEEEEE>
                    <td>DB password</td>
                    <td><input size=30 type="text" name="dbpass" value="{$config.dbpass}"></td>
                </tr>
                <tr bgcolor=#EEEEEE>
                    <td>DB name</td>
                    <td><input size=30 type="text" name="dbname" value="{$config.dbname}"></td>
                </tr>
                <tr bgcolor=#EEEEEE>
                    <td>Table prefix</td>
                    <td><input size=30 type="text" name="table_prefix" value="{$config.table_prefix}"></td>
                </tr>
                <tr bgcolor=#EEEEEE>
                    <td>Lang</td>
                    <td>
                        <select name="lang">
                            {foreach from=$langs_list item=one_lang}
                                <option value="{$one_lang.sname}"
                                        {if $one_lang.current}selected{/if}>{$one_lang.name}</option>
                            {/foreach}
                        </select>
                    </td>
                </tr>
            </table>
            <p style="margin-bottom:25px;"><b>{$lang.setup_alert_clean_bd}</p>
            <p style="font-size:14px"><b>3.</b> {$lang.setup_select_way}:</p>
        {elseif $config.type=="LOCAL"}
            <p style="font-size:14px"><b>1.</b> {$lang.setup_use_way}:</p>
            <table border=0 cellspacing=1 cellpadding=5>
                <tr bgcolor=#EEEEEE>
                    <td><input type="radio" name="use" value=1
                               onclick="document.getElementById('ip_field').style.display='none'" checked></td>
                    <td>{$lang.setup_only_local}</td>
                </tr>
                <tr bgcolor=#EEEEEE>
                    <td><input type="radio" name="use" value=2
                               onclick="document.getElementById('ip_field').style.display='block'"></td>
                    <td>{$lang.setup_local_network}</td>
                </tr>
            </table>
            <br>
            <div id="ip_field" style="display:none">
                {$lang.setup_enter_ip}:
                {if $ips}
                    <select onchange="if (this.value=='{$lang.other}') {ldelim}this.style.display='none'; document.getElementById('ip').style.display='inline'; document.getElementById('ip').value=''; document.getElementById('ip').focus();{rdelim} else document.getElementById('ip').value=this.value;">
                        {foreach from=$ips item=ip}
                            <option value="{$ip}">{$ip}</option>
                        {/foreach}
                        <option value="{$lang.other}">{$lang.other}</option>
                    </select>
                {/if}
                <input type="text" name="ip" id="ip" value="{$ips[0]}" size=12 {if $ips}style="display:none"{/if}>
                {if !$ips}<a href="detect_ip.php" target="_blank">{$lang.show_detect_ip}</a>{/if}
            </div>
            <br>
            <p style="font-size:14px"><b>2.</b> {$lang.setup_select_way}:</p>
        {else}
            <p style="font-size:14px">{$lang.setup_select_way}:</p>
        {/if}
        <table border=0 cellspacing=1 cellpadding=5>
            <tr bgcolor=#EEEEEE>
                <td><input type="radio" name="dump" id="dump1" value=1 checked></td>
                <td>{$lang.setup_standart_config}</td>
            </tr>
            <tr bgcolor=#EEEEEE>
                <td><input type="radio" name="dump" id="dump2" value=2></td>
                <td>{$lang.setup_demo_config}</td>
            </tr>
            <tr bgcolor=#EEEEEE>
                <td><input type="radio" name="dump" id="dump3" value=3></td>
                <td>{$lang.setup_null_config}</td>
            </tr>
            <tr bgcolor=#EEEEEE>
                <td><input type="radio" name="dump" id="dump4" value=4></td>
                <td>{$lang.setup_download_backup} <input type="file" name="backup" id="backup" size=20
                                                         onclick="document.getElementById('dump4').checked = true"></td>
            </tr>
        </table>
        <br>
        {if $config.type=="WEB"}<p>{$lang.setup_licence_p1} <a href="license.txt">{$lang.setup_licence_p2}</a> <input
                type="checkbox" id="check_lic"></p>{/if}<br>
        <input type="button" value="{$lang.Setup}" onclick="if (!well()) return false;">
    </form>
{/if}