<!--#include file="../Start.asp"-->
<!--#include file="Channel_Config.asp"-->
<!--#include file="../Include/PowerEasy.Cache.asp"-->
<!--#include file="../Include/PowerEasy.Channel.asp"-->
<!--#include file="../Include/PowerEasy.MD5.asp"-->
<!--#include file="../Include/PowerEasy.Common.Front.asp"-->
<!--#include file="../Include/PowerEasy.OrderFlow.asp"-->
<%
'**************************************************************
' Software name: PowerEasy SiteWeaver
' Web: http://www.powereasy.net
' Copyright (C) 2005－2008 佛山市动易网络科技有限公司 版权所有
'**************************************************************

Dim trs, rsProductList

CartID = ReplaceBadChar(Trim(Request.Cookies("Cart" & Site_Sn)("CartID")))
If CartID = "" Or IsNull(CartID) Then
    CartID = MD5(Now() & Timer() & session.SessionID, 32)
End If

ProductID = Trim(Request("ProductID"))
PresentID = Trim(Request("PresentID"))
If IsValidID(ProductID) = False Then
    ProductID = ""
End If
If IsValidID(PresentID) = False Then
    PresentID = ""
End If

IsWholesale = PE_CLng(Request("IsWholesale"))
Select Case Action
Case "Add"
    If ProductID = "" Then
        FoundErr = True
        ErrMsg = ErrMsg & "<li>请指定要购买的产品ID！</li>"
    Else
        ProductID = PE_CLng(ProductID)
        ProductList = SelectCart(CartID, 0)
        Set trs = Conn.Execute("select ProductID,ProductType,Stocks,OrderNum,EnableSale,EnableSingleSell,DependentProducts from PE_Product where ProductID=" & ProductID & "")
        If trs.BOF And trs.EOF Then
            FoundErr = True
            ErrMsg = ErrMsg & "<li>找不到指定的商品！</li>"
        Else
            If trs("EnableSale") = False Then
                FoundErr = True
                ErrMsg = ErrMsg & "<li>商品已经停止销售！</li>"
            End If
            If trs("EnableSingleSell") = False And FoundInArr(trs("DependentProducts"), ProductList, ",") = False Then
                FoundErr = True
                ErrMsg = ErrMsg & "<li>此商品不能直接购买！请先购买本商品的从属商品后再购买本商品</li>"
            End If
            If trs("Stocks") - trs("OrderNum") <= 0 Then
                FoundErr = True
                ErrMsg = ErrMsg & "<li>实在对不起，此商品已经暂时售罄！</li>"
            End If
            If trs("ProductType") = 4 Then
                FoundErr = True
                ErrMsg = ErrMsg & "<li>促销商品不能直接购买！</li>"
            End If
        End If
        Set trs = Nothing
    End If
    If FoundErr = False Then
        Call AddToCart(CartID, ProductID, 1, 0)
    End If
Case "Clear"
    Call DelCart(CartID)
Case "Modify"
    Call ModifyCart
Case "Payment"
    Call ModifyCart
    Response.Redirect "Payment.asp"
End Select

If FoundErr = True Then
    Call WriteErrMsg(ErrMsg, ComeUrl)
    Response.End
End If

Response.Cookies("Cart" & Site_Sn)("CartID") = CartID
Response.Cookies("Cart" & Site_Sn).Expires = Date + 365

ProductList = SelectCart(CartID, 0)
PresentList = SelectCart(CartID, 1)

If ProductList = "" Then       '如果购物车为空，转入提示界面
    ShowTips_CartIsEmpty = XmlText_Class("ShowTips_CartIsEmpty", "<br><br><br><br>您好！目前您的购物车中没有任何商品，是不是<a href='ShowElite.asp'><font color='#FF0000'>继续购物</font></a>？<br><br><br><br><br><br>")
Else
    ShowTips_CartIsEmpty = ""
End If

strHtml = GetTemplate(ChannelID, 9, 0)
Call ReplaceCommon
Call ReplaceUserInfo

strHtml = Replace(strHtml, "{$ShowTips_Login}", ShowTips_Login)

strHtml = Replace(strHtml, "{$ShowTips_CartIsEmpty}", ShowTips_CartIsEmpty)
strHtml = Replace(strHtml, "{$ShowCart}", ShowCart())

Response.Write strHtml
Call CloseConn

Sub ModifyCart()
    If IsWholesale = 0 Then
        Call DelCart(CartID)
    End If
    If ProductID = "" Then
        Response.Write "请指定ProductID！"
        Exit Sub
    End If
    Dim arrProductList
    Dim arrPresentList
    Dim trs, i
    arrProductList = Split(ProductID, ",")
    For i = 0 To UBound(arrProductList)
        dblAmount = Request("Amount_" & Trim(arrProductList(i)))
        If dblAmount = "" Then
            dblAmount = 1
        Else
            dblAmount = PE_CDbl(dblAmount)
        End If
        If dblAmount <= 0 Then dblAmount = 1

        Set trs = Conn.Execute("select ProductID,Stocks,OrderNum,EnableSale,EnableSingleSell,DependentProducts from PE_Product where ProductID=" & arrProductList(i) & "")
        If Not (trs.BOF And trs.EOF) Then
            If (trs("EnableSale") = True) And (trs("EnableSingleSell") = True Or (trs("EnableSingleSell") = False And FoundInArr(trs("DependentProducts"), ProductID, ",") = True)) And (trs("Stocks") - trs("OrderNum")) > 0 Then
                Call AddToCart(CartID, arrProductList(i), dblAmount, 0)
            End If
        End If
        Set trs = Nothing
    Next
    arrPresentList = Split(PresentID, ",")
    For i = 0 To UBound(arrPresentList)
        Call AddToCart(CartID, arrPresentList(i), 0, 1)
    Next
End Sub
%>
