<%
include("language.html")
if _SESSION["logined"] ~= nil then
%>
<%
local result = nil
local name = nil
if _GET["name"] ~= nil then
	name = _GET["name"]
	result = c_GetSSHKey(name)
	if result == nil then
		print("<script>alert('SSH key 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 SSH Key</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: 240px;
	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 ajaxlock = false;
var selectInput = "";
var isNewKey = 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)
	{
		if (xmlhttp.status == 200)
		{
			try
			{
				switch(page)
				{
					case 'admin_add_key':	doSubmit_result();break;
				}
			}
			catch(e)
			{
				alert("<%=LANG['error_ajax']%>");
			}
		}
		else
		{
			alert("<%=LANG['error_ajax']%>");
		}
	}
}

function doSubmit()
{
	var paramStr = "";
	name = $("name").value;
	var ssh_key_path = encodeURIComponent($("ssh_key_path").value);
	var ssh_password = encodeURIComponent($("ssh_password").value);
	if(name.length == 0 || name.search(/[\\\/\<\>\:\?\"\'\|\*\&]/i) != -1)
	{
		alert("<%=LANG['str_invalid_keyname']%>");
		return false;
	}
	paramStr = "name="+name+"&ssh_key_path="+ssh_key_path+"&ssh_password="+ssh_password+"&r="+Math.random();
	<%
	if _GET["name"] ~= nil then
		print("paramStr += '&modify=1';")
		print("isNewKey = false;")
	end
	%>
	ajaxRequest("admin_add_key",paramStr);
}

function doSubmit_result()
{
	var result = xmlhttp.responseText;
	if(result == "0" || result == 0)
	{
		alert("<%=LANG['str_existed_sshkey']%>");
	}
	else if(result == "-1" || result == -1)
	{
		alert("<%=LANG['str_invalid_sshkey']%>");
	}
	else
	{
		if(isNewKey == true)
		{
			alert("<%=LANG['str_addkey_success']%>");
		}

		top.closewindow(name);
	}
}

function chooseFile(objname)
{
	selectInput = objname;
	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)
{
	try
	{
		$("ssh_key_path").focus();
		if(value != null)
			$(selectInput).value = value;
	}
	catch(e){}
}

function createSSHKey()
{
	showMessagebox("<%=LANG['str_createsshkey']%>","<iframe src='admin_createkey_form.html' width=420 height=210 border=0 frameborder=0></iframe>",callback_return2,420,210);
}

function callback_return2(value)
{
	try
	{
		if(value != null && value != "")
		{
			var strArray = value.split("||");
			var tempname = strArray[0];
			var temppath = strArray[1];
			if($("name").value == "")
				$("name").value = tempname;
			$("ssh_password").value = strArray[2];
//			if(right(temppath,1) != "\/" || right(temppath,1) != "\\")
//				$("ssh_key_path").value = temppath+"/"+tempname+".key";
//			else
//				$("ssh_key_path").value = temppath+tempname+".key";

			if(right(temppath,1) != "\/" || right(temppath,1) != "\\")
				$("ssh_key_path").value = temppath+"/"+tempname;
			else
				$("ssh_key_path").value = temppath+tempname;
		}
	}
	catch(e){}
	$("ssh_key_path").focus();
}
</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_sshkey_config"]%></a></li>
  </ul>
  <div id="tagContent" class="tagContent">
	<div id="tagContent1">
		<table width="100%" cellSpacing="0" cellPadding="0" border="0">
		<tr><td><%=LANG["str_key_name"]%>:</td><td><input id="name" type="text" <% if name ~= nil then print("value='"..name.."' disabled") else print("onmouseover='this.focus();'") end%> style="width:200px;"></td></tr>
		<tr><td><%=LANG["str_sshkey_path"]%>:</td><td><input id="ssh_key_path" type="text" style="width:200px;" value="<% if result ~= nil then print(result["ssh_keypath"]) end%>"><button onclick="chooseFile('ssh_key_path');" title="choose ssh key file"><span><em><%=LANG["button_choose"]%></em></span></button></td></tr>
		<tr><td><%=LANG["str_password2"]%>:</td><td><input id="ssh_password" type="password" style="width:200px;" value="<% if result ~= nil then print(result["ssh_password"]) end%>"></td></tr>
		<tr><td colspan="2" height="80"><button onclick='createSSHKey();'><span><em><%=LANG["button_createsshkey"]%></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";

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