#!/usr/local/bin/perl
print "Content-type: text/plain","\n\n";

# clothing_price.cgi

# This program is part of the CGI ordering form program for the Auburn University Bookstore's
# on-line catalog.  It takes the information from the catalog order form web page and outputs
# a "confirmation page" with the subtotal (after tax and shipping) and a form for further order information.  The
# program was completely written and coded by Kerry Boyce.  Any questions about the
# programming or anything else within the Auburn University Bookstore website may be
# addressed to:
#
#				boycekb@mail.auburn.edu

# INITIALIZE THE DATA

   # Read in values
      read(STDIN,$data,$ENV{'CONTENT_LENGTH'});

# temp to see the sent string
# print "Content-type: text/plain","\n\n";

open (MAILBOX,"| mail boycekb\@mail.auburn.edu") ||
   die "Error: Can't start mail program - Please report this error to boycekb\@mail.aubrun.edu";
print MAILBOX $data;
close (MAILBOX);

   # Split into seperate name/value pairs
      @data= split(/&/,$data);
   # Translate the data
      foreach $i (0..$#data) {
      # Convert pluses to spaces
         $data[$i] =~ s/\+/ /g;
      # Split into key and value at the first"="
         ($key,$val) = split(/=/,$data[$i],2);
      # Convert %XX from hex numbers to alphanumeric symbols
         $key =~ s/%(..)/pack("c",hex($1))/ge;
         $val =~ s/%(..)/pack("c",hex($1))/ge;
      # Associate the 'key' and 'value' using \0 to seperate multiple values
         $data{$key} .= \0 if (defined($data{$key}));
         $data{$key} .= $val;
      }
   #set the variables into separate arrays
      # set $item
         $item[1] = $data{item1};
         $item[2] = $data{item2};
         $item[3] = $data{item3};
         $item[4] = $data{item4};
         $item[5] = $data{item5};
         $item[6] = $data{item6};
         $item[7] = $data{item7};
      # shorten $item entries
         foreach $i (1 .. 7) {
            if ($item[$i] eq "None") {
               undef ($item[$i]);
            }
            else {
               ($itemname[$i],$temp[$i]) = split (/\//,$item[$i],2);
               undef ($temp[$i]);
               $item[$i] = $itemname[$i];
               undef ($itemname[$i]);
            }
         }
      # set $quantity
         $quantity[1] = $data{quantity1};
         $quantity[2] = $data{quantity2};
         $quantity[3] = $data{quantity3};
         $quantity[4] = $data{quantity4};
         $quantity[5] = $data{quantity5};
         $quantity[6] = $data{quantity6};
         $quantity[7] = $data{quantity7};
      # set $color
         $color[1] = $data{color1};
         $color[2] = $data{color2};
         $color[3] = $data{color3};
         $color[4] = $data{color4};
         $color[5] = $data{color5};
         $color[6] = $data{color6};
         $color[7] = $data{color7};
      # set $size
         $size[1] = $data{size1};
         $size[2] = $data{size2};
         $size[3] = $data{size3};
         $size[4] = $data{size4};
         $size[5] = $data{size5};
         $size[6] = $data{size6};
         $size[7] = $data{size7};
      # Initialize the $subtotal variable
         $subtotal = 0;

# END OF DATA INITIALIZATION

# CALCULATE THE PRICE

   # Set price based on the item and size.

   foreach $i (1 .. $#item) {
      if ($item[$i] eq "2A: MV Sport&#174 Pro-Weave&#174 \"Auburn\" Sweatshirt") {
         if ($size[$i] eq "S" || $size[$i] eq "M" || $size[$i] eq "L" || $size[$i] eq "XL") {
            $price[$i] = 29.95;
         }
         elsif ($size[$i] eq "XXL") {
            $price[$i] = 31.95;
         }
         elsif ($size[$i] eq "3XL" || $size[$i] eq "4XL") {
            $price[$i] = 41.95;
         }
      }
      elsif ($item[$i] eq "2B: MV Sport&#174 Premium Weight Shorts") {
            $price[$i] = 16.95;
      }
      elsif ($item[$i] eq "2C: MV Sport&#174 Perfect Sweat.<sup><font size=-6>TM</font></sup>") {
         if ($size[$i] eq "S" || $size[$i] eq "M" || $size[$i] eq "L" || $size[$i] eq "XL") {
            $price[$i] = 42.95;
         }
         elsif ($size[$i] eq "XXL") {
            $price[$i] = 44.95;
         }
      }
      elsif ($item[$i] eq "2E: MV Sport&#174 Pro-Weave&#174 Hooded Sweatshirt") {
         if ($size[$i] eq "S" || $size[$i] eq "M" || $size[$i] eq "L" || $size[$i] eq "XL") {
            $price[$i] = 39.95;
         }
         elsif ($size[$i] eq "XXL") {
            $price[$i] = 41.95;
         }
      }
      elsif ($item[$i] eq "2F: MV Sport&#174 Classic Crew Sweatshirt") {
         if ($size[$i] eq "S" || $size[$i] eq "M" || $size[$i] eq "L" || $size[$i] eq "XL") {
            $price[$i] = 21.95;
         }
         elsif ($size[$i] eq "XXL") {
            $price[$i] = 23.95;
         }
      }
      elsif ($item[$i] eq "2FF: MV Sport&#174 T-shirt") {
         if ($size[$i] eq "S" || $size[$i] eq "M" || $size[$i] eq "L" || $size[$i] eq "XL") {
            $price[$i] = 11.95;
         }
         elsif ($size[$i] eq "XXL") {
            $price[$i] = 13.95;
         }
      }
      elsif ($item[$i] eq "3A: MV Sport&#174 Pro-Weave&#174 Heavyweight Sweatshirt") {
         if ($size[$i] eq "S" || $size[$i] eq "M" || $size[$i] eq "L" || $size[$i] eq "XL") {
            $price[$i] = 29.95;
         }
         elsif ($size[$i] eq "XXL") {
            $price[$i] = 31.95;
         }
         elsif ($size[$i] eq "3XL" || $size[$i] eq "4XL") {
            $price[$i] = 41.95;
         }
      }
      elsif ($item[$i] eq "3B:  MV Sport&#174 Pro-Weave&#174 T-shirt") {
         if ($size[$i] eq "S" || $size[$i] eq "M" || $size[$i] eq "L" || $size[$i] eq "XL") {
            $price[$i] = 11.95;
         }
         elsif ($size[$i] eq "XXL") {
            $price[$i] = 13.95;
         }
      }
      elsif ($item[$i] eq "3C: MV Sport&#174 Shorts") {
            $price[$i] = 16.95;
      }
      elsif ($item[$i] eq "3E: MV Sport&#174 \"Seal\" Pro-Weave&#174 Hooded Sweatshirt") {
         if ($size[$i] eq "S" || $size[$i] eq "M" || $size[$i] eq "L" || $size[$i] eq "XL") {
            $price[$i] = 39.95;
         }
         elsif ($size[$i] eq "XXL") {
            $price[$i] = 41.95;
         }
      }
      elsif ($item[$i] eq "3F: MV Sport&#174 \"Seal\" Pro-Weave&#174 Sweatshirt") {
         if ($size[$i] eq "S" || $size[$i] eq "M" || $size[$i] eq "L" || $size[$i] eq "XL") {
            $price[$i] = 29.95;
         }
         elsif ($size[$i] eq "XXL") {
            $price[$i] = 31.95;
         }
         elsif ($size[$i] eq "3XL" || $size[$i] eq "4XL") {
            $price[$i] = 41.95;
         }
      }
      elsif ($item[$i] eq "3G: MV Sport&#174 \"Seal\" T-Shirt") {
         if ($size[$i] eq "S" || $size[$i] eq "M" || $size[$i] eq "L" || $size[$i] eq "XL") {
            $price[$i] = 11.95;
         }
         elsif ($size[$i] eq "XXL") {
            $price[$i] = 13.95;
         }
      }
      elsif ($item[$i] eq "4A: MV Sport&#174 T-Shirt") {
         if ($size[$i] eq "S" || $size[$i] eq "M" || $size[$i] eq "L" || $size[$i] eq "XL") {
            $price[$i] = 11.95;
         }
         elsif ($size[$i] eq "XXL") {
            $price[$i] = 13.95;
         }
      }
      elsif ($item[$i] eq "4B: MV Sport&#174 Classic Crew Sweatshirt") {
         if ($size[$i] eq "S" || $size[$i] eq "M" || $size[$i] eq "L" || $size[$i] eq "XL") {
            $price[$i] = 23.95;
         }
         elsif ($size[$i] eq "XXL") {
            $price[$i] = 25.95;
         }
      }
      elsif ($item[$i] eq "4BB: MV Sport&#174 T-shirt") {
         if ($size[$i] eq "S" || $size[$i] eq "M" || $size[$i] eq "L" || $size[$i] eq "XL") {
            $price[$i] = 11.95;
         }
         elsif ($size[$i] eq "XXL") {
            $price[$i] = 13.95;
         }
      }
      elsif ($item[$i] eq "4C: MV Sport&#174 \"Athletics\" T-shirt") {
         if ($size[$i] eq "S" || $size[$i] eq "M" || $size[$i] eq "L" || $size[$i] eq "XL") {
            $price[$i] = 14.50;
         }
         elsif ($size[$i] eq "XXL") {
            $price[$i] = 16.50;
         }
      }
      elsif ($item[$i] eq "4D: MV Sport&#174 Shorts") {
            $price[$i] = 16.95;
      }
   # Figure price with quantity
      $quantprice[$i] = $quantity[$i] * $price[$i];
   # Figure total price
      $subtotal = $subtotal + $quantprice[$i];
   }

   # Figure the tax

      if ($data{statetax} eq "Yes") {
         $taxprice = $subtotal * .075;
      }
      else {
         $taxprice = 0;
      }

   # Figure the shipping cost

      if ($subtotal > 0 && $subtotal <= 15) {
         $shipping = 3;
      }
      elsif ($subtotal > 15 && $subtotal <= 25) {
         $shipping = 3.5;
      }
      elsif ($subtotal > 25 && $subtotal <= 50) {
         $shipping = 4.5;
      }
      elsif ($subtotal > 50 && $subtotal <= 75) {
         $shipping = 5;
      }
      elsif ($subtotal > 75 && $subtotal <= 100) {
         $shipping = 5.5;
      }
      elsif ($subtotal > 100) {
         $shipping = 6;
      }

   # Find total

      $total = $subtotal + $taxprice + $shipping;

   # Print out confirmation page

      # Print out header
      print "Content-type: text/html","\n";
      print "\n";

      print <<"end_of_header";
      <head>
      <title>Confirmation Page</title>
      </head>
      <body background="pics/ct.jpg" text="#ffffff" link="#fd7d00" alink="#fd7d00" vlink="#fd7d00">
      <img src="pics/catbut.jpg" align=left>
      <center>
      <h1>AU Bookstore On-Line Catalog</h1>
      </center>
      <br><br><br>
      <hr>
      <br>
      <font size=+2><b>Your current order is:</b></font><br>
      <center>
      <table align=center border=1>
      <tr align=center><th></th>
      <th>Item</th>
      <th>Quantity</th>
      <th>Size</th>
      <th>Color</th>
      <th>Cost Each</th>
      <th>Total Amount</th></tr>
end_of_header
      # Print out items with their prices
      foreach $i (1..$#item) {
         print "<tr align=center><td>Item <samp>#</samp> $i </td>";
         print "<td> $item[$i] </td>";
         print "<td> $quantity[$i] </td>";
         print "<td> $size[$i] </td>";
         print "<td> $color[$i] </td>";
         print "<td> $price[$i] </td>";
         print "<td> $quantprice[$i] </td></tr>";
      }
      # Print out totals, tax, and shipping
      print <<"end_of_pricing";
      <tr align=center><th></th><th></th><th></th><th></th><th></th>
      <th>Subtotal</th>
      <td> $subtotal </td></tr>
      <tr align=center><th></th><th></th><th></th><th></th><th></th>
      <th>AL Sales<br>Tax (7.5%)</th>
      <td><i> $taxprice </i></td></tr>
      <tr align=center><th></th><th></th><th></th><th></th><th></th>
      <th>Shipping</th>
      <td><i> $shipping </i></td></tr>
      <tr align=center><th></th><th></th><th></th><th></th><th></th>
      <th>Total</th>
      <th><i> $total </i></th></tr></table><br>
end_of_pricing
      # Print confirmation message
      print <<"end_of_message";
      Please check the information above.  If the information is 
      incorrect or you wish to change your order, return to the preceding 
      page to make corrections. &nbsp If the information is correct, fill 
      out the information below completely and press the "Send Order" 
      button to send your order. &nbsp Your order will be sent via e-mail to 
      the Auburn University Bookstore, after which you will be sent a 
      confirmation e-mail message. &nbsp If you do not receive a 
      confirmation message, please call or e-mail the bookstore and ask 
      about your order, or re-order and mention the problem in the message 
      box below.
      <p>
      <center>
      Phone #'s: <b>(334)-844-4241</b> or<br>
      Toll Free -- <b>1-(800)-880-0392</b><p>
      E-mail Address: <a href=\"mailto:hortopr\@mail.auburn.edu\"><b>hortopr\@mail.auburn.edu</b></a>
      <p>
      Please note that <b>all</b> merchandise is shipped UPS. &nbsp UPS will not deliver to a P.O. Box. &nbsp Please use a street address.
      <hr>
      <form>
      <table align=center border=1>
      <tr><th>Ordered By</th></tr>
      </table>
      <table border=0>
      <tr align=left><th>First Name: </th>
      <th colspan=2><input type=\"text\" name=\"orderfirst\" size=20 maxlength=20></th>
      <th></th>
      <th>Last Name: </th>
      <th><input type=\"text\" name=\"orderlast\" size=30 maxlength=30></th>
      <th></th></tr>
      <tr align=left><th>Street Address: </th>
      <th colspan=6><input type=\"text\" name=\"orderstreet\" size=70 maxlength=70></th></tr>
      <tr align=left><th>City: </th>
      <th><input type=\"text\" name=\"ordercity\" size=20 maxlength=20></th>
      <th></th><th></th>
end_of_message
      # Account for state tax information
      if ($data{statetax} eq "Yes") {
         print "<th>State: AL &nbsp &nbsp &nbsp </th>";
         print "<input type=\"hidden\" name=\"orderstate\" value=\"AL\">"
      }
      elsif ($data{statetax} eq "No") {
         print "<th>State: <select name=\"orderstate\">";
         print "<option>AK<option>AZ<option>AR<option>CA<option>CO<option>CT<option>DE<option>DC";
         print "<option>FL<option>GA<option>HI<option>ID<option>IL<option>IN<option>IA<option>KS";
         print "<option>KY<option>LA<option>ME<option>MD<option>MA<option>MI<option>MN<option>MS";
         print "<option>MO<option>MT<option>NE<option>NV<option>NH<option>NJ<option>NM<option>NY";
         print "<option>NC<option>ND<option>OH<option>OK<option>OR<option>PA<option>RI<option>SC";
         print "<option>SD<option>TN<option>TX<option>UT<option>VT<option>VA<option>WA<option>WV";
         print "<option>WI<option>WY</select></th>";
      }
      # Finish printing out order information
      print <<"end_of_address";
      <th align=center>Zip Code: <input type=\"text\" name=\"orderzip\" size=5 maxlength=5></th></tr>
      <tr align=left><th>E-mail Address: </th>
      <th colspan=6><input type=\"text\" name=\"email\" size=45 maxlength=45></th></tr></table>
      <br>
      <table align=center border=1>
      <tr><th>Send To</th></tr>
      </table>
      <table  border=0>
      <tr align=left><th>First Name: </th>
      <th colspan=2><input type=\"text\" name=\"sendfirst\" size=20 maxlength=20></th>
      <th></th>
      <th>Last Name: </th>
      <th><input type=\"text\" name=\"sendlast\" size=30 maxlength=30></th>
      <th></th></tr>
      <tr align=left><th>Street Address: &nbsp </th>
      <th colspan=6><input type=\"text\" name=\"sendstreet\" size=70 maxlength=70></th></tr>
      <tr align=left><th>City: </th>
      <th><input type=\"text\" name=\"sendcity\" size=20 maxlength=20></th>
      <th></th><th></th>
      <th>State: <select name=\"sendstate\">
      <option>AL<option>AK<option>AZ<option>AR<option>CA<option>CO<option>CT<option>DE<option>DC
      <option>FL<option>GA<option>HI<option>ID<option>IL<option>IN<option>IA<option>KS
      <option>KY<option>LA<option>ME<option>MD<option>MA<option>MI<option>MN<option>MS
      <option>MO<option>MT<option>NE<option>NV<option>NH<option>NJ<option>NM<option>NY
      <option>NC<option>ND<option>OH<option>OK<option>OR<option>PA<option>RI<option>SC
      <option>SD<option>TN<option>TX<option>UT<option>VT<option>VA<option>WA<option>WV
      <option>WI<option>WY</select></th>
      <th align=center>Zip Code: <input type=\"text\" name=\"sendzip\" size=5 maxlength=5></th></tr></table>
      <hr>
      <table align=center border=1>
      <tr align=left><th>Method Of Payment</th></tr>
      </table><br>
      <table border=0>
      <tr align=center><th><input type=\"radio\" name=\"card\" value=\"visa\">Visa</th>
      <th><input type=\"radio\" name=\"card\" value=\"mastercard\">MasterCard</th>
      <th><input type=\"radio\" name=\"card\" value=\"discover\">Discover</th></tr>
      <tr align=center>
      <th colspan=3><input type=\"radio\" name=\"card\" value=\"tigercub\">Tiger Cub Account</th></tr>
      <tr align=center><td colspan=3> Sorry, no C.O.D. orders accepted.</td></tr>
      </table>
      <table border=0>
      <tr></tr><tr></tr>
      <tr><th colspan=3><input type=\"radio\" name=\"private\" value=\"no\" checked=\"true\">\"I will supply my credit card account number and expiration date with my order.\"</th></tr>
      <tr></tr>
      <tr align=center><th> </th>
      <td>Account Number &nbsp <input type=\"text\" name=\"account\" size=16 maxlength=16></td>
      <td>Expiration Date &nbsp <input type=\"text\" name=\"expmonth\" size=2 maxlength=2>/
      <input type=\"text\" name=\"expyear\" size=2 maxlength=2></td></tr>
      <tr></tr><tr></tr>
      <tr><th colspan=3><input type=\"radio\" name=\"private\" value=\"yes\">\"I prefer to call in my credit card account number and expiration date.\"</th></tr>
      <tr align=center><td colspan=3><font size=-1>Note: Your order will <b>not</b> be processed until your credit card information has been received.</font></td></tr>
      </table>
      <hr>
      If you have any comments, special request, or questions about your order, please enter them in below.<br>
      <textarea wrap name=\"comments\" cols=80 rows=10></textarea><hr>
end_of_address
   # make the data sendable to the next cgi
   foreach $i (1 .. $#item) {
      $item[$i] =~ s/"/%22/g;
      $item[$i] =~ s/:/%3a/g;
      $item[$i] =~ s/</%3c/g;
      $item[$i] =~ s/>/%3e/g;
      $item[$i] =~ s/\\/%5c/g;
   }
   # Print out the rest of the page
   print <<"end_of_page";
      # Pass on variable values
      <input type="hidden" name="items" value="@item">
      <input type="hidden" name="quantities" value="@quantity">
      <input type="hidden" name="colors" value="@color">
      <input type="hidden" name="sizes" value="@size">
      <input type="hidden" name="prices" value="@price">
      <input type="hidden" name="quantityprices" value="@quantprice">
      <input type="hidden" name="subtotal" value="$subtotal">
      <input type="hidden" name="taxprice" value="$taxprice">
      <input type="hidden" name="shipping" value="$shipping">
      <input type="hidden" name="total" value="$total">
      <center>
      <table border=1 cellpadding=2>
      <tr align=center><td><input type="submit" value="Send Order"></td>
      <td> <input type="reset" value="Reset Form"></td></tr></table><br>
      </form>
      </body>
      </html>
end_of_page
