<!--#include file="conn.asp"-->
<!--#include file="inc/const.asp" -->
<!--#include file="NF_visual_const.asp" -->
<%
dim CurVisual,CurVisualSplit,TempSplit,ItemName,rsvisual,ItemCount
dim j,flag,SucFlag,buyitems,buycount
dim myvisualsplit
Dvbbs.LoadTemplates("")

myvisualsplit=split(v_myvisual,"|")

if not Dvbbs.Founduser or Dvbbs.MemberName=""  then
	Dvbbs.AddErrCode "您还没有<a href=login.asp>登录论坛</a>，不能使用个人形象设计功能。如果您还没有<a href=reg.asp>注册</a>，请先<a href=reg.asp>注册</a>！"
end if
CurVisual=request.cookies("myshow_"&Dvbbs.Userid)
if isnull(CurVisual) then CurVisual=""
CurVisualSplit=split(CurVisual,"|")
if CurVisual="" or ubound(CurVisualSplit)<>24 then
	Dvbbs.AddErrCode "提交的数据有错误！"
	
end if
Dvbbs.Stats="保存形象"
Showerr()
set rsvisual=server.createobject("ADODB.Recordset")
SucFlag=true
ItemCount=0
ItemName=""
BuyItems=""
BuyCount=0
for i=0 to ubound(CurVisualSplit)
	if myVisualSplit(i)<>"" then
		TempSplit=split(myVisualSplit(i),".")(0)
	else
		TempSplit=""
	end if
	if CurVisualSplit(i)<>TempSplit then
		if CurVisualSplit(i)="" then
			if cint(TempSplit)>20 then
				call DressDown()
			end if
		elseif TempSplit="" then
			if cint(CurVisualSplit(i))>20 then
				sql="select top 1 fixdate,period from visual_myitems where username='"&Dvbbs.MemberName&"' and itemid="&CurVisualSplit(i)
				rsvisual.open sql,conn,1,1
				if not (rsvisual.eof or rsvisual.bof) then
					call DressUp()
				else
					SucFlag=false
					call DressBuy()
				end if
				rsvisual.close
			end if
		else
			if cint(CurVisualSplit(i))>20 then
				if cint(TempSplit)>20 then
					call DressDown()
				end if
				sql="select top 1 fixdate,period from visual_myitems where username='"&Dvbbs.MemberName&"' and itemid="&CurVisualSplit(i)
				rsvisual.open sql,conn,1,1
				if not (rsvisual.eof or rsvisual.bof) then
					call DressUp()
				else
					SucFlag=false
					call DressBuy()
				end if
				rsvisual.close
			else
				if cint(TempSplit)>20 then
					call DressDown()
				end if
			end if
		end if
	end if
next
set rsvisual=nothing
if SucFlag and ItemName<>"" then
	curvisual=""
	set rs=server.createobject("adodb.recordset")
	set rsvisual=server.createobject("adodb.recordset")
	for i=0 to 24
		if i>0 then curvisual=curvisual&"|"
		if curvisualsplit(i)<>"" then
			rs.open "select * from visual_items where id="&curvisualsplit(i),conn,1,1
			if cint(curvisualsplit(i))>20 then
				rsvisual.open "select * from visual_myitems where itemid="&curvisualsplit(i)&" and username='"&Dvbbs.MemberName&"'",conn,1,3
				if rsvisual("type")=5 then
					if rs("type") mod 100>=5 then
						rsvisual("type")=4
					else
						rsvisual("type")=rs("type") mod 100
					end if
					rsvisual.update
				end if
				rsvisual.close
			end if
			curvisual=curvisual&curvisualsplit(i)&"."&rs("content")
			rs.close
		end if
	next
	set rs=nothing
	set rsvisual=nothing
end if
if SucFlag and ItemName<>"" then
	conn.execute("update [Dv_User] set visual='"&curvisual&"' where username='"&Dvbbs.MemberName&"'")
	response.cookies("myshow_"&Dvbbs.Userid)=""
	sucmsg = sucmsg & "<li>您成功地：" & ItemName
	call Dvbbs.nav()
	call Dvbbs.head_var(3,Dvbbs.Stats,"虚拟形象首页","NF_visual.asp")
	
	Dvbbs.Dvbbs_Suc(sucmsg)
elseif not SucFlag and BuyItems<>"" then
	Dvbbs.AddErrCode "如下商品尚未购买不能换上："
	Dvbbs.AddErrCode BuyItems
	call Dvbbs.nav()
	call Dvbbs.head_var(3,Dvbbs.Stats,"虚拟形象首页","NF_visual.asp")
	response.write "<script language=javascript>openScript('NF_visual_cartbag.asp',500,440);</script>"
	call Showerr()
else
	call Dvbbs.nav()
	call Dvbbs.head_var(3,Dvbbs.Stats,"虚拟形象首页","NF_visual.asp")
	Dvbbs.AddErrCode "您没有更换任何商品！"
	call Showerr()
end if
call Dvbbs.Footer()

sub DressDown()
	dim flag,j
	
	flag=false
	for j=0 to i-1
		if MyVisualSplit(i)=MyVisualSplit(j) then
			flag=true
			exit for
		end if
	next
	if not flag then
		ItemCount=ItemCount+1
		ItemName=ItemName&"<br>&nbsp;&nbsp;&nbsp;"&ItemCount&"、换下了<font color=red>"&conn.execute("select top 1 name from visual_items where id="&TempSplit)(0)&"</font>"
	end if
end sub

sub DressUp()
	dim flag,j
	
	flag=false
	for j=0 to i-1
		if CurVisualSplit(j)<>"" then
			if CurVisualSplit(i)=CurVisualSplit(j) then
				flag=true
				exit for
			end if
		end if
	next
	if not flag then
		ItemCount=ItemCount+1
		ItemName=ItemName&"<br>&nbsp;&nbsp;&nbsp;"&ItemCount&"、换上了<font color=blue>"&conn.execute("select top 1 name from visual_items where id="&CurVisualSplit(i))(0)&"</font>"
	end if
end sub

sub DressBuy()
	dim flag,j
	dim CartBag,rsitems
	
	flag=false
	for j=0 to i-1
		if CurVisualSplit(j)<>"" then
			if CurVisualSplit(i)=CurVisualSplit(j) then
				flag=true
				exit for
			end if
		end if
	next
	if not flag then
		BuyCount=BuyCount+1
		BuyItems=BuyItems&"<br>&nbsp;&nbsp;&nbsp;"&BuyCount&"、"&conn.execute("select top 1 name from visual_items where id="&CurVisualSplit(i))(0)
		set rsitems=server.createobject("ADODB.recordset")
		sql="select * from visual_items where id="&CurVisualSplit(i)
		rsitems.open sql,conn,1,1
		flag=true
		if rsitems("vip") and v_myvip<>1 then
			BuyItems=BuyItems&"&nbsp;★您不是VIP，不能购买VIP专用商品！"
			flag=false
		end if
		if rsitems("quantity")<=0 then
			BuyItems=BuyItems&"&nbsp;★该商品库存不足，您无法购买！"
			flag=false
		end if
		if rsitems("flag")=0 then
			BuyItems=BuyItems&"&nbsp;★保留商品，您无法购买！"
			flag=false
		end if
		if rsitems("flag")=1 and Dvbbs.MemberName="" then
			BuyItems=BuyItems&"&nbsp;★只有会员才能购买此商品，您无法购买！"
			flag=false
		end if
		if rsitems("flag")=2 and not Dvbbs.Master and not Dvbbs.BoardMaster and not Dvbbs.SuperBoardMaster then
			BuyItems=BuyItems&"&nbsp;★只有版主以上人员才能购买此商品，您无法购买！"
			flag=false
		end if
		if rsitems("flag")=3 and not Dvbbs.Master and not Dvbbs.SuperBoardMaster then
			BuyItems=BuyItems&"&nbsp;★只有超级版主和管理员才能购买此商品，您无法购买！"
			flag=false
		end if
		if rsitems("flag")=4 and not Dvbbs.Master then
			BuyItems=BuyItems&"&nbsp;★只有管理员才能购买此商品，您无法购买！"
			flag=false
		end if
		if flag then
			CartBag=request.cookies("mybuy_"&Dvbbs.Userid)
			if isnull(CartBag) then CartBag=""
			if instr(1,"|"&CartBag&"|","|"&CurVisualSplit(i)&"|")<=0 then
				if ubound(split(CartBag,"|"))<CartLength-1 then
					if CartBag<>"" then CartBag=CartBag&"|"
					CartBag=CartBag&CurVisualSplit(i)
					response.cookies("mybuy_"&Dvbbs.Userid)=CartBag
					BuyItems=BuyItems&"&nbsp;★已将此商品放入您的购物袋！"
				else
					BuyItems=BuyItems&"&nbsp;★您的购物袋已满，无法将此商品放入！"
				end if
			else
				BuyItems=BuyItems&"&nbsp;★购物袋中已有此商品，无须再次放入！"
			end if
		end if
	end if
end sub
%>