<!--- Tag Parameters --->
<CFPARAM NAME="ATTRIBUTES.Action" TYPE="string">


<!--- 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="">


<!--- This tag is being called with what ACTION? --->
<CFSWITCH EXPRESSION="#ATTRIBUTES.Action#">
  
  <!--- *** ACTION="Add" or ACTION="Update" *** --->
  <CFCASE VALUE="Add,Update">
    <!--- Tag attributes specific to this ACTION --->
    <CFPARAM NAME="ATTRIBUTES.MerchID" TYPE="numeric">
    <CFPARAM NAME="ATTRIBUTES.Quantity" TYPE="numeric" DEFAULT="1">

    <!--- Get position, if any, of MerchID in cart list --->
    <CFSET CurrentListPos = ListFind(CLIENT.CartMerchList, ATTRIBUTES.MerchID)>
    <!--- If this item *is not* already in cart, add it --->
    <CFIF CurrentListPos EQ 0>
      <CFSET CLIENT.CartMerchList = 
        ListAppend(CLIENT.CartMerchList, ATTRIBUTES.MerchID)>
      <CFSET CLIENT.CartQuantList = 
        ListAppend(CLIENT.CartQuantList, ATTRIBUTES.Quantity)>
    <!--- If item *is* already in cart, change its qty --->  
    <CFELSE>
      <!--- If Action="Add", add new Qty to existing --->
      <CFIF ATTRIBUTES.Action EQ "Add">
        <CFSET ATTRIBUTES.Quantity = 
          ATTRIBUTES.Quantity + ListGetAt(CLIENT.CartQuantList, CurrentListPos)>
      </CFIF>
      <!--- If new quantity is zero, remove item from cart --->
      <CFIF ATTRIBUTES.Quantity EQ 0>
        <CFSET CLIENT.CartMerchList = 
          ListDeleteAt(CLIENT.CartMerchList, CurrentListPos)>
        <CFSET CLIENT.CartQuantList = 
          ListDeleteAt(CLIENT.CartQuantList, CurrentListPos)>
      <!--- If new quantity not zero, update cart quantity --->  
      <CFELSE>
        <CFSET CLIENT.CartQuantList = 
          ListSetAt(CLIENT.CartQuantList, CurrentListPos, ATTRIBUTES.Quantity)>
      </CFIF>
    </CFIF>    
  </CFCASE>
  
  
  <!--- *** ACTION="Remove" *** --->
  <CFCASE VALUE="Remove">
    <!--- Tag attributes specific to this ACTION --->
    <CFPARAM NAME="ATTRIBUTES.MerchID" TYPE="numeric">
    <!--- Treat "Remove" action same as "Update" with Quant=0 --->
    <CF_ShoppingCart
      ACTION="Update"
      MerchID="#ATTRIBUTES.MerchID#"
      Quantity="0">
  </CFCASE>
  
  
  <!--- *** ACTION="Empty" *** --->
  <CFCASE VALUE="Empty">
    <CFSET CLIENT.CartMerchList = "">
    <CFSET CLIENT.CartQuantList = "">
  </CFCASE>
  
  
  <!--- *** ACTION="List" *** --->
  <CFCASE VALUE="List">
    <!--- Tag attributes specific to this ACTION --->
    <CFPARAM NAME="ATTRIBUTES.ReturnVariable" TYPE="variableName">

    <!--- Create a query, to return to calling template --->
    <CFSET q = QueryNew("MerchID,Quantity")>
    
    <!--- For each item in CLIENT lists, add row to query --->
    <CFLOOP FROM="1" TO="#ListLen(CLIENT.CartMerchList)#" INDEX="i">
      <CFSET QueryAddRow(q)>
      <CFSET QuerySetCell(q, "MerchID",  ListGetAt(CLIENT.CartMerchList, i))>
      <CFSET QuerySetCell(q, "Quantity", ListGetAt(CLIENT.CartQuantList, i))>
    </CFLOOP>
  
    <!--- Return query to calling template --->
    <CFSET "Caller.#ATTRIBUTES.ReturnVariable#" = q>
  </CFCASE>


  <!--- If an unknown ACTION was provided, display error --->
  <CFDEFAULTCASE>
     <CFTHROW 
       MESSAGE="Unknown ACTION passed to &lt;CF_ShoppingCart&gt;"
       DETAIL="Recognized ACTION values are <B>List</B>, <B>Add</B>, 
               <B>Update</B>, <B>Remove</B>, and <B>Empty</B>.">
  </CFDEFAULTCASE>
</CFSWITCH>