
<%+header%>
<!--
    This module is a demo to configure MTK' proprietary WiFi driver.
    Basic idea is to bypass uci and edit wireless profile (mt76xx.dat) directly.
    LuCI's WiFi configuration is more logical and elegent, but it's quite tricky to
    translate uci into MTK's WiFi profile (like we did in "uci2dat").

    Hua Shao <nossiac@163.com>
-->

<%
local disp = require "luci.dispatcher"
-- local request  = disp.context.path
local request = disp.context.request
local mtkwifi = require("mtkwifi")
local devname = request[5]
local devs = mtkwifi.get_all_devs()
local dev = {}
for _,v in ipairs(devs) do
    if v.devname == devname then
        dev = v
    end
end

local cfgs = mtkwifi.load_profile(dev.profile)

%>


<script type="text/javascript" src="<%=resource%>/cbi.js?v=git-17.250.41546-90ac861"></script>

<div id="loading" style="margin-top: 1em; display: none;">
<img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:e;" />
    &nbsp;&nbsp;&nbsp; Please waiting while the page is loading......
</div>

<form method="post" name="cbi" action="<%=luci.dispatcher.build_url("admin", "network", "wifi", "dev_cfg", devname)%>" enctype="multipart/form-data" onreset="return cbi_validate_reset(this)" onsubmit="return cbi_validate_form(this, 'Some fields are invalid, cannot save values!')" autocomplete="false">
    <fieldset class="cbi-section">
        <legend> 无线高级设置 - <%=devname%>
<%
local diff = mtkwifi.diff_profile(dev.profile)
if next(diff) ~= nil then
%>
    <span style="color:red;">(<a href="javascript:location.href='<%=luci.dispatcher.build_url("admin", "network", "wifi", "reload", dev.devname)%>'">reload</a> to apply changes)</span>
<%
    end
%>
        </legend>
        <table id="dev-cfg-basic" class="cbi-section-table">
            <tr><th></th><td></td><td></td></tr>
            <tr>
                <td>模式</td>
                <td>
                    <select style="width:auto" name="WirelessMode" id="WirelessMode" onchange="WirelessMode_onchange(this.options[this.options.selectedIndex].value)">
                        <% for k,v in pairs(dev.WirelessModeList) do %>
                            <option value="<%=k%>" <% if tonumber(dev.WirelessMode) == tonumber(k) then%> selected="selected"<% end %>><%=k%> - <%=v%></option>
                        <% end %>
                    </select>
                </td>
                <td></td>
            </tr>
            <tr>
                <td>信道</td>
                <td>
                    <select style="width:auto; min-width:180px;" name="Channel" id="Channel" onchange="Channel_onchange(this.value)" <% if cfgs.ApCliEnable == "1" then %> disabled="disabled" <% end %>>
                    </select>
                </td>
                <td><% if cfgs.ApCliEnable == "1" then %> APClient/Repeater Mode. <% end %></td>
            </tr>
            <tr>
                <td>区域代码</td>
                <td>
                    <select name="CountryCode" style="width:auto">
                        <option value="US" id="advCountryCodeUS" <% if cfgs.CountryCode == "US" then %> selected="selected"<% end %>>US (United States)</option>
                        <option value="JP" id="advCountryCodeJP" <% if cfgs.CountryCode == "JP" then %> selected="selected"<% end %>>JP (Japan)</option>
                        <option value="FR" id="advCountryCodeFR" <% if cfgs.CountryCode == "FR" then %> selected="selected"<% end %>>FR (France)</option>
                        <option value="TW" id="advCountryCodeTW" <% if cfgs.CountryCode == "TW" then %> selected="selected"<% end %>>TW (Taiwan)</option>
                        <option value="IE" id="advCountryCodeIE" <% if cfgs.CountryCode == "IE" then %> selected="selected"<% end %>>IE (Ireland)</option>
                        <option value="HK" id="advCountryCodeHK" <% if cfgs.CountryCode == "HK" then %> selected="selected"<% end %>>HK (Hong Kong)</option>
                        <option value="NONE" id="advCountryCodeNONE"<% if cfgs.CountryCode == "NONE" then %> checked="checked"<% end %>>NONE</option>
                    </select>
                </td>
                <td></td>
            </tr>
            <tr>
                <td>Country Region</td>
                <td>
                    <select style="width:auto; min-width:180px;" name="__cr" id="__cr" onchange="CountryRegion_onchange(this.value)">
                    </select>
                </td>
                <td></td>
            </tr>
            <% if cfgs.HT_OpMode then %>
            <tr>
                <td>无线模式</td>
                <td>
                    <select style="width:auto" name="HT_OpMode">
                        <option value="0" <% if cfgs.HT_OpMode == "0" then %> selected="selected"<% end%>>正常模式</option>
                        <option value="1" <% if cfgs.HT_OpMode == "1" then %> selected="selected"<% end%>>绿色模式</option>
                    </select>
                </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.HT_GI then %>
            <tr>
                <td>HT Guard Interval</td>
                <td>
                    <select style="width:auto" name="HT_GI">
                        <option value="0" <% if cfgs.HT_GI == "0" then %> selected="selected"<% end%>>Long</option>
                        <option value="1" <% if cfgs.HT_GI == "1" then %> selected="selected"<% end%>>Short</option>
                        <option value="2" <% if cfgs.HT_GI == "2" then %> selected="selected"<% end%>>Auto</option>
                    </select>
                </td>
                <td></td>
            </tr>
            <% end %>
            <tr>
                <td>信道带宽</td>
                <td>
                    <select style="width:auto" name="__bw" id="__bw" onchange="Bw_onchange(this.options[this.options.selectedIndex].value)">
                        <option value="20" <% if dev.__bw == "20" then %> selected="selected"<% end%>>20 MHz</option>
                        <option value="40" <% if dev.__bw == "40" then %> selected="selected"<% end%>>40 MHz</option>
                        <option value="60" <% if dev.__bw == "60" then %> selected="selected"<% end%>>20/40 MHz</option>
                        <option value="80" <% if dev.__bw == "80" then %> selected="selected"<% end%>>80 MHz</option>
                        <option value="160" <% if dev.__bw == "160" then %> selected="selected"<% end%>>160 MHz</option>
                        <option value="161" <% if dev.__bw == "161" then %> selected="selected"<% end%>>80+80 MHz</option>
                    </select>
                </td>
                <td></td>
            </tr>
            <% if cfgs.VHT_BW then %>
            <tr>
                <td>2G 40MHZ模式辅助信道</td>
                <td>
                    <select style="width:auto; min-width:180px;" name="HT_EXTCHA" id="HT_EXTCHA" <% if cfgs.ApCliEnable == "1" or dev.__bw ~= "40" or dev.__bw ~= "60" then %> disabled="disabled" <% end %>>
                    </select>
                </td>
                <td><% if cfgs.ApCliEnable == "1" then %> APClient/Repeater Mode. <% end %></td>
            </tr>
            <tr>
                <td>5G 2nd 80Mhz Channel</td>
                <td>
                    <select style="width:auto; min-width:180px;" name="VHT_Sec80_Channel" id="VHT_Sec80_Channel" <% if cfgs.ApCliEnable == "1" or dev.__bw ~= "180" then %> disabled="disabled" <% end %>>
                    </select>
                </td>
                <td><% if cfgs.ApCliEnable == "1" then %> APClient/Repeater Mode. <% end %></td>
            </tr>
            <% end %>
            <% if cfgs.HT_STBC then %>
            <tr>
                <td>STBC</td>
                <td>
                    <input type="radio" name="HT_STBC" value="1" <% if cfgs.HT_STBC == "1" then %> checked="checked"<% end %>/> 启用
                    <input type="radio" name="HT_STBC" value="0" <% if cfgs.HT_STBC == "0" then %> checked="checked"<% end %>/> 禁用
                </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.HT_AMSDU then %>
            <tr>
                <td>A-MSDU</td>
                <td>
                    <input type="radio" name="HT_AMSDU" value="1" <% if cfgs.HT_AMSDU == "1" then %> checked="checked"<% end %>/> 启用
                    <input type="radio" name="HT_AMSDU" value="0" <% if cfgs.HT_AMSDU == "0" then %> checked="checked"<% end %>/> 禁用
               </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.HT_AutoBA then %>
            <tr>
                <td>Auto Block ACK</td>
                <td>
                    <input type="radio" name="HT_AutoBA" value="1" <% if cfgs.HT_AutoBA == "1" then %> checked="checked"<% end %>/> 启用
                    <input type="radio" name="HT_AutoBA" value="0" <% if cfgs.HT_AutoBA == "0" then %> checked="checked"<% end %>/> 禁用
                </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.HT_BADecline then %>
            <tr>
                <td>Decline BA Request</td>
                <td>
                    <input type="radio" name="HT_BADecline" value="1" <% if cfgs.HT_BADecline == "1" then %> checked="checked"<% end %>/> 启用
                    <input type="radio" name="HT_BADecline" value="0" <% if cfgs.HT_BADecline == "0" then %> checked="checked"<% end %>/> 禁用
                </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.HT_DisallowTKIP then %>
            <tr>
                <td>HT Disallow TKIP</td>
                <td>
                    <input type="radio" name="HT_DisallowTKIP" value="1" <% if cfgs.HT_DisallowTKIP == "1" then %> checked="checked"<% end %>/> 启用
                    <input type="radio" name="HT_DisallowTKIP" value="0" <% if cfgs.HT_DisallowTKIP == "0" then %> checked="checked"<% end %>/> 禁用
                </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.HT_LDPC then %>
            <tr>
                <td>HT LDPC</td>
                <td>
                    <input type="radio" name="HT_LDPC" value="1" <% if cfgs.HT_LDPC == "1" then %> checked="checked"<% end %>/> 启用
                    <input type="radio" name="HT_LDPC" value="0" <% if cfgs.HT_LDPC == "0" then %> checked="checked"<% end %>/> 禁用
                </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.VHT_SGI then %>
            <tr>
                <td>VHT Short GI</td>
                <td>
                    <select style="width:auto" name="VHT_SGI">
                        <option value="0" <% if cfgs.VHT_SGI == "0" then %> selected="selected"<% end%>>Long</option>
                        <option value="1" <% if cfgs.VHT_SGI == "1" then %> selected="selected"<% end%>>Short</option>
                    </select>
                </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.VHT_STBC then %>
            <tr>
                <td>VHT STBC</td>
                <td>
                    <input type="radio" name="VHT_STBC" value="1" <% if cfgs.VHT_STBC == "1" then %> checked="checked"<% end %>/> 启用
                    <input type="radio" name="VHT_STBC" value="0" <% if cfgs.VHT_STBC == "0" then %> checked="checked"<% end %>/> 禁用
                </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.VHT_BW_SIGNAL then %>
            <tr>
                <td>VHT BW Signaling</td>
                <td>
                    <input type="radio" name="VHT_BW_SIGNAL" value="1" <% if cfgs.VHT_BW_SIGNAL == "1" then %> checked="checked"<% end %>/> 启用
                    <input type="radio" name="VHT_BW_SIGNAL" value="0" <% if cfgs.VHT_BW_SIGNAL == "0" then %> checked="checked"<% end %>/> 禁用
                    <input type="radio" name="VHT_BW_SIGNAL" value="0" <% if cfgs.VHT_BW_SIGNAL == "2" then %> checked="checked"<% end %>/> Dynamic
                </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.VHT_LDPC then %>
            <tr>
                <td>VHT LDPC</td>
                <td>
                    <input type="radio" name="VHT_LDPC" value="1" <% if cfgs.VHT_LDPC == "1" then %> checked="checked"<% end %>/> 启用
                    <input type="radio" name="VHT_LDPC" value="0" <% if cfgs.VHT_LDPC == "0" then %> checked="checked"<% end %>/> 禁用
                </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.BGProtection then %>
            <tr>
                <td>BG Protection Mode</td>
                <td>
                    <select name="BGProtection" style="width:auto">
                        <option value="0" <% if cfgs.BGProtection == "0" then %>selected="selected"<% end %>>Auto</option>
                        <option value="1" <% if cfgs.BGProtection == "1" then %>selected="selected"<% end %>>Always On</option>
                        <option value="2" <% if cfgs.BGProtection == "2" then %>selected="selected"<% end %>>Always Off</option>
                    </select>
                </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.VHT_BW then %>
            <tr>
                <td>HT Protection</td>
                <td>
                    <input type="radio" name="HT_PROTECT" value="1" <% if cfgs.HT_PROTECT == "1" then %> checked="checked"<% end %>/> 启用
                    <input type="radio" name="HT_PROTECT" value="0" <% if cfgs.HT_PROTECT == "0" then %> checked="checked"<% end %>/> 禁用
                </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.BeaconPeriod then %>
            <tr>
                <td>Beacon Interval</td>
                <td>
                    <input name="BeaconPeriod" value="<%=cfgs.BeaconPeriod%>" />ms(范围 20-999, 默认值 100)
                </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.DtimPeriod then %>
            <tr>
                <td>Data Beacon Rate (DTIM)</td>
                <td>
                    <input name="DtimPeriod" value="<%=cfgs.DtimPeriod%>" />ms(范围 1-255, 默认值 1)
                </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.FragThreshold then %>
            <tr>
                <td>Fragment Threshold</td>
                <td>
                    <input name="FragThreshold" value="<%=cfgs.FragThreshold%>" />(范围 256-2346, 默认值 2346)
                </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.RTSThreshold then %>
            <tr>
                <td>RTS Threshold</td>
                <td>
                    <input name="RTSThreshold" value="<%=cfgs.RTSThreshold%>" />(范围 256-2347, 默认值 2347)
                </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.TxPower then %>
            <tr>
                <td>TX Power</td>
                <td>
                    <input name="TxPower" value="<%=cfgs.TxPower%>" />dbm(范围 1-100, 默认值 100)
                </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.TxPreamble then %>
            <tr>
                <td>Short Preamble</td>
                <td>
                    <input type="radio" name="TxPreamble" value="1" <% if cfgs.TxPreamble == "1" then %> checked="checked"<% end %>/> 启用
                    <input type="radio" name="TxPreamble" value="0" <% if cfgs.TxPreamble == "0" then %> checked="checked"<% end %>/> 禁用
                </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.ShortSlot then %>
            <tr>
                <td>Short Slot</td>
                <td>
                    <input type="radio" name="ShortSlot" value="1" <% if cfgs.ShortSlot == "1" then %> checked="checked"<% end %>/> 启用
                    <input type="radio" name="ShortSlot" value="0" <% if cfgs.ShortSlot == "0" then %> checked="checked"<% end %>/> 禁用
                </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.TxBurst then %>
            <tr>
                <td>TX Burst</td>
                <td>
                    <input type="radio" name="TxBurst" value="1" <% if cfgs.TxBurst == "1" then %> checked="checked"<% end %>/> 启用
                    <input type="radio" name="TxBurst" value="0" <% if cfgs.TxBurst == "0" then %> checked="checked"<% end %>/> 禁用
                </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.PktAggregate then %>
            <tr>
                <td>Packet Aggregate</td>
                <td>
                    <input type="radio" name="PktAggregate" value="1" <% if cfgs.PktAggregate == "1" then %> checked="checked"<% end %>/> 启用
                    <input type="radio" name="PktAggregate" value="0" <% if cfgs.PktAggregate == "0" then %> checked="checked"<% end %>/> 禁用
                </td>
                <td></td>
            </tr>
            <% end %>
            <% if cfgs.IEEE80211H then %>
            <tr>
                <td>802.11H</td>
                <td>
                    <input type="radio" name="IEEE80211H" value="1" <% if cfgs.IEEE80211H == "1" then %> checked="checked"<% end %>/> 启用
                    <input type="radio" name="IEEE80211H" value="0" <% if cfgs.IEEE80211H == "0" then %> checked="checked"<% end %>/> 禁用
                </td>
                <td></td>
            </tr>
            <% end %>
        </table>


    </fieldset>
    <div class="cbi-page-actions">
        <input class="cbi-button cbi-button-apply" value="重启无线" type="button" onclick='wifi_reload("<%=luci.dispatcher.build_url("admin", "network", "wifi", "reload", devname)%>");' />
        <input class="cbi-button cbi-button-apply" value="保存设置" type="submit" />
        <input class="cbi-button cbi-button-apply" name="__apply" value="保存&应用" type="submit" />
        <input class="cbi-button cbi-button-reset" value="重置" type="reset" />
    </div>
</form>


<form method="post" name="cbi2" action="<%=luci.dispatcher.build_url("admin", "network", "wifi", "dev_cfg_raw", devname)%>" enctype="multipart/form-data" onreset="return cbi_validate_reset(this)" onsubmit="return cbi_validate_form(this, 'Some fields are invalid, cannot save values!')" autocomplete="off">
    <fieldset class="cbi-section">
        <legend> 无线参数（直接修改无线配置）</legend>
        <p> <span style="color: red"><b>警告</b></span> : 如果你不了解请不要修改!</p>
        <textarea name="raw" id="raw" style="width:98%; height: 200px;"><%
            local fd = io.open(dev.profile)
            for line in fd:lines() do
                print(line)
            end
        %></textarea>
    </fieldset>
    <div class="cbi-page-actions">
        <input class="cbi-button cbi-button-reset" value="重置" onclick="location.href='<%=luci.dispatcher.build_url("admin", "network", "wifi", "dev_cfg_view", devname)%>'" type="button" />
        <input class="cbi-button cbi-button-apply" id="save" value="保存" type="submit" onclick="if (confirm('你将修改无线参数，确认要修改？')) return true; else return false;" />
        <input class="cbi-button cbi-button-apply" id="reset" value="恢复出厂设置" type="button" onclick="if (confirm(' 确认恢复<%=devname%> 至出厂设置?')) location.href='<%=luci.dispatcher.build_url("admin", "network", "wifi", "dev_cfg_reset", devname)%>'" />
    </div>
</form>


<script type="text/javascript">
    function wifi_reload(url) {
        window.scrollTo(0, 0);
        document.getElementById('loading').style.display="";
        XHR.get(url, null,
            function(x)
            {
                console.log(x);
                document.getElementById("loading").style.display="none";
                window.location.reload();
            }
        );
    }

    function WirelessMode_onchange (mode) {
        var cr = GetCountryRegion(mode);

        getCountryRegionList(mode, cr);
    }

    function CountryRegion_onchange(country_region) {
        var wmode_o = document.getElementById('WirelessMode');
        var wmode;

        if (wmode_o)
            wmode = wmode_o.value;

        if (wmode == "")
            wmode = "<%=cfgs.WirelessMode%>";

        getChannelList(wmode, country_region);
    }

    function Channel_onchange (ch) {
        getBw(null, ch);
    }

    function Bw_onchange(bw, ch) {
        var ch_val;

        if (!ch){
            var ch_o = document.getElementById('Channel');

            if (ch_o)
                ch_val = ch_o.value;

            if (ch_val == "")
                ch_val = "<%=cfgs.Channel%>";
        } else {
            ch_val = ch
        }

        get5G2nd80MhzChannelList(bw, ch_val);
        getHTExtChannel(bw, ch_val);
    }

    function initCountryRegionList(list, mode){
        var select = document.getElementById('__cr');
        var cr = {};

        if (is_mode_gband(mode))
            cr = "<%=cfgs.CountryRegion%>";
        else if (is_mode_aband(mode))
            cr = "<%=cfgs.CountryRegionABand%>";

        var new_cr = initList(select, list, cr, "region");

        CountryRegion_onchange(new_cr);
    }

    function getCountryRegionList(mode) {
        XHR.get('<%=luci.dispatcher.build_url("admin", "network", "wifi", "get_country_region_list")%>', 'mode='+mode,
            function(x)
            {
                //console.log(x);
                //console.log(x.response);
                var json = eval(x.response);
                initCountryRegionList(json, mode);
            }
        );
    }

    function initChannelList(list) {
        /* choose auto select when no matching item in the lis */
        var select = document.getElementById('Channel');
        var ch = {};
        var new_ch = null;

        ch.cval = select.value;
        ch.oval = "<%=dev.Channel%>";

        var new_ch = initList(select, list, ch, "channel");
        Channel_onchange(new_ch);
    }

    function getChannelList(mode, country_region) {
        XHR.get('<%=luci.dispatcher.build_url("admin", "network", "wifi", "get_channel_list")%>', 'mode='+mode+'&country_region='+country_region,
            function(x)
            {
                console.log(x);
                console.log(x.response);
                var json = eval(x.response);
                initChannelList(json);
            }
        );
    }

    function GetCountryRegion(mode) {
        var cr_o = document.getElementById('__cr');
        var cr = "";

        if (cr_o) {
            cr = cr_o.value;
        }

        if (cr == "") {
            if (is_mode_gband(mode)) {
                cr = "<%=cfgs.CountryRegion%>";
            } else if (is_mode_aband(mode)) {
                cr = "<%=cfgs.CountryRegionABand%>";
            } else {
                alert("Cannot get valid CountryRegion from invalid WireleeMode");
            }
        }

        return cr;
    }

    function getBw(mode, ch) {
        /* choose bw as wide as possible if modification is needed */
        var bw_sel = document.getElementById('__bw');
        var wmode;

        if (!mode) {
            wmode = document.getElementById('WirelessMode');
            wmode = wmode.value*1;
        } else {
            wmode = mode*1;
        }

        if (is_mode_legacy_only(wmode)) {
            for (var idx in bw_sel.options) {
                bw_sel.options[idx].disabled = false;

                if (bw_sel.options[idx].value == 20)
                    bw_sel.options[idx].selected = true;

                if (bw_sel.options[idx].value > 20)
                    bw_sel.options[idx].disabled = true;
            }
        } else if (is_ch_gband(ch)) {
            for (var idx in bw_sel.options) {
                bw_sel.options[idx].disabled = false;

                if ((bw_sel.options[idx].value == 60) && (bw_sel.selectedIndex > idx))
                    bw_sel.options[idx].selected = true;

                if (bw_sel.options[idx].value > 60)
                    bw_sel.options[idx].disabled = true;
            }
        } else if (is_ch_aband(ch)) {
            var chosen60 = false;
            var bw160 = "<%=cfgs.VHT_BW%>"*1;

            if (bw_sel.value == 60)
                chosen60 = 1;

            for (var idx in bw_sel.options) {
                bw_sel.options[idx].disabled = false;

                if (bw_sel.options[idx].value == 40 && chosen60 && bw160 == 0) {
                    bw_sel.options[idx].selected = true;
                } else if (bw_sel.options[idx].value == 60) {
                    bw_sel.options[idx].disabled = true;
                        bw_sel.options[idx].selected = false;
                } else if (bw_sel.options[idx].value == 160 && chosen60 && bw160 == 1) {
                    bw_sel.options[idx].selected = true;
                }
            }
        }

        Bw_onchange(bw_sel.value, ch);
    }

    function init5G2nd80MhzChannelList(list) {
        var select = document.getElementById('VHT_Sec80_Channel');
        var ch2 = {};
        var new_ch2 = null;

        ch2.cval = select.value;
        ch2.oval = "<%=cfgs.VHT_Sec80_Channel%>";

        initList(select, list, ch2, "channel");

        if (select.length > 0)
            select.disabled = false;
        else
            alert("[Warning] 5G 2nd 80Mhz Channel is not available. Please Check CountryRegion.");
    }

    function get5G2nd80MhzChannelList(bw, ch) {
        var ch_5g_2nd = document.getElementById('VHT_Sec80_Channel');

            ch_5g_2nd.disabled = true;

        if (ch == "0")
            return;

        if (bw != "161" || ch == "0")
            return;

        var mode = document.getElementById('WirelessMode');
        mode = mode.value*1;

        var cr = GetCountryRegion(mode);
        XHR.get('<%=luci.dispatcher.build_url("admin", "network", "wifi", "get_5G_2nd_80Mhz_channel_list")%>', 'ch_cur='+ch+'&country_region='+cr,
            function(x)
            {
                //console.log(x);
                //console.log(x.response);
                var json = eval(x.response);
                init5G2nd80MhzChannelList(json);
            }
        );
        }

    function initHTExtChannelList(list) {
        var select = document.getElementById('HT_EXTCHA');
        var ch2 = {};
        var new_ch2 = null;

        ch2.cval = select.value;
        ch2.oval = "<%=cfgs.HT_EXTCHA%>";

        initList(select, list, ch2, "val");

        if (select.length > 0)
            select.disabled = false;
        else
            alert("[Warning] 2G 40Mhz Ext Channel is not avalable. Please Check CountryRegion.");
        }

    function getHTExtChannel(bw, ch) {
        var ch_2g_ext = document.getElementById('HT_EXTCHA');

        ch_2g_ext.disabled = true;

        if (ch == "0")
            return;

        if (bw != "40" && bw != "60")
            return;

        var mode = document.getElementById('WirelessMode');
        mode = mode.value*1;

        var cr = GetCountryRegion(mode);
        XHR.get('<%=luci.dispatcher.build_url("admin", "network", "wifi", "get_HT_ext_channel_list")%>', 'ch_cur='+ch+'&country_region='+cr,
            function(x)
            {
                console.log(x);
                console.log(x.response);
                var json = eval(x.response);
                initHTExtChannelList(json);
            }
        );
    }

    function toggle_apcli (show) {
        if (show) {
            document.getElementById('apcli_cfg').style.display = "";

            var a = document.getElementById('ApCliAuthMode')
            var to = a.options[a.options.selectedIndex].value
            if (to == "WPA2PSK") {
                document.getElementById("apcli_wpa").style.display="";
                document.getElementById("apcli_wep").style.display="none";
            } else if (to == "WEP") {
                document.getElementById("apcli_wpa").style.display="none";
                document.getElementById("apcli_wep").style.display="";
            } else {
                document.getElementById("apcli_wpa").style.display="none";
                document.getElementById("apcli_wep").style.display="none";
            }
        }
        else {
            document.getElementById('apcli_cfg').style.display = "none";
            document.getElementById('apcli_wpa').style.display = "none";
            document.getElementById('apcli_wep').style.display = "none";
        }
    }

    function ApCliAuthMode_onchange (to) {
        if (to == "WPA2PSK") {
            document.getElementById("apcli_wpa").style.display="";
            document.getElementById("apcli_wep").style.display="none";
        } else if (to == "WEP") {
            document.getElementById("apcli_wpa").style.display="none";
            document.getElementById("apcli_wep").style.display="";
        } else {
            document.getElementById("apcli_wpa").style.display="none";
            document.getElementById("apcli_wep").style.display="none";
        }
    }

    function toggle_atf(o) {
        if (o.getAttribute("value") == "1") {
            document.getElementById("VOW_Airtime_Fairness_En").value = "0";
            o.setAttribute("value", "0");
            o.className = "cbi-button cbi-button-add";
            o.innerHTML = "Enable ATC";
        } else {
            document.getElementById("VOW_Airtime_Fairness_En").value = "1";
            o.setAttribute("value", "1");
            o.className = "cbi-button cbi-button-remove";
            o.innerHTML = "Disable ATC";
        }
    }

    function toggle_atc(o) {
        if (o.getAttribute("value") == "1") {
            document.getElementById("VOW_BW_Ctrl").value = "0";
            o.setAttribute("value", "0");
            o.className = "cbi-button cbi-button-add";
            o.innerHTML = "Enable ATC";
            document.getElementById("atc-table").style.display = "none";
        } else {
            document.getElementById("VOW_BW_Ctrl").value = "1";
            o.setAttribute("value", "1");
            o.className = "cbi-button cbi-button-remove";
            o.innerHTML = "Disable ATC";
            document.getElementById("atc-table").style.display = "";
        }
    }

    function is_mode_legacy_only(mode) {
        var imode = mode*1;

        return ((imode >= 0) && (imode <= 4));
    }

    function is_mode_gband(mode) {
        var imode = mode*1;
        var gband_mode_list = [0,1,4,6,7,9,16];
        var i;

        for (i = 0; i < gband_mode_list.length; i++){
            if( gband_mode_list[i] == imode )
                return true;
        }

        return false;
    }

    function is_mode_aband(mode) {
        var imode = mode*1;
        var aband_mode_list = [2,8,11,14,15,17];
        var i;

        for (i = 0; i < aband_mode_list.length; i++){
            if( aband_mode_list[i] == imode )
                return true;
        }

        return false;
    }

    function is_ch_gband(ch) {
        ch = ch*1;

        if (ch == 0) {
            var wmode = document.getElementById('WirelessMode');

            return is_mode_gband(wmode.value);
        }

        return ((ch > 0) && (ch <= 14));
    }

    function is_ch_aband(ch) {
        ch = ch*1;

        if (ch == 0) {
            var wmode = document.getElementById('WirelessMode');

            return is_mode_aband(wmode.value);
        }

        return ((ch >= 36 ) && (ch <= 165));
    }

    function initList(selobj, list, selvals, value, text){
        var sel = {};
        var id = selobj.id;

        if (typeof(selvals) != "object")
            sel[0] = selvals;
        else
            sel = selvals;

        selobj.innerHTML = "";
        for (var i in list) {
            var opt = document.createElement('option');

            if(value)
                opt.value = list[i][value];
            else
                opt.value = list[i].value;

            if(text)
                opt.text = list[i][text];
            else
                opt.text = list[i].text;

            selobj.appendChild(opt);
        }

        var selv = null;
        for (var k in sel) {
            //console.log("[initList][" + id + "] sel[" + k + "]=" + sel[k]);
            for (var i in selobj.options){
                if (selobj.options[i].value == sel[k]){
                    selobj.options[i].selected = true;
                    selv = sel[k];
                    //console.log("[initList][" + id + "] found selv=" + selv);
                    break;
                }
            }

            if (selv) break;
        }

        if (!selv) {
            selv = selobj.value;
            //console.log("[initList][" + id + "] !selv, selv=" + selv);
        }

        return selv;
    }

    window.onload = function() {
        WirelessMode_onchange('<%=dev.WirelessMode%>');
        ApCliAuthMode_onchange('<%=cfgs.ApCliAuthMode%>');
    }

</script>

<%+footer%>
