<!--#include file="conn.asp"-->
<!--#include file="inc/const.asp" -->
<!--#include file="inc/plus_check.asp"-->
<!--#include file="Dv_Plus/Avatar/Plus_Avatar_Const.asp"-->
<%
Dim i, j, TempStr, Action, Flag
Dim AvatarArray, ItemCount, AvatarStr, TempCount

Dv_Plus.Name = "Plus_Avatar"
Dv_Plus.CheckLogin
Dvbbs.LoadTemplates "Avatar"
Dvbbs.Stats = Template.Strings(27)
Dvbbs.ActiveOnline
v_FoundErr = False
If Dvbbs.Forum_Setting(82) <> "0" Then
	v_ErrCodes = "<li>该论坛尚未开启虚拟形象功能，不能使用收藏夹！"
	v_FoundErr = True
Else
	If Dvbbs.UserID = 0 Then
		v_ErrCodes = v_ErrCodes & "<li>您不是会员，不能使用收藏夹！"
		v_FoundErr = True
	Else
		If Request("action") = "clear" Then
			Dvbbs.Plus_Execute("Delete From Dv_Plus_Avatar_Favorite Where UserName='" & Dvbbs.MemberName & "'")
			SucMsg = "<li>已经成功的清除了您的收藏夹！"
			Call ShowSuc(SucMsg, False)
			Response.Write "<script>if(window.opener) window.opener.document.getElementById('AvatarFrame').contentWindow.document.execCommand('Refresh');</script>"
		Else
			If Trim(Request("Avatar")) = "" Then
				AvatarStr = ""
			Else
				AvatarStr = Split(Trim(Request("Avatar")), ",")(0)
			End If
			If AvatarStr = "" Then
				Action = Request("action")
				AvatarStr = Trim(Request("ItemID"))
				If Not IsInteger(AvatarStr) Or (Action <> "add" And Action <> "del") Then
					If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
					v_ErrCodes = v_ErrCodes & "<li>提交的数据有错误！"
					v_FoundErr = True
				Else
					If Not IsEmotionItem(AvatarStr) And Not IsDefaultItem(AvatarStr) Then
						ReDim AvatarArray(0)
						AvatarArray(0) = CLng(AvatarStr)
						ItemCount = 1
					Else
						If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
						v_ErrCodes = v_ErrCodes & "<li>默认形象和默认表情商品不能放入收藏夹！"
						v_FoundErr = True
					End If
				End If
			Else
				Action = "add"
				ItemCount = Avatar_GetInfo(AvatarStr, AvatarArray)
				If ItemCount = 0 Or (Action <> "add" And Action <> "del") Then
					If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
					v_ErrCodes = v_ErrCodes & "<li>提交的数据有错误！"
					v_FoundErr = True
				End If
				For i = ItemCount - 1 To 0 Step -1
					If AvatarArray(i) = "" Then
						For j = i To ItemCount - 2
							AvatarArray(j) = AvatarArray(j + 1)
						Next
						ItemCount = ItemCount - 1
						ReDim Preserve AvatarArray(ItemCount - 1)
					ElseIf IsEmotionItem(AvatarArray(i)) Or IsDefaultItem(AvatarArray(i)) Then
						For j = i To ItemCount - 2
							AvatarArray(j) = AvatarArray(j + 1)
						Next
						ItemCount = ItemCount - 1
						ReDim Preserve AvatarArray(ItemCount - 1)
					Else
						For j = 0 To i - 1
							If AvatarArray(j) = AvatarArray(i) Then Exit For
						Next
						If j <= i - 1 Then
							For j = i To ItemCount - 2
								AvatarArray(j) = AvatarArray(j + 1)
							Next
							ItemCount = ItemCount - 1
							ReDim Preserve AvatarArray(ItemCount - 1)
						End If
					End If
				Next
			End If
			If ItemCount = 0 Then
				If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
				v_ErrCodes = v_ErrCodes & "<li>没有任何可供放入收藏夹的商品！"
				v_FoundErr = True
			End If
			If Not v_FoundErr Then
				For i = 0 To ItemCount - 1
					If Dvbbs.Plus_Execute("Select Top 1 ID From Dv_Plus_Avatar_Items Where ID=" & AvatarArray(i)).Eof Then
						If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
						v_ErrCodes = v_ErrCodes & "<li>提交的数据有错误！"
						v_FoundErr = True
						Exit For
					End If
				Next
				If Not v_FoundErr Then
					TempCount = Dvbbs.Plus_Execute("Select Count(*) From Dv_Plus_Avatar_Favorite Where UserName = '" & Dvbbs.MemberName & "'")(0)
					SucMsg = ""
					Flag = 0
					For i = 0 To ItemCount - 1
						Set AvatarRs = Dvbbs.Plus_Execute("Select Top 1 Name From Dv_Plus_Avatar_Items Where ID=" & AvatarArray(i))
						If Dvbbs.Plus_Execute("Select Top 1 ID From Dv_Plus_Avatar_Favorite Where UserName='" & Dvbbs.MemberName & "' And ItemID=" & AvatarArray(i)).Eof Then
							If Action = "add" Then
								If Not Dvbbs.Plus_Execute("Select Top 1 ID From Dv_Plus_Avatar_MyItems Where UserName='" & Dvbbs.MemberName & "' And ItemID=" & AvatarArray(i)).Eof Then
									If SucMsg <> "" Then SucMsg = SucMsg & "<br>"
									SucMsg = SucMsg & "<li><font color=" & Dvbbs.MainSetting(1) & ">您已经拥有商品《" & AvatarRs(0) & "》，无需放入收藏夹！</font>"
									If Flag = 0 Or Flag = 2 Then Flag = 2 Else Flag = 3
								ElseIf TempCount + 1 > CLng(AvatarXMLDOM.documentElement.selectSingleNode("AvatarInfo").Attributes.getNamedItem("FavoriteLength").Text) Then
									If SucMsg <> "" Then SucMsg = SucMsg & "<br>"
									SucMsg = SucMsg & "<li><font color=" & Dvbbs.MainSetting(1) & ">您的收藏夹已满，无法放入商品《" & AvatarRs(0) & "》！</font>"
									If Flag = 0 Or Flag = 2 Then Flag = 2 Else Flag = 3
								Else
									Dvbbs.Plus_Execute("Insert Into Dv_Plus_Avatar_Favorite (UserName,ItemID) Values ('" & Dvbbs.MemberName & "'," & AvatarArray(i) & ")")
									SucMsg = SucMsg & "<li>已将商品《" & AvatarRs(0) & "》放到您的收藏夹中！"
									TempCount = TempCount + 1
									If Flag = 0 Or Flag = 1 Then Flag = 1 Else Flag = 3
								End If
							Else
								If SucMsg <> "" Then SucMsg = SucMsg & "<br>"
								SucMsg = SucMsg & "<li><font color=" & Dvbbs.MainSetting(1) & ">商品《" & AvatarRs(0) & "》不在您的收藏夹中，无法移除！</font>"
								If Flag = 0 Or Flag = 2 Then Flag = 2 Else Flag = 3
							End If
						Else
							If Action = "add" Then
								If SucMsg <> "" Then SucMsg = SucMsg & "<br>"
								SucMsg = SucMsg & "<li><font color=" & Dvbbs.MainSetting(1) & ">商品《" & AvatarRs(0) & "》已经在您的收藏夹中，无需再次放入！</font>"
								If Flag = 0 Or Flag = 2 Then Flag = 2 Else Flag = 3
							Else
								Dvbbs.Plus_Execute("Delete From Dv_Plus_Avatar_Favorite  Where UserName= '" & Dvbbs.MemberName & "' And ItemID=" & AvatarArray(i))
								SucMsg = SucMsg & "<li>已将商品《" & AvatarRs(0) & "》移出您的收藏夹！"
								If Flag = 0 Or Flag = 1 Then Flag = 1 Else Flag = 3
							End If
						End If
						AvatarRs.Close
					Next
					If SucMsg <> "" Then SucMsg = SucMsg & "<br>"
					If Flag = 0 Then
						SucMsg = SucMsg & "<li>没有任何商品"
						If Action = "add" Then
							SucMsg = SucMsg & "放入"
						Else
							SucMsg = SucMsg & "移出"
						End If
						SucMsg = SucMsg & "您的收藏夹。"
						v_ErrCodes = SucMsg
						v_FoundErr = True
					ElseIf Flag = 1 Then
						SucMsg = SucMsg & "<li>您选择的商品已经成功地"
						If Action = "add" Then
							SucMsg = SucMsg & "放入"
						Else
							SucMsg = SucMsg & "移出"
						End If
						SucMsg = SucMsg & "您的收藏夹，请打开<a href=""Plus_Avatar_Center.asp?shopid=800"" target=""_blank""><font color=blue>收藏夹</font></a>查看。"
						Call ShowSuc(SucMsg, False)
						If Request("Avatar")="" Then Response.Write "<script>if(window.opener) window.opener.document.execCommand('Refresh');</script>"
					ElseIf Flag = 2 Then
						v_ErrCodes = SucMsg
						v_FoundErr = True
					Else
						SucMsg = SucMsg & "<li>您选择的部分商品已经成功地"
						If Action = "add" Then
							SucMsg = SucMsg & "放入"
						Else
							SucMsg = SucMsg & "移出"
						End If
						SucMsg = SucMsg & "您的收藏夹，请打开<a href=""Plus_Avatar_Center.asp?shopid=800"" target=""_blank""><font color=blue>收藏夹</font></a>查看。"
						Call ShowSuc(SucMsg, False)
					End If
				End If
			End If
		End If
	End If
End If
If v_FoundErr Then Call ShowErr(False)
Dvbbs.Footer
Set Dv_Plus = Nothing
%>