<!--#include file="conn.asp"-->
<!--#include file="inc/const.asp" -->
<!-- #include file="inc/const_admin.asp" -->
<!--#include file="z_Home_const.asp"-->
<%
Dvbbs.head()
dim CartBag,SendSplit,SumAmount,SendMsg,rs2,sendfee,j
dim itemsplit,itemcount,rsHome,curtype,CurPrice,CurDate
dim CurItemSplit
founderr=false
If Dvbbs.Userid=0 Or Dvbbs.Userid="" Then 

Dvbbs.AddErrCode(6):Dvbbs.Showerr()
	founderr=true
end if
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

if request.form("cmdclear")="清空" then
	response.cookies("myHomeBuy_"&dvbbs.userid)=""
	response.write "<script>"
	response.write "window.close();"
	response.write "</script>"
	response.end
end if
if request.form("cmddelete")="删除" then
	if itemcount>0 then
		if not isnull(request("seqno")) then
			if isnumeric(request("seqno")) then
				if cint(request("seqno"))<itemcount then
					CartBag=""
					for i=0 to itemcount-1
						if i<>cint(request("seqno")) then
							if CartBag<>"" then CartBag=CartBag&"|"
							CartBag=CartBag&itemsplit(i)
						else
							CurItemSplit=Split(itemsplit(i),"$")
							if UBound(CurItemSplit)=1 then
								if CInt(CurItemSplit(1))>1 then
									if CartBag<>"" then CartBag=CartBag&"|"
									CartBag=CartBag&CurItemSplit(0)&"$"&(CInt(CurItemSplit(1))-1)
								end if
							end if
						end if
					next
					response.cookies("myHomeBuy_"&dvbbs.userid)=CartBag
					response.redirect Request.ServerVariables("HTTP_REFERER")
				end if
			end if
		end if
	end if
end if
if request.form("cmdclear")="结算" then
	set rsHome=server.createobject("ADODB.Recordset")
	for i=0 to itemcount-1
		SendSplit=split(itemsplit(i),"$")
		sql="select top 1 * from Home_items where id="&SendSplit(0)
		rsHome.open sql,home_conn,1,1
		sendfee=int(rsHome("price1")*home_conn.execute("select top 1 send from Home_config")(0)/100+0.99)
		if v_myvip<>1 then
  		if ubound(SendSplit)=2 then
  			SumAmount=SumAmount+rsHome("price1")
  		else
  			SumAmount=SumAmount+rsHome("price1")*CInt(SendSplit(1))
  		end if
  	else
  		if ubound(SendSplit)=2 then
  			SumAmount=SumAmount+rsHome("price2")
  		else
  			SumAmount=SumAmount+rsHome("price2")*CInt(SendSplit(1))
  		end if
  	end if
  	if ubound(SendSplit)=2 then
  		SumAmount=SumAmount+SendFee
  	end if
		rsHome.close
	next
	if mymoney<SumAmount then
		SendMsg=SendMSg+"<br>"+"<li>购买的家园物品共计 "&SumAmount&" 元，您的现金 "&mymoney&" 元不够买这些家园物品。"
		founderr=true
	else
		for i=0 to itemcount-1
			SendSplit=split(itemsplit(i),"$")
			sql="select top 1 * from Home_items where id="&SendSplit(0)
			rsHome.open sql,home_conn,1,1
			sendfee=int(rsHome("price1")*home_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
			CurType=(rsHome("type") mod 100)-1
			if curtype>=5 then curtype=4
			CurDate=year(now())&"-"&month(now())&"-"&day(now())
			home_conn.execute("update Home_items set quantity=quantity-1,totalsales=totalsales+1 where id="&SendSplit(0))
			if ubound(SendSplit)<2 then
				for j=1 to SendSplit(1)
					home_conn.execute("insert into Home_myitems (ItemID,UserName,FromUser,[Type],AddDate,FixDate,Period,Price) values ("&SendSplit(0)&",'"&dvbbs.membername&"','"&dvbbs.membername&"',"&curtype&",'"&curdate&"','"&curdate&"',"&rsHome("period")&","&curprice&")")
				next
			else
				home_conn.execute("insert into Home_myitems (ItemID,UserName,FromUser,[Type],AddDate,FixDate,Period,Price) values ("&SendSplit(0)&",'"&SendSplit(1)&"','"&dvbbs.membername&"',"&curtype&",'"&curdate&"','"&curdate&"',"&rsHome("period")&","&curprice&")")
				set rs2=server.createobject("adodb.recordset")
				sql="select * from [dv_message]"      
				rs2.open sql,conn,1,3      
				rs2.addnew      
				rs2("sender")=dvbbs.membername      
				rs2("incept")=SendSplit(1)
				rs2("title")="送您一件礼物，《"&rsHome("name")&"》！"
				rs2("content")=SendSplit(2)&chr(10)&chr(10)&"家园物品已经放到您的虚拟家园中，请笑纳！"
				rs2("sendtime")=Now()

				rs2("flag")=0 
				rs2("issend")=1
				rs2.update
				rs2.close
				set rs2=nothing
			end if
	  	if ubound(SendSplit)=2 then
	  		CurPrice=CurPrice+SendFee
	  	else
	  		CurPrice=CurPrice*CInt(SendSplit(1))
	  	end if
  		conn.execute("update [dv_user] set userwealth=userwealth-"&CurPrice&" where username='"&dvbbs.membername&"'")
  		mymoney=mymoney-CurPrice
			rsHome.close
		next
		response.cookies("myHomeBuy_"&dvbbs.userid)=""
		SendMsg=SendMSg+"<br>"+"<li>您购买的家园物品已经放入您的虚拟家园，赠送的家园物品已经发送到指定人员。"
		call cartbag_head()
		response.write "<script>"&vbcrlf
		response.write "if(typeof(window.opener) == ""object"") "
		response.write "if(typeof(window.opener.spanHasCash) == ""object"") "
		response.write "window.opener.spanHasCash.innerText="""&mymoney&""";"&vbcrlf
		response.write "else "
		response.write "window.opener.document.execCommand('Refresh');"
			
		
		if request.cookies(dvbbs.userid&"_savehome")<>"" then
   dim tmpCookie
   tmpCookie=request.cookies(dvbbs.userid&"_savehome")
   response.cookies(dvbbs.userid&"_savehome")=""
   response.write "document.URL='z_home_save.asp?"&tmpCookie&"';"&vbcrlf
  end if
		
		
		
		response.write "</script>"
		call send_msg()
		dvbbs.activeonline()
		response.end
	end if
	set rsHome=nothing
end if
If Dvbbs.Userid=0 Or Dvbbs.Userid="" Then 

Dvbbs.AddErrCode(6):Dvbbs.Showerr()
	founderr=true
end if
dvbbs.stats="查看虚拟家园购物车"
if founderr then
	call cartbag_head()
	call send_msg()
else
	call cartbag_head()
	call main()
end if
Dvbbs.ActiveOnline()
Dvbbs.footer()

sub main()
	SumAmount=0%>
	<SCRIPT language=javascript>
		var picPath="<%=PicPath%>";
		var localPic=<%=LocalPic%>;
	</SCRIPT>
	<table cellpadding=3 cellspacing=1 border=0 align=center class=tableBorder1 width=97%>
		<tbody>
		<tr height=25>
			<th colspan=6><font color=red><%=dvbbs.membername%> ( <%=mymoney%> 元 )</font>：虚拟家园--购物车</th>
		</tr>
		<tr height=20 valign=middle>
			<td align=center class=tablebody2><b>家园物品名称</b></td>
			<td align=center class=tablebody2><b>单价</b></td>
			<td align=center class=tablebody2><b>数量</b></td>
			<td align=center class=tablebody2><b>受赠方</b></td>
			<td align=center class=tablebody2><b>操作</b></td>
			<td align=center class=tablebody2 rowspan="<%
				if itemcount=0 then
					response.write 2
				else
					response.write itemcount+1
				end if
			%>" width="84" height="84" border="0"><img id=itempic src="images/img_visual/blank.gif" width="84" height="84" border="1"></td>
		</tr>
		<form action=? method=post name=form1>
		<input type=hidden name=seqno value="">
		<%if itemcount=0 then%>
			<tr height=70 valign=middle>
				<td colspan=5 align=center class=tablebody1>没有购买任何家园物品！</td>
			</tr>
		<%else
			set rsHome=server.createobject("ADODB.Recordset")
			for i=0 to itemcount-1
				SendSplit=split(itemsplit(i),"$")
				sql="select top 1 * from Home_items where id="&SendSplit(0)
				rsHome.open sql,home_conn,1,1
				if not rshome.eof then
					sendfee=int(rsHome("price1")*home_conn.execute("select top 1 send from Home_config")(0)/100+0.99)%>
					<tr height=20 valign=middle itemno="<%=SendSplit(0)%>">
						<td align=left class=tablebody1 onmouseover=moveoverobject(this.parentElement,'TableBody2') onmouseout=moveoutobject(this.parentElement,'TableBody1')>&nbsp;<%=rsHome("name")%></td>
						<td align=right class=tablebody1 onmouseover=moveoverobject(this.parentElement,'TableBody2') onmouseout=moveoutobject(this.parentElement,'TableBody1')><%
							if v_myvip<>1 then
	          		if rsHome("price1")<=0 then
	          			response.write "<font color=red>免费</font>"
	          		else
	          			response.write rsHome("price1")&" 元"
	          		end if
					  		if ubound(SendSplit)=2 then
					  			SumAmount=SumAmount+rsHome("price1")
					  		else
					  			SumAmount=SumAmount+rsHome("price1")*CInt(SendSplit(1))
					  		end if
	          	else
	          		if rsHome("price2")<=0 then
	          			response.write "<font color=red>免费</font>"
	          		else
	          			response.write rsHome("price2")&" 元"
	          		end if
					  		if ubound(SendSplit)=2 then
					  			SumAmount=SumAmount+rsHome("price2")
					  		else
					  			SumAmount=SumAmount+rsHome("price2")*CInt(SendSplit(1))
					  		end if
	          	end if
					  	if ubound(SendSplit)=2 then
					  		response.write " <font color=red> + "&SendFee&" 元</font>"
					  		SumAmount=SumAmount+SendFee
					  	end if
						%>&nbsp;</td>
						<td align=right class=tablebody1 onmouseover=moveoverobject(this.parentElement,'TableBody2') onmouseout=moveoutobject(this.parentElement,'TableBody1')><%
							if ubound(SendSplit)=2 then
								response.write "1"
							else
								response.write SendSplit(1)
							end if
						%>&nbsp;</td>
						<td align=center class=tablebody1 onmouseover=moveoverobject(this.parentElement,'TableBody2') onmouseout=moveoutobject(this.parentElement,'TableBody1')><%
							if ubound(SendSplit)=2 then
								response.write "<a href=z_Home_send.asp?itemid="&SendSplit(0)&"&username="&SendSplit(1)&"&myself=0><font color=blue>"&SendSplit(1)&"</font></a>"
							else
								response.write "自用"
							end if
						%></td>
						<td align=center class=tablebody1 onmouseover=moveoverobject(this.parentElement,'TableBody2') onmouseout=moveoutobject(this.parentElement,'TableBody1')><input type="submit" name="cmddelete" value="删除" onclick="if(confirm('您确定删除这个家园物品吗?')){this.document.form1.seqno.value='<%=i%>';this.document.form1.submit();return true;}return false;"></td>
					</tr>
				<%end if
				rsHome.close
			next
			set rsHome=nothing
		end if%>
		<tr height=20 valign=middle>
			<td class=tablebody2 align=center><b>合计</b></td>
			<td class=tablebody2 align=right colspan=2><b><%=SumAmount%> 元</b>&nbsp;</td>
			<td class=tablebody2 align=center><%
				if itemcount=0 then
					%>&nbsp;<%
				else
					%><input type="submit" name="cmdclear" value="清空" onclick="if(confirm('您确定清空购物车吗?')){this.document.form1.submit();return true;}return false;"><%
				end if
			%></td>
			<td class=tablebody2 align=center><%
				if itemcount=0 then
					%>&nbsp;<%
				else
					%><input type="submit" name="cmdclear" value="结算" onclick="if(confirm('您确定结算这些家园物品吗?')){this.document.form1.submit();return true;}return false;"><%
				end if
			%></td>
			<td class=tablebody2 align=center><input type="button" name="cmdclose" value="关闭" onclick="window.close();"></td>
		</tr>
		</form>
		</tbody>
	</table>
	<script>
		function moveoverobject(object,classname) {
			var tmpSrc;
			for(var i=0;i<object.children.length;i++) 
				object.children[i].className=classname;
			if(itempic.readyState=="complete") {
  			if (localPic==1) {
  				tmpSrc="0000" + object.itemno;
  				itempic.src=picPath + '0/'+tmpSrc.substring(tmpSrc.length-4,tmpSrc.length)+'.gif';
  			}	else 
  				itempic.src=picPath + object.itemno + "/00/00/";
  		}
		}

		function moveoutobject(object,classname) {
			for(var i=0;i<object.children.length;i++) 
				object.children[i].className=classname;
			if(itempic.readyState=="complete") itempic.src='images/img_home/blank.gif';
		}
	</script>
<%end sub

sub CartBag_Head()%>
	<html>
	<head>
	<title>虚拟家园购物车</title>
	<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
	</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=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><%if founderr then%>&nbsp;&nbsp;<a href="javascript:history.go(-1)">[返回上页]</a><%end if%></td>
		</tr>
	</table>
<%end sub%>