<!--#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 NewAvatarStr, NewAvatarArray, TempSplit, DressName, DressCount, UserIsAva
Dim i, j, Flag, SucFlag, BuyItems, BuyCount, PeriodItems, PeriodCount
Dim OldAvatarArray, TempStr, ItemName

Dv_Plus.Name = "Plus_Avatar"
Dv_Plus.CheckLogin
Dvbbs.LoadTemplates "Avatar"
Dvbbs.Stats = Template.Strings(15)
Dvbbs.ActiveOnline
v_FoundErr = False
SucFlag = False
If Dvbbs.Forum_Setting(82) <> "0" Then
	v_ErrCodes = "<li>该论坛尚未开启虚拟形象功能，不能保存形象！"
	v_FoundErr = True
ElseIf Dvbbs.UserID = 0 Then
	v_ErrCodes = "<li>您不是会员，无法保存形象！"
	v_FoundErr = True
Else
	UserIsAva = Request("UserIsAva")
	If IsNull(UserIsAva) Or UserIsAva = "" Then
		UserIsAva = 0
	Else
		If Not IsNumeric(UserIsAva) Then
			UserIsAva = 0
		Else
			UserIsAva = CInt(UserIsAva)
			If UserIsAva <> 0 Then
				UserIsAva = 1
			End If
		End If
	End If
	If v_ToolByUser <> "" Then UserIsAva = 1
	Call Avatar_GetInfo(v_MyAvatar, OldAvatarArray)
	NewAvatarStr = Request.Cookies("MyAvatar_" & Dvbbs.UserID)
	If Avatar_GetInfo(NewAvatarStr, NewAvatarArray) = 0 Then
		If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
		v_ErrCodes = v_ErrCodes & "<li>提交的数据有错误！"
		v_FoundErr = True
	Else
		TempStr = Avatar_CheckItem(NewAvatarArray, v_MySex)
		If TempStr <> "" Then
			If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
			v_ErrCodes = v_ErrCodes & TempStr
			v_FoundErr = True
		Else
			TempStr = Avatar_CheckBuy(NewAvatarArray)
			If TempStr <> "" Then
				If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
				v_ErrCodes = v_ErrCodes & TempStr
				v_FoundErr = True
			End If
		End If
	End If
End If
If v_FoundErr Then
	Call ShowErr(False)
Else
	SucFlag = True
	DressCount = 0
	DressName = ""
	BuyCount = 0
	BuyItems = ""
	PeriodCount = 0
	PeriodItems = ""
	For i = 0 To UBound(NewAvatarArray)
		If NewAvatarArray(i) <> OldAvatarArray(i) Then
			If OldAvatarArray(i) <> "" Then
				If Not IsEmotionItem(OldAvatarArray(i)) And Not IsDefaultItem(OldAvatarArray(i)) Then
					Set AvatarRs = Dvbbs.Plus_Execute("Select Top 1 Name From Dv_Plus_Avatar_Items Where ID=" & OldAvatarArray(i))
					Call DressDown(AvatarRs(0),i)
					AvatarRs.Close
				End If
			End If
			If NewAvatarArray(i) <> "" Then
				If Not IsEmotionItem(NewAvatarArray(i)) And Not IsDefaultItem(NewAvatarArray(i)) Then
					AvatarSQL = "Select Top 1 Name From Dv_Plus_Avatar_Items Where ID=" & NewAvatarArray(i)
					Set AvatarRs = Dvbbs.Plus_Execute(AvatarSQL)
					ItemName = AvatarRs(0)
					AvatarRs.Close
					AvatarSQL = "Select Top 1 ID From Dv_Plus_Avatar_MyItems Where UserName='" & Dvbbs.MemberName & "' And ItemID=" & NewAvatarArray(i)
					Set AvatarRs = Dvbbs.Plus_Execute(AvatarSQL)
					If Not AvatarRs.Eof Then
						Call DressUp(ItemName,i)
					Else
						SucFlag = False
						Call DressBuy(ItemName,i)
					End If
					AvatarRs.Close
				End If
			End If
		End If
	Next
	If SucFlag Then
		NewAvatarStr = ""
		For i = 0 To 24
			If i > 0 Then NewAvatarStr = NewAvatarStr & "|"
			If NewAvatarArray(i) <> "" Then
				AvatarSQL = "Select Top 1 Content From Dv_Plus_Avatar_Items Where ID=" & NewAvatarArray(i)
				Set AvatarRs = Dvbbs.Plus_Execute(AvatarSQL)
				If Not AvatarRs.Eof Then
					NewAvatarStr = NewAvatarStr & NewAvatarArray(i) & "." & AvatarRs(0)
				End If
				AvatarRs.Close
			End If
		Next
	End If
	If SucFlag And DressName <> "" Then
		If v_ToolByUser <> "" Then
			v_ErrCodes = "<li>您正在被<b>" & v_ToolByUser & "</b>使用整人道具，无法保存形象！"
			v_FoundErr = True
			Call ShowErr(False)
		Else
			Dvbbs.Execute ("Update [Dv_User] Set UserAvaSetting='" & NewAvatarStr & "',UserIsAva=" & UserIsAva & " Where UserName='" & Dvbbs.MemberName & "'")
			SucMsg = "<li>您已经成功地："
			SucMsg = SucMsg & DressName
			Call ShowSuc(SucMsg, False)
			Response.write "<script>document.cookie='MyAvatar_" & Dvbbs.UserID & "=' + escape('');if(window.opener) window.opener.document.execCommand('Refresh');</script>"
		End If
	ElseIf Not SucFlag And BuyItems <> "" Then
		If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
		v_ErrCodes = v_ErrCodes & "<li><font color={$color}>如下商品尚未购买不能换上：</font>"
		v_ErrCodes = v_ErrCodes + BuyItems
		v_ErrCodes = v_ErrCodes & "<br><li><font color={$color}>3</font> 秒之后将打开购物袋供您结算！"
		v_FoundErr = True
		Call ShowErr(False)
		Response.write "<script language=javascript>setTimeout(""PlusOpen('Plus_Avatar_Cartbag.asp?action=1&userisava=" & UserIsAva & "',500,440)"",3000);</script>"
	Else
		If v_ToolByUser <> "" Then
			v_ErrCodes = "<li>您正在被<b>" & v_ToolByUser & "</b>使用整人道具，无法保存形象！"
			v_FoundErr = True
			Call ShowErr(False)
		Else
			Dvbbs.Execute ("Update [Dv_User] Set UserAvaSetting='" & NewAvatarStr & "',UserIsAva=" & UserIsAva & " Where UserName='" & Dvbbs.MemberName & "'")
			SucMsg = "<li>您已经成功地保存了当前的形象！"
			Call ShowSuc(SucMsg, False)
			Response.write "<script>document.cookie='MyAvatar_" & Dvbbs.UserID & "=' + escape('');if(window.opener) window.opener.document.execCommand('Refresh');</script>"
		End If
	End If
End If
If Not v_FoundErr Then Dvbbs.Footer
Set Dv_Plus = Nothing

Sub DressDown(ItemName, CurLayer)
	Dim Flag, i
	
	Flag = False
	For i = 0 To CurLayer - 1
		If OldAvatarArray(CurLayer) = OldAvatarArray(i) Then
			Flag = True
			Exit For
		End If
	Next
	If Not Flag Then
		DressCount = DressCount + 1
		DressName = DressName & "<br>&nbsp;&nbsp;&nbsp;" & DressCount & "、换下了<font color=red>" & ItemName & "</font>"
	End If
End Sub

Sub DressUp(ItemName, CurLayer)
	Dim Flag, i
	
	Flag = False
	For i = 0 To CurLayer - 1
		If NewAvatarArray(CurLayer) = NewAvatarArray(i) Then
			Flag = True
			Exit For
		End If
	Next
	If Not Flag Then
		DressCount = DressCount + 1
		DressName = DressName & "<br>&nbsp;&nbsp;&nbsp;" & DressCount & "、换上了<font color=blue>" & ItemName & "</font>"
	End If
End Sub

Sub DressBuy(ItemName, CurLayer)
	Dim Flag, i, ItemCount, CartBagArray
	Dim CartBag, Rs
	
	Flag = False
	For i = 0 To CurLayer - 1
		If NewAvatarArray(CurLayer) = NewAvatarArray(i) Then
			Flag = True
			Exit For
		End If
	Next
	If Not Flag Then
		BuyCount = BuyCount + 1
		ItemCount = CartBag_GetInfo(CartBagArray, Dvbbs.UserID)
		If CartBag_FindItem(CartBagArray, NewAvatarArray(CurLayer), "") Then
			BuyItems = BuyItems & "<br>&nbsp;&nbsp;&nbsp;" & BuyCount & "、购物袋中已有商品《" & ItemName & "》，无须再次放入！"
		ElseIf ItemCount >= CLng(AvatarXMLDOM.documentElement.selectSingleNode("AvatarInfo").Attributes.getNamedItem("CartLength").Text) Then
			BuyItems = BuyItems & "<br>&nbsp;&nbsp;&nbsp;" & BuyCount & "、购物袋已满，无法将商品《" & ItemName & "》放入！"
		Else
			Call CartBag_AddItem(CartBagArray, NewAvatarArray(CurLayer), "", "")
			Call CartBag_SetInfo(CartBagArray, Dvbbs.UserID)
			BuyItems = BuyItems & "<br>&nbsp;&nbsp;&nbsp;" & BuyCount & "、已将商品《" & ItemName & "》放入您的购物袋！"
		End If
	End If
End Sub
%>