<!--#include file="db.asp"-->
<%
StTime = Timer

	'UserCod = Session("UserCod")
	UserPost = Session("UserPost")
	'كاربران:U - امانات:K - نظرات:N - محصولات و موضوعات:P - مالي:M - عالي:A
	If Not (UserPost="P" OR UserPost="A") Then GoAWAY "[" & URLstr & "]->{UserPost} not found!"

CurPage = Request.QueryString("p")
SortOrder = Request.QueryString("s")
NameFilter = Request.Form("NameFilter")

    Msql = "Select valu FROM Setting Where IDN='Stop'"
    rs.Open Msql, con
        If Not rs.EOF Then isStop = rs("valu") Else GoSAFE "[" & URLstr & "]->{Setting.Stop.Valu} not found!"
    rs.Close
    If isStop<>"No" Then GoSAFE "[" & URLstr & "]->{Setting.Stop.Valu} is Yes!"

    Msql = "Select valu FROM Setting Where IDN='SiteName'"
    rs.Open Msql, con
        If Not rs.EOF Then SiteName = rs("valu") Else GoSAFE "[" & URLstr & "]->{Setting.SiteName.Valu} not found!"
    rs.Close

    Msql = "Select valu FROM Setting Where IDN='ListsCnt'"
    rs.Open Msql, con
        If Not rs.EOF Then ListsCnt = rs("valu") Else GoSAFE "[" & URLstr & "]->{Setting.ListsCnt.Valu} not found!"
    rs.Close

    Msql = "Select valu FROM Setting Where IDN='PageCnt'"
    rs.Open Msql, con
        If Not rs.EOF Then PageCnt = rs("valu") Else GoSAFE "[" & URLstr & "]->{Setting.PageCnt.Valu} not found!"
    rs.Close
    
If (fso.FileExists(TempPath)) Then
    Set fs = fso.OpenTextFile(TempPath, 1)
		tmp = fs.ReadAll
    fs.Close()
    
		NameFilterExp=""
		If Not Len(SortOrder)>0 Then SortOrder = "1"
		NameFilter = Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(NameFilter,"&",""),"*",""),"=",""),"+",""),"-",""),".",""),">",""),"<",""),",",""),"%",""),"""",""),"'","")
		If Len(NameFilter)>0 Then 
			NameFilterExp = "(Author.FirstName LIKE '%" & NameFilter & "%' OR Author.FirstName LIKE '" & NameFilter & "%' OR Author.FirstName LIKE '%" & NameFilter & "' OR Author.LastName LIKE '%" & NameFilter & "%' OR Author.LastName LIKE '" & NameFilter & "%' OR Author.LastName LIKE '%" & NameFilter & "')"
			CurPage="1"
		Else
			NameFilterExp = "1=1"
		End If
		Select Case SortOrder
			Case "1" 'الفباي نام
				Msql = "SELECT TOP " & ListsCnt & " * FROM Author WHERE " & NameFilterExp & " ORDER BY FirstName ASC"
			Case "2" 'وارونه الفباي نام
				Msql = "SELECT TOP " & ListsCnt & " * FROM Author WHERE " & NameFilterExp & " ORDER BY FirstName DESC"
			Case "3" 'الفباي نام خانوادگي
				Msql = "SELECT TOP " & ListsCnt & " * FROM Author WHERE " & NameFilterExp & " ORDER BY LastName ASC"
			Case "4" 'وارونه الفباي نام خانوادگي
				Msql = "SELECT TOP " & ListsCnt & " * FROM Author WHERE " & NameFilterExp & " ORDER BY LastName DESC"
			Case "5" 'تعداد محصول
				Msql = "SELECT TOP " & ListsCnt & " Author.FirstName, Author.Cod, Author.LastName FROM Author INNER JOIN (SELECT AuthorCod, Count(*) AS Pcnt FROM Product WHERE Del=0 GROUP BY AuthorCod) Product ON Author.Cod = Product.AuthorCod ORDER BY Pcnt DESC"
			Case Else
				GoSAFE "[" & URLstr & "]->{SortOrder.Select.Case} not found!"
		End Select
		If Not Len(CurPage)>0 Then CurPage="1"
		cMsql = Msql
		cMsql = "SELECT Count(*) AS mCnt" & Mid(cMsql, InStr(cMsql, " FROM"), InStrRev(cMsql, " ORDER")-InStr(cMsql, " FROM"))
		'Response.Write Msql & "<br><br>" & cMsql & "<br><br>" & rMsql : Response.End
		'Response.Write cMsql
		rs.Open cMsql, con
			ProdCount = rs("mCnt")
		rs.Close
		AuthorCount = ProdCount
		If Cint(CurPage)<1 Then CurPage="1"
		OtherProd = ProdCount - ((Cint(CurPage)-1) * Cint(ListsCnt))
		isPage = False
		If ProdCount > Cint(ListsCnt) Then
			isPage = True
			PageCount = ProdCount \ Cint(ListsCnt)
			If ProdCount MOD Cint(ListsCnt) > 0 Then PageCount = PageCount + 1
			If InStr(1, Msql, "DESC")>0 Then rMsql = Replace(Msql, "DESC", "ASC") ELSE  rMsql = Replace(Msql, "ASC", "DESC")
			rMsql = "SELECT TOP " & OtherProd & " Cod " & Mid(rMsql, InStr(rMsql, " FROM"))
			rMsql = Mid(Msql, 1, InStr(1, Msql, " ORDER")) & "AND Cod IN (" & rMsql & ") " & Mid(Msql, InStr(1, Msql, "ORDER"))
		End if
		'Response.Write Msql & "<br><br>" & cMsql & "<br><br>" & rMsql : Response.End
		'Response.Write "::" &  rMsql & "::"

		NL = GetLoop(tmp, "AuthorsList") : expNL = ""
		If isPage AND Cint(CurPage)>1 Then Msql = rMsql
		'Response.Write Msql
		rs.Open Msql, con
			cn = 0
			Do Until rs.EOF
				AuthorCod = rs("Cod")
				Msql = "SELECT Count(*) AS Pcnt FROM Product WHERE Del=0 AND AuthorCod=" & AuthorCod
				rsr.Open Msql, con
					If Not rsr.EOF Then ProductCount = rsr("Pcnt") Else ProductCount = "0"
				rsr.Close
			    strData = ""
			    strData = strData & "AuthorCod=" & AuthorCod & "|"
			    strData = strData & "FullName=" & rs("FirstName") & " " & rs("LastName") & "|"
			    strData = strData & "ProductCount=" & ProductCount & "|"
			    'Response.Write strData
				expNL = expNL + SetData(NL, strData)
				cn = cn + 1
			    rs.MoveNext
			Loop
		rs.Close
		Call SetLoop(tmp, "AuthorsList", expNL)
		
		PB = GetLoop(tmp, "Pagebar") : expPB = PB
		If isPage Then
			LP = GetLoop(PB, "LastPage") : expLP = ""
			PN = GetLoop(PB, "PageNumbers") : expPN = ""
			CP = GetLoop(PB, "CurrentPageNumbers")
			LD = GetLoop(PB, "LastDots") : expLD = ""
			ND = GetLoop(PB, "NextDots") : expND = ""
			NP = GetLoop(PB, "NextPage") : expNP = ""
			'PageCount = 24 : 'CurPage = "24"
			If Cint(CurPage)=1 Then
				Call SetLoop(expPB, "LastPage", "")
			Else
				strData = ""
				strData = strData & "LastPageNumber=" & Cint(CurPage)-1 & "|"
				expLP = expLP + SetData(LP, strData)
				Call SetLoop(expPB, "LastPage", expLP)
			End If
			If Cint(CurPage)=PageCount Then
				Call SetLoop(expPB, "NextPage", "")
			Else
				strData = ""
				strData = strData & "NextPageNumber=" & Cint(CurPage)+1 & "|"
				expNP = expNP + SetData(NP, strData)
				Call SetLoop(expPB, "NextPage", expNP)
			End If
			StartCi = 1
			EndCi = PageCount
			If PageCount > Cint(PageCnt) Then
				GropNum = Cint(CurPage) \ Cint(PageCnt)
				If Cint(CurPage) MOD Cint(PageCnt) > 0 Then GropNum = GropNum + 1
				StartCi = (GropNum-1) * Cint(PageCnt) + 1
				EndCi = GropNum * Cint(PageCnt)
				If EndCi > PageCount Then EndCi = PageCount
			End If
			If StartCi = 1 Then
				Call SetLoop(expPB, "LastDots", "")
			Else
				strData = ""
				strData = strData & "LastDotsPage=" & ((GropNum-1) * Cint(PageCnt)) & "|"
				expLD = expLD + SetData(LD, strData)
				Call SetLoop(expPB, "LastDots", expLD)
			End If
			If EndCi = PageCount Then
				Call SetLoop(expPB, "NextDots", "")
			Else
				strData = ""
				strData = strData & "NextDotsPage=" & ((GropNum * Cint(PageCnt)) + 1) & "|"
				expND = expND + SetData(ND, strData)
				Call SetLoop(expPB, "NextDots", expND)
			End If
			'Response.Write CurPage
			For ci=StartCi To EndCi
				If ci = Cint(CurPage) Then
					strData = ""
					strData = strData & "CurrentPageNumber=" & ci & "|"
					expPN = expPN + SetData(CP, strData)
				Else
					strData = ""
					strData = strData & "PageNumber=" & ci & "|"
					expPN = expPN + SetData(PN, strData)
				End If
			Next
			Call SetLoop(expPB, "PageNumbers", expPN)
			Call SetLoop(expPB, "CurrentPageNumbers", "")
			Call SetLoop(tmp, "Pagebar", expPB)
		Else
			Call SetLoop(tmp, "Pagebar", "")
		End If

		UsersFilter = ""
		SFarr = "|1|الفباي نام|2|وارونه الفباي نام|3|الفباي نام‏خانوادگي|4|وارونه الفباي نام‏خانوادگي|5|تعداد محصول|"
		If InStr(1, SFarr,"|" & SortOrder & "|")>0 Then UsersFilterS = "به ترتيب " & GetFilterName(SFarr, SortOrder)
		If Len(NameFilter)>0 Then UsersFilterT = "فيلتر: «" & NameFilter & "»" Else UsersFilterT = ""
		UsersFilter = Replace(Replace(" [" & UsersFilterT & " - " & UsersFilterS & "]", "[ - ", "["), " - ]", "]")

	Set rs = Nothing
    con.Close
    Set con = Nothing

'=================================================

	Call SetVars(tmp)
	
    EnTime = Timer
	'Response.Write EnTime - StTime 'زماني كه اجراي تمپليت طول مي كشد
	tmp = tmp & "<!-- Generate Time: " & (EnTime - StTime) & " Second -->"

	Response.Write tmp

Else
	GoSAFE "[" & URLstr & "]->{TempFile} not found!"
End If

Sub SetVars(Ltxt)
	'---------------------------------------------------------------
    Ltxt = Replace(Ltxt, "<#Page_Header_First#>", Page_Header_First)
    Ltxt = Replace(Ltxt, "<#Page_Header_Last#>", Page_Header_Last)
    Ltxt = Replace(Ltxt, "<#Page_Footer#>", Page_Footer)
    Ltxt = Replace(Ltxt, "<#Box_Header_First#>", Box_Header_First)
    Ltxt = Replace(Ltxt, "<#Box_Header_Last#>", Box_Header_Last)
    Ltxt = Replace(Ltxt, "<#Box_Footer#>", Box_Footer)
	'---------------------------------------------------------------
    Ltxt = Replace(Ltxt, "<#SiteName#>", SiteName)
    Ltxt = Replace(Ltxt, "<#AuthorCount#>", AuthorCount)
    Ltxt = Replace(Ltxt, "<#FileNaam#>", FileNaam)
    Ltxt = Replace(Ltxt, "<#ThisURL#>", Escape(FileNaam & ".asp"))
    Ltxt = Replace(Ltxt, "<#NameFilter#>", NameFilter)
    Ltxt = Replace(Ltxt, "<#SortOrder#>", SortOrder)
    Ltxt = Replace(Ltxt, "<#UsersFilter#>", UsersFilter)
    Ltxt = Replace(Ltxt, "<#ID#>", id)
End Sub

%>
<!--#include file="fc.asp"-->
