var profile_index = [];
var profile_next_index = 0;
var profile_max_number = 16;
var profile_count =0;
var profile_row_count=16;
/*
Add/Delete/Edit external server
profile_record_csid => 要填入 C_WEBUI_PDUMG_OUTLE_RECORD 的下一筆 index 用來記錄讀取CSID 的順序
profile_next_index => 下一筆的 index  為 C_PDUMG_OUTLET_SCHEDULING_RULE_NAME 第一個空白的CSID
*/
var profile_namev = [];
var sw_list=[];
function set_list(mode,set)
{
var t=new Date()
Nmode = ((mode == "wpa") ? "3" : "4");
Nset = (set!=null) ? set:0;
var xml="uplink_profile.xml?rc=&Nmode="+Nmode+"&Nset="+Nset;
xmlDoc=loadXMLDocs(xml+"&ZT="+t.getTime());
if(xmlDoc==null)return;
var table_str2 = "";
var ary_index=0;
var chk_add=0;
var chk_add_all=0;
var list_id=0;
var list_count=0;
sw_list[ary_index] = table_str2;
for(var x=0;x<profile_max_number;x++)
{
profile_rule_index = xmlDoc.getElementsByTagName("profile_record")[x].childNodes[0];
if(profile_rule_index != null)
{
index = parseInt(profile_rule_index.nodeValue);
xml_index = (index-1);
list_id=list_id+1;
list_count=list_count+1;
profile_name = xml_getvalue(xmlDoc,"profile_name",xml_index);
profile_ssid = xml_getvalue(xmlDoc,"profile_ssid",xml_index);
profile_ch = xml_getvalue(xmlDoc,"profile_ch",xml_index);
profile_auth = xml_getvalue(xmlDoc,"profile_auth",xml_index);
profile_ency = xml_getvalue(xmlDoc,"profile_ency",xml_index);
profile_mac = xml_getvalue(xmlDoc,"profile_mac",xml_index);
profile_signal = xml_getvalue(xmlDoc,"profile_signal",xml_index);
profile_priority = xml_getvalue(xmlDoc,"profile_priority",xml_index);
profile_enable = xml_getvalue(xmlDoc,"profile_enable",xml_index);
var enable_check = "";
var sw_auth=authmodestr[profile_auth];
var sw_ency=encryptypestr[profile_ency];
enable_check = (profile_enable == 1)?"checked":"";
if(profile_ch == 0) profile_ch = GetElmCont('wary_0','Auto');
table_str2 += "<tr align='center' style='word-break:break-all'>"
table_str2 += "<td>"+list_id+"</td>"
table_str2 += "<td>"+profile_name+"&nbsp;</td>"
table_str2 += "<td>"+profile_ssid+"&nbsp;</td>"
table_str2 += "<td>"+profile_ch+"&nbsp;</td>"
table_str2 += "<td>"+sw_auth+"&nbsp;</td>"
table_str2 += "<td>"+sw_ency+"&nbsp;</td>"
table_str2 += "<td>"+profile_mac+"&nbsp;</td>"
table_str2 += "<td>"+profile_signal+"&nbsp;</td>"
table_str2 += "<td>"+profile_priority+"&nbsp;</td>"
table_str2 += "<td><input type='checkbox' name='profile_rule' "+enable_check+" disabled>&nbsp;</td>"
button = "<input type='button' id='_wary_0' value='Edit' onclick=\"edit_apn_account(this,'profile',"+xml_index+",'"+mode+"')\">";
button += "<input type='checkbox' name='profile_del' value='"+(index)+"'><span id=_wary_0>Select</span>";
table_str2 += "<td align='center'>"+button+"</td>";
table_str2 += "</tr>";
profile_namev.push(profile_name);
profile_index.push(index);
chk_add =  chk_add + 1;
sw_list[ary_index] = table_str2;
if(chk_add == profile_row_count){
table_str2 = "";
chk_add = 0;
ary_index = ary_index + 1;
}
}
}
/*
profile_count記錄目前使用數
在目前使用數小於最大使用數時，從0到目前使用數+1去找下一個記錄的位置
寫到profile_next_index
*/
profile_count = profile_index.length;
if(profile_count < profile_max_number){
for(var x=0;x<=profile_count;x++)
{
profile_name = xml_getvalue(xmlDoc,"profile_name",x);
if(profile_name == "")
{
profile_next_index = x;
break;
}
}
}
}
function sw_lists(val){
var table_str = "<table width=100% border=0 align=center cellpadding=0 cellspacing=0 class='staball'>"
table_str += "<tr xclass='staball_sum'>";
table_str += "<th width='3%'><span id='_0'>ID</span></th>";
table_str += "<th width='10%'><span id='_prim_0'>Profile Name</span></th>";
table_str += "<th width='10%'><span id='_wpa_0'>SSID</span></th>";
table_str += "<th width='5%'><span id='_stawireless_0'>Channel</span></th>";
table_str += "<th width='8%'><span id='_wiz30_0'>Authentication</span></th>";
table_str += "<th width='8%'><span id='_wiz30_0'>Encryption</span></th>";
table_str += "<th width='15%'><span id='_sta_0'>MAC Address</span></th>";
table_str += "<th width='5%'><span id='_sta_0'>Signal Strength</span></th>";
table_str += "<th width='5%'><span id='_lb_0'>Priority</span></th>";
table_str += "<th width='5%'><span id='_wary_0'>Enable</span></th>";
table_str += "<th width='15%'><span id='_sta_0'>Actions</span></th>";
table_str += "</tr>";
var table_str3 = "</table>";
var div = document.getElementById("sw_profile_list");
div.innerHTML = table_str + sw_list[val] + table_str3;
chgLang()
}
function add_account(obj,type,mode)
{
var action_url="";
if(mode=="wpa") action_url= "uplink_profile_rule.htm";
else  action_url= "uplink_profile2_rule.htm";
if(profile_count >= profile_max_number)
{
alert(GetElmCont("dhcp_0","Rule could not be added anymore."));
}
else
{
obj.form.action = action_url;
obj.form.Nprofilemod.value = 1; //add
obj.form.Nprofilelen.value = profile_count;
obj.form.Nrule.value = profile_next_index;
obj.form.submit();
}
}
function edit_apn_account(obj,type,idx,mode)
{
var action_url="";
if(mode=="wpa") action_url= "uplink_profile_rule.htm";
else  action_url= "uplink_profile2_rule.htm";
f = document.forms[0];
profile_url="/"+action_url+"?rc=&Nrule="+idx + "&Ncmd=" + f.Ncmd.value;
GURL(profile_url)
}
function del_tunnel(type,mode)
{
var tmp_del = document.getElementsByName(type+"_del");
var record_index = eval(type+"_index");
var record_csid = eval(type+"_record_csid");
var del_arr = new Array();
var index = 0;
var new_index = 0;
var action_url="";
if(mode=="wpa") action_url= "uplink_profile.htm";
else  action_url= "uplink_profile2.htm";
var sent_str =action_url+"?rc=";
var iframe = document.getElementById("ifm_"+type+"list");
for(var t=0;t<tmp_del.length;t++)
{
if(tmp_del[t].checked)
{
del_arr[index] = tmp_del[t].value;
index++;
}
}
if(del_arr.length == 0)
{
alert(GetElmCont("lb_0","Please choose one or more rule."));
return false;
}
for(var r=0;r<record_index.length;r++)
{
for(var n=0;n<del_arr.length;n++)
{
if(record_index[r] == del_arr[n])
{
record_index[r] = "";
}
}
}
for(var a=0;a<record_index.length;a++)
{
if(record_index[a] != "")
{
sent_str += "&"+record_csid[new_index]+"="+record_index[a];
new_index++;
}
}
for(var c=new_index;c<record_csid.length;c++)
{
sent_str += "&"+record_csid[c]+"=";
}
for(var r=0;r<del_arr.length;r++)
{
var tunlname_index = del_arr[r]-1;
if(type=="profile"){
sent_str += "&"+eval("profile_name_csid["+tunlname_index+"]")+"=";
sent_str += "&"+eval("profile_ssid_csid["+tunlname_index+"]")+"=";
sent_str += "&"+eval("profile_signal_csid["+tunlname_index+"]")+"=";
sent_str += "&"+eval("profile_priority_csid["+tunlname_index+"]")+"=";
sent_str += "&"+eval("profile_enable_csid["+tunlname_index+"]")+"=0";
}
}
if(mode=="wpa"){
sent_str += "&N8032000C=1";
}else{
sent_str += "&N8045000C=1";
}
GURL(sent_str);
}
var aplist_ary = [];
var authmodestr = new Array(
GetElmCont("wiz30_0","Open"),
GetElmCont("wiz30_0","Shared"),
GetElmCont("wary_0","Auto"),
GetElmCont("wiz30_0","WPA-PSK"),
GetElmCont("wiz30_0","WPA"),
GetElmCont("wiz30_0","WPA2-PSK"),
GetElmCont("wiz30_0","WPA2"),
GetElmCont("wiz30_0","WPA-PSK/WPA2-PSK"),
GetElmCont("wiz30_0","WPA/WPA2"),
"&nbsp;"
);
var encryptypestr = new Array(
GetElmCont("wary_0","None"),
GetElmCont("wiz30_0","WEP"),
"",
GetElmCont("wiz30_0","AES")
);
function createaplist(mode,xmlurl)
{
var alt=document.getElementById('_sr_');
$.ajax({
url:""+xmlurl,
type: 'GET',
dataType: 'xml',
error: function(){
alt.innerHTML="<font color=blue>"+GetElmCont('prim_0','Create AP List failed!')+"</font>"
},
success: function(xml){
var xmlarys = [];					/** 用來存放每組 ap 的資料 **/
$(xml).find("ap").each(function(i){
xmlarys[i] = [];
xmlarys[i][0]=$(this).children("ssid").text();    //取得子節點中的ssid
xmlarys[i][1]=$(this).children("channel").text(); //取得子節點中的channel
xmlarys[i][2]=$(this).children("quality").text(); //取得子節點中的quality
xmlarys[i][3]=$(this).children("auth").text();    //取得子節點中的auth
xmlarys[i][4]=$(this).children("encryp").text();  //取得子節點中的encryp
xmlarys[i][5]=$(this).children("mac").text();     //取得子節點中的mac
})
clkscanap(mode,2,xmlarys);
}})
}
var scan_time=["10","10"];
/*
fr=0,先下shellex,依scan_time等待秒數後進入fr=1
fr=1,讀取xml，將csid的值寫入陣列xmlarys
fr=2,依傳回來的陣列，寫入ap list的表格中
*/
function clkscanap(mode,fr,aplists)	//WDS、REPEATER、ADAPTER
{
var alt = document.getElementById('_sr_');				/** save 的提示 tag **/
var aptab = document.getElementById('aptab');
var aplist_table = document.getElementById('stab');		/** ap 列表的資料表格 id **/
var apcli_num = null;
aplist_ary.length=0;
var t=new Date();
ztv="&ZT="+t.getTime();
apcli_num = ((mode == "wpa") ? "3" : "4");
var timer = ((mode == "wpa") ? scan_time[0] : scan_time[1]);
timer = ((timer == "") ? 5000 : parseInt(timer)*1000);
if(arguments.length < 2){
fr=0;
}
if (fr == 0){
$.ajax({
url:"aplist.xml?rc=&Ncmd="+apcli_num+ztv
})
setTimeout(function(){
clkscanap(mode,1)
}, timer);
alt.innerHTML = "<font color=blue>"+GetElmCont('wpa_0','Scanning AP... Please wait a moment...')+"</font>";
alt.style.display = '';	                 /** wait訊息 **/
aptab.style.display = "none";     /** ap表格 **/
}
if (fr == 1){
createaplist(mode,"aplist.xml?Ncsid=" + apcli_num + ztv)
}
if (fr == 2){
if(aplists){
var startrow = 1;												/** 從第1 row 開始塞資料 **/
var xrowl = aplist_table.rows.length;							/** 取得目前 row 數目 **/
for(var k = 0; k < xrowl - startrow; k++){						/** 刪除表格的所有row **/
aplist_table.deleteRow(startrow);							/** 刪除第 startrow 個 row **/
}
for(var i = 0; i < aplists.length; i++){
var z = aplist_table.insertRow(i + startrow);				/** 從第 i+2 row 開始 insert row **/
var rad = ((aplists[i][3] == 4) || (aplists[i][3] == 6) || (aplists[i][3] == 8)) ? "disabled='disabled'" : ""; /** 當 auth 為 WPA, WPA2，則disable **/
z.insertCell(0).innerHTML = aplists[i][0];					//SSID
z.insertCell(1).innerHTML = aplists[i][1];					//CHANNEL
z.insertCell(2).innerHTML = aplists[i][2] + "%";			//QUALITY
var auth = aplists[i][3];
var ency = aplists[i][4];
if(auth==7) {auth = 5;}
if(ency==4) {ency = 3;}
if(ency==0||ency==1) {auth = 9;}
z.insertCell(3).innerHTML = authmodestr[auth]                    //AUTHENTICATION
z.insertCell(4).innerHTML = encryptypestr[ency]                    //ENCRYPTION
z.insertCell(5).innerHTML = aplists[i][5];					//MAC
var aplist = null;
apclist = "<input type='radio' id='r_select_"+ i +"' name='rr' ";
apclist += "onclick='copyCLIAP(\""
+ i + "\")'" + rad + ">";
z.insertCell(6).innerHTML = apclist;
z.align = "center";											/** 設定欄位樣式 **/
z.style.lineHeight = "22px";
}
aplist_ary=aplists;
setTabStyle('stab');											/** 加入邊框 **/
setTimeout("document.getElementById('aptab').style.display=''", 1000);
setTimeout("document.getElementById('_sr_').style.display='none'", 1000);
}
else{
alert(GetElmCont("wpa_6","Create AP List failed!"));
}
}
}
function copyCLIAP(information)
{
var ris =aplist_ary[information];
document.getElementById("id_nssid").value = ris[0].replace("&nbsp;","");
document.getElementById("id_wchannel").value = ris[1];
if(ris[3]==7)	document.getElementById("id_auth").value = 5;	//WPA2-PSK
else document.getElementById("id_auth").value = ris[3];
chgauthmode(document.getElementById("id_auth"));
if(ris[4]==4) document.getElementById("id_encry").value = 3; //AES
else document.getElementById("id_encry").value = ris[4];
chgencrytype(document.getElementById("id_encry"));
document.getElementById("id_apmac").value = ris[5];
}
function resta(mode)
{
f = document.forms[0];
Nmode = ((mode == "wpa") ? "1" : "2");
var pdu_enable=document.getElementById("c_pdu_enable")
var xml2="uplink_profile.xml?rc=&Nmode="+Nmode;
var t=new Date()
xmlDoc=loadXMLDocs(xml2+"&ZT="+t.getTime());
if(xmlDoc==null)return;
var profile_status="";
profile_status= xmlDoc.getElementsByTagName("profile_status")[0].childNodes[0];
if(profile_status != null)
{
profile_status = profile_status.nodeValue;
var current_sta=document.getElementById("sw_current");
current_sta.innerHTML=profile_status;
}
if(mode=='wpa') setTimeout("resta('wpa')",5000);
else  setTimeout("resta('wpa2')",5000);
}