<!--- 
 Filename: StoreCart.cfm
 Created by: Nate Weiss (NMW)
 Purpose: Provides a simple shopping cart interface
--->

<!--- 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: 
  <cfinput type="Text" name="quant_#thisMerchID#" size="3" value="#thisQuant#">
  </cfoutput>
  </td>
  </tr>
</cfloop>
</table>
 
 <!--- Submit button to update quantities --->
 <cfinput type="submit" name="submit" value="Update Quantities">

 <!--- Submit button to Check out --->
 <cfinput type="Submit" value="Checkout" name="IsCheckingOut">
</cfform>
