<!--#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 CurItem, ItemName, UserName, MsgBody
Dim SucFlag
Dim SendSplit, CurType
Dim ItemCount,CartBagArray
Dim SendFee, CurPrice, SalePrice
Dim TempStr
Dim RetFlag

Dv_Plus.Name = "Plus_Avatar"
Dv_Plus.CheckLogin
Dvbbs.LoadTemplates "Avatar"
Dvbbs.Stats = Template.Strings(1)
Dvbbs.ActiveOnline
v_FoundErr=False
If Request("action") = "send" Then
	RetFlag = True
Else
	RetFlag = False
End If
SucFlag = 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
	If Request("myself") = 1 Then
		ItemCount = 0
		ReDim CartBagArray(-1)
	Else
		If Request("Fromuser") <> "" Then
			ItemCount = 0
			ReDim CartBagArray(-1)
		Else
			ItemCount = CartBag_GetInfo(CartBagArray,Dvbbs.UserID)
		End If
	End If
	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
	If Request("action") = "send" Then
		If IsNull(Request("username")) Or Request("username") = "" Then
			If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
			v_ErrCodes = v_ErrCodes & "<li>没有指定受赠者！"
			v_FoundErr = True
		Else
			UserName = Dvbbs.CheckStr(Request("username"))
			If LCase(Trim(UserName)) = LCase(Trim(Dvbbs.MemberName)) Then
				If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
				v_ErrCodes = v_ErrCodes & "<li>不能给自己赠送商品！"
				v_FoundErr = True
			End If
			If Dvbbs.Execute("Select Top 1 UserID From [Dv_User] Where UserName='" & UserName & "'").Eof Then
				If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
				v_ErrCodes = v_ErrCodes & "<li>受赠者不存在！"
				v_FoundErr = True
			End If
			If (IsNull(Request("modify")) Or Request("modify") = "") And CartBag_FindItem(CartBagArray, CurItem, UserName) Then
				If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
				v_ErrCodes = v_ErrCodes & "<li>向该用户赠送的这个商品已经在您的购物袋中了，无需再次赠送！"
				v_FoundErr = True
			End If
		End If
		If IsNull(Request("message")) Or Request("message") = "" Then
			If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
			v_ErrCodes = v_ErrCodes & "<li>随同赠言不可为空！"
			v_FoundErr = True
		Else
			MsgBody = Dvbbs.CheckStr(Request("message"))
		End If
	ElseIf ItemCount > 0 Then
		If IsNull(Request("username")) Or Request("username") = "" Then
			UserName = ""
		Else
			UserName = Dvbbs.CheckStr(Request("username"))
		End If
		MsgBody = CardBag_GetSendMsg(CartBagArray,CurItem,UserName)
	End If
End If
If v_FoundErr Then
	Call ShowErr(RetFlag)
Else
	If Request("Fromuser") <> "" Then
		If Request("action") = "send" Then
			Set AvatarRs = Dvbbs.Plus_Execute("Select Top 1 SalePrice,ItemID From Dv_Plus_Avatar_MyItems Where ItemID=" & CurItem & " And UserName='" & Dvbbs.CheckStr(Trim(Request("Fromuser"))) & "'")
		Else
			Set AvatarRs = Dvbbs.Plus_Execute("Select Top 1 SalePrice,ItemID From Dv_Plus_Avatar_MyItems Where ID=" & CurItem & " And UserName='" & Dvbbs.CheckStr(Trim(Request("Fromuser"))) & "'")
		End If
		If Not AvatarRs.EOF Then
			SalePrice = AvatarRs(0)
			CurItem = AvatarRs(1)
			SendFee = Int(SalePrice * CDbl(AvatarXMLDOM.documentElement.selectSingleNode("AvatarInfo").attributes.getNamedItem("SendFeeRate").text) / 100 + 0.99)
			If SalePrice + SendFee > v_MyMoney Then
				If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
				v_ErrCodes = v_ErrCodes & "<li>购买并赠送此商品需要现金 " & (SalePrice + SendFee) & " 元，您的现金(" & v_MyMoney & " 元)不足，无法赠送！"
				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
		AvatarRs.Close
	End If
	If Request("action") = "send" Then
		AvatarSQL = "Select Top 1 ItemID From Dv_Plus_Avatar_MyItems Where UserName='" & UserName & "' And ItemID=" & CurItem
		If Not Dvbbs.Plus_Execute(AvatarSQL).Eof Then
			If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
			v_ErrCodes = v_ErrCodes & "<li>用户 " & UserName & " 已经拥有该商品，无须赠送！"
			v_FoundErr = True
		End If
	End If
	If v_FoundErr Then
		Call ShowErr(RetFlag)
	Else
		AvatarSQL = "Select Top 1 * From Dv_Plus_Avatar_Items Where ID=" & CurItem
		Set AvatarRs = Dvbbs.Plus_Execute(AvatarSQL)
		If AvatarRs.Eof Then
			If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
			v_ErrCodes = v_ErrCodes & "<li>错误的商品编码！"
			v_FoundErr = True
		Else
			ItemName = AvatarRs("Name")
			CurType = AvatarRs("Class")
			If v_MyVip <> 1 Then
				CurPrice = AvatarRs("Price1")
			Else
				CurPrice = AvatarRs("Price2")
			End If
			If Not Request("Fromuser") <> "" Then SendFee = Int(CurPrice * CDbl(AvatarXMLDOM.documentElement.selectSingleNode("AvatarInfo").attributes.getNamedItem("SendFeeRate").text) / 100 + 0.99)
			If Request("myself") = 1 Then
				If InStr("|" & v_MyAvatar, "|" & CurItem & ".") > 0 Then
					If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
					v_ErrCodes = v_ErrCodes & "<li>不能赠送正在使用的商品！"
					v_FoundErr = True
				Else
					AvatarSQL = "Select Top 1 ID From Dv_Plus_Avatar_MyItems Where ItemID=" & CurItem & " And UserName='" & Dvbbs.MemberName & "'"
					If Dvbbs.Plus_Execute(AvatarSQL).Eof Then
						If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
						v_ErrCodes = v_ErrCodes & "<li>您没有这个商品，不能赠送！"
						v_FoundErr = True
					End If
				End If
			Else
				If Not Request("Fromuser") <> "" Then
					TempStr = CheckBuy(AvatarRs("Name"), AvatarRs("vip"), AvatarRs("quantity"), AvatarRs("flag"))
					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
		AvatarRs.Close
		If v_FoundErr Then
			Call ShowErr(RetFlag)
		Else
			If Request("action") = "send" Then
				If Request("myself") = 1 Then
					Dvbbs.Plus_Execute("Update Dv_Plus_Avatar_MyItems Set UserName='" & UserName & "',FromUser='" & Dvbbs.MemberName & "',Type=" & CurType & " Where UserName='" & Dvbbs.MemberName & "' And ItemID=" & CurItem)
					Dvbbs.Plus_Execute ("Delete From Dv_Plus_Avatar_Favorite Where ItemID=" & CurItem & " And UserName='" & UserName & "'")
					Call Dv_SendMsg(UserName, Dvbbs.MemberName, Replace(Template.Strings(2),"{$ItemName}",ItemName), MsgBody & Replace(Template.Strings(3),"\n",vbCrLf))
					Call UpdateUserWealth(Dvbbs.MemberName, -SendFee)
					SucMsg = "<li>已经成功地给 <font color=blue>" & UserName & "</font> 赠送了《" & ItemName & "》！"
					Call ShowSuc(SucMsg, False)
					Response.write "<script>if(window.opener) window.opener.document.execCommand('Refresh');</script>"
				Else
					If Request("Fromuser") <> "" Then
						Dvbbs.Plus_Execute ("Update Dv_Plus_Avatar_MyItems Set UserName='" & UserName & "',FromUser='" & Dvbbs.MemberName & "',Type=" & CurType & ",Price=SalePrice,SalePrice=0 Where UserName='" & Dvbbs.CheckStr(Trim(Request("Fromuser"))) & "' And Type=5 And ItemID=" & CurItem)
						Dvbbs.Plus_Execute ("Delete From Dv_Plus_Avatar_Favorite Where ItemID=" & CurItem & " And UserName='" & UserName & "'")
						Call Dv_SendMsg(UserName, Dvbbs.MemberName, Replace(Template.Strings(2),"{$ItemName}",ItemName), MsgBody & Replace(Template.Strings(3),"\n",vbCrLf))
						Call UpdateUserWealth(Dvbbs.MemberName, -(SalePrice + SendFee))
						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>已经成功地给 <font color=blue>" & UserName & "</font> 赠送了二手商品《" & ItemName & "》！"
						Call ShowSuc(SucMsg, False)
						Response.write "<script>if(window.opener) window.opener.document.execCommand('Refresh');</script>"
					Else
						SucFlag = CartBag_UpdateSendMsg(CartBagArray,CurItem,UserName,MsgBody)
						If SucFlag Then
							Call CartBag_SetInfo(CartBagArray,Dvbbs.UserID)
							Response.Redirect "Plus_Avatar_CartBag.asp"
						Else
							If ItemCount < CLng(AvatarXMLDOM.documentElement.selectSingleNode("AvatarInfo").attributes.getNamedItem("CartLength").text) Then
								Call CartBag_AddItem(CartBagArray,CurItem,UserName,MsgBody)
								ItemCount = ItemCount + 1
								Call CartBag_SetInfo(CartBagArray,Dvbbs.UserID)
								SucMsg = "<li>已将赠送给 <font color=blue>" & UserName & "</font> 的商品《" & ItemName & "》放到您的购物袋中！"
								Call ShowSuc(SucMsg, False)
							Else
								v_FoundErr = True
								If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
								v_ErrCodes = "<li>您的购物袋已满，无法将赠送给 <font color=blue>" & UserName & "</font> 的商品《" & ItemName & "》放入！"
								Call ShowErr(RetFlag)
							End If
						End If
					End If
				End If
			Else
				Dvbbs.Head
				TempStr = Template.html(5)
				TempStr = Replace(TempStr, "{$ItemName}", ItemName)
				If IsNull(Request("modify")) Or Request("modify") = "" Then
					TempStr = Replace(TempStr, "{$Modify}", "撰写")
				Else
					TempStr = Replace(TempStr, "{$Modify}", "修改")
				End If
				TempStr=Replace(TempStr,"{$Sms_maxbody}",(Dvbbs.GroupSetting(34)))
				TempStr=Replace(TempStr,"{$SendFee}",SendFee)
				TempStr=Replace(TempStr,"{$ModifyFlag}",Request("modify"))
				If UserName <> "" Then
					TempStr=Replace(TempStr,"{$SendToUserTextBox}","&nbsp;<input type=""hidden"" name=""username"" value=""" & UserName & """>" & UserName)
					TempStr=Replace(TempStr,"{$Freind_Select}","")
				Else
					TempStr=Replace(TempStr,"{$SendToUserTextBox}","<input type=""text"" name=""username"" size=""50"">")
					TempStr=Replace(TempStr,"{$Freind_Select}",Template.html(6))
					TempStr=Replace(TempStr,"{$Friend_option}",OPTION_Friend)
				End If
				If MsgBody = "" Then
					If Not Dvbbs.UserSession.documentElement.selectSingleNode("userinfo/@postdata") Is Nothing Then
						MsgBody = Server.HTMLEncode(Dvbbs.UserSession.documentElement.selectSingleNode("userinfo/@postdata").text)
					Else
						MsgBody = ""
					End If 
				End If
				TempStr=Replace(TempStr,"{$textarea}",MsgBody)
				With AvatarXMLDOM.documentElement.selectSingleNode("AvatarInfo").attributes
					TempStr=Replace(TempStr,"{$SampleWidth}",.getNamedItem("SampleWidth").text)
					TempStr=Replace(TempStr,"{$SampleHeight}",.getNamedItem("SampleHeight").text)
					TempStr=Replace(TempStr,"{$LocalPic}",.getNamedItem("LocalPic").text)
					TempStr=Replace(TempStr,"{$PicPath}",.getNamedItem("PicPath").text)
				End With
				TempStr=Replace(TempStr,"{$ItemNo}",CurItem)
				TempStr=Replace(TempStr,"{$Myself}",request("myself"))
				If Request("Fromuser") <> "" Then
					TempStr=Replace(TempStr,"{$FromUserInput}","<input type=""hidden"" name=""Fromuser"" value=""" & Request("Fromuser") & """>")
					TempStr=Replace(TempStr,"{$SalesPrice}","④ 购买该商品的费用为：" & SalePrice & " 元")
				Else
					TempStr=Replace(TempStr,"{$FromUserInput}","")
					TempStr=Replace(TempStr,"{$SalesPrice}","")
				End If
				If UserName <> "" Then
					TempStr=Replace(TempStr,"{$ReturnCartBagButton}","<input type=""button"" name=""ReturnCartBag"" value=""返回"" onclick=""parent.location='Plus_Avatar_CartBag.asp'"">&nbsp;")
				Else
					TempStr=Replace(TempStr,"{$ReturnCartBagButton}","")
				End If
				Response.Write "<script src=""images/post/post.js"" type=""text/javascript""></script>"
				Response.Write TempStr
			End If
		End If
	End If
End If
Dvbbs.Footer
Set Dv_Plus = Nothing

Function OPTION_Friend()
	DIM i,Sql,Rs
	Sql="SELECT F_friend FROM Dv_Friend WHERE F_userid=" & Dvbbs.UserID & " And F_Mod=1 ORDER BY F_addtime DESC"
	Set Rs=Dvbbs.Execute(Sql)
	If not Rs.eof Then
		SQL=Rs.GetRows(-1)
		Rs.Close:Set Rs=Nothing
	End if
	If IsArray(SQL) Then
		For i=0 To Ubound(SQL,2)
			OPTION_Friend=OPTION_Friend & "<OPTION value=""" & SQL(0,i) & """>" & SQL(0,i) & "</OPTION> "
		Next
	Else
		OPTION_Friend=""
	End If
End Function
%>