<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<%
include("checklogin.html")
include("language.html")

if _SESSION["username"] ~= nil and _SESSION["currentpath"] ~= nil then
	local dirname = _GET["dir"]
	local filename = _GET["filename"]
	local height = _GET["height"] or 520
	local canwrite = false
	local filecontent = ""

	local nowdir = string.gsub(_SESSION["currentpath"],":{{","%[")
	nowdir = string.gsub(nowdir,"}}:","%]")

	if dirname == nil or dirname == "" or filename == nil or filename == "" then
		print("<script>alert('"..LANG["error_no_filename"].."');top.closewindow();</script>")
		exit();
	end

	local status,filecontent,canwrite,charcode = c_GetFileContent(_SESSION["username"],filename,dirname)
	if status ~= 1 then
		print("<script>alert('"..RESULT_STR[tonumber(status)].."');top.closewindow();</script>")
		exit()
	end

	c_AddConnection(_SESSION["username"],nowdir,_REMOTE_IP,_SESSION_ID,"OPEN TEXTFILE")
	c_AddWebLog("Start editing text file '"..filename.."'",_SESSION_ID,DOMAIN_LOG_WEB_RESPOND)
%>
<title><%=filename%></title>
<script language="javascript" type="text/javascript" src="edit_area/edit_area_full.js"></script>
<script language="javascript" type="text/javascript">

editAreaLoader.init({
	id : "textedit"
	,language: "<%=LANG['editor_language']%>"
	,readonly: <% if canwrite == true then print("''") else print("'"..LANG['editor_readonly'].."'") end %>
	,fullscreen: true
	,allow_toggle: false
	,start_highlight: false
	,smooth_selection: false
	<% if canwrite == true then %>
	,toolbar: "save, search, go_to_line, |, undo, redo, |, select_font"
	,save_callback: "my_save"
	<% else %>
	,toolbar: "search, go_to_line, |, undo, redo, |, select_font"
	<% end %>
	,max_undo: 20
});



function $(obj)
{
	return document.getElementById(obj);
}

function default_save()
{
	var content = $("textedit").value;
	if(encodeURIComponent(content).length >= 1024*600)
	{
		alert("<%=LANG['error_long_string']%>");
		return false;
	}
	savefile("<%=charcode%>","<%=dirname%>","<%=filename%>",content);
}

function my_save(id, content)
{
	if(encodeURIComponent(content).length >= 1024*600)
	{
		alert("<%=LANG['error_long_string']%>");
		return false;
	}
	savefile("<%=charcode%>","<%=dirname%>","<%=filename%>",content);
}


function savefile(charcode,dir,filename,filecontent)
{
	var xmlhttp = false;
	var isIEBrowser = navigator.userAgent.indexOf('MSIE')==-1? 0:1;
	xmlhttp = isIEBrowser ? new ActiveXObject('Microsoft.XMLHTTP'):new XMLHttpRequest();
	if(xmlhttp) 
	{
		$("waitingdiv").style.display = "";
		xmlhttp.open("POST","savefile.html");
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState == 4) 
			{
				try
				{
					if(xmlhttp.status == 200)
					{
						try
						{
							top.Refresh();
						}
						catch(e){}
						alert(xmlhttp.responseText);
					}
					else
					{
						alert("<%=LANG['error_ajax']%>");
					}
					delete xmlhttp;
					xmlhttp = null;
				}
				catch(e)
				{
					alert("<%=LANG['error_ajax']%>");
				}
				finally
				{
					$("waitingdiv").style.display = "none";
					editAreaLoader.clear_modified();
				}
			}
		}
		xmlhttp.send("charcode="+encodeURIComponent(charcode)+"&dir="+encodeURIComponent(dir)+"&filename="+encodeURIComponent(filename)+"&filecontent="+encodeURIComponent(filecontent));
	}
}

function do_exitQuery()
{
	var isEditable = <% if canwrite == true then print("true") else print("false") end %>;
	if(isEditable == true && editAreaLoader.is_modified() == true)
	{
		if(confirm("<%=LANG['savefile_confirm']%>"))
		{
			var content = editAreaLoader.getValue("textedit");
			savefile("<%=charcode%>","<%=dirname%>","<%=filename%>",content);
		}
	}
}
</script>
</head>
<body onbeforeunload="do_exitQuery();">
<div id="waitingdiv" style="position:absolute; top:0px; left:0px; width:100%; height:100%; z-index:30000; background:#777; filter:alpha(opacity=60); opacity:0.6; display:none;" align="center">
<div style="position:absolute; top:20%; left:30%; width:200px; height:100px;"><img src="images/loading2.gif" border="0">
<H2><b><%=LANG["savefile_waiting"]%>...</b></H2></div></div>

<% 
	if canwrite == true then 
		print("<input type='button' onclick='default_save();' value='save' >") 
	end 
%>

<textarea id="textedit" style="height:<%=height%>px; width:100%;"><%=specialhtml_encode(filecontent)%></textarea>
</body>
</html>

<% 
else
	print("<script>alert('"..LANG["session_expire"].."');top.window.location = 'logout.html';</script>")
end 
%>
<noscript><H2><%=LANG["error_nojavascript"]%></H2></noscript>