<!--- Tag attributes --->
<CFPARAM NAME="ATTRIBUTES.OrderID" TYPE="numeric">
<CFPARAM NAME="ATTRIBUTES.UseHTML" TYPE="boolean" DEFAULT="Yes">

<!--- Local variables --->
<CFSET ImgSrcPath  = "http://#CGI.HTTP_HOST#/ows/images">

<!--- Retrieve order information from database --->
<CFQUERY DATASOURCE="#REQUEST.Datasource#" NAME="GetOrder">
  SELECT 
    c.ContactID, c.FirstName, c.LastName, c.Email,
    o.OrderDate, o.ShipAddress, o.ShipCity, 
    o.ShipState, o.ShipZip, o.ShipCountry,
    oi.OrderQty, oi.ItemPrice,
    m.MerchName, m.ImageNameSmall,
    f.MovieTitle
  FROM 
    Contacts c,
    MerchandiseOrders o,
    MerchandiseOrdersItems oi, 
    Merchandise m,
    Films f
  WHERE 
    o.OrderID = #ATTRIBUTES.OrderID#
    AND c.ContactID = o.ContactID
    AND m.MerchID   = oi.ItemID
    AND o.OrderID   = oi.OrderID
    AND f.FilmID    = m.FilmID
  ORDER BY
    m.MerchName
</CFQUERY>

<!--- Display an error message if query returned no records --->
<CFIF GetOrder.RecordCount EQ 0>
  <CFTHROW
    MESSAGE="Failed to obtain order information."
    DETAIL="Either the Order ID was incorrect, or order has no detail records.">
<!--- Display an error message if email blank or not valid --->    
<CFELSEIF (GetOrder.Email does not contain "@") 
  OR (GetOrder.Email does not contain ".")>
  <CFTHROW
    MESSAGE="Failed to obtain order information."
    DETAIL="E-Mail addresses need to have an @ sign and at least one 'dot'.">
</CFIF>

<!--- Query the GetOrders query to find total $$ --->
<CFQUERY DBTYPE="Query" NAME="GetTotal">
  SELECT SUM(ItemPrice * OrderQty) AS OrderTotal
  FROM GetOrder
</CFQUERY>


<!--- *** If we are sending HTML-Formatted Email *** --->
<CFIF ATTRIBUTES.UseHTML>
  
  <!--- Send E-mail to the user --->
  <!--- Because of the GROUP attribute, the inner <CFOUTPUT> --->
  <!--- block will be repeated for each item in the order --->
  <CFMAIL
    QUERY="GetOrder"
    GROUP="ContactID"
    GROUPCASESENSITIVE="No"
    STARTROW="1"
    SUBJECT="Thanks for your order (Order number #ATTRIBUTES.OrderID#)"
    TO="""#FirstName# #LastName#"" <#Email#>"
    FROM="""Orange Whip Online Store"" <orders@orangewhipstudios.com>"
    TYPE="HTML">
  
    <HTML>
    <HEAD>
      <STYLE TYPE="text/css">
        BODY {font-family:sans-serif;font-size:12px;color:navy}
        TD   {font-size:12px}
        TH   {font-size:12px;color:white;
              background:navy;text-align:left}
      </STYLE>
    </HEAD>
    <BODY>
    
    <H2>Thank you for your Order</H2>
    
    <P><B>Thank you for ordering from 
    <A HREF="http://www.orangewhipstudios.com">Orange Whip Studios</A>.</B><BR>
    Here are the details of your order, which will ship shortly.
    Please save or print this E-mail for your records.<BR>
    
    <P>
    <STRONG>Order Number:</STRONG> #ATTRIBUTES.OrderID#<BR>
    <STRONG>Items Ordered:</STRONG> #RecordCount#<BR>
    <STRONG>Date of Order:</STRONG> 
    #DateFormat(OrderDate, "dddd, mmmm d, yyyy")#
    #TimeFormat(OrderDate)#<BR>
                   
    <TABLE>
      <CFOUTPUT>
        <TR VALIGN="top">
          <TH COLSPAN="2">
            #MerchName#
          </TH>
        </TR>
        <TR>  
          <TD>
            <!--- If there is an image available... --->
            <CFIF ImageNameSmall NEQ "">
              <IMG SRC="#ImgSrcPath#/#ImageNameSmall#"
                ALT="#MerchName#"
                WIDTH="50" HEIGHT="50" BORDER="0">
            </CFIF>  
          </TD>
          <TD>
            <EM>(in commemoration of the film "#MovieTitle#")</EM><BR>
            <STRONG>Price:</STRONG> #LSCurrencyFormat(ItemPrice)#<BR>
            <STRONG>Qty:</STRONG>   #OrderQty#<BR>&nbsp;<BR>
          </TD>
        </TR> 
      </CFOUTPUT>
    </TABLE>

    <P>Order Total: #LSCurrencyFormat(GetTotal.OrderTotal)#<BR>
    
    <P><STRONG>This order will be shipped to:</STRONG><BR>
    #FirstName# #LastName#<BR>
    #ShipAddress#<BR>
    #ShipCity#<BR>
    #ShipState# #ShipZip# #ShipCountry#<BR>
    
    <P>If you have any questions, please write back to us at
    <a href="orders@orangewhipstudios.com">orders@orangewhipstudios.com</a>, 
    or just reply to this email.<BR>
    </BODY>
    </HTML>
  </CFMAIL>


<!--- *** If we are NOT sending HTML-Formatted Email *** --->
<CFELSE>

<!--- We do not want ColdFusion to suppress whitespace here --->
<CFPROCESSINGDIRECTIVE SUPPRESSWHITESPACE="No">

<!--- Send E-mail to the user --->
<!--- Because of the GROUP attribute, the inner <CFOUTPUT> --->
<!--- block will be repeated for each item in the order --->
<CFMAIL
  QUERY="GetOrder"
  GROUP="ContactID"
  GROUPCASESENSITIVE="No"
  STARTROW="1"
  SUBJECT="Thanks for your order (Order number #ATTRIBUTES.OrderID#)"
  TO="""#FirstName# #LastName#"" <#Email#>"
  FROM="""Orange Whip Online Store"" <orders@orangewhipstudios.com>"
>Thank you for ordering from Orange Whip Studios.
Here are the details of your order, which will ship shortly.
Please save or print this E-mail for your records.

Order Number:  #ATTRIBUTES.OrderID#
Items Ordered: #RecordCount#
Date of Order: #DateFormat(OrderDate, "dddd, mmmm d, yyyy")#
               #TimeFormat(OrderDate)#
               
------------------------------------------------------
<CFOUTPUT>
#CurrentRow#. #MerchName# 
   (in commemoration of the film "#MovieTitle#")
   Price: #LSCurrencyFormat(ItemPrice)#
   Qty:   #OrderQty#
</CFOUTPUT>
------------------------------------------------------
Order Total: #LSCurrencyFormat(GetTotal.OrderTotal)#

This order will be shipped to:
#FirstName# #LastName#
#ShipAddress#
#ShipCity#
#ShipState# #ShipZip# #ShipCountry#

If you have any questions, please write back to us at
orders@orangewhipstudios.com, or just reply to this email.
</CFMAIL>

</CFPROCESSINGDIRECTIVE>

</CFIF>