<%
'**************************************
'* 最后更新 2013-10-10 by 56770.Kingdge
'* Copyright www.56770.com
'**************************************
Dim error,CookiesId,id,list_id,basket_chima,basket_count,basket_color,pro_num,pro_color,pro_size,pro_p_chima_id,pro_p_color_id,sum2,sum3,rsp,ppprice,pprice,pfpprice,basket_price,product_type
Showpath = "<a href='"& Eshop_url & LangPath &"'>"& Lebi_word(0) &"</a> → "& Lebi_word(7)
id = trim(request("id"))
If not isempty(id) Then
        id=replace(id," ","")
Else
        id = 0
End If
list_id = CheckNum(request("list_id"),0)
checkuser()
GetUserId()
CookiesId = CheckNum(request.cookies(cookiesname&"_user")("cookiesid"),GetUserId())
If CookiesId = 0 Then
        response.redirect Eshop_url & PagePath(1) &"?action=nocookies"
		response.End
End If
Select Case request("action")
	   Case "buy" : buy()
	   Case "del" : del()
	   Case "del_all" : del_all()
	   Case "addcart" : addcart()
	   Case "update_cart" : update_cart()
	   Case "shoucang" : shoucang()
	   Case "coupon" : coupon()
	   Case "GiftBuy" : GiftBuy()
	   Case "GiftDel" : GiftDel()
	   Case "giftbuy" : GiftBuy()
	   Case "giftdel" : GiftDel()
	   Case "checkspecification" : CheckSpecification()
	   Case "getspinfo" : getspinfo()
	   'case Else
	   'If id <> 0 Then
	   '	       response.redirect"basket.asp?action=buy&id="& id
	   '        response.End
	   'ElseIf list_id = 0 Then
	           'response.redirect"basket.asp"
	           'response.End
	   'End If
End Select

Sub getspinfo()
		name = safereplace(request("name"))
		id = checknum(request("id"),0)
		price = checknum(request("price"),0)
		Set rs = conn.execute("select s.p_stock,s.p_price,p.sprice,p.cx,p.cprice,sp.price as buyprice,s.p_no,p.bianhao from [Lebi_Product_Stock] as s,[Lebi_Product] as p,[Lebi_product_price] as sp where s.p_id = p.id and s.p_id = "& id &" and s.s_value"& LangTxt &"='"& name &"' and sp.product_id=p.id and sp.grade_id="&Lebi_UserGrade)
		If Not rs.eof Then 
				If CLng(getGradePurview(17)) = 1 Then 
						response.write("1")
				Else
						response.write(rs(0))
				End If 
				If rs("cx") = 1 Then 
					buyprice = rs("cprice")
				Else
					buyprice = rs("buyprice")
				End If 
				response.write("|"& dw & formatnumber2((rs(1)+buyprice)/moneyhl) & dw2 &"|"& dw & formatnumber2((rs(1)+rs("sprice"))/moneyhl) & dw2 &"|"& rs("bianhao") & rs("p_no") &"|"& name &"&nbsp;&nbsp;"& Lebi_cart(32) &"："& rs(0) &"，"& Lebi_cart(21) &"："& dw & formatnumber2((price+rs(1))/moneyhl)) & dw2
		Else
				response.write("0|")
		End If 
		rs.close : Set rs = Nothing
End Sub 

Sub CheckSpecification()
		id = CheckNum(request("id"),0)
		pro_num = CheckNum(request("pro_num"),1)
		product_type=trim(request("product_type"))
		spname=Replace(safereplace(request("spname")),"undefined","")
		spvalue=Replace(safereplace(request("spvalue")),"undefined","")
		ajaxbuy = checknum(request("ajaxbuy"),0)
		listcode = safereplace(request("listcode"))
		If spname <> "" And spvalue <> "" Then NotCheck = 1
		If product_type = "unite" Then
				NotCheck = 1
		Else
				set rs = conn.execute("select P_SpecificationName,P_SpecificationValue,P_SpecificationPic,cprice,cx,s.price as buyprice,p.kucun from [Lebi_product] as p,[Lebi_product_price] as s where zhuangtai=1 and p.id="&id&" and s.product_id=p.id and s.grade_id="&Lebi_UserGrade)
				If rs.eof Then
						NotCheck = 1
				Else 
				P_SpecificationName=rs(0)
				P_SpecificationValue=rs(1)
				P_SpecificationPic=rs(2)
				If InStr(P_SpecificationPic,"|")=0 Then P_SpecificationPic = "||||||||||||||||||||||||||||||||||||||||"
				If rs(4) = 1 Then
						price = rs(3)
				Else
						price = rs(5)
				End If
				stock = rs(6)
				If P_SpecificationName <> "" Then 
						response.write("<form action="""" method=""post"" name=""LEBI_FORMBUY_9999"" id=""LEBI_FORMBUY_1000""><div class=""choosesp"">")
						If InStr(P_SpecificationName,",") = 0 Then 
								n = 0
								P_SpecificationValue = Replace(P_SpecificationValue,"|",",")
								If Right(P_SpecificationValue,1) = "|" Then P_SpecificationValue = cut_Right(P_SpecificationValue)
								If Right(P_SpecificationValue,1) = "," Then P_SpecificationValue = cut_Right(P_SpecificationValue)
								Set rs = conn.execute("select s_name"& LangTxt &" from [Lebi_SelectName] where s_id = "& checknum(P_SpecificationName,0) &"")
								If Not rs.eof Then 
										SName = rs(0)
										response.write("<dl><dt>"& SName &"：</dt>")
								End If 
								rs.close : Set rs = Nothing
								Set rs = conn.execute("select s_value"& LangTxt &",id from [Lebi_SelectValue] where s_id = "& checknum(P_SpecificationName,0) &" and id in("& P_SpecificationValue &")")
								If Not rs.eof Then 
								j = 0
								response.write("<dd>")
								Do While Not rs.eof 
										p = getN(Replace(P_SpecificationValue,"|",","),rs(1),",")
										if Split(P_SpecificationPic,"|")(p) <> "" Then
												response.write("<div><a id=""sv_"& id &"_"& n &"_"& j &""" name=""sv_n_"& id &"_"& n &""" class=""spva-img"" title="""& rs(0) &""" onclick=""select_sp('"& id &"','"& n &"','"& j &"','"& SName &"|"& rs(0) &"','"& price &"','9999',"& getGradePurview(17) &");""><img src="""&Eshop_url&PicPath&"/"&Split(P_SpecificationPic,"|")(p)&""" /><span></span></a></div>")
										Else 
												response.write("<div><a id=""sv_"& id &"_"& n &"_"& j &""" name=""sv_n_"& id &"_"& n &""" class=""spva"" onclick=""select_sp('"& id &"','"& n &"','"& j &"','"& SName &"|"& rs(0) &"','"& price &"','9999',"& getGradePurview(17) &");"">"& rs(0) &"</a></div>")
										End If 
								rs.movenext
								j = j + 1
								Loop
								response.write("</dd>")
								End If 
								rs.close : Set rs = Nothing
								response.write("<input type=""hidden"" name=""hidv_9999_"& id &""" id=""hidv_9999_"& id &"_0""></dl>")
						Else
								For n = 0 To UBound(Split(P_SpecificationName,","))
								Set rs = conn.execute("select s_name"& LangTxt &" from [Lebi_SelectName] where s_id = "& checknum(Split(P_SpecificationName,",")(n),0) &"")
								If Not rs.eof Then 
										SName = rs(0)
										response.write("<dl><dt>"& SName &"：</dt>")
								End If 
								rs.close : Set rs = Nothing
								Set rs = conn.execute("select s_value"& LangTxt &",id from [Lebi_SelectValue] where s_id = "& checknum(Split(P_SpecificationName,",")(n),0) &" and id in("& Split(P_SpecificationValue,"|")(n) &")")
								If Not rs.eof Then 
								j = 0
								response.write("<dd>")
								Do While Not rs.eof 
										p = getN(Replace(P_SpecificationValue,"|",","),rs(1),",")
										if Split(P_SpecificationPic,"|")(p) <> "" Then
												response.write("<div><a id=""sv_"& id &"_"& n &"_"& j &""" name=""sv_n_"& id &"_"& n &""" class=""spva-img"" title="""& rs(0) &""" onclick=""select_sp('"& id &"','"& n &"','"& j &"','"& SName &"|"& rs(0) &"','"& price &"','9999',"& getGradePurview(17) &");""><img src="""&Eshop_url&PicPath&"/"&Split(P_SpecificationPic,"|")(p)&""" /><span></span></a></div>")
										Else 
												response.write("<div><a id=""sv_"& id &"_"& n &"_"& j &""" name=""sv_n_"& id &"_"& n &""" class=""spva"" onclick=""select_sp('"& id &"','"& n &"','"& j &"','"& SName &"|"& rs(0) &"','"& price &"','9999',"& getGradePurview(17) &");"">"& rs(0) &"</a></div>")
										End If 
								rs.movenext
								j = j + 1
								Loop
								response.write("</dd>")
								End If 
								rs.close : Set rs = Nothing
								response.write("<input type=""hidden"" name=""hidv_9999_"& id &""" id=""hidv_9999_"& id &"_"& n &"""></dl>")
								Next 
						End If 
						response.write("<dl><dt>"& Lebi_cart(22) &"：</dt><dd><input type=""text"" name=""pro_num_9999_"& id &""" id=""pro_num_9999_"& id &""" value="""& pro_num &""" size=""3"" class=""form""></dd></dl><div id=""result_9999_"& id &""" class=""result""></div>")
						response.write("<div class=""choosesp-btn"">")
						If CLng(getGradePurview(17)) = 1 Or (CLng(getGradePurview(17)) = 0 And CLng(stock) > 0) Then 
								response.write("<input type=""button"" name=""button_9999_"& id &""" id=""button_9999_"& id &""" value="""& Lebi_cart(108) &""" onClick=""javascript:addtobasket('"& id &"','9999','',1,0,'"& listcode &"')"" class=""button"">")
						Else
								temp = temp & Lebi_pro(39)
						End If 
						response.write("</div>")
						response.write("</div></form>")
				Else
						NotCheck = 1
				End If 
				End If 
		End If 
		If NotCheck = 1 Then 
				response.redirect "?action=buy&ajaxbuy="& ajaxbuy &"&id="& id &"&pro_num="& pro_num &"&spname="& urlencode(spname) &"&spvalue="& urlencode(spvalue) &"&product_type="& product_type &"&t="& request("t")
		End If 
End Sub 

Sub coupon()
        Dim c_num,sql,rss,suc_1
        If Lebi_UserId ="" Then
				response.redirect Eshop_url & PagePath(1) &"?action=needlogin&HTTP_REFERER="& geturl()
        		response.End
        End If
        c_num = Safereplace(Trim(request("c_num")))
		Set rs = server.CreateObject("adodb.recordset")
		sql = "select c_outdate,c_ondate,c_type,c_price,c_discount,c_usetype,user_id,c_state from [Lebi_coupon] where c_num = '"& c_num &"'"
		rs.open sql,conn,1,3
		If rs.eof Then
		        rem 错误的优惠券号码
				response.redirect Eshop_url & PagePath(1) &"?action=basket_coupon&error_type=1"
		        response.End
		Else
		        If DateDiff("d",rs("c_outdate"),Date())>0 Then
				        rem 已过期
						response.redirect Eshop_url & PagePath(1) &"?action=basket_coupon&error_type=2"
		                response.End
				End If
				If Trim(rs("c_ondate"))<>"" Then
				        If DateDiff("d",rs("c_ondate"),Date())>0 Then
				                rem 已过期
								response.redirect Eshop_url & PagePath(1) &"?action=basket_coupon&error_type=2"
		                        response.End
						End If
				End If
				If rs("c_state") = 1 And rs("c_usetype") = 1 Then
				        rem 当次使用 已被使用
						response.redirect Eshop_url & PagePath(1) &"?action=basket_coupon&error_type=3"
		                response.End
				End If
				If rs("c_state") = 1 And rs("c_usetype") = 0 And rs("user_id") <> Lebi_UserId Then
				        rem 当次使用 已被使用
						response.redirect Eshop_url & PagePath(1) &"?action=basket_coupon&error_type=4"
		                response.End
				End If
				'If IsNull(rs("c_ondate")) Or rs("c_ondate") = "" Then
				'        rs("c_ondate") = Date()
				'End If
				'rs("user_id") = Lebi_UserId
				'rs("c_state") = 1
				'rs.update
				Set rslshy = server.CreateObject("adodb.recordset")
				rslshy.open "select sessionid,basket_date,coupon_num from [Lebi_lshy] where sessionid = "& CookiesId &"",conn,1,3
				If rslshy.eof Then
						rslshy.addnew
						rslshy("sessionid") = CookiesId
						rslshy("basket_date") = Now()
						rslshy("coupon_num") = c_num
						rslshy.update
				Else
						rslshy("coupon_num") = c_num
						rslshy.update
				End If
				rslshy.close : Set rslshy = Nothing
                response.redirect Eshop_url & PagePath(2) &"?do=basket_coupon&suc_1="&rs("c_type")&"&suc_2="&rs("c_price")/moneyhl&"&suc_3="&rs("c_discount")&""
		        response.End
		End If
		rs.close : Set rs = Nothing
End Sub

Sub buy()
		Dim is_booking,advance_money
		is_booking = 0
		advance_money = 0
		product_type=trim(request("product_type"))
		spname=safereplace(request("spname"))
		spvalue=safereplace(request("spvalue"))
		ajaxbuy = checknum(request("ajaxbuy"),0)
		If product_type = "unite" Then
				buyUnite()  '捆绑促销
		ElseIf product_type = "groupon" Then
				buyGroupon()  '团购
		Else
				id = CheckNum(request("id"),0)
				pro_num = CheckNum(request("pro_num"),1)
				If pro_num = 0 Then pro_num = 1
				set rsp = conn.execute("select product_name"& LangTxt &",bianhao,sprice,cprice,cx,p_jifen,kucun,p_weight,p_color,p_chima,s.price as buyprice,categoryid from [Lebi_product] as p,[Lebi_product_price] as s  where zhuangtai=1 and p.id="&id&" and s.product_id=p.id and s.grade_id="&Lebi_UserGrade)
				If not rsp.eof Then
						stock = rsp("kucun")
						p_no = ""
						If spvalue <> "" Then 
								Set rs = conn.execute("select p_stock,p_no from [Lebi_Product_Stock] where p_id = "& id &" and s_value"& LangTxt &"='"& spvalue &"'")
								If Not rs.eof Then 
										stock = rs(0)
										p_no = rs(1)
								End If 
								rs.close : Set rs = Nothing
						End If 
						If clng(stock)<clng(pro_num) Then
							If CInt(getGradePurview(17)) <> 1 Then
								If stock<=0 Then
									Select Case ajaxbuy
									case 0 
										response.write("<script>alert('"& Lebi_cart(40) &"');javascript:history.back();</script>")
										response.End 
									Case 1 
										response.write(""& Lebi_cart(40))
										response.End 
									End Select 
								Else
									Select Case ajaxbuy
									case 0 
										response.write("<script>alert('"& Lebi_cart(37)&"\n"& Lebi_cart(32)&"："& stock &"');javascript:history.back();</script>")
										response.End 
									Case 1 
										response.write(""&Lebi_cart(37)&"<br/>"& Lebi_cart(32)&"："& stock)
										response.End 
									End Select 
								End if
								response.End
							Else
								is_booking = 1
							End If
						End If
						If rsp("cx") = 1 Then
								basket_price = rsp("cprice")
						Else
								basket_price = rsp("buyprice")
						End If
						If spvalue <> "" Then 
								Set rs = conn.execute("select p_price,p_weight from [Lebi_Product_Stock] where p_id = "& id &" and s_value"& LangTxt &"='"& spvalue &"'")
'								If rs.eof Then 
'										response.write("无此属性库存,请选择其他属性")
'										response.End 
'								Else
								If Not rs.eof Then 
										sp_price = rs(0)
										sp_weight = rs(1)
								End If 
								rs.close : Set rs = Nothing
								p_weight = sp_weight
								basket_price = basket_price + sp_price
						Else
								p_weight = rsp("p_weight")
						End If 
						rem	<-{ 预订 by 56770.WangLei
						If is_booking = 1 And CInt(getGradePurview(19))=1 Then
							advance_money = CDbl(basket_price) * CDbl(getGradePurview(20))
						ElseIf is_booking = 1 And CInt(getGradePurview(19))=0 Then
							advance_money = 0
						End If
						rem	}->
						set rs = conn.execute("select sessionid from [Lebi_lsbuy] where cpid="&id&" and spvalue='"&spvalue&"' and (sessionid="& CookiesId &" or (User_Id = '"& Lebi_UserId &"' and User_Id <> '')) and IsGift=0")
						If rs.eof Then
								conn.execute("Insert into [Lebi_lsbuy](cpid,product_name,bianhao,sprice,price,sessionid,basket_date,p_jifen,IsGift,p_weight,spname,spvalue,basket_count,father,is_booking,advance_money,product_type,User_Id)values("&id&",'"&rsp(0)&"','"&rsp("bianhao") & p_no &"',"&rsp("sprice")&","&basket_price&","& CookiesId &","&sqlnowstring&","&rsp("p_jifen")&",0,"&p_weight&",'"&spname&"','"&spvalue&"',"&pro_num&",1,"&is_booking&","&advance_money&",'','"& Lebi_UserId &"')")
						Else
								conn.execute("Update [Lebi_lsbuy] set basket_date="& sqlnowstring &",basket_count="& pro_num &",is_booking="& is_booking &",advance_money="& advance_money &" where cpid="& id &" and spvalue='"&spvalue&"' and (sessionid="& CookiesId &" or (User_Id = '"& Lebi_UserId &"' and User_Id <> '')) and IsGift=0")
						End If
						rs.close:set rs=Nothing
				End If
				rsp.close:Set rsp=Nothing
		End If
		Select Case ajaxbuy
		case 0 
			response.redirect Eshop_url & PagePath(3)
		End Select 
End Sub

rem 添加收藏夹内的商品
Sub addcart
		Dim rsc,rss,rsss,rs1,i,a_id,idArr,pType,is_booking,advance_money
		is_booking = 0
		advance_money = 0
		a_id = safereplace(Replace(request("a_id")," ",""))
		if a_id="" then
				Write_Lebi_Eshop("<SCRIPT language=JavaScript>alert('"& Lebi_cart(194) &"');javascript:window.close();</SCRIPT>")
				response.end
		End If
		idArr = split(a_id,",")
		for i=0 to ubound(idArr)
				Set rs = conn.execute("select id,product_type from [Lebi_Action] where actionid = "& idArr(i) &"")
				idArr(i) = rs(0)
				pType = rs(1)
				rs.close
				If Trim(pType) = "unite" Then ' WangLei 捆绑商品加入购物车
						set rs = conn.execute("select id,product_name"& LangTxt &",bianhao,sprice,cprice,p_weight,kucun from [Lebi_product_unite] where id = "& idArr(i) &" ")
						If Not rs.eof Then
								If rs(6)<=0 Then
										If CInt(getGradePurview(17)) <> 1 Then
												response.redirect Eshop_url & PagePath(1) &"?action=basket&error_type=1"
												response.End
										Else
												is_booking = 1
										End If
								End If
								basket_price = rs("cprice")
								rem	<-{ 预订 by 56770.WangLei
								If is_booking = 1 And CInt(getGradePurview(19))=1 Then
									advance_money = CDbl(basket_price) * CDbl(getGradePurview(20))
								ElseIf is_booking = 1 And CInt(getGradePurview(19))=0 Then
									advance_money = 0
								End If
								rem	}->
								set rsc = conn.execute("select * from [Lebi_lsbuy] where cpid="&rs("id")&" and (sessionid="& CookiesId &" or (User_Id = '"& Lebi_UserId &"' and User_Id <> '')) and IsGift=0")
								If rsc.eof Then
										pro_size = "-"
										pro_color = "-"
										set rss=server.CreateObject("adodb.recordset")
										rss.open "select * from [Lebi_lsbuy]",conn,1,3
										rss.addnew
										rss("cpid")=rs(0)
										rss("product_name")=rs(1)
										rss("bianhao")=rs(2)
										rss("sprice")=rs(3)
										rss("price")=basket_price
										rss("sessionid")=CookiesId
										rss("basket_date")=now()
										rss("IsGift")=0
										rss("p_jifen")=0
										rss("p_weight")=rs(5)
										rss("chima")=pro_size
										rss("color")=pro_color
										rss("father")=1
										rss("product_type") = "unite"
										rss("User_Id") = Lebi_UserId
										rss("is_booking") = is_booking
										rss("advance_money") = advance_money
										rss("basket_count") = 1
										rss.update
										rss.close
										set rss=Nothing
								Else
										conn.execute("Update [Lebi_lsbuy] set basket_date="& sqlnowstring &" where cpid="& rs("id") &" and (sessionid="& CookiesId &" or (User_Id = '"& Lebi_UserId &"' and User_Id <> '')) and IsGift=0")
								End If
								rsc.close
								set rsc=Nothing
						End If
						rs.close:Set rs=Nothing
				Else
						set rs = conn.execute("select p.id,product_name"& LangTxt &",bianhao,sprice,cprice,p_color,p_chima,p_jifen,p_weight,kucun,cx,s.price as buyprice from [Lebi_product] as p,[Lebi_product_price] as s where zhuangtai=1 and p.id = "& idArr(i) &"  and s.product_id=p.id and s.grade_id="&Lebi_UserGrade)
						If Not rs.eof Then
								If rs(9)<=0 Then
										If CInt(getGradePurview(17)) <> 1 Then
												response.redirect Eshop_url & PagePath(1) &"?action=basket&error_type=1"
												response.End
										Else
												is_booking = 1
										End If
								End If
								If rs(10) = 1 Then
										basket_price = rs(4)
								Else
										basket_price = rs(11)
								End If
								rem	<-{ 预订 by 56770.WangLei
								If is_booking = 1 And CInt(getGradePurview(19))=1 Then
									advance_money = CDbl(basket_price) * CDbl(getGradePurview(20))
								ElseIf is_booking = 1 And CInt(getGradePurview(19))=0 Then
									advance_money = 0
								End If
								rem	}->
								set rsc = conn.execute("select * from [Lebi_lsbuy] where cpid="&rs(0)&" and (sessionid="& CookiesId &" or (User_Id = '"& Lebi_UserId &"' and User_Id <> '')) and IsGift=0")
								If rsc.eof Then
										If Trim(Replace(rs(6),",",""))<>"" Then
												If InStr(rs(6),",")>0 Then
														pro_p_chima_id = Split(rs(6),",")(0)
												Else
														pro_p_chima_id = rs(6)
												End If
												If IsNumeric(Trim(pro_p_chima_id)) Then
														set rsss = conn.execute("select c_name"& LangTxt &" from [Lebi_chima] where c_id = "& pro_p_chima_id &"")
														If not rsss.eof Then
																pro_size = Trim(rsss(0))
														Else
																pro_size = "-"
														End If
														rsss.close:Set rsss=Nothing
												Else
														pro_size = "-"
												End If
										Else
												pro_size = "-"
										End If
										If Trim(rs(5))<>"" Then
												If InStr(rs(5),",")>0 Then
														pro_p_color_id = Split(rs(5),",")(0)
												Else
														pro_p_color_id = rs(5)
												End If
												If IsNumeric(Trim(pro_p_color_id)) Then
														set rsss = conn.execute("select c_name"& LangTxt &" from [Lebi_color] where c_id = "& pro_p_color_id &"")
														If not rsss.eof Then
																pro_color = Trim(rsss(0))
														Else
																pro_color = "-"
														End If
														rsss.close:Set rsss=Nothing
												Else
														pro_color = "-"
												End If
										Else
												pro_color = "-"
										End If
										set rss=server.CreateObject("adodb.recordset")
										rss.open "select * from [Lebi_lsbuy]",conn,1,3
										rss.addnew
										rss("cpid")=rs(0)
										rss("product_name")=rs(1)
										rss("bianhao")=rs(2)
										rss("sprice")=rs(3)
										rss("price")=basket_price
										rss("sessionid")=CookiesId
										rss("basket_date")=now()
										rss("IsGift")=0
										rss("p_jifen")=rs(7)
										rss("p_weight")=rs(8)
										rss("chima")=pro_size
										rss("color")=pro_color
										rss("father")=1
										rss("User_Id") = Lebi_UserId
										rss("is_booking") = is_booking
										rss("advance_money") = advance_money
										rss("basket_count") = 1
										rss("product_type") = ""
										rss.update
										rss.close
										set rss=Nothing
								Else
										conn.execute("Update [Lebi_lsbuy] set basket_date="& sqlnowstring &" where cpid="& rs("id") &" and (sessionid="& CookiesId &" or (User_Id = '"& Lebi_UserId &"' and User_Id <> '')) and IsGift=0")
								End If
								rsc.close
								set rsc=nothing
						End If
						rs.close:Set rs=Nothing
				End If
		Next
		response.redirect Eshop_url & PagePath(3)
		response.End
End Sub

Sub del_all
        conn.execute("Delete from [Lebi_lsbuy] where (sessionid="& CookiesId &" or (User_Id = '"& Lebi_UserId &"' and User_Id <> ''))")
        conn.execute("Delete from [Lebi_lshy] where sessionid="& CookiesId &"")
	    Application(cookiesname&"d_cart")=""
		response.redirect Eshop_url & PagePath(3)
        response.end
End Sub

Sub del
        Dim del_id,rss
        del_id = CheckNum(request("del_id"),0)
	    Set rs=conn.execute("select cpid,father,product_name,bianhao from [Lebi_lsbuy] where (sessionid="& CookiesId &" or (User_Id = '"& Lebi_UserId &"' and User_Id <> '')) and id="& del_id &" and IsGift=0")
	    If Not rs.eof Then
	            If rs("father")=0 Then
                        conn.execute("Delete from [Lebi_lsbuy] where (sessionid="& CookiesId &" or (User_Id = '"& Lebi_UserId &"' and User_Id <> '')) and id="& del_id &" and IsGift=0")
                Else
			            set Rss = conn.execute("select top 1 id from [Lebi_lsbuy] where (sessionid="& CookiesId &" or (User_Id = '"& Lebi_UserId &"' and User_Id <> '')) and cpid = "& rs("cpid") &" and father=0 order by id asc")
			            If not rss.eof Then
                                conn.execute("update [Lebi_lsbuy] set father=1,product_name='"& rs("product_name") &"',bianhao='"& rs("bianhao") &"' where (sessionid="& CookiesId &" or (User_Id = '"& Lebi_UserId &"' and User_Id <> '')) and id = "& rss("id") &"")
			            End If
		                rss.close:set rss=nothing
			            conn.execute("Delete from [Lebi_lsbuy] where (sessionid="& CookiesId &" or (User_Id = '"& Lebi_UserId &"' and User_Id <> '')) and id="& del_id &" and IsGift=0")
		       End If
	    End If
	    rs.close:Set rs=Nothing
        response.redirect Eshop_url & PagePath(3)
		response.end
End Sub

Sub Update_cart
        dim cpid,is_booking,checkout,stock,basket_count,spvalue,IsGift
		checkout = request("checkout")
        set rs = conn.execute("select * from [Lebi_lsbuy] where (sessionid="& CookiesId &" or (User_Id = '"& Lebi_UserId &"' and User_Id <> ''))")
        do while not rs.eof
        cpid=rs("cpid")
        id=rs("id")
		is_booking=rs("is_booking")
		IsGift = rs("IsGift")
        basket_count = CheckNum(request("I_num_"&id&""),0)
        spvalue = Safereplace(request("I_spvalue_"&id&""))
        If basket_count <= 0 Then
               basket_count = rs("basket_count")
               If basket_count <= 0 Then basket_count = 1
        End If
        dim rsk,errormes
		If product_type = "" Or IsNull(product_type) Then 
				errormes=errormes&""&Lebi_word(3)&"\n\n"&Lebi_cart(37)&"\n\n"
				If IsGift = 0 Then 
					set rsk = conn.execute("select kucun,product_name"& LangTxt &" from [Lebi_product] where zhuangtai=1 and id = "& cpid &"")
					If not rsk.eof Then
							stock = rsk(0)
							Set rsstock = conn.execute("select p_stock from [Lebi_Product_Stock] where p_id = "& cpid &" and s_value"& LangTxt &"='"& spvalue &"'")
							If Not rsstock.eof Then 
									stock = rsstock(0)
							End If 
							rsstock.close : Set rsstock = Nothing
							rem 如果库存小于等于0时并开启预订功能时不提示库存不足 2011-09-15 by 56770.kingdge 
							If stock <= 0 And CInt(getGradePurview(17)) <> 1 Then
									 error=1
									 errormes=errormes&"\n\n"&Lebi_cart(17)&"："&rsk(1)&"\n"&Lebi_cart(32)&"："&stock
							End If
							rem 如果库存大于0时（关闭预订）不允许客户购买超过库存的数量 2012-08-28 by 56770.kingdge
							If (stock > 0 And clng(stock)<clng(basket_count)) And CInt(getGradePurview(17)) <> 1 Then
									 error=1
									 errormes=errormes&"\n\n"&Lebi_cart(17)&"："&rsk(1)&"\n"&Lebi_cart(32)&"："&stock
							End If
					End If
					rsk.close:Set rsk=Nothing
				Else
					sql = "select kucun,product_name"& LangTxt &" from [Lebi_vipproduct] where id = "& cpid &""
					if IsSqlDataBase ="1" then sql = sql &" and datediff(d,endtime,"&sqlnowstring&")<=0"
					if IsSqlDataBase ="0" then sql = sql &" and datediff('d',endtime,"&sqlnowstring&")<=0"
					set rsk = conn.execute(sql)
					If not rsk.eof Then
							stock = rsk(0)
							If stock <= 0 Then
									 error=1
									 errormes=errormes&"\n\n"&Lebi_cart(17)&"："&rsk(1)&"\n"&Lebi_cart(32)&"："&stock
							End If
							If (stock > 0 And clng(stock)<clng(basket_count)) Then
									 error=1
									 errormes=errormes&"\n\n"&Lebi_cart(17)&"："&rsk(1)&"\n"&Lebi_cart(32)&"："&stock
							End If
					End If
					rsk.close:Set rsk=Nothing
				End If 
		ElseIf product_type = "groupon" Then 
				set rsk = conn.execute("select kucun,product_name,buy_low,buy_most from [Lebi_groupon_product] where zhuangtai=1 and id ="&cpid&"")
				If not rsk.eof Then
						If rsk("buy_low") > 0 Then 
								If CLng(basket_count) < CLng(rsk("buy_low")) Then basket_count = rsk("buy_low")
						End If 
						If rsk("buy_most") > 0 Then 
								If CLng(basket_count) > CLng(rsk("buy_most")) Then
										 basket_count = rsk("buy_most")
										 error=1
										 errormes=errormes&"本商品每人最多可以购买的数量为"& rsk("buy_most") &""
								End If 
						End If 
						rem 如果库存小于等于0时提示库存不足 2012-07-22 by 56770.kingdge 
						If rsk("kucun") <= 0 Then
								 error=1
								 errormes=errormes&"\n\n"&Lebi_cart(17)&"："&rsk("product_name")&"   |  "&Lebi_cart(32)&"："&rsk("kucun")
						End If
						rem 如果库存大于0时不允许客户购买超过库存的数量
						If rsk("kucun") > 0 And clng(rsk("kucun"))<clng(basket_count) Then
								 error=1
								 errormes=errormes&"\n\n"&Lebi_cart(17)&"："&rsk("product_name")&"   |  "&Lebi_cart(32)&"："&rsk("kucun")
						End If
				End If
				rsk.close:Set rsk=Nothing
		End If 
        If Error<>1 Then
                conn.execute("Update [Lebi_lsbuy] set basket_count = "& basket_count &" where id = "& id &"")
        End If
        rs.movenext
        loop
        rs.close
        If Error=1 Then
                Write_Lebi_Eshop("<SCRIPT language=JavaScript>alert('"&errormes&"');javascript:history.go(-1)</SCRIPT>")
        Else
				If checkout = "checkout" Then
					response.redirect Eshop_url & PagePath(17)
				Else
					response.redirect Eshop_url & PagePath(3)
				End If
        End If
End Sub

Sub shoucang
        Dim rsc
        If  Lebi_UserId <>"" Then
                set rs = conn.execute("select * from [Lebi_lsbuy] where (sessionid="& CookiesId &" or (User_Id = '"& Lebi_UserId &"' and User_Id <> '')) and IsGift=0 and father=1")
                do while not rs.eof
		        set rsc=conn.execute ("select * from [Lebi_action] where id="&rs("cpid")&" and user_id='"& Lebi_UserId &"' and product_type='"& rs("product_type") &"'")
                If rsc.eof Then
                        conn.execute"insert into [Lebi_action](id,product_name,user_id,actiondate,product_type)values("& rs("cpid") &",'"& rs("product_name") &"','"& Lebi_UserId &"',"& sqlnowstring &",'"& rs("product_type") &"')"
		        Else
                        conn.execute("Update [Lebi_action] set actiondate="& sqlnowstring &" where id="& rs("cpid") &" and user_id='"& Lebi_UserId &"'")
                End If
                rsc.close:set rsc=nothing
                rs.movenext
                loop
                rs.close:Set rs=Nothing
				Write_Lebi_Eshop("<SCRIPT language=JavaScript>alert('["&Lebi_cart(9)&"]\n\n"&Lebi_cart(35)&"');javascript:history.go(-1)</SCRIPT>")
				response.End
        Else
				Write_Lebi_Eshop("<SCRIPT language=JavaScript>alert('["&Lebi_cart(9)&"]\n\n"&Lebi_cart(36)&"');javascript:history.go(-1)</SCRIPT>")
				response.End
        End If
End Sub

Sub GiftBuy()
        Dim count
		If Lebi_UserId ="" Then
		        response.redirect Eshop_url & PagePath(1) &"?action=needlogin&HTTP_REFERER="& geturl()
				response.End
		End If
		id = CheckNum(request("id"),0)
        count = CheckNum(request("count"),1)
        Dim strcount,product_name,jifen,p_weight,rss
		Set rs = conn.execute("select kucun,product_name"& LangTxt &",jifen,p_weight from [Lebi_vipproduct] where id="& id &"")
		strcount=rs(0)
		product_name=rs(1)
		jifen=rs(2)
		p_weight=rs(3)
		rs.close:Set rs=Nothing
		Set rs = conn.execute("select jifen from [Lebi_user] where user_id = '"& Lebi_UserId &"'")
		If Not rs.eof Then
				If CLng(rs("jifen"))<CLng(jifen) Then
						response.redirect Eshop_url & PagePath(1) &"?action=basket&error_type=4"
						response.End
				End If
		End If
		rs.close : Set rs = Nothing
		If CLng(count) > CLng(strcount) Then
				Write_Lebi_Eshop("<SCRIPT language=JavaScript>alert('"&Lebi_word(3)&"\n\n"&Lebi_cart(37)&"');javascript:history.go(-1)</SCRIPT>")
        Else
                set rs = conn.execute("select * from [Lebi_lsbuy] where cpid="& id &" and (sessionid="& CookiesId &" or (User_Id = '"& Lebi_UserId &"' and User_Id <> '')) and IsGift=1")
                If rs.eof Then
                        conn.execute("Insert into   [Lebi_lsbuy](cpid,product_name,price,sessionid,basket_date,basket_count,IsGift,p_weight,User_Id,father)values("& id &",'"& product_name &"',"& jifen &","& CookiesId &","& sqlnowstring &","& count &",1,"& p_weight &",'"& Lebi_UserId &"',1)")
                Else
                        conn.execute("Update [Lebi_lsbuy] set basket_date="& sqlnowstring &",basket_count="& count &" where cpid="& id &" and (sessionid="& CookiesId &" or (User_Id = '"& Lebi_UserId &"' and User_Id <> '')) and IsGift=1")
                End if
                conn.execute("Delete FROM [Lebi_lsbuy] where IsGift=1 and ("&sqlnowstring&"-basket_date)>1")
                rs.close:set rs=Nothing
        End If
        response.redirect Eshop_url & PagePath(3)
		response.End
End Sub

Sub GiftDel()
		Dim del_id
        del_id = CheckNum(request("del_id"),0)
	    conn.execute("Delete from [Lebi_lsbuy] where (sessionid="& CookiesId &" or (User_Id = '"& Lebi_UserId &"' and User_Id <> '')) and id="& del_id &" and IsGift=1")
        response.redirect Eshop_url & PagePath(3)
End Sub

'==========START WangLei 2008-1-12 ===================================
Sub buyUnite()  '捆绑促销
		id = CheckNum(request("id"),0)
		pro_num = CheckNum(request("pro_num"),1)
		product_type = Safereplace(trim(request("product_type")))
		set rsp = conn.execute("select product_name"& LangTxt &",bianhao,sprice,cprice,p_jifen,p_weight,kucun from [Lebi_product_unite] where id="& id &"")
		If not rsp.eof Then
				If rsp("kucun")<=0 Then
						response.redirect Eshop_url & PagePath(1) &"?action=basket&error_type=1"
						response.End
				ElseIf clng(rsp("kucun"))<clng(pro_num) Then
						response.redirect Eshop_url & PagePath(1) &"?action=basket&error_type=2&stock="& rsp("kucun") &""
						response.End
				Else
						basket_price=rsp("cprice")
						set rs = conn.execute("select sessionid from [Lebi_lsbuy] where cpid="& id &" and father=1 and (sessionid="& CookiesId &" or (User_Id = '"& Lebi_UserId &"' and User_Id <> '')) and IsGift=0 and product_type='"& product_type &"'")
						If rs.eof Then
								conn.execute("Insert into [Lebi_lsbuy](cpid,product_name,bianhao,sprice,price,sessionid,basket_date,p_jifen,IsGift,p_weight,chima,color,basket_count,father,product_type,User_Id)values("& id &",'"& rsp(0) &"','"& rsp("bianhao") &"',"& rsp("sprice") &","& basket_price &","& CookiesId &","& sqlnowstring &","& rsp("p_jifen") &",0,"& rsp("p_weight") &",'-','-',"& pro_num &",1,'"& product_type &"','"& Lebi_UserId &"')")
						Else
								conn.execute("Update [Lebi_lsbuy] set basket_date="& sqlnowstring &",basket_count="& pro_num &" where cpid="& id &" and (sessionid="& CookiesId &" or (User_Id = '"& Lebi_UserId &"' and User_Id <> '')) and product_type='"& product_type &"' and IsGift=0")
						End If
						rs.close:set rs=Nothing
				End IF
				rsp.close:Set rsp=Nothing
		End If
End Sub
Sub buyGroupon()  '团购
		id = CheckNum(request("id"),0)
		product_type = Safereplace(trim(request("product_type")))
		pro_num = 1
		set rsp = conn.execute("select product_name"& LangTxt &",bianhao,sprice,gpprice,p_jifen,p_weight,kucun,buy_low from [Lebi_groupon_product] where id="& id &"")
		If not rsp.eof Then
				If rsp("buy_low") > 0 Then 
						pro_num = rsp("buy_low")
				End If 
				If rsp("kucun")<=0 Then
						response.redirect Eshop_url & PagePath(1) &"?action=basket&error_type=1"
						response.End
				ElseIf clng(rsp("kucun"))<clng(pro_num) Then
						response.redirect Eshop_url & PagePath(1) &"?action=basket&error_type=2&stock="& rsp("kucun") &""
						response.End
				Else
						basket_price=rsp("gpprice")
						set rs = conn.execute("select sessionid from [Lebi_lsbuy] where cpid="& id &" and father=1 and (sessionid="& CookiesId &" or (User_Id = '"& Lebi_UserId &"' and User_Id <> '')) and IsGift=0 and product_type='"& product_type &"'")
						If rs.eof Then
								conn.execute("Insert into [Lebi_lsbuy](cpid,product_name,bianhao,sprice,price,sessionid,basket_date,p_jifen,IsGift,p_weight,chima,color,basket_count,father,product_type,User_Id)values("& id &",'"& rsp(0) &"','"& rsp("bianhao") &"',"& rsp("sprice") &","& basket_price &","& CookiesId &","& sqlnowstring &","& rsp("p_jifen") &",0,"& rsp("p_weight") &",'-','-',"& pro_num &",1,'"& product_type &"','"& Lebi_UserId &"')")
						Else
								conn.execute("Update [Lebi_lsbuy] set basket_date="& sqlnowstring &",basket_count="& pro_num &" where cpid="& id &" and (sessionid="& CookiesId &" or (User_Id = '"& Lebi_UserId &"' and User_Id <> '')) and product_type='"& product_type &"' and IsGift=0")
						End If
						rs.close:set rs=Nothing
				End IF
				rsp.close:Set rsp=Nothing
		End If
End Sub 
%>