<%
include("language.html")
if _SESSION["logined"] ~= nil then
%>
<%
local result = nil
local host = nil
local running = false
local licenseinfo = ""

if _GET["host"] ~= nil then
	host = _GET["host"]
	result = c_GetGateway(host)
	if result == nil then
		print("<script>alert('Gateway does not exist or no permission!');top.closewindow();</script>")
		exit()
	end

	if _GET["status"] ~= nil and _GET["status"] == "1" then
		running = true
		licenseinfo = c_GatewayInfo(host)
	end

end
%>
<!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>Add/Modify gateway</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
html, body {height: 100%;}
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: 470px;
	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: 310px;
	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 ajaxlock = false;
var gatewaytest = false;
var isNewHost = true;

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("");
	}
}

function ajaxResponse()
{
	ajaxlock = false;
	if (xmlhttp.readyState == 4)
	{
		$("waitingdiv").style.display = "none";

		if (xmlhttp.status == 200)
		{
			try
			{
				switch(page)
				{
					case 'admin_add_gateway':	DoSubmit_result();break;
					case 'admin_gateway_register':	Register_result();break;
				}
			}
			catch(e)
			{
				alert("<%=LANG['error_ajax']%>");
			}
		}
		else
		{
			alert("<%=LANG['error_ajax']%>");
		}
	}
}

function DoSubmit()
{
	var paramStr = "";
	var host = $("host").value;
	var port = $("port").value;
	var pass = encodeURIComponent($("pass").value);
	var description = encodeURIComponent($("description").value);
	var enable = ($("enable").checked ? 1:0);
	var failover = ($("failover").checked ? 1:0);
	var singleip = ($("singleip").checked ? 1:0);

	if(host.length == 0)
	{
		alert("<%=LANG['str_invalid_host']%>");
		return false;
	}
	if(port.search(/[\D]/i) != -1 || parseInt(port) < 1 || parseInt(port) > 65535)
	{
		alert("<%=LANG['str_invalid_port']%>");
		return false;
	}

	paramStr = "host="+host+"&port="+port+"&pass="+pass+"&description="+description+"&enable="+enable+"&failover="+failover+"&singleip="+singleip+"&r="+Math.random();
	
	<%
	if _GET["host"] ~= nil then
		print("paramStr += '&modify=1';")
		print("isNewHost = false;")
	end
	%>

	gatewaytest = false;
	ajaxRequest("admin_add_gateway",paramStr);
}

function DoSubmit_result()
{
	var result = xmlhttp.responseText;

	if(gatewaytest == true)
	{
		if(result == "GATEWAY_OK")
		{
			alert("<%=LANG['str_testgateway_ok']%>");
			return;
		}
		else if(result == "GATEWAY_ERR")
		{
			alert("<%=LANG['str_testgateway_fail']%>");
			return;
		}
	}

	if(result == "0" || result == 0)
	{
		alert("<%=LANG['str_existed_gateway']%>");
	}
	else if(result == "-1" || result == -1)
	{
		alert("<%=LANG['str_invalid_gateway']%>");
	}
	else
	{
		if(isNewHost == true)
		{
			alert("<%=LANG['str_addgateway_ok']%>");
		}

		top.closewindow(host);
	}
}

function callback_return(value)
{
	try
	{
		$("host").focus();
	}
	catch(e){}
}

function TestGateway()
{
	var paramStr = "";
	var host = $("host").value;
	var port = $("port").value;
	var pass = encodeURIComponent($("pass").value);

	if(host.length == 0)
	{
		alert("<%=LANG['str_invalid_host']%>");
		return false;
	}
	if(port.search(/[\D]/i) != -1 || parseInt(port) < 1 || parseInt(port) > 65535)
	{
		alert("<%=LANG['str_invalid_port']%>");
		return false;
	}

	paramStr = "host="+host+"&port="+port+"&pass="+pass+"&test=1&r="+Math.random();
	gatewaytest = true;
	ajaxRequest("admin_add_gateway",paramStr);
	$("waitingdiv").style.display = "";
}

function formatTimeDays(time) {

	days = parseInt(time / (3600*24) );
	labelDays = "";
	hours = parseInt(time / 3600);
	if(days > 0)
	{
		hours = parseInt( (time-3600*24*days) / 3600);
		labelDays = days + " days, ";
	}
	if(hours < 10)
	{
		hours = "0"+hours;
	}
	seconds = time % 3600;
	minutes = parseInt(seconds / 60);
	if(minutes < 10)
	{
		minutes = "0"+minutes;
	}
	seconds = seconds % 60;
	if(seconds < 10)
	{
		seconds = "0"+seconds;
	}

	return labelDays + hours + ":" + minutes + ":" + seconds;
}

function RegisterGateway()
{
	var licenseinfo = "<%=licenseinfo%>";
	var strInfo = "";
	if(licenseinfo == "")
	{
		alert("<%=LANG['str_gateway_stopped']%>");
		return false;
	}


	var arrayInfo = licenseinfo.split("||");
	if(arrayInfo.length == 8)
	{
		strInfo += "<fieldset style='border-color: #999; border-style: solid; border-width: 1px;'><legend><b>Wing Gateway <%=LANG['str_registration_info']%></b></legend><div style='padding:5px;line-height:25px;'>";
		if(arrayInfo[0] == "1")
		{
			var strCopyNum = arrayInfo[2] + " copies";
			if(arrayInfo[2] == "0")
			{
				strCopyNum = "Site license";
			}
			else if(arrayInfo[2] == "1")
			{
				strCopyNum = arrayInfo[2] + " copy";
			}
			strInfo += "<span style=\"color:#0000FF\"><%=LANG['str_registered_to']%>:  <b>"+arrayInfo[1]+"</b></span><br><span style=\"color:#0000FF\"><%=LANG['str_copies']%>:  <b>"+strCopyNum+"</b></span><br>";
		}
		else if(arrayInfo[0] == "2")
		{
			strInfo += "<span style=\"color:#0000FF\"><%=LANG['str_edition']%>:  <b><%=LANG['str_trial_version']%></b></span><br><span style=\"color:#0000FF\"><%=LANG['str_left_days']%>:  <b>"+arrayInfo[3]+"</b></span><br>";
		}
		else if(arrayInfo[0] == "3")
		{
			strInfo += "<span style=\"color:#FF0000\"><%=LANG['str_evaluation_expired2']%></span><br>";
		}
		else if(arrayInfo[0] == "4")
		{
			strInfo += "<span style=\"color:#FF0000\"><%=LANG['str_invalid_license']%></span><br>";
		}

		strInfo += "<span style=\"color:#666\"><%=LANG['str_installed_version']%>:  <b>"+arrayInfo[7]+"</b></span><br><span style=\"color:#666\"><%=LANG['str_server_runtime']%>:  <b>"+formatTimeDays(arrayInfo[4])+"</b></span><br><span style=\"color:#666\"><%=LANG['str_total_session']%>:  <b>"+arrayInfo[5]+"</b></span><br><span style=\"color:#666\"><%=LANG['str_connected_wingftp']%>:  <b>"+arrayInfo[6]+"</b></span></div></fieldset>";
	}

	showMessagebox("<%=LANG['str_register']%> Wing Gateway","<table width='100%' border='0' cellpadding='0' cellspacing='0' style='background-color:#E7E6DA;padding:10px;'><tr><td align=left>"+strInfo+"</td></tr><tr><td align=left><%=LANG['str_enter_regcode']%>:<br><textarea style='width:420px;height:80px;' id='regcode'></textarea></td></tr><tr><td height=20><button id='btn_submit' type='submit' onclick='return Register_submit()'><span><em><%=LANG['button_register']%></em></span></button>&nbsp;<button id='btn_purchase' onclick='return Purchase();'><span><em><%=LANG['button_purchase']%></em></span></button>&nbsp;<button id='btn_cancel' onclick='top.closewindow();'><span><em><%=LANG['button_cancel']%></em></span></button></td></tr></table>",null,455,365);
	top.$("regcode").focus();

	top.$("btn_submit").onclick = function(){return Register_submit();};
	top.$("btn_purchase").onclick = function(){return Purchase();};
	top.$("btn_cancel").onclick = function(){top.closewindow();};

}

function Purchase()
{
	window.open("https://www.wftpserver.com/ordergateway.htm");
}

function Register_submit()
{
	var strHost = '<%=_GET["host"] or ""%>';
	var strCode = trim(top.$("regcode").value);
	if(strCode == "")
	{
		alert("<%=LANG['str_invalid_regcode']%>");
		return;
	}
	regcode = strCode;
	ajaxRequest("admin_gateway_register","host="+strHost+"&regcode="+encodeURIComponent(regcode)+"&r="+Math.random());
}

function Register_result()
{
	var result = trim(xmlhttp.responseText);
	if(result == "true")
	{
		alert("<%=LANG['str_register_success']%>");
		top.closewindow();
		top.closewindow();
	}
	else
	{
		alert("<%=LANG['str_register_failed']%>");
	}
}
</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); -moz-opacity: 0.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_gateway_configure"]%></a></li>
  </ul>
  <div id="tagContent" class="tagContent">
	<div id="tagContent1">
		<table width="100%" cellSpacing="0" cellPadding="0" border="0">
		<tr><td width="25%"><%=LANG["str_enable_gateway"]%>:</td><td><input id="enable" type="checkbox" <% if result ~= nil then if result["gateway_enable"] == true then print("checked") end else print("checked") end%>><b><%=LANG["str_yes"]%></b></td></tr>
		<tr><td><%=LANG["str_gateway_host"]%>:</td><td><input id="host" type="text" <% if host ~= nil then print("value='"..host.."' disabled") else print("onmouseover='this.focus();'") end%> style="width:230px;"></td></tr>
		<tr><td><%=LANG["str_port"]%>:</td><td><input id="port" type="text" style="width:230px;" value="<% if result ~= nil then print(result["gateway_port"]) else print("7466") end%>"></td></tr>
		<tr><td><%=LANG["str_password"]%>:</td><td><input id="pass" type="password" style="width:230px;" value="<% if result ~= nil then print(specialhtml_encode(result["gateway_password"])) end%>"></td></tr>
		<tr><td><%=LANG["title_description"]%>:</td><td><input id="description" type="text" style="width:230px;" value="<% if result ~= nil then print(specialhtml_encode(result["gateway_description"])) end%>"></td></tr>
		<tr><td><%=LANG["str_gateway_failover"]%>:</td><td><input id="failover" type="checkbox" <% if result ~= nil then if result["gateway_failover"] == true then print("checked") end else print(" ") end%>><b><%=LANG["str_yes"]%></b>&nbsp;&nbsp;(<%=LANG["str_failover_tip"]%>)</td></tr>
		<tr><td><%=LANG["str_gateway_singleip"]%>:</td><td><input id="singleip" type="checkbox" <% if result ~= nil then if result["gateway_singleip"] == true then print("checked") end else print(" ") end%>><b><%=LANG["str_yes"]%></b></td></tr>
		<tr>
		<td colspan="2" height="60">
		<%
			if _GET["host"] ~= nil then
		%>
		<button onclick='RegisterGateway();'><span><em><%=LANG["str_register"]%> Wing Gateway</em></span></button>
		<%
			else
		%>
		<button onclick='TestGateway();'><span><em><%=LANG["str_conn_test"]%></em></span></button>
		<%
			end
		%>
		</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";

$("enable").focus();

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;
}

</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>