<%
include("language.html")
if _SESSION["logined"] ~= nil then

local IpList = c_GetLocalIpList()

local httpport = c_GetAdminOptionInt(ADMIN_OPTION_LISTEN_PORT_INT)
local httpsecure = c_GetAdminOptionInt(ADMIN_OPTION_SECURE_ENABLE_INT)
local httpcert = c_GetAdminOptionStr(ADMIN_OPTION_SSL_NAME_STR)
local listenerip = c_GetAdminOptionStr(ADMIN_OPTION_LISTEN_IP_STR)
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Admin General setting</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
a {text-decoration:none;}
a,a:visited {color:#000;background:inherit;}
html, body {height: 100%;margin:0px;padding:0px;}
<%if _COOKIE["admin_lang"] == "schinese" or _COOKIE["admin_lang"] == "tchinese" or _COOKIE["admin_lang"] == "japanese" then%>
body,td,span {font:12px/20px Verdana, Geneva, Arial, Helvetica, sans-serif;}
<%else%>
body,td,span {font:11px/20px Verdana, Geneva, Arial, Helvetica, sans-serif;}
<%end%>
img {border:0px;}
input,select,textarea{margin:4px;}
input,textarea{background-color:expression((this.disabled && this.disabled==true)?"#D4D0C8":"")}

button{
border:none;background:none;padding:0;
margin:0;width:auto;overflow:visible;text-align:center;	
white-space:nowrap;height:24px;line-height:22px;
cursor:pointer;
color:#000;
margin-left:2px;
margin-top:2px;
}
button span, button em{
display:block;height:24px;line-height:22px;margin:0;
}	
button span{
padding-left:14px;
background:url("images/button_left.gif") no-repeat 0 0;
}
button em{
font-style:normal;padding-right:14px;
background:url("images/button.gif") no-repeat 100% 0;
}
button:hover{
filter:alpha(opacity=60);
opacity:0.60;
color:#5488C9;
}
button span:hover{
filter:alpha(opacity=60);
opacity:0.60;
color:#5488C9;
}
.buttonhover{
filter:alpha(opacity=60);
opacity:0.60;
color:#5488C9;
}


.listhead
{
		border-top: 1px solid white; border-right: 1px solid #ACA899; border-left: 1px solid white; border-bottom: 3px solid #CBC7B8;
		font: 12px/22px arial; cursor: hand; text-align: center; background-color: #EBEADB; overflow:hidden; height:18px;
}

.listhead2
{
		border-top: 1px solid white; border-right: 1px solid #ACA899; border-left: 1px solid white; border-bottom: 3px solid #FCC247;
		font: 12px/22px arial; cursor: hand; text-align: center; background-color: #FAF9F4; overflow:hidden; height:18px;
}


#tabDiv {
	width: 520px;
	margin: 0px auto 0px auto;
	position: relative;
	border:1px solid transparent;
	border:1px solid #E7E6DA;
}
#tabDiv ul {
	padding: 0px;
	position: absolute;
	margin: 4px;
}
#tabDiv li {
	list-style-type: none;
	float: left;
	margin-right: 1px;
	height: 24px;
	line-height: 24px;
	padding: 0px;
}
#tabDiv li a {
	display: block;
	text-decoration: none;
	padding-right: 10px;
	padding-left: 10px;
}
.tagContent {
	clear:both;
	float:none;
	margin-top: 27px;
	border: 1px solid #919B9C;
}
.tagContent div {
	margin-top: 0px;
	height: 290px;
	overflow-y:auto;
	background-image: url('images/tabbg.gif');
	background-repeat: repeat-x;
	background-color: #fff;
	padding: 12px;
	display: none;
}
.selectedTag {
	background-image: url('images/tableft.gif');
	background-repeat: no-repeat;
	background-position: left top;
}
.selectedTag a {
	background-image: url('images/tabright.gif');
	background-repeat: no-repeat;
	background-position: right top;
	color:#000;
}
.normalTag {
	background-image: url('images/tableft.gif');
	background-repeat: no-repeat;
	background-position: left bottom;
}
.normalTag a {
	background-image: url('images/tabright.gif');
	background-repeat: no-repeat;
	background-position: right bottom;
	color:#49658F
}
.emptyTag { width:20px;}
</style>

<script language="javascript" src="include/common.js"></script>
<script language="javascript">
var httpport = <%=httpport%>;
var listenerip = "<%=listenerip%>";
var httpsecure = <% if httpsecure == 1 then print("1") else print("0") end %>;
var cert_valid = false;
var new_url = "";

var IpList = "<%=IpList%>";
var IpListArray = IpList.split(",");

var ajaxlock = false;
var xmlhttp,page,post;
xmlhttp = isIE ? new ActiveXObject('Microsoft.XMLHTTP'):new XMLHttpRequest();

function ajaxRequest(pagename,poststring,method)
{
	if(ajaxlock == false)
	{	
		delete xmlhttp;
		xmlhttp = isIE ? new ActiveXObject('Microsoft.XMLHTTP'):new XMLHttpRequest();

		ajaxlock = true;
		page = pagename;
		post = poststring;

		if(method == "GET")
			xmlhttp.open("GET",page+".html"+post);
		else
			xmlhttp.open("POST",page+".html");

		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.onreadystatechange = ajaxResponse;
		if(method != "GET")
			xmlhttp.send(post);
		else
			xmlhttp.send("");
		$("waitingdiv").style.display = "";
	}
}

function ajaxResponse()
{
	ajaxlock = false;
	$("waitingdiv").style.display = "none";
	if (xmlhttp.readyState == 4)
	{
		if (xmlhttp.status == 200)
		{
			try
			{
				switch(page)
				{
					case 'admin_changelistener':	doSubmit_result();break;
					case 'admin_admin_sslcert':		changeSslcert_result();break;
				}
			}
			catch(e)
			{
				alert("<%=LANG['error_ajax']%>");
			}
		}
		else
		{
			alert("<%=LANG['error_ajax']%>");
		}
	}
}


function doSubmit()
{
	if($("httpport").value.search(/[\D]/i) != -1 || parseInt($("httpport").value) < 1 || parseInt($("httpport").value) > 65535)
	{
		alert("port number must more than 0, less than 65535!");
		$("httpport").focus();
		return false;
	}

	var new_httpport = parseInt($("httpport").value);
	var new_address = $("listener_address").value;
	var new_httpsecure = ($("httpsecure").checked ? 1:0);
	if(new_httpport == httpport && new_httpsecure == httpsecure && new_address == listenerip)
	{
		top.closewindow(1);
		return false;
	}
	else
	{
		if(new_httpsecure == 1 && cert_valid == false)
		{
			new_httpsecure = 0;
			top.closewindow(1);
			return false;
		}

		if(new_address != "*" && new_address != "All IPv4 addresses" && new_address != listenerip)
		{
			if(new_httpsecure)
				new_url = "https://"+new_address+":"+new_httpport;
			else
				new_url = "http://"+new_address+":"+new_httpport;

			if(confirm("The web admin address will become "+new_url+", continue?"))
			{
			}
			else
			{
				new_url = "";
				return false;
			}
		}
		else
		{
			new_url = "";
		}

		httpport = new_httpport;
		httpsecure = new_httpsecure;
		listenerip = new_address;

		ajaxRequest("admin_changelistener","httpport="+httpport+"&httpsecure="+httpsecure+"&listenerip="+listenerip+"&r="+Math.random());
		return true;
	}
}

function doSubmit_result()
{
	var result = xmlhttp.responseText;
	if(result == "0" || result == 0)
	{
		alert("<%=LANG['str_adminlistener_tip1']%>");
	}
	else
	{
		alert("<%=LANG['str_adminlistener_tip2']%>");
		if(location.hostname != "127.0.0.1" || navigator.userAgent.indexOf('MSIE 7.0;') == -1)
		{
			if(new_url == "")
			{
				if(httpsecure)
					top.location = "https://"+location.hostname+":"+httpport;
				else
					top.location = "http://"+location.hostname+":"+httpport;
			}
			else
			{
				top.location = new_url;
			}
		}
		else
		{
			top.closewindow();
		}
	}
}


function AddSSLCert()
{
	var frametag = new Date().getTime();
	showMessagebox("<%=LANG['str_addsslcert']%>","<iframe src='admin_addcert_form.html?seeds=" + Math.random() +"' width=480 height=350 border=0 frameborder=0 id='add"+frametag+"'></iframe>",callback_return,480,350);
}

function callback_return(value)
{
	if(value != null)
	{
		var objOption = document.createElement("OPTION");
		if(!window.ActiveXObject)
			objOption.innerHTML = value;
		else
			objOption.innerText = value;
		objOption.value = value;
		objOption.selected = true;
		$("sslcert").appendChild(objOption);
		SaveSSLCert(value);
	}
	$("hiddenInput").focus();
	$("hiddenInput").blur();
	return true;
}

function SaveSSLCert(value)
{
	ajaxRequest("admin_admin_sslcert","ssl_cert="+value);
}

function changeSslcert_result()
{
	var result = xmlhttp.responseText;
	if(result == "0" || result == 0)
	{
		cert_valid = false;
		$("cert_valid").src = "images/false.gif";
		alert("<%=LANG['str_adminlistener_tip3']%>");
	}
	else
	{
		cert_valid = true;
		$("cert_valid").src = "images/true.gif";
	}
}

</script>
</head>
<body style="background-color:#E7E6DA;">

<div id="waitingdiv" style="position:absolute; z-index:100; top:0px; left:0px; width:100%; height:100%; background:#777; filter:alpha(opacity=60); opacity:0.60; display:none;" align="center">
<div style="position:absolute; top:20%; left:20%; width:250px; height:100px;"><img src="images/loading2.gif" border="0">
<H2><b><%=LANG["title_updating"]%></b></H2></div></div>

<div id="tabDiv">
  <ul id="tags">
  	<li class="emptyTag"></li>
	<li class="normalTag"><a href="javascript:void(0)" onfocus="this.blur()" onclick="switchTag('tagContent1',this)"><%=LANG["str_modifylistener"]%></a></li>
  </ul>
  <div id="tagContent" class="tagContent">
	<div id="tagContent1">
		<table cellSpacing="0" cellPadding="0" border="0" width="100%">
		<tr><td height="40" colspan="2">
		<input id="httpsecure" type="checkbox" <% if httpsecure == 1 then print("checked") end %>> <%=LANG["str_usesslremote"]%>
		</td></tr>
		<tr><td width="120" height="40">
		<%=LANG["title_listenerport"]%>:
		</td><td>
		<input id="httpport" type="text" style="width:140px;" value="<%=httpport%>" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')">
		</td></tr>
		<tr><td height="40">
		<%=LANG["title_listeneraddr"]%>:
		</td><td>
		<select id="listener_address" style="width:150px">
		</select>
		(<%=LANG["str_listeneraddr_tip"]%>)
		</td></tr>
		<tr><td height="40">
		<%=LANG["str_sslcert"]%>:
		</td><td>
		<select id="sslcert" style="width:150px" onchange="SaveSSLCert(this.value);">
		<%
			local certexist = false
			local stateimg = "false.gif"
			print("<option value=''></option>")
			for _,cert in pairs(c_GetSSLCertList()) do
				if httpcert == cert["name"] then
					certexist = true
					print("<option value='"..cert["name"].."' selected>"..cert["name"].."</option>")
				else
					print("<option value='"..cert["name"].."'>"..cert["name"].."</option>")
				end
			end

			if httpcert ~= nil then
				if c_CheckSSLCertificate(httpcert) == true then
					stateimg = "true.gif"
				else
					stateimg = "false.gif"
				end
			end
		%>	
		</select>
		<img id='cert_valid' src='images/<%=stateimg%>'>&nbsp;&nbsp;<button onclick="AddSSLCert();"><span><em><%=LANG["button_addsslcert"]%></em></span></button>
		<input id="hiddenInput" type="text" style="width:0px;height:0px; border:0px;">
		</td></tr>
		</table>
	</div>
  </div>	
</div>

<div style="padding:3px;text-align:right;margin-right:10px;">
<button type='submit' onclick='doSubmit();'><span><em><%=LANG["button_submit"]%></em></span></button>&nbsp;
<button onclick='top.closewindow();'><span><em><%=LANG["button_cancel"]%></em></span></button>
</div>

<script type="text/javascript">

$("tagContent1").style.display="block";
$("tags").getElementsByTagName("li")[1].className="selectedTag";
$("hiddenInput").focus();
$("hiddenInput").blur();

for(var i=0;i<IpListArray.length;i++)
{
	var nowIpaddr = trim(IpListArray[i]);
	if(nowIpaddr == "All IPv6 addresses")
		break;

	$("listener_address").options.add(new Option(nowIpaddr, nowIpaddr));
	if(nowIpaddr == listenerip)
		$("listener_address").options[i].selected = true;

}

function switchTag(tagContent,obj){

	for(var i=1; i<$("tags").getElementsByTagName("li").length; i++)
	{
		$("tags").getElementsByTagName("li")[i].className="normalTag";
	}
	obj.parentNode.className="selectedTag";
	
	for(var j=0; j<$("tagContent").getElementsByTagName("div").length; j++)
	{
		$("tagContent").getElementsByTagName("div")[j].style.display="none";
	}
	$(tagContent).style.display="block";
	selectedRow = null;
	lastsortcol = -1;
}

<% if stateimg == "true.gif" then %>
	cert_valid = true;
<% end %>
</script>
</body>
</html>

<% 
else
	print("<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>")
	print("<script>alert('"..LANG["session_expire"].."');top.location='admin_login.html';</script>")
end 
%>

<noscript><center><H2><%=LANG["error_nojavascript"]%></H2></center></noscript>