<!--#include File="../../inc/conn.asp"-->
<%
If app_comment = 0 Then writeE "评论关闭中"

dim action
action = be("get", "action")

Select Case action
	Case "save": save
	Case "glist" : glist
	Case "list" : list
	Case "tongji" : tongji
	Case Else: main
End Select
dispseObj()
    
Sub save()
	dim c_id,c_vid,c_type,c_rid,c_audit,c_name,c_content,c_time,c_ip,verifycode,checkscode
	c_vid = be("all", "c_vid"): c_vid = chkSql(c_vid, True)
    c_type = be("all", "c_type"): c_type = chkSql(c_type, True)
    c_rid = be("all", "c_rid"): c_rid = chkSql(c_rid, True)
    c_name = be("all", "c_name") : c_content = be("all", "c_content")
    verifycode = be("all","verifycode")
    
    If Not isNum(c_vid) Or Not isNum(c_type) Then writeA "1": Exit Sub
    if Not isN(c_rid) then
    	if not isNum(c_rid) then writeA "1": Exit Sub
    end if
    if isN(c_name) or isN(c_content) then writeA "1": Exit Sub
    checkscode = Session("code_comment")
    if checkscode = "" then checkscode = getCookie("code_comment")
    
    If app_commentverify=1 And checkscode <> verifycode Then writeA "2": Exit Sub
    If getTimeSpan("lastCommentTime") < app_commenttime Then writeA "3": Exit Sub
    
    c_name = badFilter(c_name) : c_name = chkSql(c_name, True)
    c_content = badFilter(c_content) : c_content = chkSql(c_content, True)
    c_ip = getIP()
    c_time = Now()
	if app_commentaudit=1 then c_audit=0 else c_audit=1
	if len(c_name) >64 then c_name = left(c_name,64)
	if len(c_content) >128 then c_content = left(c_content,128)
	
	objdb.add "{pre}comment", Array("c_name", "c_vid","c_rid", "c_type","c_audit" ,"c_ip", "c_time", "c_content"), Array(c_name, c_vid,c_rid, c_type, c_audit, c_ip, c_time, c_content)
	Session("lastCommentTime") = Now
    writeA "0"
End Sub

Sub tongji()
	dim c_vid,c_type,count1,count2
	c_vid = be("get", "id") : c_vid = chkSql(c_vid, True)
	c_type = be("get", "type") : c_type = chkSql(c_type, True)
	If Not isNum(c_vid) Or Not isNum(c_type) Then writeA "err": Exit Sub
	count1 = objdb.db("select count(*) from {pre}comment where c_type="&c_type&" and c_rid=0 and c_vid="&c_vid,"exe")(0)
	count2 = objdb.db("select count(*) from {pre}comment where c_type="&c_type&" and c_vid="&c_vid,"exe")(0)
	writeA "["&count1&","&count2&"]"
End Sub

Sub main()
	dim c_vid,c_type
	c_vid = be("get", "id") : c_vid = chkSql(c_vid, True)
	c_type = be("get", "type") : c_type = chkSql(c_type, True)
	If Not isNum(c_vid) Or Not isNum(c_type) Then writeA "err": Exit Sub
%>
<iframe width="100%" height="1" frameborder="0" marginheight="0" marginwidth="0" scrolling="yes" src="<%=app_installdir%>plus/comment/?action=list&id=<%=c_vid%>&type=<%=c_type%>&title=" id="mac_comment" name="mac_comment"></iframe>
<%
End Sub

Sub list()
	dim c_vid,c_type,c_name
	c_vid = be("get", "id") : c_vid = chkSql(c_vid, True)
	c_type = be("get", "type") : c_type = chkSql(c_type, True)
	If Not isNum(c_vid) Or Not isNum(c_type) Then writeA "err": Exit Sub
	If isN(session("username")) then c_name = "游客" else c_name = session("username")
%>
<!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" />
<title>评论留言</title>
<link type="text/css" rel="stylesheet" href="images/style.css" />
<script type="text/javascript" src="../../js/jquery.js"></script>
</head>
<body>
<script>
var maccms_path = "<%=app_installdir%>";
var commentverify = "<%=app_commentverify%>";
var c_vid = "<%=c_vid%>";
var c_type = "<%=c_type%>";
var c_name = "<%=c_name%>";
function reinitIframe()
{
	try{
	var main = $(window.parent.document).find("#mac_comment");
	var thisheight = $(document).height() ;
	main.height(thisheight);
	}catch (ex){}
}
$(document).ready(function(){
	window.setInterval("reinitIframe()", 200);
});
</script>
<script type="text/javascript" src="comment.js"></script>
</body>
</html>
<%
End Sub

Sub glist
	dim c_id,c_vid,c_type,c_rid,c_name,c_content,c_time,c_ip,tmpIpArr
	dim page,sql,rs,rs1,replystr,i,total,p,bgcolorArr,bgstr,span,j
	c_vid = be("get", "id") : c_vid = chkSql(c_vid, True)
	c_type = be("get", "type") : c_type = chkSql(c_type, True)
	page = be("all","page")
	if not isNum(page) then page=1 else page=cint(page)
	If Not isNum(c_vid) Or Not isNum(c_type) Then writeA "err": Exit Sub
	
	sql = "SELECT c_id,c_vid,c_type,c_rid,c_name,c_content,c_time,c_ip from {pre}comment WHERE c_vid=" & c_vid & " AND c_rid=0 AND c_type= " & c_type & " ORDER BY c_id DESC"
    bgcolorArr=Array("#D66203","#513DBD","#784E1A","#C55200","#DA6912","#537752","#C58200","#519DBD","#D60103","#531752")
	Set rs = objdb.DB(sql, "rs1")
	i = 0
	If rs.Bof And rs.EOF Then
		writeA "<center><h3> 暂无评论，快来抢沙发吧！</h3></center>"
	Else
		rs.pagesize = app_commentnum
		total = rs.pagecount
		p = rs.recordcount
		If page > total Then
			page = total
		Else
			rs.absolutePage = page
		End If
		Do While Not rs.EOF
			i = i + 1
			c_id = rs("c_id")
			c_rid = rs("c_rid")
			
			if i > app_commentnum then exit do
			
			replystr = ""
			bgstr = bgcolorArr(getRndNum(1))
			sql = "SELECT c_id,c_name,c_content,c_time,c_ip,c_vid,c_type from {pre}comment WHERE c_rid="&c_id&" ORDER BY c_id DESC"
			set rs1 = objdb.db(sql,"rs1")
			Do While Not rs1.EOF
				c_name = rs1("c_name")
				c_content = rs1("c_content")
				c_content = regReplace(c_content, "\[em:(\d{1,})?\]", "<img src=""../../images/face/$1.gif"" border=0/>")
				c_time = rs1("c_time")
				c_ip = rs1("c_ip")
				If Not isN(c_ip) Then
					tmpIpArr = Split(c_ip, ".")
					c_ip = tmpIpArr(0) & "." & tmpIpArr(1) & "." & tmpIpArr(2) & ".*"
				End If
				
				replystr = replystr & "<div class=""citation""><div class=""citation-title""><span class=""author""><span class=""name"" style=""color:"&bgstr&";"">"&c_name&"</span><span class=""address"">&nbsp;IP:["&c_ip&"]</span></span><span class=""post-time"">"&c_time&"</span><div class=""clear""><br/></div></div><p class=""content"">"&c_content&"</p></div>"
				rs1.movenext
			loop
			objdb.c(rs1)
			
			c_name = rs("c_name")
			c_content = rs("c_content")
			c_content = regReplace(c_content, "\[em:(\d{1,})?\]", "<img src=""../../images/face/$1.gif"" border=0/>")
			c_time = rs("c_time")
			c_ip = rs("c_ip")
			If Not isN(c_ip) Then
				tmpIpArr = Split(c_ip, ".")
				c_ip = tmpIpArr(0) & "." & tmpIpArr(1) & "." & tmpIpArr(2) & ".*"
			End If
			span=""
			if len(c_name) <4 then
				for j=len(c_name) to 4
					span = span&"&nbsp;&nbsp;"
				next
			end if
			
			writeA "<div class=""dt""><span class=""author""><span class=""name"" style=""background-color:"&bgstr&";"">"&c_name&span&"</span><span class=""address"">&nbsp;IP:["&c_ip&"]</span></span><span class=""post-time"">发表于["&c_time&"]&nbsp;第"&(page-1)*app_commentnum+i&"楼</span><div class=""clear""><br/></div></div><div class=""dd""><p class=""content"">"&c_content&"</p><div class=""toolbar"" id=""toolbar-"&c_id&"""><a href=""#cmt_reply"" onclick=""comment_reply("&c_id&");"">回复</a></div>"&replystr&"</div>"
            rs.movenext
        Loop
        objdb.C (rs)
        writeA "<div class=""pages"">" & getPageInfo(total, page) & "</div>"
	End If
End Sub
    
Function getPageInfo(totalPage, absolutePage)
        Dim pgStr, has1, has2, begin, endnum, i
        has1 = absolutePage - 5 >= 1
        has2 = absolutePage + 5 <= totalPage
        begin = 1: endnum = 1
        
        If has1 And has2 Then
          begin = absolutePage - 4
          endnum = absolutePage + 5
        ElseIf Not has1 And has2 Then
          begin = 1
          If totalPage >= 10 Then endnum = 10 Else endnum = totalPage
        ElseIf has1 And Not has2 Then
          begin = totalPage - 9
          If begin < 1 Then begin = 1
          endnum = totalPage
        Else
          begin = 1
          endnum = totalPage
        End If
        
        pgStr = "共" & totalPage & "页&nbsp;"
        If absolutePage > 1 Then
            pgStr = pgStr & "<a href='javascript:' p='1' title='首页'>首页</a>"
        End If
        
        If absolutePage > 1 Then
            pgStr = pgStr & " <a href='javascript:' p='" & (absolutePage - 1) & "' title='上一页'>上一页</a>"
        End If
        
        For i = begin To endnum
            If i <> absolutePage Then
                pgStr = pgStr & " <a href='javascript:' p='" & i & "'"  & ">" & i & "</a>"
            Else
                pgStr = pgStr & " <span class=""active""" & ">" & i & "</span>"
            End If
        Next
        
        If absolutePage < totalPage Then
            pgStr = pgStr & " <a href='javascript:' p='" & (absolutePage + 1) & "' title='下一页'>下一页</a>"
            pgStr = pgStr & " <a href='javascript:' p='" & (totalPage) & "' title='尾页'>尾页</a>"
        End If
        getPageInfo = pgStr
    End Function
%>