<!--- Show header images, etc., for Online Store --->
<CFINCLUDE TEMPLATE="StoreHeader.cfm">

<!--- URL parameter for MerchID --->
<CFPARAM NAME="URL.AddMerchID" TYPE="string" DEFAULT="">

<!--- These two variables track MerchIDs / Quantities  --->
<!--- for items in user's cart (start with empty cart) --->
<CFPARAM NAME="CLIENT.CartMerchList" TYPE="string" DEFAULT="">
<CFPARAM NAME="CLIENT.CartQuantList" TYPE="string" DEFAULT="">

<!--- If MerchID was passed in URL --->
<CFIF IsNumeric(URL.AddMerchID)>
  <!--- Get position, if any, of MerchID in cart list --->
  <CFSET CurrentListPos=ListFind(CartMerchList, URL.AddMerchID)>
  <!--- If this item *is not* already in cart, add it --->
  <CFIF CurrentListPos EQ 0>
    <CFSET CLIENT.CartMerchList=ListAppend(CLIENT.CartMerchList, URL.AddMerchID)>
    <CFSET CLIENT.CartQuantList=ListAppend(CLIENT.CartQuantList, 1)>
  <!--- If item *is* already in cart, change its qty --->  
  <CFELSE>
    <CFSET CurrentQuant=ListGetAt(CLIENT.CartQuantList, CurrentListPos)>
    <CFSET UpdatedQuant=CurrentQuant + 1>
    <CFSET CLIENT.CartQuantList=ListSetAt(CLIENT.CartQuantList, CurrentListPos, UpdatedQuant)>
  </CFIF>

<!--- If no MerchID passed in URL --->  
<CFELSE>
  <!--- For each item currently in user's cart --->
  <CFLOOP FROM="1" TO="#ListLen(CLIENT.CartMerchList)#" INDEX="i">
    <CFSET ThisMerchID=ListGetAt(CLIENT.CartMerchList, i)>
    
    <!--- If FORM field exists for this item's Quant --->
    <CFIF IsDefined("FORM.Quant_#ThisMerchID#")>
      <!--- The FORM field value is the new quantity --->
      <CFSET NewQuant=FORM["Quant_#ThisMerchID#"]>
      <!--- If new quant is 0, remove item from cart --->
      <CFIF NewQuant EQ 0>
        <CFSET CLIENT.CartMerchList=ListDeleteAt(CLIENT.CartMerchList, i)>
        <CFSET CLIENT.CartQuantList=ListDeleteAt(CLIENT.CartQuantList, i)>
      <!--- Otherwise, Update cart with new quantity --->  
      <CFELSE>      
        <CFSET CLIENT.CartQuantList=ListSetAt(CLIENT.CartQuantList, i, NewQuant)>
      </CFIF>
    </CFIF>
  </CFLOOP>
  
  <!--- If user submitted form via "Checkout" button --->
  <CFIF IsDefined("FORM.IsCheckingOut")>
    <CFLOCATION URL="StoreCheckout.cfm">
  </CFIF>
</CFIF>

<!--- Stop here if user's cart is empty --->
<CFIF CLIENT.CartMerchList EQ "">
  There is nothing in your cart.
  <CFABORT>
</CFIF>

<!--- Create form that submits to this template --->
<CFFORM ACTION="#CGI.SCRIPT_NAME#">
  <TABLE>
    <TR>
      <TH COLSPAN="2" BGCOLOR="Silver">Your Shopping Cart</TH>
    </TR>
    <!--- For each piece of merchandise --->
    <CFLOOP FROM="1" TO="#ListLen(CLIENT.CartMerchList)#" INDEX="i">
      <CFSET ThisMerchID=ListGetAt(CLIENT.CartMerchList, i)>
      <CFSET ThisQuant=ListGetAt(CLIENT.CartQuantList, i)>
      <TR>
        <TD>
          <!--- Show this piece of merchandise --->
          <CF_MerchDisplay
            MerchID="#ThisMerchID#"
            ShowAddLink="No">
        </TD>  
        <TD>
          <!--- Display Quantity in Text entry field --->
          <CFOUTPUT>
            Quantity: 
            <INPUT TYPE="Text" 
              NAME="Quant_#ThisMerchID#" 
              SIZE="3" 
              VALUE="#ThisQuant#">
          </CFOUTPUT>
        </TD>
      </TR>
    </CFLOOP>
  </TABLE>
  
  <!--- Submit button to update quantities --->
  <INPUT TYPE="Submit" VALUE="Update Quantities">

  <!--- Submit button to Check out --->
  <INPUT TYPE="Submit" VALUE="Checkout" NAME="IsCheckingOut">
</CFFORM>