<!--#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
Dim SaleFee, SalePrice, TempStr
Dim MinPrice, CurType, CurPrice
Dim RetFlag

Dv_Plus.Name = "Plus_Avatar"
Dv_Plus.CheckLogin
Dvbbs.LoadTemplates "Avatar"
Dvbbs.Stats = Template.Strings(12)
Dvbbs.ActiveOnline
v_FoundErr = False
If Request("action") = "sale" Then
	RetFlag = True
Else
	RetFlag = False
End If
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
	If IsNull(Request("itemid")) Or 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 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 i.Name,i.Class,i.Price1,i.Price2,m.Period,m.FixDate,m.Type,m.SalePrice From Dv_Plus_Avatar_MyItems m Inner Join Dv_Plus_Avatar_Items i On m.ItemID=i.ID Where m.ItemID=" & CurItem & " And m.UserName = '" & Dvbbs.MemberName & "'"
		Set AvatarRs = Dvbbs.Plus_Execute(AvatarSQL)
		If Not AvatarRs.EOF Then
			ItemName = AvatarRs(0)
			CurType = AvatarRs(1)
			If v_MyVip <> 1 Then
				CurPrice = AvatarRs(2)
			Else
				CurPrice = AvatarRs(3)
			End If
			MinPrice = Int(CurPrice * CDbl(AvatarXMLDOM.documentElement.selectSingleNode("AvatarInfo").Attributes.getNamedItem("LowestSellPrice").Text) / 100 + 0.99)
			If AvatarRs(4) <> 0 And DateDiff("d", AvatarRs(5), Now()) >= AvatarRs(4) Then CurType = 0
			If Request("StopSale") = 1 Then
				If AvatarRs(6) <> 5 Then
					If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
					v_ErrCodes = v_ErrCodes & "<li>您尚未出售这个商品，不能停售！"
					v_FoundErr = True
				Else
					SalePrice = AvatarRs(7)
				End If
			Else
				If AvatarRs(6) = 5 Then
					If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
					v_ErrCodes = v_ErrCodes & "<li>您已经出售了这个商品，无须再次出售！"
					v_FoundErr = True
				End If
			End If
		Else
			If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
			If Request("StopSale") = 1 Then
				v_ErrCodes = v_ErrCodes & "<li>您没有这个商品或该商品已被人购买，不能停售！"
			Else
				v_ErrCodes = v_ErrCodes & "<li>您没有这个商品，不能出售！"
			End If
			v_FoundErr = True
		End If
		AvatarRs.Close
		If Request("action") = "sale" Then
			If IsNull(Request("SalePrice")) Or Request("SalePrice") = "" Then
				If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
				v_ErrCodes = v_ErrCodes & "<li>出售价格不可为空！"
				v_FoundErr = True
				RetFlag = True
			Else
				SalePrice = Request("SalePrice")
				If Not isInteger(SalePrice) Then
					If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
					v_ErrCodes = v_ErrCodes & "<li>出售价格必须是整数！"
					v_FoundErr = True
					RetFlag = True
				Else
					SalePrice = CLng(SalePrice)
					If SalePrice < MinPrice Then
						If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
						v_ErrCodes = v_ErrCodes & "<li>出售价格必须大于" & MinPrice & " 元！"
						v_FoundErr = True
						RetFlag = True
					End If
				End If
			End If
		End If
	End If
End If
If v_FoundErr Then 
	Call ShowErr(RetFlag)
Else
	SaleFee = Int(SalePrice * CDbl(AvatarXMLDOM.documentElement.selectSingleNode("AvatarInfo").Attributes.getNamedItem("SellFeeRate").Text) / 100 + 0.99)
	If Request("action") = "sale" Then
		If SaleFee > v_MyMoney Then
			If v_ErrCodes <> "" Then v_ErrCodes = v_ErrCodes & "<br>"
			v_ErrCodes = v_ErrCodes & "<li>您的现金(" & v_MyMoney & " 元)不足，无法支付二手市场的手续费 " & SaleFee & " 元，出售失败！"
			v_FoundErr = True
		End If
	End If
	If v_FoundErr Then
		Call ShowErr(RetFlag)
	Else
		If Request("stopsale") = 1 Then
			Dvbbs.Plus_Execute ("Update Dv_Plus_Avatar_MyItems Set SalePrice=0,Type=" & CurType & " Where UserName='" & Dvbbs.MemberName & "' And ItemID=" & CurItem)
			Call UpdateUserWealth(Dvbbs.MemberName, SaleFee)
			SucMsg = "<li>您已经成功地取消了《" & ItemName & "》的出售！"
			Call ShowSuc(SucMsg, False)
			Response.Write "<script>if(window.opener) window.opener.document.execCommand('Refresh');</script>"
		Else
			If Request("action") = "sale" Then
				Dvbbs.Plus_Execute ("Update Dv_Plus_Avatar_MyItems Set SalePrice=" & SalePrice & ",Type=5 Where UserName='" & Dvbbs.MemberName & "' And ItemID=" & CurItem)
				Call UpdateUserWealth(Dvbbs.MemberName, -SaleFee)
				SucMsg = "<li>您已经成功地出售了《" & ItemName & "》，等待他人的购买！"
				Call ShowSuc(SucMsg, False)
				Response.Write "<script>if(window.opener) window.opener.document.execCommand('Refresh');</script>"
			Else
				Dvbbs.Head
				TempStr = Template.HTML(11)
				TempStr = Replace(TempStr, "{$ItemName}", ItemName)
				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, "{$SellFeeRate}", AvatarXMLDOM.documentElement.selectSingleNode("AvatarInfo").Attributes.getNamedItem("SellFeeRate").Text)
				TempStr = Replace(TempStr, "{$MinPrice}", MinPrice)
				TempStr = Replace(TempStr, "{$MyMoney}", v_MyMoney)
				TempStr = Replace(TempStr, "{$CurPrice}", CurPrice)
				Response.Write TempStr
			End If
		End If
	End If
End If
Dvbbs.Footer
Set Dv_Plus = Nothing
%>