<!--#include file=connection.asp-->
<!--#include file="inc/const.asp" -->
<!-- #include file="inc/DvADChar.asp" -->
<!--#include file="z_home_const.asp" -->
<%
'Dvbbs.head()
dim CurItem,ItemName,rsHome,UserName,MsgBody,CurID
dim SucFlag,CartBag
dim SendSplit,CurType
dim itemsplit,itemcount
dim sendfee,CurDate,CurPrice,SalePrice

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

Dvbbs.AddErrCode(6):Dvbbs.Showerr()
	founderr=true
end if
SucFlag=False
if request("myself")=1 then
	itemcount=0
else
	if request("fromuser")<>"" then
		itemcount=0
	else
		CartBag=request.cookies("myHomeBuy_"&dvbbs.userid)
		if isnull(CartBag) then CartBag=""
		if CartBag="" then
			itemcount=0
		else
			itemsplit=split(CartBag,"|")
			itemcount=ubound(itemsplit)+1
		end if
	end if
end if
set rsHome=server.createobject("ADODB.Recordset")
if request("fromuser")<>"" or request("myself")=1 then
	CurID=CheckStr(request("itemid"))
	if isnull(curid) then
		errmsg=errmsg+"<br>"+"<li>没有指定家园物品编号！"
		founderr=true
	elseif not isnumeric(curid) then
		errmsg=errmsg+"<br>"+"<li>提交的数据有错误！"
		founderr=true
	else
		sql="select itemid from Home_myitems where id="&CurID
		rsHome.open sql,conn,1,1
		CurItem=rsHome(0)
		rsHome.Close
	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 request("action")="send" then
	if isnull(Request("username")) or Request("username")="" then
		errmsg=errmsg+"<br>"+"<li>受赠者不可为空！"
		founderr=true
	else
		UserName=CheckStr(Request("username"))
		if lcase(trim(username))=lcase(trim(dvbbs.membername)) then
			errmsg=errmsg+"<br>"+"<li>不能给自己赠送家园物品！"
			founderr=true
		end if
		if conn.execute("select  Jauid from Jauser where jauname='"&username&"'").eof then
			errmsg=errmsg+"<br>"+"<li>受赠者不存在！"
			founderr=true
		end if
	end if
	if isnull(Request("msgbody")) or Request("msgbody")="" then
		errmsg=errmsg+"<br>"+"<li>随同赠言不可为空！"
		founderr=true
	else
		msgbody=CheckStr(Request("msgbody"))
	end if
elseif itemcount>0 then
	if isnull(Request("username")) or Request("username")="" then
		UserName=""
	else
		UserName=CheckStr(Request("username"))
	end if
	MsgBody=""
	for i=0 to itemcount-1
		SendSplit=split(itemsplit(i),"$")
		if ubound(SendSplit)=2 then
			if SendSplit(0)=CurItem and lcase(trim(SendSplit(1)))=lcase(trim(UserName)) then
				MsgBody=SendSplit(2)
				SucFlag=true
				exit for
			end if
		end if
	next
end if
if founderr then
	call send_head()
	call Send_Msg()
else
	sql="select * from Home_items where id="&CurItem
	rsHome.open sql,conn,1,1
	ItemName=rsHome("name")
	CurType=(rsHome("type") mod 100)-1
	if CurType>=5 then CurType=4
	sendfee=int(rsHome("price1")*conn.execute("select top 1 send from Home_config")(0)/100+0.99)
	if v_myvip<>1 then
		CurPrice=rsHome("price1")
	else
		CurPrice=rsHome("price2")
	end if
	if request("myself")=1 then
		set rs=server.createobject("adodb.recordset")
		sql="select * from Home_myitems where itemid="&CurItem&" and username='"&dvbbs.membername&"'"
		rs.open sql,conn,1,1
		if rs.eof then
			errmsg=errmsg+"<br>"+"<li>您没有这个家园物品，不能赠送！"
			founderr=true
		end if
		rs.close
		set rs=nothing
	else
		if request("fromuser")<>"" then
			set rs=server.createobject("adodb.recordset")
			sql="select saleprice from Home_myitems where id="&CurID&" and username='"&checkstr(trim(request("fromuser")))&"'"
			rs.open sql,conn,1,1
			if not rs.eof() then
				saleprice=rs("salePrice")
				if saleprice+sendfee>mymoney then
					errmsg=errmsg+"<br>"+"<li>购买并赠送此家园物品需要现金 "&(saleprice+sendfee)&" 元，您的现金("&mymoney&" 元)不足，无法赠送！"
					founderr=true
				end if
			else
				errmsg=errmsg+"<br>"+"<li>此家园物品并非由用户 "&trim(request("fromuser"))&" 出售，无法购买！"
				founderr=true
			end if
			rs.close
			set rs=nothing
		else
			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
	end if
	rsHome.close
	set rsHome=nothing
	if founderr then
		call send_head()
		call Send_Msg()
	else
		if request("action")="send" then
			if request("myself")=1 then
				CurDate=year(now())&"-"&month(now())&"-"&day(now())
				conn.execute("update Home_myitems set username='"&username&"',FromUser='"&dvbbs.membername&"',Type="&CurType&" where username='"&dvbbs.membername&"' and id="&curID)
				set rs=server.createobject("adodb.recordset")
				sql="select * from [dv_message]"      
				rs.open sql,conn,1,3      
				rs.addnew      
				rs("sender")=dvbbs.membername      
				rs("incept")=UserName
				rs("title")="送您一件礼物，《"&ItemName&"》！"
				rs("content")=MsgBody&chr(10)&chr(10)&"家园物品已经放到您的虚拟家园中，请笑纳！"
				rs("sendtime")=Now()
				rs("flag")=0 
				rs("issend")=1
				rs.update
				rs.close
				set rs=nothing
	  		conn.execute("update Jauser set jamonery=jamonery-"&SendFee&" where jauname='"&dvbbs.membername&"'")
				errmsg="<li>已经成功地给 <font color=blue>"&username&"</font> 赠送了《<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
				if request("fromuser")<>"" then
					CurDate=year(now())&"-"&month(now())&"-"&day(now())
					conn.execute("update Home_myitems set username='"&username&"',FromUser='"&dvbbs.membername&"',Type="&CurType&",price=saleprice,saleprice=0 where username='"&checkstr(trim(request("fromuser")))&"' and type=5 and id="&curID)
					set rs=server.createobject("adodb.recordset")
					sql="select * from [dv_message]"      
					rs.open sql,conn,1,3      
					rs.addnew      
					rs("sender")=dvbbs.membername      
					rs("incept")=UserName
					rs("title")="送您一件礼物，《"&ItemName&"》！"
					rs("content")=MsgBody&chr(10)&chr(10)&"家园物品已经放到您的虚拟家园中，请笑纳！"
					rs("sendtime")=Now()
					rs("flag")=0 
					rs("issend")=1
					rs.update
					rs.close
					set rs=nothing
		  		conn.execute("update Jauser set Jamonery=Jamonery-"&(saleprice+SendFee)&" where Jauname='"&dvbbs.membername&"'")
		  		conn.execute("update Jauser set Jamonery=Jamonery+"&saleprice&" where Jauname='"&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("sendtime")=Now()
					rs("flag")=0 
					rs("issend")=1
					rs.update
					rs.close
					set rs=nothing
					errmsg="<li>已经成功地给 <font color=blue>"&username&"</font> 赠送了二手家园物品《<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=""
					if CurItem>0 then
						for i=0 to itemcount-1
							if CartBag<>"" then CartBag=CartBag&"|"
							SendSplit=split(itemsplit(i),"$")
							if ubound(SendSplit)=2 then
								if SendSplit(0)=CurItem and lcase(trim(SendSplit(1)))=lcase(trim(UserName)) then
									SendSplit(2)=MsgBody
									SucFlag=true
								end if
								CartBag=CartBag&SendSplit(0)&"$"&SendSplit(1)&"$"&SendSplit(2)
							else
								CartBag=CartBag&ItemSplit(i)
							end if
						next
					end if
					if SucFlag then
						response.cookies("myHomeBuy_"&dvbbs.userid)=CartBag
						errmsg="<li>已经修改了给 <font color=blue>"&username&"</font> 的随同赠言！"
					else
						if itemcount<CartLength then
							if CartBag<>"" then CartBag=CartBag&"|"
							CartBag=CartBag&CurItem&"$"&username&"$"&msgbody
							response.cookies("myHomeBuy_"&dvbbs.userid)=CartBag
							errmsg="<li>已将赠送给 <font color=blue>"&username&"</font> 的这个家园物品放到您的购物车中！"
						else
							founderr=true
							errmsg="<li>您的购物车已满，无法将赠送给 <font color=blue>"&username&"</font> 的这个家园物品放入！"
						end if
					end if
					call send_head()
					call Send_Msg()
				end if
			end if
		else
			call send_head()%>
			<form action="?" method=post name=messager>
			<input type=hidden name="action" value="send">
			<table cellpadding=3 cellspacing=1 align=center class=tableBorder1 width=97%>
				<tr> 
					<th colspan=3>赠送家园物品 -- 随同赠言（请输入完整信息）</th>
				</tr>
				<tr> 
					<td class=tablebody1 valign=middle><b>受赠人：</b></td>
					<td class=tablebody1 valign=middle colspan=2><%if username<>"" then%><input type=hidden name="username" value="<%=username%>"><b><%=username%></b><%else%><input type=text name="username" size=45> <SELECT onchange=document.messager.username.value=this.options[this.selectedIndex].value;document.messager.username.focus()><OPTION selected value="">选择</OPTION><%
						set rs=server.createobject("adodb.recordset")
						sql="select F_friend from dv_Friend where F_username='"&dvbbs.membername&"' order by F_addtime desc"
						rs.open sql,conn,1,1
						do while not rs.eof
							%><OPTION value="<%=rs(0)%>"><%=rs(0)%></OPTION><%
							rs.movenext
						loop
						rs.close
						set rs=nothing
					%></SELECT><%end if%></td>
				</tr>
				<tr> 
					<td class=tablebody1 valign=top width=135><b>内容：</b></td>
					<td width="460" valign=middle class=tablebody1><textarea cols=50 rows=6 name="msgbody" title="Ctrl+Enter发送"><%=msgbody%></textarea></td>
         	<td valign=middle class=tablebody1 width="328" height="84"><%
         	  if localpic=1 then
  						response.write "<img src="""&PicPath&"0/"&right("0000"&CurItem,4)&".GIF"" border=1>"
  					else
  						response.write "<img src="""&PicPath&CurItem&"/00/00/"" border=1>"
  					end if
					%></td>
				</tr>
				<tr> 
					<td class=tablebody1 colspan=3><b>说明</b>：<br>① 您可以使用<b>Ctrl+Enter</b>键快捷发送赠言<br>② 内容最多<b>500</b>个字符<br><font color=red>③ 赠送该家园物品的手续费为：<%=SendFee%> 元<%
						if request("fromuser")<>"" then
							response.write "<br>④ 购买该家园物品的费用为："&saleprice&" 元"
						end if
					%></font></td>
				</tr>
				<tr> 
					<td class=tablebody2 valign=middle colspan=3 align=center><input type=hidden name=itemid value="<%
						if request("fromuser")<>"" or request("myself")=1 then
							response.write CurID
						else
							response.write curitem
						end if
					%>"><input type=hidden name=myself value="<%=request("myself")%>"><%if request("fromuser")<>"" then%><input type=hidden name=fromuser value=<%=request("fromuser")%>><%end if%><input type=Submit value="保存" name=Submit>&nbsp;<input type="reset" name="Clear" value="清除">&nbsp;<input type="button" name="close" value="关闭" onclick="window.close()"><%if SucFlag then%>&nbsp;<a href=z_Home_cartbag.asp>[返回购物车]</a><%end if%></td>
				</tr>
			</table>
			</form>
		<%end if
	end if
end if
'dvbbs.footer()

sub send_head()%>
	<html><head>
	<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
	<title>赠送家园物品</title>
	<link type="text/css" rel="stylesheet" href="Style/sky/css.css">
	</head>
	<body " onkeydown="if(event.keyCode==13 && event.ctrlKey && document.messager=='[object]')messager.submit()"><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=red>"
					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>&nbsp;&nbsp;<%
				if SucFlag then
					%><a href="z_Home_cartbag.asp">[返回购物车]</a><%
				'else
					%><!--<a href="javascript:history.go(-1)">[返回上页]</a>--><%
				end if
			%></td>
		</tr>
	</table>
<%end sub%>