<%
include("language.html")
if _SESSION["logined"] ~= nil then
%>
<%
local result = nil
local name = nil
if _GET["name"] ~= nil then
	name = _GET["name"]
	result = c_GetSMTP(name)
	if result == nil then
		print("<script>alert('SMTP configuration does not exist or no permission!');top.closewindow();</script>")
		exit()
	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 SMTP Config</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;}

button{
border:none;background:none;padding:0;
margin:0;width:auto;overflow:visible;text-align:center;
white-space:nowrap;height:24px;line-height:24px;
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{
font-size:8pt;
padding-left:12px;
background:url("images/button_left.gif") no-repeat 0 0;
}
<%if _COOKIE["admin_lang"] == "schinese" or _COOKIE["admin_lang"] == "tchinese" or _COOKIE["admin_lang"] == "japanese" then%>
button em{
font-style:normal;padding-right:14px;
background:url("images/button.gif") no-repeat 101% 0;
}
<%else%>
button em{
font-style:normal;padding-right:10px;
background:url("images/button.gif") no-repeat 100% 0;
}
<%end%>
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: 2px solid #E1E2E3;
		font: 12px/22px arial; cursor: hand; text-align: center; background-color: #EFEFEF; overflow:hidden; height:18px;
}

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


#tabDiv {
	width: 460px;
	margin: 0px auto 0px auto;
	position: relative;
	border:1px solid transparent;
	border:1px solid #E6E6E6;
}
#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 name = "";
var smtptest = false;
var ajaxlock = false;
var selectInput = "";
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_smtp':	doSubmit_result();break;
				}
			}
			catch(e)
			{
				alert("<%=LANG['error_ajax']%>");
			}
		}
		else
		{
			alert("<%=LANG['error_ajax']%>");
		}
	}
}

function doSubmit()
{
	var paramStr = "";
	name = $("name").value;
	if(name.length == 0 || name.search(/[\\\/\<\>\:\?\"\'\|\*\&\%]/i) != -1)
	{
		alert("<%=LANG['str_invalidname']%>");
		return false;
	}
	var sender_name = encodeURIComponent($("sender_name").value);
	var sender_email = $("sender_email").value;
	var need_auth = ($("need_auth").checked ? 1:0);
	var smtp_server = $("smtp_server").value;
	if(smtp_server.length == 0)
	{
		alert("<%=LANG['str_invalid_smtpserver']%>");
		return false;
	}
	var smtp_port = $("smtp_port").value;
	if(smtp_port.search(/[\D]/i) != -1 || parseInt(smtp_port) < 1 || parseInt(smtp_port) > 65535)
	{
		alert("<%=LANG['str_invalid_smtpport']%>");
		return false;
	}
	var smtp_username = encodeURIComponent($("smtp_username").value);
	var smtp_password = encodeURIComponent($("smtp_password").value);
	var use_ssl = ($("use_ssl").checked ? 1:0);
	paramStr = "name="+name+"&sender_name="+sender_name+"&sender_email="+sender_email+"&need_auth="+need_auth+"&smtp_server="+smtp_server+"&smtp_port="+smtp_port+"&smtp_username="+smtp_username+"&smtp_password="+smtp_password+"&use_ssl="+use_ssl+"&r="+Math.random();
	<%
	if _GET["name"] ~= nil then
		print("paramStr += '&modify=1';")
	end
	%>
	ajaxRequest("admin_add_smtp",paramStr);
	smtptest = false;
	$("waitingdiv").style.display = "";
}

function testSMTP()
{
	var paramStr = "";
	name = $("name").value;
	if(name.length == 0 || name.search(/[\\\/\<\>\:\?\"\'\|\*\&]/i) != -1)
	{
		alert("<%=LANG['str_invalidname']%>");
		return false;
	}
	var sender_name = $("sender_name").value;
	var sender_email = $("sender_email").value;
	var need_auth = ($("need_auth").checked ? 1:0);
	var smtp_server = $("smtp_server").value;
	if(smtp_server.length == 0)
	{
		alert("<%=LANG['str_invalid_smtpserver']%>");
		return false;
	}
	var smtp_port = $("smtp_port").value;
	if(smtp_port.search(/[\D]/i) != -1 || parseInt(smtp_port) < 1 || parseInt(smtp_port) > 65535)
	{
		alert("<%=LANG['str_invalid_smtpport']%>");
		return false;
	}
	var smtp_username = $("smtp_username").value;
	var smtp_password = $("smtp_password").value;
	var use_ssl = ($("use_ssl").checked ? 1:0);
	paramStr = "name="+name+"&sender_name="+sender_name+"&sender_email="+sender_email+"&need_auth="+need_auth+"&smtp_server="+smtp_server+"&smtp_port="+smtp_port+"&smtp_username="+smtp_username+"&smtp_password="+smtp_password+"&use_ssl="+use_ssl+"&testsmtp=1&r="+Math.random();
	ajaxRequest("admin_add_smtp",paramStr);
	smtptest = true;
	$("waitingdiv").style.display = "";
}

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

	if(smtptest == true)
	{
		if(result == "SMTP_OK")
		{
			alert("<%=LANG['str_testsmtp_ok']%>");
			return;
		}
		else if(result == "")
		{
			window.location.reload();
			return;
		}
		else
		{
			showMessagebox("<%=LANG['str_field_mailerr']%>","<table width='100%' height='100%' border='0' cellpadding='0' cellspacing='0' style='background-color:#E6E6E6;padding:8px;'><tr><td align='left'><img src='images/vista_warning.gif' align='absmiddle'><br><textarea readonly  style='width:430px;height:180px;border:0;background-color:#E6E6E6;word-break:keep-all;overflow:hidden;'>"+removeXSS(result)+"</textarea></td></tr><tr><td><button id='btn_submit' type='submit' onclick='top.closewindow();' class='button1'><span><em><%=LANG['button_submit']%></em></span></button>&nbsp;<button id='btn_cancel' onclick='top.closewindow();' class='button1'><span><em><%=LANG['button_cancel']%></em></span></button><br><br></td></tr></table>",null,450,300);
			return;
		}
	}
	else
	{
		if(result == "0" || result == 0)
		{
			alert("<%=LANG['str_existed_smtp']%>");
		}
		else
		{
			top.closewindow(name);
		}
	}
}
</script>
</head>
<body style="background-color:#E6E6E6;">

<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_smtpconfiure"]%></a></li>
  </ul>
  <div id="tagContent" class="tagContent">
	<div id="tagContent1">
		<table cellSpacing="0" cellPadding="0" border="0">
		<tr><td><%=LANG["title_name"]%>:</td><td><input id="name" type="text" <% if name ~= nil then print("value='"..name.."' disabled") end%> style="width:120px;"></td></tr>
		<tr><td><%=LANG["str_sendername"]%>:</td><td><input id="sender_name" type="text" style="width:240px;" value="<% if result ~= nil then print(result["sender_name"]) end%>"></td></tr>
		<tr><td><%=LANG["str_senderemail"]%>:</td><td><input id="sender_email" type="text" style="width:240px;" value="<% if result ~= nil then print(result["sender_email"]) end%>"></td></tr>
		<tr><td><%=LANG["str_needauth"]%>:</td><td><input id="need_auth" type="checkbox" <% if result ~= nil then if result["need_auth"] == true then print("checked") end end%>><%=LANG["str_yes"]%></td></tr>
		<tr><td><%=LANG["str_smtpserver"]%>:</td><td><input id="smtp_server" type="text" style="width:240px;" value="<% if result ~= nil then print(result["smtp_server"]) end%>"></td></tr>
		<tr><td><%=LANG["str_smtpport"]%>:</td><td><input id="smtp_port" type="text" style="width:240px;" value="<% if result ~= nil then print(result["smtp_port"]) else print("25") end%>"></td></tr>
		<tr><td><%=LANG["str_smtpusername"]%>:</td><td><input id="smtp_username" type="text" style="width:240px;" value="<% if result ~= nil then print(result["smtp_username"]) end%>"></td></tr>
		<tr><td><%=LANG["str_smtppassword"]%>:</td><td><input id="smtp_password" type="password" autocomplete="new-password" style="width:240px;" value="<% if result ~= nil then print(result["smtp_password"]) end%>"></td></tr>
		<tr><td><%=LANG["str_smtpusessl"]%>:</td><td><input id="use_ssl" type="checkbox" <% if result ~= nil then if result["use_ssl"] == true then print("checked") end end%>><%=LANG["str_yes"]%>&nbsp;&nbsp;&nbsp;&nbsp;
		<button id="testsmtp_btn" onclick='testSMTP();'><span style="vertical-align:middle;"><em> <%=LANG["str_test_smtp"]%></em></span></button>
		</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";

$("sender_name").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>