<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%option explicit%>
<!--#include file="../Conn.asp"-->
<!--#include file="../KS_Cls/Kesion.MemberCls.asp"-->
<!--#include file="../KS_Cls/Kesion.Label.CommonCls.asp"-->
<%

Dim KSCls
Set KSCls = New ShoppingCart
KSCls.Kesion()
Set KSCls = Nothing

Class ShoppingCart
        Private KS, KSRFObj,KSUser,DomainStr
		Private ProductList,LoginTF
		Private TotalPrice,RealPrice,Price_Original,Discount,Amount
		Private Sub Class_Initialize()
		  Set KS=New PublicCls
		  DomainStr=KS.GetDomain
		  Set KSUser = New UserCls
		  Set KSRFObj = New Refresh
		End Sub
        Private Sub Class_Terminate()
		 Call CloseConn()
		 Set KS=Nothing
		 Set KSUser=Nothing
		End Sub
		%>
		<!--#include file="PackCart.asp"-->
		<%
		Public Sub Kesion()
		    LoginTF=KSUser.UserLoginChecked
		    ProductList = Session("ProductList")
			GCls.ComeUrl=GCls.GetUrl()
		    Dim FileContent,Products,i,RS,strsql,CarListStr	
		 	Products = Split(Replace(KS.S("id")," ",""), ",")
			If Replace(KS.S("id")," ","")="" And KS.S("action")="set" Then 
			   ProductList=""
			Else 
				For I=0 To UBound(Products)
				   PutToShopBag Products(I), ProductList,I
				Next
			End iF
			If KS.S("Action")="Del" Then 
			  Call DelProduct(KS.S("ID"))
			ElseIf KS.S("Action")="present" Then
			  AddPresent()
			ElseIf KS.S("Action")="delpack" Then
			  DelPack()    '删除礼包
			ElseIf KS.S("Action")="addBundleSale" Then
		      call addBundleSale()
			ElseIf KS.S("Action")="BundleSale" Then
			  call delBundleSale()
			End If
			Session("ProductList") = KS.FilterIds(ProductList)
			
		           If KS.Setting(121)="" Then Response.Write "请先到""基本信息设置->模板绑定""进行模板绑定操作!":response.end
				   FileContent = KSRFObj.LoadTemplate(KS.Setting(121))
				   FCls.RefreshType = "ShoppingCart" '设置刷新类型，以便取得当前位置导航等
				    Fcls.RefreshFolderID = "0"        '设置当前刷新目录ID 为"0" 以取得通用标签
					If Trim(FileContent) = "" Then FileContent = "商城购物车模板不存在!"
Set RS=Server.CreateObject("ADODB.RecordSet") 

		 Dim ProBuyAttr
		 If Not KS.IsNul(Session("ProductList")) Then
		  '删除没有在当前购物车内的捆绑商品
		   conn.execute("delete from KS_ShopBundleSelect where username='" & getuserid & "' and proid not in(" & Session("ProductList") &")")
		strsql="select I.ID,I.Title,I.ProductType,I.Price_Original,I.Price,I.Price_Member,I.Discount,I.TotalNum,I.GroupPrice,I.PhotoUrl,I.Unit,I.IsLimitBuy,I.LimitBuyPrice,I.LimitBuyAmount,L.LimitBuyBeginTime,L.LimitBuyEndTime from Product I Left Join KS_ShopLimitBuy L On I.LimitBuyTaskID=L.Id where I.ID in ("&Session("ProductList")&") order by I.IsChangedBuy,I.ID"
         Else
strsql="select I.ID,I.Title,I.ProductType,I.Price_Original,I.Price,I.Price_Member,I.Discount,I.TotalNum,I.GroupPrice,I.PhotoUrl,I.Unit,I.IsLimitBuy,I.LimitBuyPrice,I.LimitBuyAmount,L.LimitBuyBeginTime,L.LimitBuyEndTime from Product I Left Join KS_ShopLimitBuy L On I.LimitBuyTaskID=L.Id where 1=0 order by I.IsChangedBuy,I.ID"		 
         End If
		 If KS.S("Action")="set" then Call SetBundleSaleAmount()  '设置捆绑促销购买数量
		 
RS.open strsql,conn,1,1
	CarListStr="<form action=""ShoppingCart.asp?action=set"" method=""POST"" name=""check"">"&vbcrlf
	CarListStr=CarListStr&"      <table border=""0"" cellspacing=""1"" cellpadding=""1"" align=""center"" width=""100%"" class=""border"">" & vbcrlf
	CarListStr=CarListStr&"            <tr class='title' height=""25"">" & vbcrlf 
	CarListStr=CarListStr&"            <td width=""6%"" align=""center"">编 号</td>" &vbcrlf
	CarListStr=CarListStr&"            <td align=""center"">商 品 名 称</td>" & vbcrlf
	CarListStr=CarListStr&"            <td width=""10%"" align=""center"">数 量</td>" & vbcrlf
	CarListStr=CarListStr&"			   <td width=""10%"" align=""center"">原 价</td>" & vbcrlf
	CarListStr=CarListStr&"			   <td width=""10%"" align=""center"">折 扣</td>"&vbcrlf
	CarListStr=CarListStr&"			   <td width=""10%"" align=""center"">实 价</td>" & vbcrlf
	CarListStr=CarListStr&"			   <td width=""12%"" align=""center"">总 计</td>" & vbcrlf
	CarListStr=CarListStr&"			   <td width=""12%"" align=""center"">操作</td>" & vbcrlf
	CarListStr=CarListStr&"          </tr>"&vbcrlf
	
If Not RS.Eof Then
Amount = 1
Do While Not RS.EOF
     Amount = KS.ChkClng(KS.S( "Q_" & RS("ID")))
     If Amount <= 0 Then 
        Amount = KS.ChkClng(Session("Amount"&RS("ID")))
        If Amount <= 0 Then Amount = 1
     End If
Session("Amount"&RS("ID")) = Amount

IF RS("TotalNum") < Amount Then
	Amount = 1
	Session("Amount"&RS("ID")) = 1
	response.write "<script language=javascript>alert('对不起，["&RS("Title")&"]暂时库存不足，最多只能购买" & RS("TotalNum") & RS("unit") & "！');history.back(-1);</script>" 
	response.End()
End IF


Dim ProDiscount:ProDiscount=""
IF RS("IsLimitBuy")<>"0" and RS("LimitBuyAmount") < Amount Then
	Amount = 1
	Session("Amount"&RS("ID")) = 1
	If RS("LimitBuyAmount")=0 Then
	Conn.Execute("Update Product Set IsLimitBuy=0 Where ID=" & RS("ID"))
	response.write "<script language=javascript>alert('对不起,["&RS("Title")&"]已被抢购完,价格已恢复!');location.href='ShoppingCart.asp';</script>" 
	Else
	response.write "<script language=javascript>alert('对不起，["&RS("Title")&"]还剩" & RS("LimitBuyAmount") & RS("unit") & "供抢购！');history.back(-1);</script>" 
	rs.close:set rs=nothing
	response.End()
	End If
End If
ProBuyAttr=""

If Trim(RS("ID"))=trim(Session("ChangeBuyID")) Then
   RealPrice=Session("ChangeBuyPrice")
   
ElseIf RS("IsLimitBuy")="1" And Now>RS("LimitBuyBeginTime") And RS("LimitBuyEndTime")>Now And RS("LimitBuyAmount")>0 Then
   RealPrice=RS("LimitBuyPrice")
   ProBuyAttr="<span style='color:green'>(限时抢购)</span>"
   ProDiscount="---"
ElseIf RS("IsLimitBuy")="2" And RS("LimitBuyAmount")>0 Then
   RealPrice=RS("LimitBuyPrice")
   ProBuyAttr="<span style='color:blue'>(限量抢购)</span>"
   ProDiscount="---"
ElseIF Cbool(LoginTF)=true Then
  If RS("GroupPrice")=0 Then
   RealPrice=RS("Price_Member")
  Else
   Dim RSP:Set RSP=Conn.Execute("Select Price From KS_ProPrice Where GroupID=" & KSUser.GroupID & " And ProID=" & RS("ID"))
   If RSP.Eof Then
     RealPrice=RS("Price_Member")
   Else
     RealPrice=RSP(0)
   End If
   RSP.Close:Set RSP=Nothing
  End If
Else
  RealPrice=RS("Price")
End If
TotalPrice=TotalPrice+Round(RealPrice*Amount,2)
Dim PhotoUrl:PhotoUrl=RS("PhotoUrl")
If KS.IsNul(PhotoUrl) Then PhotoUrl="/images/nopic.gif"
If KS.S("AttributeCart")<>"" Then Session("AttributeCart"&KS.S("ID"))=KS.CheckXss(KS.S("AttributeCart"))

          CarListStr=CarListStr&"<tr class='tdbg' height=""25"" align=""center""> " & vbcrlf
          CarListStr=CarListStr&"  <td><input type=""CheckBox"" name=""id"" value=""" & RS("ID") & """ Checked></td>" & vbcrlf
          CarListStr=CarListStr&"  <td align=""left"" valign=""top""><img src=""" & PhotoUrl & """ alt=""" & RS("Title") & """ width=""50"" height=""50"" align=""left"" style=""border:1px solid #ccc;padding:2px""/><a href=""../item/show.asp?m=5&d=" & RS("id") & """ target=""_blank"">" & RS("Title")
		  If Trim(RS("ID"))=trim(Session("ChangeBuyID")) Then
		  CarListStr=CarListStr& "(换购)"
		  Else
		  CarListStr=CarListStr& ProBuyAttr
		  End If
		  
		  
		  CarListStr=CarListStr&"</a><br/>"
		  CarListStr=CarListStr& Session("AttributeCart"&RS("ID")) 
		  CarListStr=CarListStr & "</td>" & vbcrlf
		  If Trim(RS("ID"))=trim(Session("ChangeBuyID")) Then
          CarListStr=CarListStr&"  <td><input type=""hidden"" name=""Q_" & RS("ID") & """ value=""1"" size=""5"" style=""text-align:center"" class=""textbox"" readonly> 1</td>" & vbcrlf
		  Else
          CarListStr=CarListStr&"  <td><input type=""Text"" name=""Q_" & RS("ID") & """ value=""" & Amount & """ size=""5"" style=""text-align:center"" class=""textbox""></td>" & vbcrlf
		  End If
	   	  CarListStr=CarListStr&"	<td>￥" & FormatNumber(RS("Price_Original"),2,-1) & "</td>" & vbcrlf
		  If Trim(RS("ID"))=trim(Session("ChangeBuyID")) Then
		  CarListStr=CarListStr&"	<td align='center'>换购</td>" & vbcrlf
		  ElseIf ProDiscount<>"" Then
		  CarListStr=CarListStr&"	<td align='center'>" & ProDiscount & "</td>" & vbcrlf
		  ElseIf RS("Discount")=10 Then
		  CarListStr=CarListStr&"	<td align='center'>无折扣</td>" & vbcrlf
		  Else
		  CarListStr=CarListStr&"	<td>" & RS("Discount") & "折</td>" & vbcrlf
		  End If
		  CarListStr=CarListStr&"	<td>￥" & FormatNumber(RealPrice,2,-1) & "</td>" & vbcrlf
		  CarListStr=CarListStr&"	<td>￥" & FormatNumber(Round(RealPrice*Amount,2),2,-1)  & "</td>" & vbcrlf
		  CarListStr=CarListStr&"	<td><a href=""?Action=Del&ID=" & RS("ID") & """>删除</a> <a href=""../User/index.asp?User_Favorite.asp?Action=Add&ChannelID=5&InfoID=" & RS("ID") & """ target=""_blank"">收藏</a></td>" & vbcrlf
          CarListStr=CarListStr&"</tr>" & vbcrlf
		  CarListStr=CarListStr & GetBundleSalePro(RS("ID"),true)   '获得捆绑促销的商品
     RS.MoveNext
     Loop
	 
	    CarListStr=CarListStr& GetPackage(true)         '礼包
	Else 
	    dim packstr:packstr=GetPackage(true)           '礼包
		if packstr="" then
		  CarListStr=CarListStr&"	<tr class='tdbg'><td colspan=8>您的购物车没有商品!</td></tr>" & vbcrlf
		else
		  CarListStr=CarListStr& packstr
		end if
	End If
	RS.close
	
	

	CarListStr=CarListStr&"<tr class='tdbg'> " & vbcrlf
	CarListStr=CarListStr&" <td colspan=""6"" align=""center"" nowrap><font color=""#ff6600"">删除购物车物品，请将复选框的勾去掉并按“调整购物车”按钮</font>" & vbcrlf             
    CarListStr=CarListStr&" <input type=""image"" name=""order"" src=""images/shop_btn1.gif"">&nbsp;" & vbcrlf
    CarListStr=CarListStr&" <input type=""image"" name=""payment"" src=""images/shop_btn2.gif"" onclick=""this.form.action='PayMent.asp';this.form.target='_parent';"">&nbsp; " & vbcrlf
	Dim ComeUrl:ComeUrl=request.ServerVariables("http_referer")
	If ComeUrl="" Then ComeUrl="../index.asp"
    CarListStr=CarListStr&" <a href='" &comeurl & "'><img  src=""images/shop_btn3.gif"" border=0></a>" & vbcrlf
    CarListStr=CarListStr&"  </td>" &vbcrlf
	CarListStr=CarListStr&"<td colspan=""3"" height=""25"">合计：<font color=""#FF6600"">￥" & Round(TotalPrice,2) & "</font>&nbsp;元</td>" & vbcrlf
    CarListStr=CarListStr&"    </tr>" & vbcrlf
    CarListStr=CarListStr&"</table>" & vbcrlf
    CarListStr=CarListStr&"</form>"&vbcrlf
	
	'检查换购品合法性
	Call CheckChangeBuy(TotalPrice)
			 	 
	Dim ShowChangeBuy:ShowChangeBuy=true
	If Session("ProductList")<>"" Then
	  RS.Open "Select * From KS_ShopBundleSale Where ProID in(" & Session("ProductList") & ")",conn,1,1
	  If Not RS.Eof Then
	   ShowChangeBuy=false
	  End If
	  RS.Close
	End If
	If ShowChangeBuy=false Then       '不允许显示换购时,显示捆绑销售的商品
	  Dim ProID:ProID=KS.ChkClng(Request("id"))
	  If ProID=0 Then  Proid=Conn.Execute("Select top 1 ProID From KS_ShopBundleSale Where ProID in(" & Session("ProductList") & ")")(0)  '如果没有传商品ID过来,随机找一条有捆绑销售的产品
	     RS.Open "Select I.ID,I.Title,I.Price,I.PhotoUrl,B.KBPrice,B.Proid From Product I Inner Join KS_ShopBundleSale b on i.id=b.kbproid Where B.proid=" & proid,conn,1,1
		 If Not RS.Eof Then
		    Set GXML=KS.RsToXml(RS,"row","")
		 End If
		 RS.Close
		 If IsObject(GXML) Then
		    CarListStr=CarListStr&"<table width=""100%"" border=""0"" cellpadding=""0"" cellspacing=""0"" bgcolor=""#FFFFFF"" class=""carttr"">" &vbcrlf
		    CarListStr=CarListStr&"<tr><td style='border-bottom:1px solid #cccccc;padding-bottom:4px;color:green;font-size:14px'><strong><img src='../images/nav_icon.gif' align='absmiddle' /> 您可能还需要以下商品(捆绑促销)</strong></td></tr>" &vbcrlf
		    CarListStr=CarListStr&"<tr><td>" &vbcrlf
			CarListStr=CarListStr&"<div class='kblist'><ul>"
			 For Each Node In GXML.DocumentElement.SelectNodes("row")
					PhotoUrl=Node.SelectSingleNode("@photourl").text
					If KS.IsNul(PhotoUrl) Then PhotoUrl="../images/nopic.gif"
			        CarListStr=CarListStr&"<li><table border='0'><tr><td><a href='../item/show.asp?m=5&d=" & Node.SelectSingleNode("@id").text & "' target='_blank'><img src='" & PhotoUrl & "' border='0' width='80' height='80' title='" & Node.SelectSingleNode("@title").text & "'></a></td><td style='line-height:26px'><a href=""../item/show.asp?m=5&d=" & Node.SelectSingleNode("@id").text & """ target=""_blank"" class=""t"">" & Node.SelectSingleNode("@title").text & "</a><br/><span style='color:#ff6600'>仅需￥" & formatnumber(Node.SelectSingleNode("@kbprice").text,2,-1) & "元</span><br/><a href='?action=addBundleSale&pid=" & Node.SelectSingleNode("@id").text & "&proid=" & node.selectsinglenode("@proid").text & "&id=" & ks.chkclng(ks.s("id")) & "'><img src='images/addcart.gif' border='0'></a></td></tr></table></li>"
			 Next
			CarListStr=CarListStr&"</ul></div>" &vbcrlf
		    CarListStr=CarListStr&"</td></tr>" &vbcrlf
		    CarListStr=CarListStr&"</table>" &vbcrlf
		 End If
	  
	Else
	
			'换购商品
			Dim XML,Node,Param,GXML,GNode
			RS.Open "select ChangeBuyNeedPrice,ChangeBuyPresentPrice from product  where IsChangedBuy=1 group by ChangeBuyPresentPrice,ChangeBuyNeedPrice",Conn,1,1
			 If Not RS.Eof Then
			  Set GXML=KS.RsToXml(RS,"row","")
			 End If
			 RS.Close
			 If IsObject(GXML) Then
			  Dim oldchangebuypresentprice,clickStr
			  CarListStr=CarListStr&"<table width=""100%"" border=""0"" cellpadding=""0"" cellspacing=""0"" bgcolor=""#FFFFFF"" class=""carttr"">" &vbcrlf
			  For Each GNode In GXML.DocumentElement.SelectNodes("row")
					  CarListStr=CarListStr&"<tr><td align=""left"">" & vbcrlf
					  If GNode.SelectSingleNode("@changebuypresentprice").text=0 Then 
							CarListStr=CarListStr&"<div style=""width:100%; text-align:left; background:#eeeeee""><div style=""background:#000000; float:left; height:25px; padding:0 20px; line-height:25px"">　<b style=""font-size:14px; color:#FFFFFF"">免费领取</b></div><div style=""background:#eeeeee; float:left; height:25px; padding:0 20px; line-height:25px""><font color=""#000000""><b>满<font color=""#ff0000"">￥" & FormatNumber(GNode.SelectSingleNode("@changebuyneedprice").text,2,-1) & "</font>元,可免费领取以下礼品</b>"&vbcrlf
						   If round(TotalPrice)<round(GNode.SelectSingleNode("@changebuyneedprice").text) Then
						   CarListStr=CarListStr&"[您只需要再购买<font color=red>￥" &FormatNumber(GNode.SelectSingleNode("@changebuyneedprice").text-TotalPrice,2,-1) & "</font>元的商品,就可免费领取赠品]"
						   clickStr=" onclick=""alert('对不起,您的订单费用不足,还差￥" &FormatNumber(GNode.SelectSingleNode("@changebuyneedprice").text-TotalPrice,2,-1) & "元!');return false"" "
						   else
							clickstr=""
						   End If
						
					   Else
							CarListStr=CarListStr&"<div style=""width:100%; text-align:left; background:#eeeeee""><div style=""background:#000000; float:left; height:25px; padding:0 20px; line-height:25px"">　<b style=""font-size:14px; color:#FFFFFF"">换购商品</b></div><div style=""background:#eeeeee; float:left; height:25px; padding:0 20px; line-height:25px""><font color=""#000000""><b>满<font color=""#ff0000"">￥" & FormatNumber(GNode.SelectSingleNode("@changebuyneedprice").text,2,-1) & "</font>元,加<font color=""#ff0000"">￥" & FormatNumber(GNode.SelectSingleNode("@changebuypresentprice").text,2,-1) & "</font>元,可换购以下商品</b>"&vbcrlf
							
						   If round(TotalPrice)<round(GNode.SelectSingleNode("@changebuyneedprice").text) Then
						   CarListStr=CarListStr&"[您只需要再购买<font color=red>￥" &FormatNumber(GNode.SelectSingleNode("@changebuyneedprice").text-TotalPrice,2,-1) & "</font>元的商品,就可选购本级别的赠品]"
						   clickStr=" onclick=""alert('对不起,您的订单费用不足,还差￥" &FormatNumber(GNode.SelectSingleNode("@changebuyneedprice").text-TotalPrice,2,-1) & "元!');return false"" "
						   Else
							clickstr=""
						   End If
			
					   End If
					 CarListStr=CarListStr&"</font></div></td></tr>" &vbcrlf  
					 CarListStr=CarListStr&"<tr><td>" &vbcrlf 		   
					 CarListStr=CarListStr&"<div class='prolist'><ul>" &vbcrlf 	
					 
					If Session("ProductList")<>"" Then
					 Param=" and id not in(" & Session("ProductList") & ")"
					End If
					RS.Open "Select top 50 ID,Title,ChangeBuyNeedPrice,ChangeBuyPresentPrice,Price,PhotoUrl From Product Where IsChangedBuy=1 And verific=1 and ChangeBuyPresentPrice=" & GNode.SelectSingleNode("@changebuypresentprice").text & " and ChangeBuyNeedPrice=" & GNode.SelectSingleNode("@changebuyneedprice").text &" and  deltf=0 " & Param & " Order By ChangeBuyPresentPrice,Id Desc"
					If Not RS.Eof Then 
					 Set Xml=KS.RsToXml(RS,"row","")
					End If
					RS.Close
					If IsObject(XML) Then
					  For Each Node In XML.DocumentElement.SelectNodes("row")
							PhotoUrl=Node.SelectSingleNode("@photourl").text
							If KS.IsNul(PhotoUrl) Then PhotoUrl="../images/nopic.gif"
							CarListStr=CarListStr&"<li><a "&clickstr &" href='?action=present&id=" & Node.SelectSingleNode("@id").text & "'><img src='" & PhotoUrl & "' border='0' width='60' height='60' title='" & Node.SelectSingleNode("@title").text & "'></a><br/><a href=""../item/show.asp?m=5&d=" & Node.SelectSingleNode("@id").text & """ target=""_blank"" class=""t"">" & Node.SelectSingleNode("@title").text & "</a></li>"
					  Next
					
					End If
					CarListStr=CarListStr&"</ul></div>"  &vbcrlf 
					CarListStr=CarListStr&"</td></tr>"  &vbcrlf
			  Next	 
			 
			 End If
	 
   End If

	
	 set RS=nothing
	 
	
	  CarListStr=CarListStr&"</table>" & vbcrlf
	
	
		   FileContent = Replace(FileContent,"{$ShowShoppingCart}",CarListStr)
		   If Cbool(KSUser.UserLoginChecked)=False Then 
		   FileContent = Replace(FileContent,"{$ShowLoginTips}","<strong><font color=ff6600>温馨提示：您还没有注册或登录。享受更多会员优惠，请先<a href=""../user/login/"">登录</a>或<a href=""../?do=reg"" target=""_blank"">注册</a>成为商城会员！</font></strong>")
           Else
		   FileContent = Replace(FileContent,"{$ShowLoginTips}","亲爱的" & KSUser.UserName &"! 您的个人信息-> 用户组："&KS.GetUserGroupName(KSUser.GroupID)&"&nbsp;可用资金：&nbsp;<font color=""green"">" & FormatNumber(KSUser.GetUserInfo("Money"),2,-1,0,0) & "</font>&nbsp;元 " & KS.Setting(45) & "：&nbsp;<font color=green>" & KSUser.GetUserInfo("Point") & "</font>&nbsp;" & KS.Setting(46)&" 积分：&nbsp;<font color=""green"">" & KSUser.GetUserInfo("Score") & "</font>&nbsp;分")
		   End If
		   FileContent=KSRFObj.KSLabelReplaceAll(FileContent)
		   Response.Write FileContent  
		End Sub
		Sub PutToShopBag( Prodid, ProductList ,I)
		   if KS.S("action")="set" then
			   If i = 0 Then
				  ProductList =Prodid
			   ElseIf KS.FoundInArr( ProductList, Prodid,",")=false Then
				  ProductList = ProductList&", "&Prodid &""
			   End If
		   else
			   If Len(ProductList) = 0 Then
				  ProductList =Prodid
			   ElseIf KS.FoundInArr( ProductList, Prodid,",")=false Then
				  ProductList = ProductList&", "&Prodid &""
			   End If
		  end if
		  
		  If KS.S("Action")="present" and Session("ChangeBuyID")<>KS.S("ID") Then
		   Call DelProduct(Session("ChangeBuyID"))
		  End If
      End Sub
	  Sub DelProduct(DelID)
	  If DelID<>"" Then
	   Conn.Execute("Delete From KS_ShopBundleSelect Where UserName='" & getuserid & "' and pid=" & DelID)
	   End If
	   Dim i,Parr:Parr=Split(ProductList,",")
	   Dim NewPList
	   For i=0 To Ubound(Parr)
	    If trim(Parr(i))<>trim(DelID) Then
		 If NewPlist="" Then
		  NewPlist=Parr(i)
		 Else
		  NewPlist=NewPlist & "," & Parr(I)
		 End If
		End If
	   Next
	   ProductList=NewPlist
	  End Sub
	  
	  Sub AddPresent()
	   If KS.S("ID")="" Then KS.AlertHintScript "对不起,您没有选择换购品!"
	   If Session("ChangeBuyID")<>"" Then
	     If Session("ChangeBuyID")<>KS.S("ID") and ks.s("f")="" Then
		   KS.Die "<script>if (confirm('您已选过换购品了,是否替换?')){location.href='?f=ok&action=present&id=" & KS.S("id") &"'}else{location.href='shoppingcart.asp'}</script>"
		 End If
	   End If
	   
	   Dim RS:Set RS=Conn.Execute("Select top 1 ChangeBuyNeedPrice,ChangeBuyPresentPrice From Product Where ID=" & KS.ChkClng(KS.S("ID")))
	   IF Not RS.Eof Then
	     Session("ChangeBuyID")=KS.FilterIds(KS.S("ID"))
		 Session("ChangeBuyNeedPrice")=RS(0)
		 Session("ChangeBuyPrice")=RS(1)
	   End If
	   RS.Close:Set RS=Nothing
	  End Sub
	  
	  '检查换购品合法性
	  Sub  CheckChangeBuy(TotalPrice)
       If KS.IsNul(Session("ChangeBuyID")) Or KS.IsNul(Session("ChangeBuyNeedPrice")) Then Exit Sub

	      If Round(TotalPrice)>=Round(Session("ChangeBuyNeedPrice")) Then
			Exit Sub
		  Else
		    Call DelProduct(Session("ChangeBuyID"))
			Session("ChangeBuyID")=""
			Session("ProductList")=KS.FilterIds(ProductList)
			Response.Redirect "ShoppingCart.asp"
			'Call KS.Alert("对不起,您的订单金额不够换购此商品!","ShoppingCart.asp")
		  End If
	  End Sub
	  
	  
	  '删除礼包
	  Sub DelPack()
	    Dim ID:ID=KS.ChkClng(Request("id"))
		If ID<>0 Then
		Conn.Execute("Delete From KS_ShopPackageSelect Where PackID=" & ID & " and username='" & GetUserID &"'")
		End If
		Response.Redirect Request.ServerVariables("HTTP_REFERER")
	  End Sub
		
	  Sub addBundleSale()
	     '删除超过5天的记录
		 Conn.Execute("Delete From KS_ShopBundleSelect Where datediff(" & DataPart_D & ",adddate," & SqlNowString & ")>5")

	     Dim Pid:Pid=KS.ChkCLng(Request("pid"))        '选购的商品ID
		 Dim ProID:ProID=KS.ChkCLng(request("proid"))  '绑定的商品ID
		 If ProID<>0 and pid<>0 and ProductList<>"" then
		     
			 
			 'If KS.FoundInArr(ProductList,Pid,",")=false Then
			   ' ProductList=ProductList & "," & pid
			' End If
			 
		     Dim Price
			 Dim RS:Set RS=Server.CreateObject("adodb.recordset")
			 RS.Open "Select top 1 KBPrice From KS_ShopBundleSale Where ProID=" & ProID & " And KBProID=" & Pid,conn,1,1
			 If RS.Eof Then
			    RS.Close :Set RS=Nothing
				Exit Sub
			 Else
			    Price=RS(0)
			 End If
			 rs.close
			 
			 RS.Open "Select top 1 * From KS_ShopBundleSelect where username='" & GetUserID & "' and pid=" & pid & " and proid=" & proid,conn,1,3
			 If RS.Eof Then
				RS.AddNew
				RS("UserName")=GetUserID
				RS("Pid")=Pid
				RS("ProID")=ProID
				RS("Amount")=1
				RS("AddDate")=Now
				RS("Price")=Price
				RS.Update
			 End If
			 RS.Close : Set RS=Nothing
		 End If
	  End Sub
	  Sub delBundleSale()
	    Dim SelID:SelID=KS.ChkClng(Request("SelID"))
		If SelID=0 Then Exit Sub
		Conn.Execute("Delete From KS_ShopBundleSelect Where ID=" & SelID)
	  End Sub
	  
	  
End Class
%>
