<!--#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 ItemName, ItemCount, CartBagArray
Dim CurItem, CurType, SalePrice, TempStr

Dv_Plus.Name = "Plus_Avatar"
Dv_Plus.CheckLogin
Dvbbs.LoadTemplates "Avatar"
Dvbbs.Stats = Template.Strings(8)
Dvbbs.ActiveOnline
v_FoundErr = False
If Dvbbs.Forum_Setting(82) <> "0" Then
	v_ErrCodes = v_ErrCodes & "<li>该论坛尚未开启虚拟形象功能，不能购买形象商品！"
	v_FoundErr = True
ElseIf Dvbbs.UserID = 0 Then
	v_ErrCodes = "<li>您不是会员，无法购买形象商品！"
	v_FoundErr = True
Else
	CurItem = Dvbbs.CheckStr(Request("itemid"))
	If IsNull(Request("itemid")) Or Request("itemid") = "" Or Not isInteger(Request("itemid")) Then
		If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
		v_ErrCodes = v_ErrCodes & "<li>没有指定商品编号！"
		v_FoundErr = True
	Else
		CurItem = CLng(Request("itemid"))
	End If
End If
If v_FoundErr Then
	Call ShowErr(False)
Else
	If Request("FromUser") <> "" Then
		Set AvatarRs = Dvbbs.Plus_Execute("Select Top 1 i.ID,m.SalePrice,i.Name,i.Class,m.Period,m.FixDate From Dv_Plus_Avatar_MyItems m Inner Join Dv_Plus_Avatar_Items i On m.ItemID=i.ID Where m.ID=" & CurItem & " And m.UserName='" & Dvbbs.CheckStr(Trim(Request("fromuser"))) & "'")
		If Not AvatarRs.EOF Then
			CurItem = AvatarRs(0)
			SalePrice = AvatarRs(1)
			ItemName = AvatarRs(2)
			CurType = AvatarRs(3)
			If DateDiff("d", AvatarRs(5), now()) > AvatarRs(4) And AvatarRs(4) > 0 Then CurType = 0
			If SalePrice > v_MyMoney Then
				If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
				v_ErrCodes = v_ErrCodes & "<li>购买此商品需要现金 " & SalePrice & " 元，您的现金(" & v_MyMoney & " 元)不足，无法购买！</font>"
				v_FoundErr = True
			End If
		Else
			If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
			v_ErrCodes = v_ErrCodes & "<li>此商品并非由用户 " & Trim(Request("fromuser")) & " 出售，无法购买！</font>"
			v_FoundErr = True
		End If
		AvatarRs.Close
	End If
	If v_FoundErr Then
		Call ShowErr(False)
	Else
		Set AvatarRs = Dvbbs.Plus_Execute("Select Top 1 ItemID From Dv_Plus_Avatar_MyItems Where UserName='" & Dvbbs.MemberName & "' And ItemID=" & CurItem)
		If Not AvatarRs.EOF Then
			If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
			v_ErrCodes = v_ErrCodes & "<li>您已经拥有该商品，无须再次购买！"
			v_FoundErr = True
		Else
			If Not Request("fromuser") <> "" Then
				Set AvatarRs = Dvbbs.Plus_Execute("Select Top 1 Name,Vip,Quantity,Flag From Dv_Plus_Avatar_Items Where ID=" & CurItem)
				If Not AvatarRs.EOF Then
					ItemName = AvatarRs(0)
					TempStr = CheckBuy(AvatarRs(0), AvatarRs(1), AvatarRs(2), AvatarRs(3))
					If TempStr <> "" Then
						If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
						v_ErrCodes = v_ErrCodes & TempStr
						v_FoundErr = True
					End If
				Else
					If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
					v_ErrCodes = v_ErrCodes & "<li>错误的商品编码！"
					v_FoundErr = True
				End If
			End If
		End If
		AvatarRs.Close
		Set AvatarRs = Nothing
		If v_FoundErr Then
			Call ShowErr(False)
		Else
			If Request("fromuser") <> "" Then
				Dvbbs.Plus_Execute ("Update Dv_Plus_Avatar_MyItems Set Type=" & CurType & ",Price=Saleprice,SalePrice=0,FromUser=UserName,UserName='" & Dvbbs.MemberName & "' Where ItemID=" & CurItem & " And Type=5 And UserName='" & Dvbbs.CheckStr(Trim(Request("fromuser"))) & "'")
				Dvbbs.Plus_Execute ("Delete From Dv_Plus_Avatar_Favorite Where ItemID=" & CurItem & " And UserName='" & Dvbbs.MemberName & "'")
				Call UpdateUserWealth(Dvbbs.MemberName, -SalePrice)
				Call UpdateUserWealth(Dvbbs.CheckStr(Trim(Request("fromuser"))), SalePrice)
				Call Dv_SendMsg(Dvbbs.CheckStr(Trim(Request("fromuser"))), Dvbbs.Forum_Info(0), Template.Strings(4), Replace(Replace(Template.Strings(5), "{$ItemName}", ItemName), "{$UserName}", Dvbbs.MemberName))
				SucMsg = "<li>已经成功地购买了二手商品《" & ItemName & "》！"
				Call ShowSuc(SucMsg, False)
				Response.Write "<script>if(window.opener) window.opener.document.execCommand('Refresh');</script>"
			Else
				ItemCount = CartBag_GetInfo(CartBagArray, Dvbbs.UserID)
				If CartBag_FindItem(CartBagArray, CurItem, "") Then
					If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
					v_ErrCodes = v_ErrCodes & "<li>您的购物袋中已有商品《" & ItemName & "》，无须再次购买！"
					v_FoundErr = True
					Call ShowErr(False)
				ElseIf ItemCount < CLng(AvatarXMLDOM.documentElement.selectSingleNode("AvatarInfo").Attributes.getNamedItem("CartLength").Text) Then
					Call CartBag_AddItem(CartBagArray, CurItem, "", "")
					ItemCount = ItemCount + 1
					Call CartBag_SetInfo(CartBagArray, Dvbbs.UserID)
					SucMsg = "<li>已将商品《" & ItemName & "》放到您的购物袋中！"
					Call ShowSuc(SucMsg, False)
				Else
					If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
					v_ErrCodes = v_ErrCodes & "<li>您的购物袋已满，无法将商品《" & ItemName & "》放入！"
					v_FoundErr = True
					Call ShowErr(False)
				End If
			End If
		End If
	End If
End If
Dvbbs.Footer
Set Dv_Plus = Nothing
%>