<!--#include file=conn.asp-->
<!--#include file="inc/const.asp" -->
<!-- #include file="inc/const_admin.asp" -->
<!--#include file="z_home_const.asp" -->
<%
Dvbbs.head()
dim ItemName,rshome,CurID,Forum_Info
dim SucFlag,CurItem,CartBag,CurType,SalePrice

founderr=false
If Dvbbs.Userid=0 Or Dvbbs.Userid="" Then 

Dvbbs.AddErrCode(6):Dvbbs.Showerr()
	founderr=true
end if
if request("fromuser")<>"" then
	CurID=CheckStr(request("itemid"))
	if isnull(curid) then
	
	
	sqlvisual=sqlvisual&" and flag>=1"
		errmsg=errmsg+"<br>"+"<li>没有指定家园物品编号！"
		founderr=true
	else
		if not isnumeric(curid) then
			errmsg=errmsg+"<br>"+"<li>提交的数据有错误！"
			founderr=true
		end if
	end if
else
	CurItem=CheckStr(request("itemid"))
	if isnull(curitem) then
		errmsg=errmsg+"<br>"+"<li>没有指定家园物品编号！"
		founderr=true
	else
		if not isnumeric(curitem) then
			errmsg=errmsg+"<br>"+"<li>提交的数据有错误！"
			founderr=true
		end if
	end if
end if

if founderr then
	call send_head()
	call send_msg()
else
	set rshome=server.createobject("ADODB.Recordset")
	if request("fromuser")<>"" then
		sql="select m.username,m.saleprice,v.name,v.type,m.itemid from home_myitems m inner join home_items v on m.itemid=v.id where m.id="&CurID&" and username='"&checkstr(trim(request("fromuser")))&"'"
		rshome.open sql,home_conn,1,1
		if not rshome.eof then
			ItemName=rshome("name")
			CurItem=rshome("itemid")
			CurType=(rshome("type") mod 100)-1
			if CurType>=5 then CurType=4
			saleprice=rshome("salePrice")
			if saleprice>mymoney then
				errmsg=errmsg+"<br>"+"<li>购买此家园物品需要现金 "&saleprice&" 元，您的现金("&mymoney&" 元)不足，无法购买！"
				founderr=true
			end if
		else
			errmsg=errmsg+"<br>"+"<li>此家园物品并非由用户 "&trim(request("fromuser"))&" 出售，无法购买！"
			founderr=true
		end if
	else
		sql="select * from home_items where id="&CurItem
		rshome.open sql,home_conn,1,1
		ItemName=rshome("name")
		if rshome("vip") and v_myvip<>1 then
			errmsg=errmsg+"<br>"+"<li>您不是VIP，不能购买VIP专用家园物品！"
			founderr=true
		end if
		if rshome("quantity")<=0 then
			errmsg=errmsg+"<br>"+"<li>该家园物品库存不足，您无法购买！"
			founderr=true
		end if
		if rshome("flag")=0 then
			errmsg=errmsg+"<br>"+"<li>保留家园物品，您无法购买！"
			founderr=true
		end if
		if rshome("flag")=1 and dvbbs.MemberName="" then
			errmsg=errmsg+"<br>"+"<li>只有会员才能购买此家园物品，您无法购买！"
			founderr=true
		end if
		if rshome("flag")=2 and not dvbbs.master and not dvbbs.boardmaster and not dvbbs.superboardmaster then
			errmsg=errmsg+"<br>"+"<li>只有版主以上人员才能购买此家园物品，您无法购买！"
			founderr=true
		end if
		if rshome("flag")=3 and not dvbbs.master and not dvbbs.superboardmaster then
			errmsg=errmsg+"<br>"+"<li>只有超级版主和管理员才能购买此家园物品，您无法购买！"
			founderr=true
		end if
		if rshome("flag")=4 and not dvbbs.master then
			errmsg=errmsg+"<br>"+"<li>只有管理员才能购买此家园物品，您无法购买！"
			founderr=true
		end if
	end if
	if founderr then
		call send_head()
		call send_msg()
	else	
		if request("fromuser")<>"" then
			home_conn.execute("update home_myitems set type="&curtype&",price=saleprice,saleprice=0,fromuser=username,username='"&dvbbs.MemberName&"' where id="&curid)
			conn.execute("update [dv_user] set userwealth=userwealth-"&saleprice&" where username='"&dvbbs.MemberName&"'")
			conn.execute("update [dv_user] set userwealth=userwealth+"&saleprice&" where username='"&checkstr(trim(request("fromuser")))&"'")
			set rs=server.createobject("adodb.recordset")
			sql="select * from [Dv_Message]"      
			rs.open sql,conn,1,3      
			rs.addnew   
			rs("sender")=forum_info(0)
			rs("incept")=trim(request("fromuser"))
			rs("title")="您出售的家园物品有人购买了！"
			rs("content")="您在二手市场出售的家园物品《[color=blue]"&itemName&"[/color]》已经被"&dvbbs.MemberName&"购买了！"
			
			rs("flag")=0 
			rs("issend")=1
			rs.update
			rs.close
			set rs=nothing
			errmsg="<li>已经成功地购买了二手家园物品 <font color=blue>"&ItemName&"</font>！"
			call send_head()
			response.write "<script>"
			response.write "window.opener.document.execCommand('Refresh');"
			response.write "</script>"
			call send_msg()
		else
			CartBag=request.cookies("myHomeBuy_"&dvbbs.userid)
			if isnull(CartBag) then CartBag=""
			dim CartBagSplit,NewCartBag,CartBagItemSplit,isSucc
			cartbagsplit=split(CartBag,"|")
			if ubound(CartBagSplit)<CartLength-1 then
				NewCartBag=""
				isSucc=0
				for i=0 to ubound(cartbagsplit)
					CartBagItemSplit=Split(CartBagSplit(i),"$")
					if CInt(CartBagItemSplit(0))=CInt(CurItem) then
						if CInt(CartBagItemSplit(1))>=10 then
							isSucc=2
							exit for
						else
							isSucc=1
							CartBagItemSplit(1)=CInt(CartBagItemSplit(1))+1
						end if
						if NewCartBag<>"" then NewCartBag=NewCartBag&"|"
						NewCartBag=NewCartBag&CartBagItemSplit(0)&"$"&CartBagItemSplit(1)
					else
						if NewCartBag<>"" then NewCartBag=NewCartBag&"|"
						NewCartBag=NewCartBag&CartBagSplit(i)
					end if
				next
				if isSucc=0 then
					if NewCartBag<>"" then NewCartBag=NewCartBag&"|"
					NewCartBag=NewCartBag&CurItem&"$1"
				end if
				if isSucc<>2 then
					response.cookies("myHomeBuy_"&dvbbs.userid)=NewCartBag
					errmsg="<li>已将家园物品 <font color=blue>"&ItemName&"</font> 放到您的购物车中！"
				else
					errmsg=errmsg+"<br>"+"<li>您购物车中家园物品 <font color=blue>"&ItemName&"</font> 的数量已经超过10个，无法继续放入此家园物品！"
					founderr=true
				end if
			else
				errmsg=errmsg+"<br>"+"<li>您的购物车已满，无法将此家园物品放入！"
				founderr=true
			end if	
			call send_head()
			call send_msg()
		end if
	end if
	rshome.close
	set rshome=nothing
end if
Dvbbs.footer()

sub send_head()%>
	<html><head>
	<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
	<title>购买家园物品</title>
	
	</head>
	<body><br>
<%end sub

sub send_msg()%>
	<br>
	<table cellpadding=3 cellspacing=1 align=center class=tableBorder1 width=97%>
		<tr align=center>
			<th width="100%">购买家园物品信息</th>
		</tr>
		<tr>
			<td width="100%" class=tablebody1><b>操作结果：</b><br><%
				if founderr then
					response.write "<font color=blue>"
					response.write errmsg
					response.write "</font>"
				else
					response.write errmsg
				end if
			%></td>
		</tr>
		<tr align=center>
			<td width="100%" class=tablebody2><a href="javascript:window.close()">[关闭窗口]</a></td>
		</tr>
	</table>
<%end sub%>