<%
include("language.html")
if _SESSION["logined"] ~= nil and _SESSION["admin_readonly"] == 0 then
%>
<!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>Weblink Manager</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<%
local domain = _GET["domain"] or _POST["domain"]
local is_uplink = _GET["is_uplink"] or _POST["is_uplink"] or "0"

local count_title = LANG["str_total_downfile"]
if is_uplink == "1" then
	count_title = LANG["str_total_upfile"]
end
%>
<style type="text/css">
html, body {height: 100%;}
<%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:12px/20px Verdana, Geneva, Arial, Helvetica, sans-serif;}
<%end%>
img {border:0px;}

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;
}
span{
	vertical-align:middle;
}


.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:22px;
}

.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:22px;
}

.listhead3
{
		border-top: 1px solid white; border-right: 1px solid white; 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:22px;
}

.listhead4
{
		border-top: 1px solid white; border-right: 1px solid white; 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:22px;
}

.listtr01
{
		background-color: #FFFFFF;
		text-align: left;
		padding: 3px;
}

.listtr02
{
		background-color: #3366CC;
		color: #FFF;
		text-align: left;
		padding: 3px;
}

.resizeblock{
	background-color:transparent;
	width:2px;
	float:right;
	cursor:e-resize;
}

.fixedwidth
{
	word-wrap: break-word;
	word-break: normal;
}

</style>

<!¨C[if IE 6]>
<link rel="stylesheet" type="text/css" href="css/ie6.css" />
<![endif]¨C>

<script language="javascript" src="include/common.js"></script>
<script language="javascript">
var domain = "<%=domain%>";
var is_uplink = "<%=is_uplink%>";
var totalrecords = 0;
var totalpages = 0;
var nowpage = 1;
var ajaxlock = false;
var selectedRow = null;
var lastObj = null;
var lastObjIndex = -1;
var filters = "";
var selectAll = false;

function translateTime(timevalue)
{
	var nDate = new Date(timevalue * 1000);

	var year = nDate.getFullYear();
	var month = nDate.getMonth()+1;
	var day = nDate.getDate();
	var hours = nDate.getHours();
	var minutes = nDate.getMinutes();
	var seconds = nDate.getSeconds();

	var dateString = year;
	dateString += ((month < 10) ? "-0" : "-") + month;
	dateString += ((day < 10) ? "-0" : "-") + day;

	var timeString = ((hours < 10) ? "0" : "") + hours;
	timeString += ((minutes < 10) ? ":0" : ":") + minutes;
	timeString += ((seconds < 10) ? ":0" : ":") + seconds;

	return dateString + " " + timeString;
}

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(page != "")
			$("waitingdiv").style.display = "";

		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_get_weblink':	
					{
						weblink_result();
						break;
					}
					case 'admin_delete_weblink':	
					{
						DoRefresh();
						break;
					}
				}
			}
			catch(e)
			{
				alert("<%=LANG['error_ajax']%>");
			}
			finally
			{
				$("waitingdiv").style.display = "none";
			}
		}
		else
		{
			$("waitingdiv").style.display = "none";
			alert("<%=LANG['error_ajax']%>");
		}
	}
}

function getloglistpage(page)
{
	ajaxRequest("admin_get_weblink","domain="+domain+"&is_uplink="+is_uplink+"&page="+page+"&filters="+encodeURIComponent(filters)+"&r=" + Math.random() );
}

function weblink_result()
{
	$("waitingdiv").style.display = "";
	try
	{
		var headcell = $("listhead").rows[0].cells;
		var htmltext = "<table id='listtable' width='100%' border='0' cellpadding='2' cellspacing='0' style='table-layout:fixed;'><thead><tr height='0'><td width='"+headcell[0].width+"'></td><td width='"+headcell[1].width+"'></td><td width='"+headcell[2].width+"'></td><td width='"+headcell[3].width+"'></td><td width='"+headcell[4].width+"'></td><td width='"+headcell[5].width+"'><td width='"+headcell[6].width+"'></td></tr></thead><tbody id='maintable'>";

		var logArray = xmlhttp.responseText.split("\n");
		var pages = parseInt(logArray[0]);

		if(totalpages != pages)
		{


			$("page_selector").options.length=0;

			if(pages > 0)
			{
				try
				{
					var fragment = document.createDocumentFragment();
					for (var n=1; n<=pages; n++)
					{
						if(pages <= 100)
						{
							var option = document.createElement("option");
							option.value = n;
							option.innerText = n;
							fragment.appendChild(option);
						}
						else if(pages > 100 && (n < 50 || n > pages-50 || n % 10 == 0))
						{
							var option = document.createElement("option");
							option.value = n;
							option.innerText = n;
							fragment.appendChild(option);
						}
					}

					$("page_selector").appendChild(fragment);
				}
				catch(e)
				{
					for (var n=1; n<=pages; n++)
						$("page_selector").options[n-1] = new Option(n, n);
				}
			}

			totalpages = pages;
			$("page_number").innerHTML = nowpage + " / " + totalpages;
		}

		var nowDate = new Date(); 

		for(var i=1;i<logArray.length;i++)
		{
			if(logArray[i] != "" && logArray[i] != " ")
			{
				var tempVal = logArray[i];
				var fieldArray = tempVal.split("||");
				var username = txt2html(fieldArray[3]);
				var filesize = fieldArray[10] == "0" ? "0":fileSize(fieldArray[10]);
				var filename = txt2html(fieldArray[8]);
				if(is_uplink == "1" && filename.substr(0,1) != "/")
				{
					filename = "/"+txt2html(fieldArray[8]);
				}

				var strExpired = "";
				var strExpiredTips = "";

				try
				{
					var nExpiredDate = fieldArray[13].replace("|", ""); 
					if(nExpiredDate != "-1")
					{
						strExpiredTips = "<%=LANG['str_expire_on']%>: "+translateTime(nExpiredDate);
						var expireDate = new Date(nExpiredDate * 1000);
						if(nowDate.getTime() > expireDate.getTime())
							strExpired = "<font color='red'><%=LANG['str_yes']%></font>";
						else
							strExpired = "<font color='gray'><%=LANG['str_no']%></font>";
					}
				}
				catch(e)
				{
				}

				htmltext += "<tr class='listtr01' onclick='do_list_click(this);'><td><input type='checkbox' id='cid_"+i+"' cid='"+i+"'> "+fieldArray[0]+"</td><td class='fixedwidth' id='td_"+i+"' title='"+fieldArray[1]+"'>"+fieldArray[1]+"</td><td title='"+username+"' align='center'><b>"+username+"</b></td><td>"+fieldArray[6]+"</td><td class='fixedwidth' title='"+txt2html(fieldArray[9])+"'><b>"+filename+"</b></td><td>"+filesize+"</td><td>"+fieldArray[7]+"</td><td title='"+strExpiredTips+"'>"+strExpired+"</td><td></td></tr>";
			}
		}
		htmltext += "</tbody></table>";
		clear("listview_div");
		write("listview_div",htmltext);
		selectedRow = null;
	}
	catch(e)
	{
	}

	$("waitingdiv").style.display = "none";
}

function do_list_click(obj)
{
	var row_index = obj.rowIndex-1;
	if(selectedRow != $("maintable").rows[row_index])
	{
		if(lastObj != null)
			lastObj.className = "listtr01";

		selectedRow = $("maintable").rows[row_index];
		obj.className = "listtr02";
		lastObj = obj;
		try{
			lastObjIndex = parseInt(selectedRow.cells[0].innerHTML);
		}catch(e){
			lastObjIndex = -1;
		}
	}

	return false;
}


function sortSQLTable(obj, iCol) 
{
<%
if (_SESSION["admin_readonly"] ~= nil and _SESSION["admin_readonly"] == 1) or (_SESSION["admin_domainadmin"] ~= nil and _SESSION["admin_domainadmin"] == 1) then
	print("return false;")
end
%>

	var orderby = "asc";

	if (lastsortcol == iCol)
	{
		sortArrow = !sortArrow;
	}
	else
	{
		sortArrow = false;
	}

	if(sortArrow == true)
	{
		orderby = "desc";
	}

	if(obj != null)
	{
		if(lastArrow != null)
			lastArrow.style.backgroundImage="";
		if(sortArrow == false)
			obj.style.backgroundImage="url('images/sortup.gif')";
		else
			obj.style.backgroundImage="url('images/sortdown.gif')";
		obj.style.backgroundPosition="10px";
		obj.style.backgroundRepeat="no-repeat";
		lastArrow = obj;
	}

	lastsortcol = iCol;
	filters = " 1=1 order by " + lastsortcol + " " + orderby;

	nowpage = 1;
	getloglistpage(nowpage);
	$("page_selector").value = 1;

}

function DoRefresh()
{
	getloglistpage(nowpage);
}

function DoDelete()
{
	var checkedlist = getCheckedList();
	if(checkedlist != "")
	{
		if(confirm("<%=LANG['str_delweblinks_tip']%>?"))
		{
			ajaxRequest("admin_delete_weblink","weblinks="+urlEncode(checkedlist)+"&is_uplink="+is_uplink);
		}
	}
	else
	{
		if(selectedRow == null)
		{
			alert("<%=LANG['str_norow_tip']%>");
			return;
		}

		var weblink = selectedRow.cells[1].innerHTML;
		if(confirm("<%=LANG['str_delweblink_tip']%> \""+weblink+"\"?"))
			ajaxRequest("admin_delete_weblink","weblinks="+weblink+"&is_uplink="+is_uplink);
	}
}

function DoSelect()
{
	selectAll = !selectAll;
	var checkboxs = document.getElementsByTagName("input");
	for(var i=0;i<checkboxs.length;i++)
	{
		if(checkboxs[i].type == "checkbox")
		{
			if(selectAll)
				checkboxs[i].checked = true;
			else
				checkboxs[i].checked = false;
		}
	}
}

function getCheckedList()
{ 
	var checkedlist = "";
	var checkboxs = document.getElementsByTagName("input");
	for(var i=0;i<checkboxs.length;i++)
	{
		if(checkboxs[i].type == "checkbox" && checkboxs[i].checked == true)
		{
			try
			{
				var cid = checkboxs[i].getAttribute("cid");
				if(checkedlist != "") checkedlist += "||";
				checkedlist += htmldecode($("td_"+cid).innerHTML);
			}
			catch (e){}
		}
	}
	return checkedlist;
}
</script>
</head>
<body style="background-color:#E7E6DA;" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<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:40%; width:250px; height:100px;"><img src="images/loading2.gif" border="0">
<H2><b><%=LANG["title_updating"]%></b></H2></div></div>

<div style="padding-top:2px; overflow-x:hidden;">
	<table width="100%" border="0" cellpadding="0" cellspacing="0" style="table-layout:fixed;">
	<tr>
	<td width="70%" style="padding-left:2px;">
		<button onclick="DoSelect();"><span><em><img src="images/multiline.gif" align="absmiddle"> <%=LANG["str_chkbox_select"]%></em></span></button>
		<button onclick="DoDelete();"><span><em><img src="images/reset.gif" align="absmiddle"> <%=LANG["button_delete"]%></em></span></button>
		<button onclick="DoRefresh();"><span><em><img src="images/refresh.gif" align="absmiddle"> <%=LANG["button_refresh"]%></em></span></button>
	</td>
	<td align="right" style="padding-right:10px;"><span><%=LANG["str_domain"]%>:<b><%=specialhtml_encode(unescape(_GET["domain"]))%></b><span>
	</td>
	</tr>
	</table>

</div>

<div style="background-color:#E7E6DA;">
	<div style="margin-top:3px; margin-left:5px; border:1px solid #919B9C; border-bottom:0px; width:98%;">
		<table id="listhead" width="100%" border="0" cellpadding="0" cellspacing="0" style="table-layout:fixed;">
			<tr style="background-color: #EBEADB;">
			<td class="listhead" width="7%" onmousedown="sortSQLTable(this,'f_id')"><%=LANG["title_id"]%></td>
			<td class="listhead" width="20%" onmousedown="sortSQLTable(this,'f_weblink')"><%=LANG["str_weblink"]%></td>
			<td class="listhead" width="12%" onmousedown="sortSQLTable(this,'f_username')"><%=LANG["title_username"]%></td>
			<td class="listhead" width="12%" onmousedown="sortSQLTable(this,'f_update_time')"><%=LANG["str_modify_date"]%></td>
			<td class="listhead" width="20%" onmousedown="sortSQLTable(this,'f_filename')"><%=LANG["str_filename"]%></td>
			<td class="listhead" width="10%" onmousedown="sortSQLTable(this,'f_filesize')"><%=LANG["title_size"]%></td>
			<td class="listhead" width="10%" onmousedown="sortSQLTable(this,'f_count')"><%=count_title%></td>
			<td class="listhead" width="9%" onmousedown="sortSQLTable(this,'f_expire_time')"><%=LANG["str_expired"]%></td>
			<td class="listhead3">&nbsp;</td>
			</tr>
		</table>
	</div>
	<div id="listview_div" style="background-color:#FFF; margin-left:5px; border:1px solid #919B9C; border-top:0px; width:98%; height:420px; overflow:auto; overflow-x:hidden;">
		<table id="listtable" width="100%" border="0" cellpadding="2" cellspacing="0" style="table-layout:fixed;">
		<thead><tr height="0">
		<td width="7%"></td>
		<td width="20%"></td>
		<td width="12%"></td>
		<td width="12%"></td>
		<td width="20%"></td>
		<td width="10%"></td>
		<td width="10%"></td>
		<td width="9%"></td>
		<td></td>
		</tr></thead>
		<tbody id="maintable">
		</tbody>
		</table>
	</div>
</div>

<div style="padding-left:5px; padding-top:2px; ">
<table width="100%" border="0" cellpadding="0" cellspacing="0" style="table-layout:fixed;">
	<tr>

	<td align="right" style="padding-right:10px;">

	<button onclick='jumpprevious();'><span><em><%=LANG["button_previous_page"]%></em></span></button>
	<select id='page_selector' onchange="pagejump(this.value)">
		<option value="1">1</option>
	</select>
	<span id="page_number" style="*vertical-align:20%;"></span>
	<button onclick='jumpnext();'><span><em><%=LANG["button_next_page"]%></em></span></button>&nbsp;
	<button onclick="DoRefresh();"><span><em><%=LANG["button_refresh"]%></em></span></button>

	</td>
	</tr>
	</table>

<script language="javascript">
pagejump(nowpage);


function jumpprevious()
{
	page = nowpage-1;
	pagejump(page);
}

function jumpnext()
{
	page = nowpage+1;
	pagejump(page);
}

function pagejump(page)
{
	page = parseInt(page);
	if(page > totalpages)
		page = totalpages;
	if(page < 1)
		page = 1;
	nowpage = page;
	getloglistpage(nowpage);

	$("page_selector").value = nowpage;
	$("page_number").innerHTML = nowpage + " / " + totalpages;
}

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