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

local domain = _GET["domain"]
if domain ~= nil and _GET["type"] ~= nil and _GET["event"] ~= nil and _GET["eventName"] ~= nil then
	local bExecute,strExeFile,strParam,bLogfile,strLogFileName,strLogText,bSendMail
	local strMailTo,strSubject,strPlainText,strAttachFile,nPriority,bLua,strLua
	local type = string.lower(_GET["type"])
	local eventID = tonumber(_GET["event"])
	local eventName = _GET["eventName"]
	local luaScriptFirst = false
	if type == "http" then
		bExecute,strExeFile,strParam,bLogfile,strLogFileName,strLogText,bSendMail,strMailTo,strSubject,strPlainText,strAttachFile,nPriority,bLua,strLua = c_GetHTTPEvent(domain, eventID)
		if eventID >= WEB_BEFORE_LOGIN_EVENT then
			luaScriptFirst = true
		end
	elseif type == "ssh" then
		bExecute,strExeFile,strParam,bLogfile,strLogFileName,strLogText,bSendMail,strMailTo,strSubject,strPlainText,strAttachFile,nPriority,bLua,strLua = c_GetSSHEvent(domain, eventID)
		if eventID >= SSH_BEFORE_LOGIN_EVENT then
			luaScriptFirst = true
		end
	else
		bExecute,strExeFile,strParam,bLogfile,strLogFileName,strLogText,bSendMail,strMailTo,strSubject,strPlainText,strAttachFile,nPriority,bLua,strLua = c_GetFTPEvent(domain, eventID)
		if eventID >= FTP_BEFORE_LOGIN_EVENT then
			luaScriptFirst = true
		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>Event Editor</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;}
img {border:0px;}
<%if _COOKIE["admin_lang"] == "schinese" or _COOKIE["admin_lang"] == "tchinese" or _COOKIE["admin_lang"] == "japanese" then%>
body,td,span,textarea {font:12px Verdana, Geneva, Arial, Helvetica, sans-serif;}
<%else%>
body,td,span,textarea {font:11px Verdana, Geneva, Arial, Helvetica, sans-serif;}
<%end%>
input,select,textarea{margin:4px;}
input,textarea{background-color:expression((this.disabled && this.disabled==true)?"#D4D0C8":"")}
td{line-height:160%; text-align:left;}

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 {
	height: 380px;
	width: 560px;
	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;}

.selectContent{margin:0;padding:0;position:absolute;background:#eee;top:130px;left:120px;border:1px solid #333;overflow:hidden;color:#999;}
.selectContent ul{margin:0;padding:0;border:0;background:none;cursor:pointer;padding:0;color:#666}
.selectContent ul li{margin:0;padding:0;padding:0 5px;line-height:20px;}
.optionSelected{background:#666;color:#eef}
</style>

<script language="javascript" src="include/common.js"></script>
<script language="javascript">
var smtpconfig = "<%=c_GetOptionStr(domain,DOPTION_SMTP_NAME_STR)%>";
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("");
	}
}

function ajaxResponse()
{
	if (xmlhttp.readyState == 4)
	{
		ajaxlock = false;
		if (xmlhttp.status == 200)
		{
			try
			{
				switch(page)
				{
					case 'admin_modifyevent':	modifyEvent_result();break;
				}
			}
			catch(e)
			{
				alert("<%=LANG['error_ajax']%>");
			}
		}
		else
		{
			alert("<%=LANG['error_ajax']%>");
		}
	}
}

function modifyEvent_result()
{
	var result = xmlhttp.responseText;
	if(result == "0" || result == 0)
	{
		alert("<%=LANG['str_modifyevent_fail']%>");
	}
	else if(result == "1" || result == 1)
	{
		top.closewindow(1);
	}
	else
	{
		alert("<%=LANG['str_lua_error']%>\n"+result);
	}
}

function eventSubmit()
{
	var paramStr = "";
	var domain = $("domain").value;
	var type = $("type").value;
	var event = $("event").value;
	var bExecute = ($("bExecute").checked ? 1:0);
	var strExeFile = encodeURIComponent($("strExeFile").value);
	var strParam = encodeURIComponent($("strParam").value);
	var bLogfile = ($("bLogfile").checked ? 1:0);
	var strLogFileName = encodeURIComponent($("strLogFileName").value);
	var strLogText = encodeURIComponent($("strLogText").value);
	var bSendMail = ($("bSendMail").checked ? 1:0);
	if($("strMailTo").value == "<%=LANG['str_multiple_emails']%>")
	{
		$("strMailTo").value = "";
	}
	var strMailTo = encodeURIComponent($("strMailTo").value);
	var strSubject = encodeURIComponent($("strSubject").value);
	var strPlainText = encodeURIComponent($("strPlainText").value);
	var strAttachFile = encodeURIComponent($("strAttachFile").value);
	var nPriority = $("nPriority").value;
	var bLua = ($("bLua").checked ? 1:0);
	var strLua = encodeURIComponent($("strLua").value);

	<%
	local nType = c_GetLicenseInfo()
	if nType == LICENSE_STANDARD or nType == LICENSE_SECURE or nType == LICENSE_FREE then
	%>
	alert("<%=LANG['str_notsupport_feature2']%>");
	<%
	end
	%>
	
	paramStr = "domain="+domain+"&type="+type+"&event="+event+"&bExecute="+bExecute+"&strExeFile="+strExeFile+"&strParam="+strParam+"&bLogfile="+bLogfile+"&strLogFileName="+strLogFileName+"&strLogText="+strLogText+"&bSendMail="+bSendMail+"&strMailTo="+strMailTo+"&strSubject="+strSubject+"&strPlainText="+strPlainText+"&strAttachFile="+strAttachFile+"&nPriority="+nPriority+"&bLua="+bLua+"&strLua="+strLua+"&r="+Math.random();
	ajaxRequest("admin_modifyevent",paramStr);
}

function chooseExecuteFile()
{
	showMessagebox("<%=LANG['str_choosefile']%>","<iframe src='admin_file_selector.html' width=550 height=500 border=0 frameborder=0></iframe>",callback_return,550,500);
}

function callback_return(value)
{
	if(value != null)
		$("strExeFile").value = value;
	$("strExeFile").focus();
}

function chooseAttachFile()
{
	showMessagebox("<%=LANG['str_choosefile']%>","<iframe src='admin_file_selector.html' width=550 height=500 border=0 frameborder=0></iframe>",callback_return2,550,500);
}

function callback_return2(value)
{
	if(value != null)
		$("strAttachFile").value = value;
	$("strAttachFile").focus();
}

function chooseLogFile()
{
	showMessagebox("<%=LANG['str_choosefile']%>","<iframe src='admin_file_selector.html?checkfile=0' width=550 height=500 border=0 frameborder=0></iframe>",callback_return3,550,500);
}

function callback_return3(value)
{
	if(value != null)
		$("strLogFileName").value = value;
	$("strLogFileName").focus();
}

function insertText(strText)
{
    var input = $("strLua");
    var len = input.value.length;
    input.focus();
    if(typeof(document.selection) !="undefined")
    {
        document.selection.createRange().text = strText; 
    }
    else
    {
        input.value = input.value.substr(0,input.selectionStart)+strText+input.value.substring(input.selectionStart,len);
    }
}

function luaHelp()
{
	window.open("help/english/index.html?server_lua_api.htm","_blank");
}
</script>
</head>
<body style="background-color:#E7E6DA;">

<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_execommand"]%></a></li>
	<li class="normalTag"><a href="javascript:void(0)" onfocus="this.blur()" onclick="switchTag('tagContent2',this)"><%=LANG["str_writelog"]%></a></li>
	<li class="normalTag"><a href="javascript:void(0)" onfocus="this.blur()" onclick="switchTag('tagContent3',this)"><%=LANG["str_sendmail"]%></a></li>
	<li class="normalTag"><a href="javascript:void(0)" onfocus="this.blur()" onclick="switchTag('tagContent4',this)"><%=LANG["str_luascript"]%></a></li>
  </ul>
  <div id="tagContent" class="tagContent">
  	<div id="tagContent1">
		<input id="domain" type="hidden" value="<%=domain%>">
		<input id="type" type="hidden" value="<%=type%>">
		<input id="event" type="hidden" value="<%=eventID%>">
		<input id="eventName" type="hidden" value="<%=eventName%>">
		<table cellSpacing="0" cellPadding="0" border="0">
		<tr><td colspan="2"><input id="bExecute" type="checkbox" onclick="switchExecute();" <%if bExecute == true then print("checked") end%> ><%=LANG["str_enable_execute"]%></td></tr>
		<tr><td colspan="2" height="10">&nbsp;</td></tr>
		<tr><td><%=LANG["str_execute_path"]%>:</td><td><input id="strExeFile" type="text" style="width:270px;" value="<%=strExeFile%>">
		<button id="chooseExecute" onclick='chooseExecuteFile();'><span><em><%=LANG["button_choose"]%></em></span></button>
		</td></tr>
		<tr><td><%=LANG["str_parameters"]%>:</td><td><textarea id="strParam" style="width:320px;height:50px;"><%=strParam%></textarea></tr>
		</table>
	</div>
	<div id="tagContent2">
		<table cellSpacing="0" cellPadding="0" border="0">
		<tr><td colspan="2"><input id="bLogfile" type="checkbox" onclick="switchLogfile();" <%if bLogfile == true then print("checked") end%> ><%=LANG["str_enable_writelog"]%></td></tr>
		<tr><td colspan="2" height="10">&nbsp;</td></tr>
		<tr><td><%=LANG["str_writelog_path"]%>:</td><td><input id="strLogFileName" type="text" style="width:300px;" value="<%=strLogFileName%>"><button id="chooseLogfile" onclick='chooseLogFile();'><span><em><%=LANG["button_choose"]%></em></span></button></td></tr>
		<tr><td><%=LANG["str_logtext"]%>:</td><td><textarea id="strLogText" style="width:320px;height:190px;"><%=strLogText%></textarea></tr>
		</table>
	</div>
	<div id="tagContent3">
		<table cellSpacing="0" cellPadding="0" border="0">
		<tr><td colspan="2"><input id="bSendMail" type="checkbox" onclick="switchSendmail();" <%if bSendMail == true then print("checked") end%> ><%=LANG["str_enable_sendmail"]%></td></tr>
		<tr><td><%=LANG["str_sendto"]%>:</td><td><input id="strMailTo" type="text" onblur="if(this.value=='') this.value='<%=LANG["str_multiple_emails"]%>'" onclick="if(this.value =='<%=LANG["str_multiple_emails"]%>' ) this.value=''" style="width:300px;color:#898989;" value="<%=specialhtml_encode(strMailTo)%>"></td></tr>
		<tr><td><%=LANG["str_subject"]%>:</td><td><input id="strSubject" type="text" style="width:300px;" value="<%=specialhtml_encode(strSubject)%>"></td></tr>
		<tr><td><%=LANG["str_plaintext"]%>:</td><td><textarea id="strPlainText" style="width:300px;height:130px;"><%=strPlainText%></textarea></tr>
		<tr><td><%=LANG["str_attachfile"]%>:</td><td><input id="strAttachFile" type="text" style="width:290px;" value="<%=strAttachFile%>">
		<button id="chooseAttach" onclick='chooseAttachFile();'><span><em><%=LANG["button_choose"]%></em></span></button>
		</td></tr>
		<tr><td><%=LANG["str_priority"]%>:</td><td>
		<select id="nPriority" style="width:100px;"><option value="2" <%if nPriority == 2 then print("selected") end%>><%=LANG["str_priority1"]%></option><option value="1" <%if nPriority == 1 then print("selected") end%>><%=LANG["str_priority2"]%></option><option value="3" <%if nPriority == 3 then print("selected") end%>><%=LANG["str_priority3"]%></option></select>
		</td></tr>
		</table>
	</div>
	<div id="tagContent4">
		<table cellSpacing="0" cellPadding="0" border="0">
		<tr><td colspan="2"><input id="bLua" type="checkbox" onclick="switchLua();"  <%if bLua == true then print("checked") end%> ><%=LANG["str_enablelua"]%> <span id="BeforeEventTips"></span></td></tr>
		<tr><td colspan="2" height="10"></td></tr>
		<tr><td>
		<table cellSpacing="0" cellPadding="0" border="0">
		<tr><td>
		<%=LANG["str_luascript"]%>:
		</td></tr>
		<tr><td>
		<input type="button" value="<%=LANG["str_commonapi"]%>" id="selectHead" style="width:100px;height:24px;border:0px;cursor:pointer;background:url('images/btn3_1.gif') no-repeat;" onclick="drawSelect('selectHead','selectContent',insertText)">
		</td></tr>
		<tr><td valign="bottom" height="100">
		<span style="cursor:pointer;" onclick="luaHelp();"><img src="images/ico_question.gif"><%=LANG["str_luahelp"]%></span>
		</td></tr>	
		</table>
		</td>		
		<td><textarea id="strLua" style="width:410px;height:210px;"><%=strLua%></textarea></tr>
		</table>
	</div>
  </div>	
</div>

<div id="selectContent" class="selectContent" style="display:none;">
	<ul>
	<%
		local i = 0
		for key,value in ipairs(FUNCTION_KEY) do
			print("<li title='"..FUNCTION_VALUE[i+1].."'>"..value.."</li>")
			i = i + 1
		end
	%>
	</ul>
</div>

<div style="padding:3px;text-align:right;margin-right:10px;">
<button type='submit' onclick='eventSubmit();'><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">

<% if luaScriptFirst == true then %>
$("tagContent4").style.display="block";
$("tags").getElementsByTagName("li")[1].style.display="none";
$("tags").getElementsByTagName("li")[2].style.display="none";
$("tags").getElementsByTagName("li")[3].style.display="none";
$("tags").getElementsByTagName("li")[4].className="selectedTag";
$("BeforeEventTips").innerHTML = "<br><img src='images/ico_info.gif'>&nbsp;<%=LANG['str_block_event']%>&nbsp;(bCancelEvent = true)";
<% else %>
$("tagContent1").style.display="block";
$("tags").getElementsByTagName("li")[1].className="selectedTag";
$("strExeFile").focus();
<% end %>



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



function switchExecute()
{
	if($("bExecute").checked)
	{
		$("strExeFile").disabled = false;
		$("strParam").disabled = false;
		$("chooseExecute").disabled = false;
		$("strExeFile").focus();
	}
	else
	{
		$("strExeFile").disabled = true;
		$("strParam").disabled = true;
		$("chooseExecute").disabled = true;
	}
}

function switchLogfile()
{
	if($("bLogfile").checked)
	{
		$("strLogFileName").disabled = false;
		$("strLogText").disabled = false;
		$("chooseLogfile").disabled = false;

		if($("strLogText").value == "")
			$("strLogText").value = " ";
	}
	else
	{
		$("strLogFileName").disabled = true;
		$("strLogText").disabled = true;
		$("chooseLogfile").disabled = true;
	}
}

function switchSendmail()
{
	if($("bSendMail").checked)
	{
		if(smtpconfig == "")
		{
			alert("<%=LANG['str_no_smtpconfig']%>");
		}
		$("strMailTo").disabled = false;
		if($("strMailTo").value == "")
		{
			$("strMailTo").value = "<%=LANG['str_multiple_emails']%>";
		}
		$("strSubject").disabled = false;
		$("strPlainText").disabled = false;
		$("strAttachFile").disabled = false;
		$("nPriority").disabled = false;
		$("chooseAttach").disabled = false;

		if($("strPlainText").value == "")
			$("strPlainText").value = " ";
	}
	else
	{
		$("strMailTo").disabled = true;
		$("strSubject").disabled = true;
		$("strPlainText").disabled = true;
		$("strAttachFile").disabled = true;
		$("nPriority").disabled = true;
		$("chooseAttach").disabled = true;
	}
}

function switchLua()
{
	if($("bLua").checked)
	{
		$("selectHead").disabled = false;
		$("strLua").disabled = false;

		if($("strLua").value == "")
			$("strLua").value = " ";
	}
	else
	{
		$("selectHead").disabled = true;
		$("strLua").disabled = true;
	}
}

switchExecute();
switchLogfile();
switchSendmail();
switchLua();


var TextAreaTab = 
{ 
   Register : function(obj) 
   {
       obj.onclick = new Function('TextAreaTab.CreateInsertPosition(this)'); 
       obj.onselect = new Function('TextAreaTab.CreateInsertPosition(this)'); 
       obj.onkeyup = new Function('TextAreaTab.CreateInsertPosition(this)'); 
       obj.onkeydown = new Function('TextAreaTab.InsertTab(this)'); 
   },
   CreateInsertPosition : function(obj) 
   { 
       if (obj.createTextRange) // IE 
       { 
           obj.InsertPosition = document.selection.createRange().duplicate();
       } 
   }, 
   InsertTab : function(obj) 
   { 
       var evt = this.GetEvent();
       if (evt.keyCode == 9)
       { 
           if (obj.createTextRange && obj.InsertPosition) // IE 
           {
               obj.InsertPosition.text = '\t'; 
               evt.returnValue = false; 
           } 
           else if (window.getSelection) // FF 
           { 
               var scrollTop = obj.scrollTop;
               var start = obj.selectionStart;

               var pre = obj.value.substr(0, obj.selectionStart);
               var next = obj.value.substr(obj.selectionEnd);
               obj.value = pre + '\t' + next; 

               evt.preventDefault(); 

               obj.selectionStart = start + 1; 
               obj.selectionEnd = start + 1; 
               obj.scrollTop = scrollTop; 
           } 
       } 
   }, 
   GetEvent : function() 
   { 
       if(document.all) // IE 
       { 
           return window.event; 
       } 

       func = this.GetEvent.caller; 
       while(func != null) 
       { 
           var arg0 = func.arguments[0]; 
           if(arg0) 
           { 
               if((arg0.constructor == Event || arg0.constructor == MouseEvent) 
                   ||(typeof(arg0) == "object" && arg0.preventDefault && arg0.stopPropagation)) 
               { 
                   return arg0; 
               } 
           } 
           func = func.caller; 
       } 
       return null; 
   } 
} 
var textarea = document.getElementById("strLua"); 
TextAreaTab.Register(textarea); 
</script>
</body>
</html>
<% else %>
	<script>alert("error parameters!");</script>
<% end %>

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