#!/usr/bin/perl
require "ustorekeeperoz-lib.pl";

#---------------------------------------------------------------------------
# uStorekeeper(tm) Online Shopping System - Runtime Script
# Copyright (c) 1999 - 2001, Microburst Technologies, Inc.
# All rights reserved.
#
# ustorekeeperoz.pl
# version 1.83 with CVN (CVC2, CVV2, or CID code)
#
# See the uStorekeeper User's Guide for assistance installing and using
# uStorekeeper.  Also visit our website at https://www.uburst.com
#
#---------------------------------------------------------------------------

#END#

# Separate some common parameters into key-value pairs.
&parse_form_data(*form_text);
$pCommand   = $form_text{'command'};
$pProductId = $form_text{'pid'};
$pPrevious  = $form_text{'previous_page'};
$lpcommand  = $form_text{'oid'};

# Perform some initial validations.
$pCommand = &validate_string($pCommand);
$lpcommand = &validate_string($lpcommand);
$pProductId =~ s/[^-_A-z0-9]//g;
$pPrevious = &validate_filename($pPrevious);
$pId = &get_id();
$pRtn = &get_rtn();

# Decipher the command and perform the desired operation.
if ($pCommand eq "view_cart")
{
   # Display the cart.
   &view_cart($pId,$pRtn,$pPrevious);
}
elsif ($pCommand eq "place_order")
{
   # Display the order form.
   &create_order_form($pId,$pRtn,$pPrevious);
}
elsif ($pCommand eq "review_order")
{
   # Display cart and order information.
   &review_order($pId,$pRtn,$pPrevious);
}
elsif ($pCommand eq "information")
{
   # Display help/information.
   &goto_page($pId,$pRtn,$gInformation_file,$pProductId);
}
elsif ($pCommand eq "Update Quantities")
{
   # Change the quantities and display the shopping cart.
   &change_quantities($pId);
   &view_cart($pId,$pRtn,$pPrevious);
}
elsif ($pCommand eq "Continue Shopping")
{
   # Referrer check.
   &validate_referral_page($runtime_script_url);

   # Go to the previous page.
   &goto_page($pId,$pRtn,$pPrevious,$pProductId);
}
elsif ($pCommand eq "Place Order")
{
   # Referrer check.
   &validate_referral_page($runtime_script_url);

   # Display the order form.
   &create_order_form($pId,$pRtn,$pPrevious);
}
elsif ($pCommand eq "goto")
{
   # Go to the appropriate page.
   $pFile = $form_text{'file'};
   $pFile = &validate_filename($pFile);
   &goto_page($pId,$pRtn,$pFile,$pProductId);
}
elsif ($pCommand eq "search")
{
   # Search.
   $pKeywords = $form_text{'keywords'};
   &search_categories($pId,$pRtn,$pKeywords);
}
elsif ($pCommand eq "add")
{
   # Add To Cart.
   &add_to_cart($pId,$pRtn,$pPrevious);
}
elsif ($pCommand eq "Authorize.Net")
{
   # Payment by Authorize.Net.
   &authorize_net_order();
}
elsif (($lpcommand =~ /\S/) && ($lpcommand ne ""))
{
   # Payment by LinkPoint.
   &linkpoint_order();
}
elsif ($pCommand eq "Credit Card")
{
   # Payment by Credit Card.
   &create_credit_card_form($pId,$pRtn,$pPrevious);
}
elsif ($pCommand eq "cc_order")
{
   # Complete Credit Card Payment.
   &credit_card_order($pId,$pRtn,$pPrevious);
}
elsif ($pCommand eq "Electronic Check")
{
   # Payment by Electronic Check.
   &create_electronic_check_form($pId,$pRtn,$pPrevious);
}
elsif ($pCommand eq "echeck_order")
{
   # Complete Electronic Check Payment.
   &electronic_check_order($pId,$pRtn,$pPrevious);
}
elsif ($pCommand eq "Print")
{
   # Payment by Print (Phone, Fax, Mail).
   &print_order($pId,$pRtn,$pPrevious);
}
elsif ($pCommand eq "Contact Me")
{
   # Payment by Contact Me.
   &contact_order($pId,$pRtn,$pPrevious);
}
else
{
   if (!(-e $gCategory_index_file))
   {
      &display_message("uStorekeeper Shopping System</H1>Copyright (c) 1999 - 2001; Microburst Technologies, Inc.",
                       "You must first login to the uStorekeeper manager script at<BR><A HREF=\"$secure_manager_script_url\">$secure_manager_script_url</A><BR>in order to create at least one product category.");
   }
   else
   {
      # Just display the first page.
      &display_first_store_page($pId,$pRtn);
   }
}


#---------------------------------------------------------
#
# START OF SUBROUTINES.
#
#
#---------------------------------------------------------

# Subroutine to get/create/set the customer id.
sub get_id
{
   local ( $customer_id, $cookie, $value, $random_number );

   $customer_id = $form_text{'id'};
   $customer_id =~ s/[^A-z0-9]//g;
   
   if (!($customer_id =~ /\S/))
   {
      # Try to read an existing sessionid cookie.
      foreach (split(/;/, $ENV{'HTTP_COOKIE'}))
      {
         ($cookie,$value) = split(/=/);
         if ($cookie eq "sessionid")
         {
            $customer_id = $value;
            $customer_id =~ s/[^A-z0-9]//g;
         }
      }
   }

   if ((!($customer_id =~ /\S/)) || (-e $data_directory . $customer_id . ".order"))
   {
      # Create a random sessionid.
      srand(time|$$);
      $random_number = int(rand(10000000));
      $customer_id = "C" . $random_number;      
      print "Set-cookie: sessionid=$customer_id\n";
   }
   
   return $customer_id;
}

# Subroutine to get/set the reference tracking number.
sub get_rtn
{
   local ( $tracking_id, $cookie_id, $cookie, $value );

   # Get the tracking id from the POST or GET.
   $tracking_id = $form_text{'rtn'};
   $tracking_id =~ s/[^-._A-z0-9]//g;

   $cookie_id = "NONE";
   foreach (split(/; /, $ENV{'HTTP_COOKIE'}))
   {
      ($cookie,$value) = split(/=/);
      if ($cookie eq "referid")
      {
         $cookie_id = $value;
         $cookie_id =~ s/[^-._A-z0-9]//g;
      }
   }

   if (!($tracking_id =~ /\S/))
   {
      if ($cookie_id ne "NONE")
      {
         $tracking_id = $cookie_id;
      }
   }
   else
   {
      if ($cookie_id eq "NONE")
      {
         local($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst,$expiration_string,$time_to_expire);
         local(@days) = ("Sun","Mon","Tue","Wed","Thu","Fri","Sat"); 
         local(@months) = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); 
         $time_to_expire = time + 604800;  # Expire in one week.
         ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($time_to_expire);
         $year += 1900;
         $expiration_string = "expires\=$days[$wday], $mday-$months[$mon]-$year 00:00:00 GMT";
    
         print "Set-cookie: referid=$tracking_id; $expiration_string\n";
      }
   }
   
   return $tracking_id;
}

# Subroutine to go to the appropriate page.
sub goto_page
{
   local ($customer_id, $tracking_number, $html_file, $product_id) = @_;
   local ($random_number, $category_file);

   # Validate parameters.
   $customer_id =~ s/[^A-z0-9]//g;
   $tracking_number =~ s/[^-._A-z0-9]//g;
   $html_file = &validate_filename($html_file);
   $product_id =~ s/[^-_A-z0-9]//g;

   # Check if category was NOTSELECTED.
   if ($html_file eq "NOTSELECTED")
   {
      &missing_input("Missing Category","Please select the category to <B>Go!</B> to.");
   }
   
   # SECURITY FIX
   if ((!($html_file =~ /.html$/)) && ($html_file =~ /\S/))
   {
      &missing_input("Invalid Category","Invalid Category");	
   } 
   
   # Check category file name.
   if (!($html_file =~ /\S/))
   {
      $html_file = "NONE";
   }
   elsif ($html_file eq "NOTSELECTED")
   {
      &missing_input("Missing Category","Please select the category to <B>Go!</B> to.");
   }    
   
   # Check product ID.
   if (!($product_id =~ /\S/))
   {
      $product_id = "NONE";
   }
  
   if ($html_file eq "NONE")
   {
      if ($product_id eq "NONE")
      {
         # No file and no product id was specified.
         &display_first_store_page($customer_id, $tracking_number);
      }
      else
      {
         # A product id but no file was specified.
         $category_file = &get_category_file($product_id);
         if ($category_file eq "NONE")
         {
            &display_first_store_page($customer_id, $tracking_number);
         }
         else
         {
            &display_product_page($customer_id, $tracking_number, $category_file, $product_id);
         }	
      }	   
   }
   else
   {
      if ($product_id eq "NONE")
      {
         # A file was specified with no product id.
         &display_store_page($customer_id, $tracking_number, $html_file);
      }
      else
      {    
         # A file and product id was specified.
         $category_file = &get_category_file($product_id);
         &display_product_page($customer_id, $tracking_number, $category_file, $product_id);
      }	
   }
}

# Subroutine to display the first category page.
sub display_first_store_page
{
   local ($customer_id,$tracking_number) = @_;
   local ($line, $category_name, $file_name );

   # Validate parameters (these checks will have already been performed above).
   $customer_id =~ s/[^A-z0-9]//g;
   $tracking_number =~ s/[^-._A-z0-9]//g;
   
   # Open category file for reading.
   open (CATEGORY_INDEX_FILE, $gCategory_index_file) ||
      &return_error("File Error","Unable to open category index file.");

   # Read the first line.
   $line = <CATEGORY_INDEX_FILE>;
   
   # Close the file.
   close(CATEGORY_INDEX_FILE);

   # Remove carriage return.
   chop($line);

   # Seperate the category name and the file name.
   ($category_name, $file_name) = split (/\|/, $line);
      
   # Create the HTML file name.
   $file_name =~ s/.txt/.html/;

   # Create the .html page.
   &display_store_page($customer_id,$tracking_number,$file_name);
}

# Subroutine to display a store page with id inserted.
sub display_store_page
{
   local ($customer_id, $tracking_number, $html_file) = @_;
   local ($line, $random_number);

   # Validate parameters (these checks will have already been performed above).
   $customer_id =~ s/[^A-z0-9]//g;
   $tracking_number =~ s/[^-._A-z0-9]//g;
   $html_file = &validate_filename($html_file);
   
   # SECURITY FIX
   if ((!($html_file =~ /.html$/)) || ($html_file =~ /\.\./) || ($html_file =~ /\//))
   {
      &missing_input("Invalid HTML Page","You have requested and invalid HTML page.");	
   } 
   
   # Open html file for reading.
   open (HTML_FILE, $data_directory . $html_file) ||
      &return_error("File Error","Unable to open $html_file");

   # Output .html page header.
   print "Content-type: text/html\n\n";

   # Read category file and create .html pages.
   while (<HTML_FILE>)
   {
      # Set $line equal to the last line read from file.
      $line = $_;

      # Check if this line contains the start of a form.
      if ($line =~ /FORM ACTION/i)
      {
         print $line;
         print "<INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"" . $customer_id . "\">";
         print "<INPUT TYPE=\"hidden\" NAME=\"rtn\" VALUE=\"" . $tracking_number . "\">";
         print "<INPUT TYPE=\"hidden\" NAME=\"previous_page\" VALUE=\"" . $html_file . "\">";
      }
      elsif ($line =~ /USTOREKEEPER CART TAG/)
      {     
         print "<a href=\"" . $runtime_script_url . "?command=view_cart&id=" . $customer_id . "&rtn=" . $tracking_number ."&previous_page=" . $html_file . "\">\n";
      }
      elsif ($line =~ /USTOREKEEPER ORDER TAG/)
      {     
         print "<a href=\"" . $secure_runtime_script_url . "?command=place_order&id=" . $customer_id . "&rtn=" . $tracking_number ."&previous_page=" . $html_file . "\">\n";
      }      
      elsif ($line =~ /USTOREKEEPER INFO TAG/)
      {     
         print "<a href=\"" . $runtime_script_url . "?command=information&id=" . $customer_id . "&rtn=" . $tracking_number ."&previous_page=" . $html_file . "\">\n";
      }      
      else
      {
      	 print $line;
      }
   }

   # Close file.
   close(HTML_FILE);

   exit;
}

# Subroutine to display a page with a specific product on it.
sub display_product_page
{
   local ($customer_id, $tracking_number, $category_file, $specified_id) = @_;
   local ($category_selections,
          $category_name,
          $category_description,
          $category_image,
          $background_image,
          $background_color,
          $text_color,
          $max_ppp,
          $top_html,
          $middle_html,
          $bottom_html,
          $product_id,
          $product_name,
          $product_description,
          $product_short_description,
          $product_notes,
          $product_keywords,
          $product_weight,
          $product_price,
          $product_taxable,
          $product_option1,
          $product_option2,
          $product_option3,
          $product_min_quantity,
          $product_max_quantity,
          $product_text_color,
          $product_text_label,
          $product_textarea_label,
          $product_image,
          $product_image2,
          $product_layout,
          $product_top_html,
          $product_bottom_html,
          $line, 
          @key_value_pairs, 
          $key_value,
          $start_html,
          $product_html,
          $end_html,
          $view_cart_image,
          $place_order_image,
          $information_image);

   # Validate parameters (these checks will have already been performed above).
   $customer_id =~ s/[^A-z0-9]//g;
   $tracking_number =~ s/[^-._A-z0-9]//g;
   $category_file = &validate_filename($category_file);
   $specified_id =~ s/[^-_A-z0-9]//g;

   # Get general settings.
   &initialize_settings();
   $view_cart_image = $settings[84];
   $place_order_image = $settings[85];
   $information_image = $settings[86];
   
   # Read the layout file.
   &initialize_layout();

   # Open file for reading.
   open(CATEGORY_DATA_FILE, $data_directory . $category_file) ||
      &return_error("File Error","Unable to open $category_file");

   # Read the first line.
   $line = <CATEGORY_DATA_FILE>;
   
   # Split line into key-value pairs.
   @key_value_pairs = split (/\|/, $line);
   $category_name = $key_value_pairs[0];
   $category_description = $key_value_pairs[1];
   $category_image = $key_value_pairs[2];
   $background_image = $key_value_pairs[3];
   $background_color = $key_value_pairs[4];
   $text_color = $key_value_pairs[5];
   $max_ppp = $key_value_pairs[6];
   $top_html = $key_value_pairs[7];
   $middle_html = $key_value_pairs[8];
   $bottom_html = $key_value_pairs[9];
   
   # Get the HTML for the start of the HTML page.
   $start_html = &get_start_html("DYNAMIC PROD",
                           $category_name,
                           $category_description,
                           $category_image,
                           $background_image,
                           $background_color,
                           $text_color,
                           $top_html,
                           $middle_html,
                           $bottom_html,
                           $view_cart_image,
                           $place_order_image,
                           $information_image);

   # Read listing file and write to temp file.
   while (<CATEGORY_DATA_FILE>)
   {
      # Set $line equal to the last line read from file.
      $line = $_;

      # Is this the product we're looking for?
      if ($line =~ /$specified_id/i)
      {
         # Split line into key-value pairs.
         @key_value_pairs = split (/\|/, $line);
         $product_id = $key_value_pairs[0];
         $product_name = $key_value_pairs[1];
         $product_description = $key_value_pairs[2];
         $product_short_description = $key_value_pairs[3];
         $product_notes = $key_value_pairs[4];
         $product_keywords = $key_value_pairs[5];
         $product_weight = $key_value_pairs[6];
         $product_price = $key_value_pairs[7];
         $product_taxable = $key_value_pairs[8];
         $product_option1 = $key_value_pairs[9];
         $product_option2 = $key_value_pairs[10];
         $product_option3 = $key_value_pairs[11];
         $product_min_quantity = $key_value_pairs[12];
         $product_max_quantity = $key_value_pairs[13];
         $product_text_color = $key_value_pairs[14];
         $product_text_label = $key_value_pairs[15];
         $product_textarea_label = $key_value_pairs[16];
         $product_image = $key_value_pairs[17];
         $product_image2 = $key_value_pairs[18];
         $product_layout = $key_value_pairs[19];
         $product_top_html = $key_value_pairs[20];
         $product_bottom_html = $key_value_pairs[21];
      
         # Get the formatted HTML for this product.
         $product_html = &get_product_html($category_file,
                                           $product_id,
                                           $product_name,
                                           $product_description,
                                           $product_short_description,
                                           $product_notes,
                                           $product_keywords,
                                           $product_weight,
                                           $product_price,
                                           $product_taxable,
                                           $product_option1,
                                           $product_option2,
                                           $product_option3,
                                           $product_min_quantity,
                                           $product_max_quantity,
                                           $product_text_color,
                                           $product_text_label,
                                           $product_textarea_label,
                                           $product_image,
                                           $product_image2,
                                           $product_top_html,
                                           $product_bottom_html,
                                           $product_layout,
                                           "ACTIVE",
                                           $text_color);
                                        
         # Get the HTML for the end of the HTML page.
         $end_html = &get_end_html("DYNAMIC PROD",
                                   $category_name,
                                   $category_description,
                                   $category_image,
                                   $background_image,
                                   $background_color,
                                   $text_color,
                                   $top_html,
                                   $middle_html,
                                   $bottom_html,
                                   $view_cart_image,
                                   $place_order_image,
                                   $information_image,
                                   $category_file);

         # Close file.
         close(CATEGORY_DATA_FILE);    
         
         # Output .html page header.
         print "Content-type: text/html\n\n";
         print $start_html;
         print "<BR>" . $product_html . "<BR>";
         print $end_html;
         
         exit;                             
      }
   }

   # Close file.
   close(CATEGORY_DATA_FILE);
   
   # Product was not found so display first category page.
   &display_first_store_page($customer_id, $tracking_number);     
}

# Subroutine to get the category file with the specified product.
sub get_category_file
{
   local ($product_id) = @_;
   local (@lines, $i, $cat_name, $cat_file);

   # Validate parameters (these checks will have already been performed above).
   $product_id =~ s/[^-_A-z0-9]//g;
   
   # Open configuration file for reading.
   open (CATEGORY_INDEX_FILE, $gCategory_index_file) ||
      &return_error("File Error","Unable to open category index file.");
      
   # Read the entire configuration file.
   @lines = <CATEGORY_INDEX_FILE>;
   
   # Close the configuration file.
   close(CATEGORY_INDEX_FILE);

   # Loop through category index data and check for the product id.
   for ($i = 0; $i <= $#lines; $i++)
   {
      # Get the category name and the category file name.
      ($cat_name,$cat_file) = split (/\|/, $lines[$i]);
      
      # Remove the carriage return.
      chop($cat_file);

      # Check if product is in this category.
      if (&check_for_duplicate_product($cat_file,$product_id))
      {
         # Return the category file name.
         return ($cat_file);
      }
   }
   
   return "NONE";
}

# Subroutine to search the categories for the specified keywords.
sub search_categories
{
   local ($customer_id,$tracking_number,$keywords) = @_;
   local (@lines,$i,$cat_name,$cat_file,$html_results,$temp_results,$mcount,$random_number,$view_cart_image,$place_order_image,$information_image,$default_background_image,$store_banner);

   # Referrer check.
   &validate_referral_page($runtime_script_url);

   # Validate parameters (these checks will have already been performed above).
   $customer_id =~ s/[^A-z0-9]//g;
   $tracking_number =~ s/[^-._A-z0-9]//g;
   $keywords =~ s/[^-._\s\&\|\'A-z0-9]//g;
   $keywords =~ tr/\&/ /;
   $keywords =~ tr/\|/ /;

   # Get general settings.
   &initialize_settings();
   $view_cart_image = $settings[84];
   $place_order_image = $settings[85];
   $information_image = $settings[86];
   $default_background_image = $settings[87];
   $store_banner = $settings[88];
   
   # Read the layout file.
   &initialize_layout();

   # Reset counter.
   $mcount = 0;
   
   # Open configuration file for reading.
   open (CATEGORY_INDEX_FILE, $gCategory_index_file) ||
      &return_error("File Error","Unable to open category index file.");
      
   # Read the entire configuration file.
   @lines = <CATEGORY_INDEX_FILE>;
   
   # Close the configuration file.
   close(CATEGORY_INDEX_FILE);

   # Get the starting HTML for the search results page.
   $html_results = &get_start_html("DYNAMIC MSG",
                                   "Search Results",
                                   "",
                                   $store_banner,
                                   $default_background_image,
                                   "",
                                   "",
                                   "",
                                   "",
                                   "",
                                   $view_cart_image,
                                   $place_order_image,
                                   $information_image);

   # Loop through category index data and re-create .html pages.
   for ($i = 0; $i <= $#lines; $i++)
   {
      # Get the category name and the category file name.
      ($cat_name,$cat_file) = split (/\|/, $lines[$i]);
      
      # Remove the carriage return.
      chop($cat_file);

      # Search the category.
      $temp_results = &search_file($customer_id, $tracking_number, $cat_file, $keywords);
      if ($temp_results ne "NONE")
      {
         $html_results .= "<FONT SIZE=+2 FACE=\"Arial\">";
         $html_results .= "<B>" . $cat_name . "</B>";
         $html_results .= "<BR></FONT><FONT SIZE=-1 FACE=\"Arial\"><BR>";
         $html_results .= $temp_results;
         $html_results .= "</FONT><BR><BR>";
         $mcount++;
      }
   }
   
   if ($mcount < 1)
   {
      $html_results .= "<I>No matches were found</I><BR><BR>";
   }

   # End the results page.
   $html_results .= &get_end_html("DYNAMIC MSG",
                                  "Search Results",
                                  "",
                                  $store_banner,
                                  $default_background_image,
                                  "",
                                  "",
                                  "",
                                  "",
                                  "",
                                  $view_cart_image,
                                  $place_order_image,
                                  $information_image,
                                  "");
   
   # Show results.
   print "Content-type: text/html\n\n";
   print $html_results;
   
   exit;        
}

# Subroutine to search a file for a specified product id.
sub search_file
{
   local ($customer_id, $tracking_number, $category_file, $keywords) = @_;
   local ($line,
          $search_data,
          $html_file, 
          @key_value_pairs, 
          $found_flag, 
          $return_html,
          $current_id,
          $current_name,
          $current_description,
          $current_short_description,
          $current_notes,
          $current_keywords,
          $page_counter,
          $product_counter,
          $suffix,
          $max_ppp);
   
   # Reset found flag.
   $found_flag = 0;
   
   # Reset the page counter.
   $page_counter = 1;
   
   # Reset product counter.
   $product_counter = 0;
   
   # Initialize return html.
   $return_html = "<OL>";
   
   # Initialize html file name.
   $category_file = &validate_filename($category_file);   
   $html_file = $category_file;
   $html_file =~ s/.txt/.html/;
   
   # Open category file for reading.
   if (!(open(CATEGORY_FILE, $data_directory . $category_file)))
   {
      &return_error("Unknown Category","Unable to open $category_file");
   }
   
   # Read the catagory information line.
   $line = <CATEGORY_FILE>;
   @key_value_pairs = split (/\|/, $line);
   $max_ppp = $key_value_pairs[6];
   
   # Read category file and locate specified product.
   while (<CATEGORY_FILE>)
   {
      # Set $line equal to the last line read from file.
      $line = $_;
     
      # Increment product counter.
      $product_counter++;
      
      # Change HTML page name?
      if ($product_counter > $max_ppp)
      {
      	 $page_counter++;
      	 $product_counter = 0;
         $html_file = $category_file;
         $suffix = $page_counter . ".html";
         $html_file =~ s/.txt/$suffix/;
      }

      # Split line into key-value pairs.
      @key_value_pairs = split (/\|/, $line);
      $current_id = $key_value_pairs[0];
      $current_name = $key_value_pairs[1];
      #$current_description = $key_value_pairs[2];
      #$current_short_description = $key_value_pairs[3];
      #$current_notes = $key_value_pairs[4];
      #$current_keywords = $key_value_pairs[5];
   
      # Check the line for the keywords.
      if ($line =~ /$keywords/i)
      {
         $return_html .= "<LI>";
         $return_html .= "<A HREF=\"" . $runtime_script_url . "?command=goto&id=" . $customer_id . "&rtn=" . $tracking_number . "&file=" . $html_file . "&pid=" . $current_id ."\">";
         $return_html .= $current_name . "</A></LI><BR><BR>";
         $found_flag = 1;         
      }
   }

   # Close file.
   close(CATEGORY_FILE);

   # There were no matches for this category, reset return html.
   if ($found_flag == 0)
   { 
      $return_html = "NONE";
   }
   else
   {
      $return_html .= "</OL>";
   }
   
   return ($return_html);
}

# Subroutine to add an item to the shopping cart.
sub add_to_cart
{
   local ($customer_id,$tracking_number,$previous_page) = @_;
   local ($file_name,
          $product_id,
          $product_name,
          $product_short_description,
          $product_weight,
          $product_price,
          $product_taxable,
          $product_option1,
          $product_option2,
          $product_option3,
          $product_text,
          $product_textarea,
          $product_min_quantity,
          $product_max_quantity,
          $product_quantity,
          $random_number,
          $price_check);

   # Referrer check.
   &validate_referral_page($runtime_script_url);

   # Validate parameters (these checks will have already been performed above).
   $customer_id =~ s/[^A-z0-9]//g;
   $tracking_number =~ s/[^-._A-z0-9]//g;
   $previous_page = &validate_filename($previous_page);

   # Get product information from last POST.
   $product_id = $form_text{'product_id'};
   $product_name = $form_text{'product_name'};
   $product_short_description = $form_text{'product_short_description'};
   $product_weight = $form_text{'product_weight'};
   $product_price = $form_text{'product_price'};
   $product_taxable = $form_text{'product_taxable'};
   $product_option1 = $form_text{'product_option1'};
   $product_option2 = $form_text{'product_option2'};
   $product_option3 = $form_text{'product_option3'};
   $product_text = $form_text{'product_text'};
   $product_textarea = $form_text{'product_textarea'};
   $product_min_quantity = $form_text{'product_min_quantity'};
   $product_max_quantity = $form_text{'product_max_quantity'};
   $product_quantity = $form_text{'product_quantity'};

   # Validate input.
   $product_id = &validate_string($product_id);
   $product_name = &validate_string($product_name);
   $product_short_description = &validate_description($product_short_description);
   $product_weight = &validate_string($product_weight);
   $product_price = &validate_string($product_price);
   $product_taxable = &validate_string($product_taxable);
   $product_option1 = &validate_string($product_option1);
   $product_option2 = &validate_string($product_option2);
   $product_option3 = &validate_string($product_option3);
   $product_text = &validate_string($product_text);
   $product_textarea = &validate_string($product_textarea);
   $product_min_quantity = &validate_string($product_min_quantity);
   $product_max_quantity = &validate_string($product_max_quantity);
   $product_quantity = &validate_string($product_quantity);
   if (!($product_quantity =~ /\S/))
   {
      $product_quantity = "0";
   }

   # Check the minimum quantity.
   if ($product_quantity < $product_min_quantity)
   {
      &missing_input("Invalid Quantity","The minimum quantity for this item is <B>" . $product_min_quantity . "</B>.  Please press BACK and adjust the quantity.");
   }
      
   # Check the maximum quantity.
   if (($product_max_quantity ne "NONE") && ($product_quantity > $product_max_quantity))
   {
      &missing_input("Invalid Quantity","The maximum quantity for this item is <B>" . $product_max_quantity . "</B>.  Please press BACK and adjust the quantity.");
   }

   # Check the price.
   if (!($product_price =~ /\,/))
   {
      $price_check = $product_price;
      $price_check = &strip_letters($price_check);
      $product_price = $price_check;
      if ($price_check < 0.0)
      {
         &missing_input("Invalid Price","Price must be greater than zero.  Please press BACK to continue.");
      }
   }

   # Validate customer id by removing all extra characters except letters and numbers.
   $customer_id =~ s/[^A-z0-9]//g;

   # Set file name based on the $customer_id.
   $file_name = $data_directory . $customer_id . ".cart";
   
   # Append to customer's cart file.
   if (open(CART_FILE, ">>" . $file_name))
   {
      # Write product information to customer's cart file.
      print CART_FILE $product_id . "|" .
                      $product_name . "|" .
                      $product_short_description . "|" .
                      $product_weight . "|" .
                      $product_price . "|" .
                      $product_taxable . "|" .
                      $product_option1 . "|" .
                      $product_option2 . "|" .
                      $product_option3 . "|" .
                      $product_text . "|" .
                      $product_textarea . "|" .
                      $product_min_quantity . "|" .
                      $product_max_quantity . "|" .
                      $product_quantity  . "\n";
       
      # Close file.
      close(CART_FILE);
   }
   else
   {
      # Indicate that the file was not opened.
      &return_error("File Error","Unable to open $customer_id cart file.");
   }
   
   # Display the cart.
   &view_cart($customer_id,$tracking_number,$previous_page);
}

# Subroutine to display the shopping cart.
sub view_cart
{
   local ($customer_id,$tracking_number,$previous_page) = @_;
   local ($start_html,$end_html,$cart_html,$view_cart_image,$place_order_image,$information_image,$default_background_image,$store_banner);

   # Validate parameters (these checks will have already been performed above).
   $customer_id =~ s/[^A-z0-9]//g;
   $tracking_number =~ s/[^-._A-z0-9]//g;
   $previous_page = &validate_filename($previous_page);

   # Get general settings.
   &initialize_settings();
   $view_cart_image = $settings[84];
   $place_order_image = $settings[85];
   $information_image = $settings[86];
   $default_background_image = $settings[87];
   $store_banner = $settings[88];
   
   # Read the layout file.
   &initialize_layout();

   # Get the starting HTML.
   $start_html = &get_start_html("DYNAMIC MSG",
                                 "Your Shopping Cart",
                                 "",
                                 $store_banner,
                                 $default_background_image,
                                 "",
                                 "",
                                 "",
                                 "",
                                 "",
                                 $view_cart_image,
                                 $place_order_image,
                                 $information_image);

   # Get the cart HTML.
   $cart_html = &get_cart_html($customer_id,$tracking_number,$previous_page);
   
   # Get the ending HTML.
   $end_html = &get_end_html("DYNAMIC MSG",
                             "Your Shopping Cart",
                             "",
                             $store_banner,
                             $default_background_image,
                             "",
                             "",
                             "",
                             "",
                             "",
                             $view_cart_image,
                             $place_order_image,
                             $information_image,
                             "");
   
   # Show results.
   print "Content-type: text/html\n\n";
   print $start_html;
   print $cart_html;
   print $end_html;
   
   exit;      
}

# Subroutine to calculate totals and display the shopping cart.
sub get_cart_html
{
   local ($customer_id,$tracking_number,$previous_page) = @_;
   local ($product_id,
          $product_name,
          $product_short_description,
          $product_weight,
          $product_price,
          $product_taxable,
          $product_option1,
          $product_option2,
          $product_option3,
          $product_text,
          $product_textarea,
          $product_min_quantity,
          $product_max_quantity,
          $product_quantity,
          $total_quantity,
          $price,
          $total, 
          $cart_total, 
          $file_name,
          @lines, 
          @key_value_pairs, 
          $i,          
          $formatted_price,
          $formatted_total, 
          $key_value,
          $key,
          $value,
          $cart_html,
          $cart_font,
          $cart_font_color,
          $cart_title_color,
          $cart_background_color);

   # Get general settings.
   &initialize_settings();
   $cart_font = $settings[76];
   $cart_font_color = $settings[77];
   $cart_title_color = $settings[78];
   $cart_background_color = $settings[79];
 
   # Validate parameters (these checks will have already been performed above).
   $customer_id =~ s/[^A-z0-9]//g;
   $tracking_number =~ s/[^-._A-z0-9]//g;
   $previous_page = &validate_filename($previous_page);

   # Set file name based on the $customer_id.
   $file_name = $data_directory . $customer_id . ".cart";

   # Reset Cart Total.
   $cart_total = 0.0;
   
   # Start .html page. 
   $cart_html = "<form action=\"" . $runtime_script_url . "\" method=\"POST\">";
   $cart_html .= "<input type=\"hidden\" name=\"id\" value=\"" . $customer_id . "\">";
   $cart_html .= "<input type=\"hidden\" name=\"rtn\" value=\"" . $tracking_number . "\">";
   $cart_html .= "<input type=\"hidden\" name=\"previous_page\" value=\"" . $previous_page . "\">";
   $cart_html .= "<TABLE BORDER=.5 WIDTH=100% CELLPADDING=3>";
   $cart_html .= "<TR>";
   $cart_html .= "<TH BGCOLOR=\"$cart_title_color\" WIDTH=12%><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><B>Id</TH>";
   $cart_html .= "<TH BGCOLOR=\"$cart_title_color\" WIDTH=17%><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><B>Name</TH>";
   $cart_html .= "<TH BGCOLOR=\"$cart_title_color\" WIDTH=35%><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><B>Description</TH>";
   $cart_html .= "<TH BGCOLOR=\"$cart_title_color\" WIDTH=13%><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><B>Price</TH>";
   $cart_html .= "<TH BGCOLOR=\"$cart_title_color\" WIDTH=10%><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><B>Quantity</TH>";
   $cart_html .= "<TH BGCOLOR=\"$cart_title_color\" WIDTH=13%><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><B>Total Cost</TH>";
   $cart_html .= "</TR>";
 
   # Open cart file for reading.
   if (open(CART_FILE, $file_name))
   {
      # Read the entire cart file.
      @lines = <CART_FILE>;
   
      # Close the cart file.
      close(CART_FILE);

      # Loop through cart, update totals, and generate html.
      for ($i = 0; $i <= $#lines; $i++)
      {
         # Split line into values.
         @key_value_pairs = split (/\|/, $lines[$i]);
         $product_id = $key_value_pairs[0];
         $product_name = $key_value_pairs[1];
         $product_short_description = $key_value_pairs[2];
         $product_weight = $key_value_pairs[3];
         $product_price = $key_value_pairs[4];
         $product_taxable = $key_value_pairs[5];
         $product_option1 = $key_value_pairs[6];
         $product_option2 = $key_value_pairs[7];
         $product_option3 = $key_value_pairs[8];
         $product_text = $key_value_pairs[9];
         $product_textarea = $key_value_pairs[10];
         $product_min_quantity = $key_value_pairs[11];
         $product_max_quantity = $key_value_pairs[12];
         $product_quantity = $key_value_pairs[13];
      
         # Get the total quantity of this product id from cart.
         $total_quantity = &get_product_quantity($customer_id,$product_id);
      
         # Set the product price and total.
         $price = &get_product_price($product_option1,
                                     $product_option2,
                                     $product_option3,
                                     $total_quantity,
                                     $product_price);

         $price = &strip_letters($price);
         $total = $price * $product_quantity;
   
         # Update Cart Total.
         $cart_total += $total;
      
         # Format the price and total.
         $formatted_price = sprintf("%.2f",$price);
         if ($product_taxable =~ /no/i)
         {
            $formatted_total = sprintf("%.2f *",$total);
         }
         else
         {
            $formatted_total = sprintf("%.2f",$total);
         }
      
         # Add line to cart table.
         $cart_html .= "<TR>";
         $cart_html .= "<TD BGCOLOR=\"$cart_background_color\" ALIGN=CENTER><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><A HREF=\"" . $runtime_script_url . "?command=goto&id=" . $customer_id . "&rtn=" . $tracking_number . "&previous_page=" . $previous_page . "&pid=" . $product_id . "\">" . $product_id . "</A></TD>";
         $cart_html .= "<TD BGCOLOR=\"$cart_background_color\" ALIGN=LEFT><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\">" . $product_name . "</TD>";
         $cart_html .= "<TD BGCOLOR=\"$cart_background_color\" ALIGN=LEFT><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\">";
         if (($product_short_description =~ /\S/) && ($product_short_description ne "NONE"))
         {
            $cart_html .= $product_short_description . " ";
         }
         if ($product_option1 =~ /\S/)
         {
            $cart_html .= $product_option1;
         }
         if ($product_option2 =~ /\S/)
         {
            $cart_html .= "; " . $product_option2;
         }
         if ($product_option3 =~ /\S/)
         {
         	 $cart_html .= "; " . $product_option3;
         }
         if (($product_text =~ /\S/) && ($product_text ne "NONE"))
         {
         	 $cart_html .= "<BR>Text: " . $product_text;
         }         
         if (($product_textarea =~ /\S/) && ($product_textarea ne "NONE"))
         {
         	 $cart_html .= "<BR>Text: " . $product_textarea;
         }         
         $cart_html .= "&nbsp;";
         $cart_html .= "</TD>";
         $cart_html .= "<TD BGCOLOR=\"$cart_background_color\"><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\">" . $currency_symbol . " " . $formatted_price . "</TD>";
         $cart_html .= "<TD BGCOLOR=\"$cart_background_color\" ALIGN=CENTER><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><INPUT TYPE=text NAME=\"Q" . $i . "\" value=\"" . $product_quantity . "\" size=4\"></TD>";
         $cart_html .= "<TD BGCOLOR=\"$cart_background_color\"><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\">" . $currency_symbol . " " . $formatted_total . "</TD>";
         $cart_html .= "</TR>";
      }
   }
   else
   {
      $cart_html .= "<TR>";
      $cart_html .= "<TD BGCOLOR=\"$cart_background_color\" COLSPAN=6><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><BR><B><i>Your shopping cart is empty.</i></B><BR><BR></TD>";
      $cart_html .= "</TR>";
   }   	
   
   # Add total cart table.
   $formatted_total = sprintf("%.2f",$cart_total);   
   $cart_html .= "<TR>";
   $cart_html .= "<TD BGCOLOR=\"$cart_background_color\" COLSPAN=5 ALIGN=RIGHT><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><B>Subtotal: </B></TD>";
   $cart_html .= "<TD BGCOLOR=\"$cart_background_color\"><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\">" . $currency_symbol . " " . $formatted_total . "</TD>";
   $cart_html .= "</TR>";
   $cart_html .= "</TABLE>";
   
   $cart_html .= "<CENTER>";
   $cart_html .= "<BR><BR>";
   $cart_html .= "<TABLE BORDER=0 CELLPADDING=3 WIDTH=100%>";
   
   if (-e $file_name)
   {
      $cart_html .= "<TR><TD ALIGN=CENTER WIDTH=200 HEIGHT=60>";
      $cart_html .= "<FONT FACE=\"$cart_font\" size=2>";
      $cart_html .= "<input type=\"submit\" name=\"command\" value=\"Update Quantities\" WIDTH=190 HEIGHT=30>";
      $cart_html .= "</FORM>";
      $cart_html .= "</FONT></TD><TD WIDTH=350 VALIGN=CENTER>"; 
      $cart_html .= "<FONT FACE=\"$cart_font\" size=2>";
      $cart_html .= "Click here to change/update the quantities in the cart.";
      $cart_html .= "</FONT></TD></TR>";
   }
   else
   {
      $cart_html .= "</FORM>";
   }
   
   $cart_html .= "<TR><TD ALIGN=CENTER WIDTH=200 HEIGHT=60>";
   $cart_html .= "<FONT FACE=\"$cart_font\" size=2>";
   $cart_html .= "<FORM ACTION=\"" . $runtime_script_url . "\" METHOD=\"POST\">";
   $cart_html .= "<input type=\"hidden\" name=\"id\" value=\"" . $customer_id . "\">";
   $cart_html .= "<input type=\"hidden\" name=\"rtn\" value=\"" . $tracking_number . "\">";
   $cart_html .= "<input type=\"hidden\" name=\"previous_page\" value=\"" . $previous_page . "\">";
   $cart_html .= "<input type=\"submit\" name=\"command\" value=\"Continue Shopping\" WIDTH=190 HEIGHT=30>";
   $cart_html .= "</FORM>";
   $cart_html .= "</FONT></TD><TD WIDTH=350 VALIGN=CENTER>"; 
   $cart_html .= "<FONT FACE=\"$cart_font\" size=2>";
   $cart_html .= "Click here to continue shopping.";
   $cart_html .= "</FONT></TD></TR>";
   
   if (-e $file_name)
   {
      $cart_html .= "<TR><TD ALIGN=CENTER WIDTH=200 HEIGHT=60>";
      $cart_html .= "<FONT FACE=\"$cart_font\" size=2>";
      $cart_html .= "<FORM ACTION=\"" . $secure_runtime_script_url . "\" METHOD=\"POST\">";
      $cart_html .= "<input type=\"hidden\" name=\"id\" value=\"" . $customer_id . "\">";
      $cart_html .= "<input type=\"hidden\" name=\"rtn\" value=\"" . $tracking_number . "\">";
      $cart_html .= "<input type=\"hidden\" name=\"previous_page\" value=\"" . $previous_page . "\">";
      $cart_html .= "<input type=\"submit\" name=\"command\" value=\"Place Order\" WIDTH=190 HEIGHT=30>";
      $cart_html .= "</FORM>";
      $cart_html .= "</FONT></TD><TD WIDTH=350 VALIGN=CENTER>"; 
      $cart_html .= "<FONT FACE=\"$cart_font\" size=2>";
      $cart_html .= "Click here to place your order.";
      $cart_html .= "</FONT></TD></TR>";
      $cart_html .= "</TD></TR>";
   }
   
   $cart_html .= "</TABLE></CENTER>";
   $cart_html .= "<BR>";

   return ($cart_html);     
}

# Subroutine to determine the total quantity of a product
# with the specified id.
sub get_product_quantity
{
   local ($customer_id,$product_id) = @_;
   local ($search_id,$current_id,$current_quantity,$total_quantity,$file_name,@lines,@key_value_pairs,$i);

   # Set file name based on the $customer_id.
   $file_name = $data_directory . $customer_id . ".cart";

   # Reset total quanity.
   $total_quantity = 0;

   # Set the search ID to be just the first 3 characters of the specified product ID.
   if (length($product_id) > 3)
   {
      $search_id = substr($product_id,0,3);
   }
   else
   {
   	  $search_id = $product_id;
   }
   
   # Open cart file for reading.
   open (CART_FILE, $file_name) ||
      &return_error("File Error","Unable to open $customer_id cart file.");
      
   # Read the entire cart file.
   @lines = <CART_FILE>;
   
   # Close the cart file.
   close(CART_FILE);

   # Loop through cart and count total quantity of items with this id.
   for ($i = 0; $i <= $#lines; $i++)
   {
      @key_value_pairs = split (/\|/, $lines[$i]);
      $current_id = $key_value_pairs[0];
      $current_quantity = $key_value_pairs[13];

      if ($current_id =~ /^$search_id/i)
      {
         $total_quantity += $current_quantity;
      }
   }
   
   return ($total_quantity);
}

# Subroutine to determine the price of an item based on
# options and quantity.
sub get_product_price
{
   local ($option1,$option2,$option3,$quantity,$price) = @_;
   local ($option1_modifier,$option2_modifier,$option3_modifier,$price_value,@key_value_pairs,$key_value,$key,$value);

   # Check if there are any option price modifiers.
   $option1_modifier = 0.0;
   if ($option1 =~ /\+/)
   {
      ($key, $option1_modifier) = split (/\+/, $option1);
      $option1_modifier = &strip_letters($option1_modifier);
   }
   elsif ($option1 =~ /\-/)
   {
      ($key, $option1_modifier) = split (/\-/, $option1);
      $option1_modifier = &strip_letters($option1_modifier);
      $option1_modifier = $option1_modifier * (-1);
   }
   
   $option2_modifier = 0.0;
   if ($option2 =~ /\+/)
   {
      ($key, $option2_modifier) = split (/\+/, $option2);
      $option2_modifier = &strip_letters($option2_modifier);
   }
   elsif ($option2 =~ /\-/)
   {
      ($key, $option2_modifier) = split (/\-/, $option2);
      $option2_modifier = &strip_letters($option2_modifier);
      $option2_modifier = $option2_modifier * (-1);
   }

   $option3_modifier = 0.0;
   if ($option3 =~ /\+/)
   {
      ($key, $option3_modifier) = split (/\+/, $option3);
      $option3_modifier = &strip_letters($option3_modifier);
   }
   elsif ($option3 =~ /\-/)
   {
      ($key, $option3_modifier) = split (/\-/, $option3);
      $option3_modifier = &strip_letters($option3_modifier);
      $option3_modifier = $option3_modifier * (-1);
   }
   
   # Check if there are any quantity price modifiers.
   $price_value = "NONE";
   if ($price =~ /\@/) 
   {
      @key_value_pairs = split (/,/, $price);
      foreach $key_value (@key_value_pairs)
      {
         ($key, $value) = split (/\@/, $key_value);
         if ($key <= $quantity)
         {
         	$price_value = $value;
         }
      }
      
      if ($price_value eq "NONE")
      {
         # If a quantity was not matched, just use last price.
         $price_value = $value;
      }
   }
   else
   {
      $price_value = $price;
   }
   
   $price_value = &strip_letters($price_value);
   
   # Add option modifiers....if any.
   $price_value += $option1_modifier;
   $price_value += $option2_modifier;
   $price_value += $option3_modifier;
   
   return ($price_value);
}

# Subroutine to change the shopping cart quantities.
sub change_quantities
{
   local ($customer_id) = @_;
   local ($product_id,
          $product_name,
          $product_short_description,
          $product_weight,
          $product_price,
          $product_taxable,
          $product_option1,
          $product_option2,
          $product_option3,
          $product_text,
          $product_textarea,
          $product_min_quantity,
          $product_max_quantity,
          $product_quantity,
          $quantity_field,
          $new_quantity,
          $file_name,
          @key_value_pairs,
          $random_number,
          $counter);

   # Referrer check.
   &validate_referral_page($runtime_script_url);

   # Validate customer id by removing all extra characters except letters and numbers.
   $customer_id =~ s/[^A-z0-9]//g;

   # Set file name based on the $customer_id.
   $file_name = $data_directory . $customer_id . ".cart";

   # Check that customer has added items to the shopping cart.
   if (!(-e $file_name))
   {
      &missing_input("Shopping Cart Is Empty","Your shopping cart is empty.");
   }

   # Create a random temporary file name.
   srand(time|$$);
   $random_number = int(rand(10000000));
   $temp_file = $data_directory . "ustorekeeperoz" . $random_number . ".tmp";

   # Open cart file for reading.
   open (CART_FILE, $file_name) ||
      &return_error("File Error","Unable to open $customer_id cart file.");

   # Open temp file for writing.
   if (!(open(TEMP_FILE, ">" . $temp_file)))
   {
      # Close listing file.
      close(CART_FILE);
    
      # Indicate that the file was not opened.
      &return_error("File Error","Unable to open temporary file.");
   }

   # Reset counter.
   $counter = 0;
   
   # Read customer's cart file and write modified quantities to temp file.
   while (<CART_FILE>)
   {
      # Set $line equal to the last line read from file.
      $line = $_;
      
      # Split line into values.
      @key_value_pairs = split (/\|/, $line);
      $product_id = $key_value_pairs[0];
      $product_name = $key_value_pairs[1];
      $product_short_description = $key_value_pairs[2];
      $product_weight = $key_value_pairs[3];
      $product_price = $key_value_pairs[4];
      $product_taxable = $key_value_pairs[5];
      $product_option1 = $key_value_pairs[6];
      $product_option2 = $key_value_pairs[7];
      $product_option3 = $key_value_pairs[8];
      $product_text = $key_value_pairs[9];
      $product_textarea = $key_value_pairs[10];
      $product_min_quantity = $key_value_pairs[11];
      $product_max_quantity = $key_value_pairs[12];
      $product_quantity = $key_value_pairs[13];

      # Get the new quantity from last POST (Q0, Q1, Q2,...).
      $quantity_field = "Q" . $counter;
      $new_quantity = $form_text{$quantity_field};
      $new_quantity = &strip_letters($new_quantity);

      # Check the minimum quantity.
      if (($new_quantity > 0) && ($new_quantity < $product_min_quantity))
      {
         &missing_input("Invalid Quantity","The minimum quantity for this item is <B>" . $product_min_quantity . "</B>.  Please press BACK and adjust the quantity.");
      }
      
      # Check the maximum quantity.
      if (($product_max_quantity ne "NONE") && ($new_quantity > $product_max_quantity))
      {
         &missing_input("Invalid Quantity","The maximum quantity for this item is <B>" . $product_max_quantity . "</B>.  Please press BACK and adjust the quantity.");
      }

      # Increment counter.
      $counter++;
      
      # Write product information to temp cart file.
      if ($new_quantity > 0)
      {
         print TEMP_FILE $product_id . "|" .
                      $product_name . "|" .
                      $product_short_description . "|" .
                      $product_weight . "|" .
                      $product_price . "|" .
                      $product_taxable . "|" .
                      $product_option1 . "|" .
                      $product_option2 . "|" .
                      $product_option3 . "|" .
                      $product_text . "|" .
                      $product_textarea . "|" .
                      $product_min_quantity . "|" .
                      $product_max_quantity . "|" .
                      $new_quantity  . "\n";
      }
   }
   
   # Close file.
   close(CART_FILE);

   # Close temp file.
   close(TEMP_FILE);

   # Replace file with temp file.
   unlink($file_name);
   rename($temp_file,$file_name);
}

# Subroutine to create the order form.
sub create_order_form
{
   local ($customer_id, $tracking_number, $previous_page) = @_;
   local ($ord_html, 
          $secure_store_banner,
          $customer_first_name,
          $customer_last_name,
          $customer_street1,
          $customer_street2,
          $customer_city,
          $customer_state,
          $customer_zip_code,
          $customer_country,
          $customer_phone,
          $customer_fax,
          $customer_email,
          $customer_tax_rate,
          $customer_coupon_id,
          $customer_shipping_name,
          $customer_shipping_street1,
          $customer_shipping_street2,
          $customer_shipping_city,
          $customer_shipping_state,
          $customer_shipping_zip_code,
          $customer_shipping_country,
          $customer_shipping,
          $customer_giftwrap,
          $customer_gift_message,
          $customer_comments,
          $cookie, 
          $value,
          $cvalue,
          @key_value_pairs);

   # Validate parameters (these checks will have already been performed above).
   $customer_id =~ s/[^A-z0-9]//g;
   $tracking_number =~ s/[^-._A-z0-9]//g;
   $previous_page = &validate_filename($previous_page);

   # Check if a customer id has already been assigned.
   if (!($customer_id =~ /\S/))
   {
      &missing_input("Shopping Cart Is Empty","Please Add items to your shopping cart.");
   }    

   # Check that customer has added items to the shopping cart.
   if (!(-e $data_directory . $customer_id . ".cart"))
   {
      &missing_input("Shopping Cart Is Empty","Please Add items to your shopping cart.");
   }       	
   
   # Get general settings.
   &initialize_settings();
      
   # Decipher settings.
   $secure_store_banner = $settings[89];

   # Initialize customer information.
   $customer_first_name = "";
   $customer_last_name = "";
   $customer_street1 = "";
   $customer_street2 = "";
   $customer_city = "";
   $customer_state = "";
   $customer_zip_code = "";
   $customer_country = "";
   $customer_phone = "";
   $customer_fax = "";
   $customer_email = "";
   $customer_tax_rate = "";
   $customer_coupon_id = "";
   $customer_shipping_name = "";
   $customer_shipping_street1 = "";
   $customer_shipping_street2 = "";
   $customer_shipping_city = "";
   $customer_shipping_state = "";
   $customer_shipping_zip_code = "";
   $customer_shipping_country = "";
   $customer_shipping = "";
   $customer_giftwrap = "";
   $customer_gift_message = "";
   $customer_comments = "";

   # Get previous customer information (if any).
   if ($remember_customer =~ /yes/i)
   {
      # Get the customer cookie.
      foreach (split(/; /, $ENV{'HTTP_COOKIE'}))
      {
         ($cookie,$value) = split(/=/);
         if ($cookie eq "ustorekeeperoz_customer")
         {
            $cvalue = $value;
         }
      }
 
      if ($cvalue =~ /\S/)
      {
         # Split line into key-value pairs.
         @key_value_pairs = split (/\|/, $cvalue);
         $customer_first_name = $key_value_pairs[0];
         $customer_last_name = $key_value_pairs[1];
         $customer_street1 = $key_value_pairs[2];
         $customer_street2 = $key_value_pairs[3];
         $customer_city = $key_value_pairs[4];
         $customer_state = $key_value_pairs[5];
         $customer_zip_code = $key_value_pairs[6];
         $customer_country = $key_value_pairs[7];
         $customer_phone = $key_value_pairs[8];
         $customer_fax = $key_value_pairs[9];
         $customer_email = $key_value_pairs[10];
         $customer_tax_rate = $key_value_pairs[11];
         $customer_shipping_name = $key_value_pairs[12];
         $customer_shipping_street1 = $key_value_pairs[13];
         $customer_shipping_street2 = $key_value_pairs[14];
         $customer_shipping_city = $key_value_pairs[15];
         $customer_shipping_state = $key_value_pairs[16];
         $customer_shipping_zip_code = $key_value_pairs[17];
         $customer_shipping_country = $key_value_pairs[18];
         $customer_shipping = $key_value_pairs[19];
         
         # Validate fields just in case.
         $customer_first_name = &validate_string($customer_first_name);
         $customer_last_name = &validate_string($customer_last_name);
         $customer_street1 = &validate_string($customer_street1);
         $customer_street2 = &validate_string($customer_street2);
         $customer_city = &validate_string($customer_city);
         $customer_state = &validate_string($customer_state);
         $customer_zip_code = &validate_string($customer_zip_code);
         $customer_country = &validate_string($customer_country);
         $customer_phone = &validate_string($customer_phone);
         $customer_fax = &validate_string($customer_fax);
         $customer_email = &validate_email($customer_email);
         $customer_tax_rate = &validate_string($customer_tax_rate);
         $customer_shipping_name = &validate_string($customer_shipping_name);
         $customer_shipping_street1 = &validate_string($customer_shipping_street1);
         $customer_shipping_street2 = &validate_string($customer_shipping_street2);
         $customer_shipping_city = &validate_string($customer_shipping_city);
         $customer_shipping_state = &validate_string($customer_shipping_state);
         $customer_shipping_zip_code = &validate_string($customer_shipping_zip_code);
         $customer_shipping_country = &validate_string($customer_shipping_country);
         $customer_shipping = &validate_string($customer_shipping);
      }
   }
   
   # Output .html form.
   print "Content-type: text/html\n\n";
   print "<!--         The uStorekeeper Online Shopping System          -->\n";
   print "<!-- Copyright (c) 1999 - 2001, Microburst Technologies, Inc. -->\n";
   print "<!--                 https://www.uburst.com                    -->\n\n";
   print "<HTML><HEAD><TITLE>Placing Your Order</TITLE></HEAD>";
   print "<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"$orderform_font_color\">";
   print "<CENTER>";
   print "<IMG SRC=\"" . $secure_store_banner . "\">";
   print "<BR>";
   print "<FORM ACTION=\"" . $secure_runtime_script_url . "\" METHOD=\"POST\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"command\" VALUE=\"review_order\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"" . $customer_id . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"rtn\" VALUE=\"" . $tracking_number . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"previous_page\" VALUE=\"" . $previous_page . "\">";
   
   $ord_html = &get_order_form_html("ORDERFORM",
                                    $settings[51],
                                    $customer_first_name,
                                    $customer_last_name,
                                    $customer_street1,
                                    $customer_street2,
                                    $customer_city,
                                    $customer_state,
                                    $customer_zip_code,
                                    $customer_country,
                                    $customer_phone,
                                    $customer_fax,
                                    $customer_email,
                                    $customer_tax_rate,
                                    $customer_coupon_id ,
                                    $customer_shipping_name,
                                    $customer_shipping_street1,
                                    $customer_shipping_street2,
                                    $customer_shipping_city,
                                    $customer_shipping_state,
                                    $customer_shipping_zip_code,
                                    $customer_shipping_country,
                                    $customer_shipping,
                                    $customer_giftwrap,
                                    $customer_gift_message,
                                    $customer_comments);
   print $ord_html;
   
   print "<BR>";
   print "<CENTER>";
   print "<FONT FACE=\"$orderform_font\" COLOR=\"#000000\" size=2>";
   print "<INPUT TYPE=\"submit\" VALUE=\" Continue >>\">";
   print "</FONT>";
   print "</FORM>";
   print "</BODY></HTML>";
   
   exit;
}

#---------------------------------------------------------
# Subroutine to create the review order.
#---------------------------------------------------------
sub review_order
{
   local ($customer_id, $tracking_number, $previous_page) = @_;
   local ($ord_html,
          $tax_states,
          $tax_options,
          $shipping1_type,
          $shipping1_name,
          $shipping1_table,
          $shipping2_type,
          $shipping2_name,
          $shipping2_table,
          $shipping3_type,
          $shipping3_name,
          $shipping3_table,
          $shipping4_type,
          $shipping4_name,
          $shipping4_table,
          $shipping5_type,
          $shipping5_name,
          $shipping5_table,
          $handling_default,
          $handling_countries,
          $charge_per_giftbox,
          $global_discount,
          $coupon1_number,
          $coupon1_discount,
          $coupon2_number,
          $coupon2_discount,
          $coupon3_number,
          $coupon3_discount,
          $tax_shipping,
          $customer_first_name,
          $customer_last_name,
          $customer_street1,
          $customer_street2,
          $customer_city,
          $customer_state,
          $customer_zip_code,
          $customer_country,
          $customer_phone,
          $customer_fax,
          $customer_email,
          $customer_tax_rate,
          $customer_coupon_id,
          $customer_shipping_name,
          $customer_shipping_street1,
          $customer_shipping_street2,
          $customer_shipping_city,
          $customer_shipping_state,
          $customer_shipping_zip_code,
          $customer_shipping_country,
          $customer_shipping,
          $customer_giftwrap,
          $customer_gift_message,
          $customer_comments,
          $cart_html,
          $current_tax_rate,
          $current_discount_percent,
          $current_discount_fee,
          $coupon_discount,
          $subtotal1,
          $current_taxable_subtotal,
          $current_nontaxable_subtotal,
          $current_quantity,
          $current_giftwrap_cost,
          $subtotal2,
          $current_taxable_percent_discount,
          $current_nontaxable_percent_discount,
          $total_percent_discount,
          $subtotal3,
          $current_tax,
          $current_shipping,
          $current_weight,
          $current_handling,
          $current_shipping_handling,
          $current_total,
          $hidden_fields,
          @key_value_pairs,
          $tax_based_on,
          $determining_state,
          $cvalue);

   # Referrer check.
   &validate_referral_page($secure_runtime_script_url);

   # Validate parameters (these checks will have already been performed above).
   $customer_id =~ s/[^A-z0-9]//g;
   $tracking_number =~ s/[^-._A-z0-9]//g;
   $previous_page = &validate_filename($previous_page);
   
   # Check that customer has added items to the shopping cart.
   if (!(-e $data_directory . $customer_id . ".cart"))
   {
      &missing_input("Shopping Cart Is Empty","Please Add items to your shopping cart.");
   }       	

   # Get general settings.
   &initialize_settings();

   # Decipher general settings.
   $tax_states = $settings[18];
   $tax_options = $settings[19];
   $shipping1_type = $settings[22];
   $shipping1_name = $settings[23];
   $shipping1_table = $settings[24];
   $shipping2_type = $settings[25];
   $shipping2_name = $settings[26];
   $shipping2_table = $settings[27];
   $shipping3_type = $settings[28];
   $shipping3_name = $settings[29];
   $shipping3_table = $settings[30];
   $shipping4_type = $settings[31];
   $shipping4_name = $settings[32];
   $shipping4_table = $settings[33];
   $shipping5_type = $settings[34];
   $shipping5_name = $settings[35];
   $shipping5_table = $settings[36];
   $handling_default = $settings[37];
   $handling_countries = $settings[38];
   $charge_per_giftbox = $settings[40];
   $global_discount = $settings[42];
   $coupon1_number = $settings[44];
   $coupon1_discount = $settings[45];
   $coupon2_number = $settings[46];
   $coupon2_discount = $settings[47];
   $coupon3_number = $settings[48];
   $coupon3_discount = $settings[49];
   $tax_shipping = $settings[53];
   $tax_based_on = $settings[94];
   
   # Get customer's input.
   $customer_first_name = $form_text{'first_name'};
   $customer_last_name = $form_text{'last_name'};
   $customer_street1 = $form_text{'street1'};
   $customer_street2 = $form_text{'street2'};
   $customer_city = $form_text{'city'};
   $customer_state = $form_text{'state'};
   $customer_zip_code = $form_text{'zip_code'};
   $customer_country = $form_text{'country'};
   $customer_phone = $form_text{'phone'};
   $customer_fax = $form_text{'fax'};
   $customer_email = $form_text{'email'};
   $customer_tax_rate = $form_text{'tax_rate'};
   $customer_coupon_id = $form_text{'coupon_id'};
   $customer_shipping_name = $form_text{'shipping_name'};
   $customer_shipping_street1 = $form_text{'shipping_street1'};
   $customer_shipping_street2 = $form_text{'shipping_street2'};
   $customer_shipping_city = $form_text{'shipping_city'};
   $customer_shipping_state = $form_text{'shipping_state'};
   $customer_shipping_zip_code = $form_text{'shipping_zip_code'};
   $customer_shipping_country = $form_text{'shipping_country'};
   $customer_shipping = $form_text{'shipping_type'};
   $customer_giftwrap = $form_text{'giftwrap'};
   $customer_gift_message = $form_text{'gift_message'};
   $customer_comments = $form_text{'comments'};

   # Validate customers input.
   $customer_first_name = &validate_string($customer_first_name);
   $customer_last_name = &validate_string($customer_last_name);
   $customer_street1 = &validate_string($customer_street1);
   $customer_street2 = &validate_string($customer_street2);
   $customer_city = &validate_string($customer_city);
   $customer_state = &validate_string($customer_state);
   $determining_state = $customer_state;
   $customer_zip_code = &validate_string($customer_zip_code);
   $customer_country = &validate_string($customer_country);
   $customer_phone = &validate_string($customer_phone);
   $customer_fax = &validate_string($customer_fax);
   $customer_email = &validate_email($customer_email);
   $customer_tax_rate = &validate_string($customer_tax_rate);
   $customer_coupon_id = &validate_string($customer_coupon_id);
   $customer_shipping_name = &validate_string($customer_shipping_name);
   $customer_shipping_street1 = &validate_string($customer_shipping_street1);
   $customer_shipping_street2 = &validate_string($customer_shipping_street2);
   $customer_shipping_city = &validate_string($customer_shipping_city);
   $customer_shipping_state = &validate_string($customer_shipping_state);
   $customer_shipping_zip_code = &validate_string($customer_shipping_zip_code);
   $customer_shipping_country = &validate_string($customer_shipping_country);
   $customer_shipping = &validate_string($customer_shipping);
   $customer_giftwrap = &validate_string($customer_giftwrap);
   $customer_gift_message =~ s/[\<\>]//g;
   $customer_gift_message = &validate_description($customer_gift_message);
   $customer_comments =~ s/[\<\>]//g;
   $customer_comments = &validate_description($customer_comments);

   if (!($customer_first_name =~ /\S/))
   {
      &missing_input("Missing Input","Please enter your <B>*First Name</B>.");
   }
   if (!($customer_last_name =~ /\S/))
   {
      &missing_input("Missing Input","Please enter your <B>*Last Name</B>.");
   }
   if (!($customer_street1 =~ /\S/))
   {
      &missing_input("Missing Input","Please enter your <B>*Address</B>.");
   }
   if (!($customer_city =~ /\S/))
   {
      &missing_input("Missing Input","Please enter your <B>*City</B>.");
   }
   if (!($customer_zip_code =~ /\S/))
   {
      &missing_input("Missing Input","Please enter your <B>*Zip Code</B>.");
   }
   if (!($customer_country =~ /\S/))
   {
      &missing_input("Missing Input","Please enter your <B>*Country</B>.");
   }
   if (!($customer_phone =~ /\S/))
   {
      &missing_input("Missing Input","Please enter your <B>*Phone Number</B>.");
   }
   if (!($customer_email =~ /\S/))
   {
      &missing_input("Missing Input","Please enter your <B>*Email Address</B>.");
   }
   $current_discount_percent = 0.0;
   $current_discount_fee = 0.0;
   if (($global_discount =~ /\S/) && ($global_discount ne "NONE"))
   {
      if ($global_discount =~ /%/)
      {
         $current_discount_percent = $global_discount;
         $current_discount_percent =~ tr/%/ /;
      }
      else
      {
         $current_discount_fee = $global_discount;
         $current_discount_fee = &strip_letters($current_discount_fee);
      }
   }
   if (($customer_coupon_id =~ /\S/) && ($customer_coupon_id ne "NONE"))
   {
      if ($customer_coupon_id eq $coupon1_number)
      {
         $coupon_discount = $coupon1_discount;
      }
      elsif ($customer_coupon_id eq $coupon2_number)
      {
         $coupon_discount = $coupon2_discount;
      }
      elsif ($customer_coupon_id eq $coupon3_number)
      {
         $coupon_discount = $coupon3_discount;
      }
      else
      {
         $coupon_discount = "0.0";
         &missing_input("Invalid Coupon","The coupon that you entered is invalid.  Please check that you entered the correct coupon ID.");
      }
      
      if ($coupon_discount =~ /%/)
      {
      	$coupon_discount =~ tr/%/ /;
      	$current_discount_percent += $coupon_discount;
      }
      else
      {
      	$coupon_discount = &strip_letters($coupon_discount);
      	$current_discount_fee += $coupon_discount;
      }
   }
   if ($settings[20] =~ /yes/i)
   {
      if ($customer_shipping_name =~ /\S/)
      {
         if (!($customer_shipping_street1 =~ /\S/))
         {
            &missing_input("Missing Input","Please enter the <B>Ship To Address</B>.");
         }
         if (!($customer_shipping_city =~ /\S/))
         {
            &missing_input("Missing Input","Please enter the <B>Ship To City</B>.");
         }
         if (!($customer_shipping_zip_code =~ /\S/))
         {
            &missing_input("Missing Input","Please enter the <B>Ship To Zip Code</B>.");
         }
         if (!($customer_shipping_country =~ /\S/))
         {
            &missing_input("Missing Input","Please enter the <B>Ship To Country</B>.");
         }
         if ($tax_based_on =~ /shipping/i)
         {   
            $determining_state = $customer_shipping_state;
         }
      }
   }
   
   if ($tax_options =~ /,/)
   {
      @key_value_pairs = split (/,/, $tax_options);
      $current_tax_rate = $key_value_pairs[0];
   }
   else
   {
      $current_tax_rate = $tax_options;
   }
   $current_tax_rate =~ tr/%/ /;
   if (($tax_states =~ /\S/) && ($tax_states ne "NONE"))
   {
      # 02/26/00 - To be able to charge everyone in one state X% and everyone else 0%.
      if (!($tax_options =~ /,/))
      {
         $customer_tax_rate = $current_tax_rate;
         $current_tax_rate = 0.0;
      }

      if (($determining_state =~ /\S/) && ($determining_state ne "NONE"))
      {
         if (($tax_states =~ /^$determining_state/i) || ($tax_states =~ /,$determining_state/i))
         {
         	$current_tax_rate = $customer_tax_rate;
         	$current_tax_rate =~ tr/%/ /;
         	if ($current_tax_rate <= 0.0)
         	{
               if ($tax_based_on =~ /shipping/i)
               {
                  &missing_input("Missing Input","Please select the <B>Tax Rate</B> for the order destination.");
               }
               else
               {
                  &missing_input("Missing Input","Please select the <B>Tax Rate</B> for your area.");
               }
            }
         }
      }    
   }
   # 02/27/00 - Make sure $customer_tax_rate is equal to the current tax rate.
   $customer_tax_rate = $current_tax_rate;
  
   if ($settings[39] =~ /yes/i)
   {
      if ($customer_giftwrap =~ /yes/i)
      {
         if (!($customer_gift_message =~ /\S/))
         {
            &missing_input("Missing Input","Please enter your <B>Gift Message</B>.");
         }
      }
   }

   # Save customer information in a cookie.
   if ($remember_customer =~ /yes/i)
   {
      $cvalue = $customer_first_name . "|";
      $cvalue .= $customer_last_name . "|";
      $cvalue .= $customer_street1 . "|";
      $cvalue .= $customer_street2 . "|";
      $cvalue .= $customer_city . "|";
      $cvalue .= $customer_state . "|";
      $cvalue .= $customer_zip_code . "|";
      $cvalue .= $customer_country . "|";
      $cvalue .= $customer_phone . "|";
      $cvalue .= $customer_fax . "|";
      $cvalue .= $customer_email . "|";
      $cvalue .= $customer_tax_rate . "|";
      $cvalue .= $customer_shipping_name . "|";
      $cvalue .= $customer_shipping_street1 . "|";
      $cvalue .= $customer_shipping_street2 . "|";
      $cvalue .= $customer_shipping_city . "|";
      $cvalue .= $customer_shipping_state . "|";
      $cvalue .= $customer_shipping_zip_code . "|";
      $cvalue .= $customer_shipping_country . "|";
      $cvalue .= $customer_shipping . "|";
      
      local($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst,$expiration_string,$time_to_expire);
      local(@days) = ("Sun","Mon","Tue","Wed","Thu","Fri","Sat"); 
      local(@months) = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); 
      $time_to_expire = time + 63072000;  # Expire in two years.
      ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($time_to_expire);
      $year += 1900;
      $expiration_string = "expires\=$days[$wday], $mday-$months[$mon]-$year 00:00:00 GMT";
      print "Set-cookie: ustorekeeperoz_customer=$cvalue; $expiration_string\n";
   }

   # Get original subtotal.
   $subtotal1 = &get_subtotal($customer_id,0);

   # Get taxable subtotal.
   $current_taxable_subtotal = &get_subtotal($customer_id,1);
   
   # Get non-taxable subtotal.
   $current_nontaxable_subtotal = $subtotal1 - $current_taxable_subtotal;
   
   # Adjust totals for percent discount.
   $current_taxable_percent_discount = $current_taxable_subtotal * ($current_discount_percent / 100.0);
   $current_taxable_subtotal -= $current_taxable_percent_discount;
   $current_nontaxable_percent_discount = $current_nontaxable_subtotal * ($current_discount_percent / 100.0);
   $current_nontaxable_subtotal -= $current_nontaxable_percent_discount;
   $total_percent_discount = $subtotal1 * ($current_discount_percent / 100.0);
   $subtotal2 = $subtotal1 - $total_percent_discount;

   # Get the total quantity of items in the cart.
   $current_quantity = &get_total_quantity($customer_id);
   
   # Calculate the gift box total.
   $current_giftwrap_cost = 0.0;
   if ($customer_giftwrap =~ /yes/i)
   {
      $charge_per_giftbox = &strip_letters($charge_per_giftbox);
      $current_giftwrap_cost = $charge_per_giftbox * $current_quantity;
   }

   # Adjust subtotals for gift boxes.
   $subtotal3 = $subtotal2 + $current_giftwrap_cost;
   $current_taxable_subtotal += $current_giftwrap_cost;
   
   # Calculate Shipping.
   $current_shipping = 0.0;
   $current_weight = &get_weight($customer_id);
   if ($customer_shipping eq $shipping1_name)
   {
   	   $current_shipping = &get_shipping($shipping1_type,$shipping1_table,$current_quantity,$current_weight,$subtotal3);
   }
   elsif ($customer_shipping eq $shipping2_name)
   {
   	   $current_shipping = &get_shipping($shipping2_type,$shipping2_table,$current_quantity,$current_weight,$subtotal3);
   }
   elsif ($customer_shipping eq $shipping3_name)
   {
   	   $current_shipping = &get_shipping($shipping3_type,$shipping3_table,$current_quantity,$current_weight,$subtotal3);
   }
   elsif ($customer_shipping eq $shipping4_name)
   {
   	   $current_shipping = &get_shipping($shipping4_type,$shipping4_table,$current_quantity,$current_weight,$subtotal3);
   }
   elsif ($customer_shipping eq $shipping5_name)
   {
   	   $current_shipping = &get_shipping($shipping5_type,$shipping5_table,$current_quantity,$current_weight,$subtotal3);
   }
   	   
   # Calculate Handling.
   $current_handling = $handling_default;
   if (($handling_countries =~ /\S/) && ($handling_countries ne "NONE"))
   {
      if ($customer_shipping_country =~ /\S/)
      {
         $current_handling = &get_handling($handling_default,$handling_countries,$customer_shipping_country);
      }
      else
      {
         $current_handling = &get_handling($handling_default,$handling_countries,$customer_country);
      }
   }
   $current_handling = &strip_letters($current_handling);
   
   # Calculate Shipping & Handling.
   $current_shipping_handling = $current_shipping + $current_handling;

   # Adjust current_taxable_subtotal.
   if ($tax_shipping =~ /yes/i)
   {
      $current_taxable_subtotal += $current_shipping_handling;
   }
   
   # Calculate Tax.
   $current_tax = ($current_tax_rate / 100.0) * $current_taxable_subtotal;
     
   # Calculate Total.
   $current_total = $subtotal3 + $current_tax + $current_shipping_handling - $current_discount_fee;
   
   # Output .html form.
   print "Content-type: text/html\n\n";
   print "<!--         The uStorekeeper Online Shopping System          -->\n";
   print "<!-- Copyright (c) 1999 - 2001, Microburst Technologies, Inc. -->\n";
   print "<!--                 https://www.uburst.com                    -->\n\n";
   print "<HTML><HEAD><TITLE>Reviewing Your Order</TITLE></HEAD>";
   print "<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"$settings[81]\">";
   print "<CENTER>";
   print "<IMG SRC=\"" . $settings[89] . "\">";
   print "<BR><BR>";

   $ord_html = &get_order_form_html("REVIEWFORM",
                                    $settings[52],
                                    $customer_first_name,
                                    $customer_last_name,
                                    $customer_street1,
                                    $customer_street2,
                                    $customer_city,
                                    $customer_state,
                                    $customer_zip_code,
                                    $customer_country,
                                    $customer_phone,
                                    $customer_fax,
                                    $customer_email,
                                    $customer_tax_rate,
                                    $customer_coupon_id ,
                                    $customer_shipping_name,
                                    $customer_shipping_street1,
                                    $customer_shipping_street2,
                                    $customer_shipping_city,
                                    $customer_shipping_state,
                                    $customer_shipping_zip_code,
                                    $customer_shipping_country,
                                    $customer_shipping,
                                    $customer_giftwrap,
                                    $customer_gift_message,
                                    $customer_comments);
   print $ord_html;
   
   # Print Cart.
   print "<BR>";
   print "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=595>";
   print "<TR><TD>";
   $cart_html = &get_review_cart_html($customer_id,
                                      $subtotal1,
                                      $current_discount_percent,
                                      $subtotal2,
                                      $current_giftwrap_cost,
                                      $current_tax,
                                      $current_tax_rate,
                                      $current_shipping_handling,
                                      $tax_shipping,
                                      $current_discount_fee,
                                      $current_total);

   print $cart_html;
   print "</TD></TR></TABLE>";
   
   # Create hidden fields.
   $hidden_fields = "<INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"" . $customer_id . "\">";
   $hidden_fields .= "<INPUT TYPE=\"hidden\" NAME=\"rtn\" VALUE=\"" . $tracking_number . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"previous_page\" VALUE=\"" . $previous_page . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"final_subtotal1\" VALUE=\"" . $subtotal1 . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"final_discount_percent\" VALUE=\"" . $current_discount_percent . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"final_subtotal2\" VALUE=\"" . $subtotal2 . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"final_giftwrap_cost\" VALUE=\"" . $current_giftwrap_cost . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"final_tax\" VALUE=\"" . $current_tax . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"final_tax_rate\" VALUE=\"" . $current_tax_rate . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"final_shipping_handling\" VALUE=\"" . $current_shipping_handling . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"final_tax_shipping\" VALUE=\"" . $tax_shipping . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"final_discount_fee\" VALUE=\"" . $current_discount_fee . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"final_total\" VALUE=\"" . $current_total . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_first_name\" VALUE=\"" . $customer_first_name . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_last_name\" VALUE=\"" . $customer_last_name . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_street1\" VALUE=\"" . $customer_street1 . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_street2\" VALUE=\"" . $customer_street2 . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_city\" VALUE=\"" . $customer_city . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_state\" VALUE=\"" . $customer_state . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_zip_code\" VALUE=\"" . $customer_zip_code . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_country\" VALUE=\"" . $customer_country . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_phone\" VALUE=\"" . $customer_phone . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_fax\" VALUE=\"" . $customer_fax . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_email\" VALUE=\"" . $customer_email . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_tax_rate\" VALUE=\"" . $customer_tax_rate . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_coupon_id\" VALUE=\"" . $customer_coupon_id . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_name\" VALUE=\"" . $customer_shipping_name . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_street1\" VALUE=\"" . $customer_shipping_street1 . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_street2\" VALUE=\"" . $customer_shipping_street2 . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_city\" VALUE=\"" . $customer_shipping_city . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_state\" VALUE=\"" . $customer_shipping_state . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_zip_code\" VALUE=\"" . $customer_shipping_zip_code . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_country\" VALUE=\"" . $customer_shipping_country . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_type\" VALUE=\"" . $customer_shipping . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_giftwrap\" VALUE=\"" . $customer_giftwrap . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_gift_message\" VALUE=\"" . $customer_gift_message . "\">";
   $hidden_fields .=  "<INPUT TYPE=\"hidden\" NAME=\"customer_comments\" VALUE=\"" . $customer_comments . "\">";
   
   # Create payment options.
   print "<BR><BR>";
   print "<TABLE BORDER=0 CELLPADDING=4 WIDTH=575>";
   print "<TR><TD COLOR=\"#000000\" COLSPAN=2><HR><BR>";
   print "<FONT FACE=\"$settings[80]\" COLOR=\"#000000\" size=3>";
   print "<B>Please Select Payment Option Below:</B></FONT><BR><BR><BR></TD></TR>";
   if ($settings[68] =~ /yes/i)
   {
      print "<TR><TD ALIGN=CENTER VALIGN=TOP WIDTH=175 HEIGHT=100>";
      print "<FONT FACE=\"$settings[80]\" COLOR=\"#000000\" size=2>";
      print "<FORM ACTION=\"" . $settings[69] . "\" METHOD=\"POST\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Login\" VALUE=\"" . $settings[70] . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Cust_ID\" VALUE=\"" . $settings[71] . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_ADC_URL\" VALUE=\"" . $secure_runtime_script_url . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_ADC_Relay_Response\" VALUE=\"" . $x_ADC_Relay_Response . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Show_Form\" VALUE=\"" . $x_Show_Form . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Version\" VALUE=\"" . $x_Version . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Test_Request\" VALUE=\"" . $settings[73] . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Receipt_Link_Method\" VALUE=\"" . $x_Receipt_Link_Method . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Receipt_Link_URL\" VALUE=\"" . $secure_runtime_script_url . "\">";
      # NOT USED - print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_AVS_Filter\" VALUE=\"" . $settings[72] . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Invoice_Num\" VALUE=\"" . $customer_id . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Amount\" VALUE=\"" . $current_total . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_First_Name\" VALUE=\"" . $customer_first_name . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Last_Name\" VALUE=\"" . $customer_last_name . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Address\" VALUE=\"" . $customer_street1 . " " . $customer_street2 . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_City\" VALUE=\"" . $customer_city . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_State\" VALUE=\"" . $customer_state . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Zip\" VALUE=\"" . $customer_zip_code . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Country\" VALUE=\"" . $customer_country . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Phone\" VALUE=\"" . $customer_phone . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Fax\" VALUE=\"" . $customer_fax . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Ship_To_First_Name\" VALUE=\"" . $customer_shipping_name . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Ship_To_Address\" VALUE=\"" . $customer_shipping_street1 . " " . $customer_shipping_street2 . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Ship_To_City\" VALUE=\"" . $customer_shipping_city . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Ship_To_State\" VALUE=\"" . $customer_shipping_state . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Ship_To_Zip\" VALUE=\"" . $customer_shipping_zip_code . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Ship_To_Country\" VALUE=\"" . $customer_shipping_country . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Email_Customer\" VALUE=\"" . $x_Email_Customer . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Email\" VALUE=\"" . $customer_email . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Email_Merchant\" VALUE=\"" . $x_Email_Merchant . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"x_Merchant_Email\" VALUE=\"" . $settings[5] . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"USER1\" VALUE=\"return_cmd\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"USER2\" VALUE=\"" . $tracking_number . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"USER3\" VALUE=\"" . $customer_tax_rate . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"USER4\" VALUE=\"" . $customer_coupon_id . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"USER5\" VALUE=\"" . $customer_shipping . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"USER6\" VALUE=\"" . $customer_giftwrap . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"USER7\" VALUE=\"" . $customer_gift_message . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"USER8\" VALUE=\"" . $customer_comments . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"USER9\" VALUE=\"" . $subtotal1 . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"USER10\" VALUE=\"" . $current_discount_percent . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"USER11\" VALUE=\"" . $subtotal2 . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"USER12\" VALUE=\"" . $current_giftwrap_cost . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"USER13\" VALUE=\"" . $current_tax . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"USER14\" VALUE=\"" . $current_tax_rate . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"USER15\" VALUE=\"" . $current_shipping_handling . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"USER16\" VALUE=\"" . $tax_shipping . "\">";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"USER17\" VALUE=\"" . $current_discount_fee . "\">";
      print "<input type=\"submit\" name=\"command\" value=\"Authorize.Net\" WIDTH=170 HEIGHT=50>";
      print "</FORM>";
      print "</FONT></TD><TD WIDTH=400 VALIGN=TOP>"; 
      print "<FONT FACE=\"$settings[80]\" COLOR=\"#000000\" size=2>";
      print $settings[74]; 
      print "</FONT></TD></TR>";
   }
   if ($settings[90] =~ /yes/i)
   {
      print "<TR><TD ALIGN=CENTER VALIGN=TOP WIDTH=175 HEIGHT=100>";
      print "<FONT FACE=\"$settings[80]\" COLOR=\"#000000\" size=2>\n\n";
      print "<FORM ACTION=\"" . $settings[92] . "\" METHOD=\"POST\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"mode\" VALUE=\"payplus\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"chargetotal\" VALUE=\"$current_total\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"total\" VALUE=\"$current_total\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"storename\" VALUE=\"$settings[93]\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"zip\" VALUE=\"$customer_zip_code\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"addr\" VALUE=\"$customer_street1 $customer_street2\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"email\" VALUE=\"$customer_email\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"oid\" VALUE=\"$customer_id\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"bname\" VALUE=\"$customer_first_name $customer_last_name\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"baddr1\" VALUE=\"$customer_street1\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"baddr2\" VALUE=\"$customer_street2\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"bcity\" VALUE=\"$customer_city\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"bstate\" VALUE=\"$customer_state\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"bzip\" VALUE=\"$customer_zip_code\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"bcountry\" VALUE=\"$customer_country\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"sname\" VALUE=\"$customer_shipping_name\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"saddr1\" VALUE=\"$customer_shipping_street1\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"saddr2\" VALUE=\"$customer_shipping_street2\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"scity\" VALUE=\"$customer_shipping_city\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"sstate\" VALUE=\"$customer_shipping_state\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"szip\" VALUE=\"$customer_shipping_zip_code\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"scountry\" VALUE=\"$customer_shipping_country\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"phone\" VALUE=\"$customer_phone\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"fax\" VALUE=\"$customer_fax\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"shipping\" VALUE=\"$current_shipping_handling\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"tax\" VALUE=\"$current_tax\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"subtotal\" VALUE=\"$subtotal3\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"extra1\"  VALUE=\"" . $tracking_number . "\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"extra2\"  VALUE=\"" . $customer_tax_rate . "\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"extra3\"  VALUE=\"" . $customer_coupon_id . "\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"extra4\"  VALUE=\"" . $customer_shipping . "\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"extra5\"  VALUE=\"" . $customer_giftwrap . "\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"extra6\"  VALUE=\"" . $customer_gift_message . "\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"extra7\"  VALUE=\"" . $customer_comments . "\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"extra8\"  VALUE=\"" . $subtotal1 . "\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"extra9\"  VALUE=\"" . $current_discount_percent . "\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"extra10\" VALUE=\"" . $current_giftwrap_cost . "\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"extra11\" VALUE=\"" . $subtotal2 . "\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"extra12\" VALUE=\"" . $tax_shipping . "\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"extra13\" VALUE=\"" . $current_discount_fee . "\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"extra14\" VALUE=\"" . $current_shipping_handling . "\">\n";
      print "<INPUT TYPE=\"HIDDEN\" NAME=\"extra15\" VALUE=\"" . $current_tax . "\">\n";
      print "<input type=\"submit\" name=\"command\" value=\"LinkPoint\" WIDTH=170 HEIGHT=50>\n";
      print "</FORM>\n\n";
      print "</FONT></TD><TD WIDTH=400 VALIGN=TOP>"; 
      print "<FONT FACE=\"$settings[80]\" COLOR=\"#000000\" size=2>";
      print $settings[91]; 
      print "</FONT></TD></TR>";
   }
   if ($settings[7] =~ /yes/i)
   {
      print "<TR><TD ALIGN=CENTER VALIGN=TOP WIDTH=175 HEIGHT=100>";
      print "<FONT FACE=\"$settings[80]\" COLOR=\"#000000\" size=2>";
      print "<FORM ACTION=\"" . $secure_runtime_script_url . "\" METHOD=\"POST\">";
      print $hidden_fields;
      print "<input type=\"submit\" name=\"command\" value=\"Credit Card\" WIDTH=170 HEIGHT=50>";
      print "</FORM>";
      print "</FONT></TD><TD WIDTH=400 VALIGN=TOP>"; 
      print "<FONT FACE=\"$settings[80]\" COLOR=\"#000000\" size=2>";
      print $settings[9];
      print "</FONT></TD></TR>";
   }
   if ($settings[10] =~ /yes/i)
   {
      print "<TR><TD ALIGN=CENTER VALIGN=TOP WIDTH=175 HEIGHT=100>";
      print "<FONT FACE=\"$settings[80]\" COLOR=\"#000000\" size=2>";
      print "<FORM ACTION=\"" . $secure_runtime_script_url . "\" METHOD=\"POST\">";
      print $hidden_fields;
      print "<input type=\"submit\" name=\"command\" value=\"Electronic Check\" WIDTH=170 HEIGHT=50>";
      print "</FORM>";
      print "</FONT></TD><TD WIDTH=400 VALIGN=TOP>"; 
      print "<FONT FACE=\"$settings[80]\" COLOR=\"#000000\" size=2>";
      print $settings[12];
      print "</FONT></TD></TR>";
   }
   if ($settings[13] =~ /yes/i)
   {
      print "<TR><TD ALIGN=CENTER VALIGN=TOP WIDTH=175 HEIGHT=100>";
      print "<FONT FACE=\"$settings[80]\" COLOR=\"#000000\" size=2>";
      print "<FORM ACTION=\"" . $secure_runtime_script_url . "\" METHOD=\"POST\">";
      print $hidden_fields;
      print "<input type=\"submit\" name=\"command\" value=\"Print\" WIDTH=170 HEIGHT=50>";
      print "</FORM>";
      print "</FONT></TD><TD WIDTH=400 VALIGN=TOP>"; 
      print "<FONT FACE=\"$settings[80]\" COLOR=\"#000000\" size=2>";
      print $settings[14];
      print "</FONT></TD></TR>";
   }
   if ($settings[15] =~ /yes/i)
   {
      print "<TR><TD ALIGN=CENTER VALIGN=TOP WIDTH=175 HEIGHT=100>";
      print "<FONT FACE=\"$settings[80]\" COLOR=\"#000000\" size=2>";
      print "<FORM ACTION=\"" . $secure_runtime_script_url . "\" METHOD=\"POST\">";
      print $hidden_fields;
      print "<input type=\"submit\" name=\"command\" value=\"Contact Me\" WIDTH=170 HEIGHT=50>";
      print "</FORM>";
      print "</FONT></TD><TD WIDTH=400 VALIGN=TOP>"; 
      print "<FONT FACE=\"$settings[80]\" COLOR=\"#000000\" size=2>";
      print $settings[16];
      print "</FONT></TD></TR>";
   }
   print "</TD></TR></TABLE>";
   print "</BODY></HTML>";   
   exit;
}

# Subroutine to calculate the subtotal.
sub get_subtotal
{
   local ($customer_id,$tax_only) = @_;  # Note: Set $tax_only = 1 to get the taxable subtotal.
   local ($product_id,
          $product_price,
          $product_taxable,
          $product_option1,
          $product_option2,
          $product_option3,
          $product_quantity,
          $total_quantity,
          $price,
          $total, 
          $cart_total, 
          $file_name,
          @lines, 
          @key_value_pairs, 
          $i);

   # Set file name based on the $customer_id.
   $file_name = $data_directory . $customer_id . ".cart";

   # Reset Cart Total.
   $cart_total = 0.0;
   
   # Open cart file for reading.
   if (open(CART_FILE, $file_name))
   {
      # Read the entire cart file.
      @lines = <CART_FILE>;
   
      # Close the cart file.
      close(CART_FILE);

      # Loop through cart, update totals, and generate html.
      for ($i = 0; $i <= $#lines; $i++)
      {
         # Split line into values.
         @key_value_pairs = split (/\|/, $lines[$i]);
         $product_id = $key_value_pairs[0];
         $product_price = $key_value_pairs[4];
         $product_taxable = $key_value_pairs[5];
         $product_option1 = $key_value_pairs[6];
         $product_option2 = $key_value_pairs[7];
         $product_option3 = $key_value_pairs[8];
         $product_quantity = $key_value_pairs[13];
      
         # Get the total quantity of this product id from cart.
         $total_quantity = &get_product_quantity($customer_id,$product_id);
      
         # Set the product price and total.
         $price = &get_product_price($product_option1,
                                     $product_option2,
                                     $product_option3,
                                     $total_quantity,
                                     $product_price);

         $price = &strip_letters($price);
         $total = $price * $product_quantity;

         # Update Cart Total.
         if ($tax_only == 1)
         {
            if ($product_taxable =~ /yes/i)
            {
         	   $cart_total += $total;
            }
         }
         else
         {
         	$cart_total += $total;
         }
      }
   }

   return ($cart_total);   
}

# Subroutine to calculate the weight.
sub get_weight
{
   local ($customer_id) = @_;  
   local ($product_weight,$product_quantity,$cart_weight,$file_name,@lines,@key_value_pairs,$i);

   # Set file name based on the $customer_id.
   $file_name = $data_directory . $customer_id . ".cart";

   # Reset Cart Total.
   $cart_weight = 0.0;
   
   # Open cart file for reading.
   if (open(CART_FILE, $file_name))
   {
      # Read the entire cart file.
      @lines = <CART_FILE>;
   
      # Close the cart file.
      close(CART_FILE);

      # Loop through cart, update totals, and generate html.
      for ($i = 0; $i <= $#lines; $i++)
      {
         # Split line into values.
         @key_value_pairs = split (/\|/, $lines[$i]);
         $product_weight = $key_value_pairs[3];
         $product_quantity = $key_value_pairs[13];
      
         # Update weight.
         $cart_weight += ($product_weight * $product_quantity);
      }
   }

   return ($cart_weight);    
}

# Subroutine to calculate the total quantity of items.
sub get_total_quantity
{
   local ($customer_id) = @_;  
   local ($product_quantity,$cart_quantity,$file_name,@lines,@key_value_pairs,$i);

   # Set file name based on the $customer_id.
   $file_name = $data_directory . $customer_id . ".cart";

   # Reset Cart Total.
   $cart_quantity = 0;
   
   # Open cart file for reading.
   if (open(CART_FILE, $file_name))
   {
      # Read the entire cart file.
      @lines = <CART_FILE>;
   
      # Close the cart file.
      close(CART_FILE);

      # Loop through cart, update totals, and generate html.
      for ($i = 0; $i <= $#lines; $i++)
      {
         # Split line into values.
         @key_value_pairs = split (/\|/, $lines[$i]);
         $product_quantity = $key_value_pairs[13];
      
         # Update quantity.
         $cart_quantity += $product_quantity;
      }
   }

   return ($cart_quantity);    
}

# Subroutine to calculate the shipping.
sub get_shipping
{
   local ($shipping_type,$shipping_table,$quantity,$weight,$subtotal) = @_;  
   local ($shipping_total,@key_value_pairs,$key,$value,$compare_value,$i);

   # Reset Shipping.
   $shipping_total = 0.0;
   
   # Remove currency symbols from table.
 
   # 3/29/00 - Don't strip commas or equals signs here!
   # $shipping_table = &strip_letters($shipping_table);
   $shipping_table =~ s/[^0-9.,=]//g;
   $shipping_table =~ tr/$currency_symbol/ /;

   # Flat Fee.
   if ($shipping_type =~ /FLAT_FEE/i)
   {
      return ($shipping_table);
   }
   # Quantity.
   elsif ($shipping_type =~ /BY_QUANTITY/i)
   {
      $compare_value = $quantity;
   }
   # Weight.
   elsif ($shipping_type =~ /BY_WEIGHT/i)
   {
      $compare_value = $weight;
   }
   # Subtotal.
   elsif ($shipping_type =~ /BY_SUBTOTAL/i)
   {
      $compare_value = $subtotal;
   }

   # Check for per item basis.   
   if (!($shipping_table =~ /,/))
   {
      $shipping_total = $compare_value * $shipping_table;
      return ($shipping_total);
   }

   # Split table into values.
   @key_value_pairs = split (/,/, $shipping_table);
   for ($i = 0; $i <= $#key_value_pairs; $i++)
   {
      # Split pair into key and value.
      ($key,$value) = split (/=/, $key_value_pairs[$i]);
      $key = &strip_letters($key);
         
      if ($compare_value >= $key)
      {
         $value = &strip_letters($value);
         $shipping_total = $value;
      }
      else
      {
         return ($shipping_total);
      }
   }
   
   return ($shipping_total);    
}

# Subroutine to calculate the handling.
sub get_handling
{
   local ($handling_default,$handling_countries,$country) = @_;  
   local ($handling_total,@key_value_pairs,$key,$value,$i);

   # Reset Handling.
   $handling_total = $handling_default;
   
   # Split table into values.
   @key_value_pairs = split (/,/, $handling_countries);
   for ($i = 0; $i <= $#key_value_pairs; $i++)
   {
      # Split pair into key and value.
      # 10/30/00 Changed "\@" to "=" in the next line.
      ($key,$value) = split (/=/, $key_value_pairs[$i]);
         
      if ($country =~ /^$key/i)
      {
         $handling_total = $value;
      }
   }

   # Remove currency symbols from handling.
   $handling_total = &strip_letters($handling_total);
      
   return ($handling_total); 
}

# Subroutine to get the HTML to review the shopping cart.
sub get_review_cart_html
{
   local ($customer_id,
          $subtotal1,
          $discount_percent,
          $subtotal2,
          $giftwrap,
          $tax,
          $tax_rate,
          $shipping_handling,
          $tax_shipping_handling,
          $discount_fee,
          $total_amount) = @_;

   local ($product_id,
          $product_name,
          $product_short_description,
          $product_weight,
          $product_price,
          $product_taxable,
          $product_option1,
          $product_option2,
          $product_option3,
          $product_text,
          $product_textarea,
          $product_min_quantity,
          $product_max_quantity,
          $product_quantity,
          $total_quantity,
          $price,
          $total, 
          $file_name,
          @lines, 
          @key_value_pairs, 
          $i,          
          $formatted_price,
          $formatted_total,
          $formatted_output,
          $key_value,
          $key,
          $value,
          $cart_html,
          $cart_font,
          $cart_font_color,
          $cart_title_color,
          $cart_background_color);

   # Get general settings.
   &initialize_settings();
   $cart_font = $settings[76];
   $cart_font_color = $settings[77];
   $cart_title_color = $settings[78];
   $cart_background_color = $settings[79];

   # Set file name based on the $customer_id.
   $file_name = $data_directory . $customer_id . ".cart";

   # Start .html page. 
   $cart_html = "<TABLE BORDER=.5 WIDTH=100% CELLPADDING=3>";
   $cart_html .= "<TR>";
   $cart_html .= "<TH BGCOLOR=\"$cart_title_color\" WIDTH=12%><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><B>Id</TH>";
   $cart_html .= "<TH BGCOLOR=\"$cart_title_color\" WIDTH=17%><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><B>Name</TH>";
   $cart_html .= "<TH BGCOLOR=\"$cart_title_color\" WIDTH=35%><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><B>Description</TH>";
   $cart_html .= "<TH BGCOLOR=\"$cart_title_color\" WIDTH=13%><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><B>Price</TH>";
   $cart_html .= "<TH BGCOLOR=\"$cart_title_color\" WIDTH=10%><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><B>Quantity</TH>";
   $cart_html .= "<TH BGCOLOR=\"$cart_title_color\" WIDTH=13%><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><B>Total Cost</TH>";
   $cart_html .= "</TR>";
 
   # Open cart file for reading.
   if (open(CART_FILE, $file_name))
   {
      # Read the entire cart file.
      @lines = <CART_FILE>;
   
      # Close the cart file.
      close(CART_FILE);

      # Loop through cart, update totals, and generate html.
      for ($i = 0; $i <= $#lines; $i++)
      {
         # Split line into values.
         @key_value_pairs = split (/\|/, $lines[$i]);
         $product_id = $key_value_pairs[0];
         $product_name = $key_value_pairs[1];
         $product_short_description = $key_value_pairs[2];
         $product_weight = $key_value_pairs[3];
         $product_price = $key_value_pairs[4];
         $product_taxable = $key_value_pairs[5];
         $product_option1 = $key_value_pairs[6];
         $product_option2 = $key_value_pairs[7];
         $product_option3 = $key_value_pairs[8];
         $product_text = $key_value_pairs[9];
         $product_textarea = $key_value_pairs[10];
         $product_min_quantity = $key_value_pairs[11];
         $product_max_quantity = $key_value_pairs[12];
         $product_quantity = $key_value_pairs[13];
      
         # Get the total quantity of this product id from cart.
         $total_quantity = &get_product_quantity($customer_id,$product_id);
      
         # Set the product price and total.
         $price = &get_product_price($product_option1,
                                     $product_option2,
                                     $product_option3,
                                     $total_quantity,
                                     $product_price);

         $price = &strip_letters($price);
         $total = $price * $product_quantity;
   
         # Format the price and total.
         $formatted_price = sprintf("%.2f",$price);
         if ($product_taxable =~ /no/i)
         {
            $formatted_total = sprintf("%.2f *",$total);
         }
         else
         {
            $formatted_total = sprintf("%.2f",$total);
         }
      
         # Add line to cart table.
         $cart_html .= "<TR>";
         $cart_html .= "<TD BGCOLOR=\"$cart_background_color\" ALIGN=CENTER><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\">" . $product_id . "</TD>";
         $cart_html .= "<TD BGCOLOR=\"$cart_background_color\" ALIGN=LEFT><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\">" . $product_name . "</TD>";
         $cart_html .= "<TD BGCOLOR=\"$cart_background_color\" ALIGN=LEFT><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\">";
         if (($product_short_description =~ /\S/) && ($product_short_description ne "NONE"))
         {
            $cart_html .= $product_short_description . " ";
         }
         if ($product_option1 =~ /\S/)
         {
            $cart_html .= $product_option1;
         }
         if ($product_option2 =~ /\S/)
         {
            $cart_html .= "; " . $product_option2;
         }
         if ($product_option3 =~ /\S/)
         {
         	 $cart_html .= "; " . $product_option3;
         }
         if (($product_text =~ /\S/) && ($product_text ne "NONE"))
         {
         	 $cart_html .= "<BR>Text: " . $product_text;
         }         
         if (($product_textarea =~ /\S/) && ($product_textarea ne "NONE"))
         {
         	 $cart_html .= "<BR>Text: " . $product_textarea;
         }         
         $cart_html .= "&nbsp;";
         $cart_html .= "</TD>";
         $cart_html .= "<TD BGCOLOR=\"$cart_background_color\"><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\">" . $currency_symbol . " " . $formatted_price . "</TD>";
         $cart_html .= "<TD BGCOLOR=\"$cart_background_color\" ALIGN=CENTER><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\">" . $product_quantity . "</TD>";
         $cart_html .= "<TD BGCOLOR=\"$cart_background_color\"><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\">" . $currency_symbol . " " . $formatted_total . "</TD>";
         $cart_html .= "</TR>";
      }
   }
   else
   {
      $cart_html .= "<TR>";
      $cart_html .= "<TD BGCOLOR=\"$cart_background_color\" COLSPAN=6><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><BR><B><i>Your shopping cart is empty.</i></B><BR><BR></TD>";
      $cart_html .= "</TR>";
   }   	

   # Display subtotal.
   $formatted_output = sprintf("%.2f",$subtotal1);   
   $cart_html .= "<TR>";
   $cart_html .= "<TD BGCOLOR=\"$cart_background_color\" COLSPAN=5 ALIGN=RIGHT><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><B>Subtotal: </B></TD>";
   $cart_html .= "<TD BGCOLOR=\"$cart_background_color\"><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\">" . $currency_symbol . " " . $formatted_output . "</TD>";
   $cart_html .= "</TR>";
   # Display subtotal minus percent discount.
   if ($discount_percent > 0.0)
   {
      $formatted_output = sprintf("%.2f",$subtotal2);   
      $cart_html .= "<TR>";
      $cart_html .= "<TD BGCOLOR=\"$cart_background_color\" COLSPAN=5 ALIGN=RIGHT><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><B>Discounted Subtotal (-" . $discount_percent . "%): </B></TD>";
      $cart_html .= "<TD BGCOLOR=\"$cart_background_color\"><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\">" . $currency_symbol . " " . $formatted_output . "</TD>";
      $cart_html .= "</TR>";
   }
   # Display gift charge.
   if ($giftwrap > 0.0)
   {
      $formatted_output = sprintf("%.2f",$giftwrap);   
      $cart_html .= "<TR>";
      $cart_html .= "<TD BGCOLOR=\"$cart_background_color\" COLSPAN=5 ALIGN=RIGHT><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><B>Giftwrap Charge: </B></TD>";
      $cart_html .= "<TD BGCOLOR=\"$cart_background_color\"><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\">" . $currency_symbol . " " . $formatted_output . "</TD>";
      $cart_html .= "</TR>";
   }
   # Display shipping.
   if ($tax_shipping_handling =~ /yes/i)
   {
      $formatted_output = sprintf("%.2f",$shipping_handling);   
      $cart_html .= "<TR>";
      $cart_html .= "<TD BGCOLOR=\"$cart_background_color\" COLSPAN=5 ALIGN=RIGHT><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><B>Shipping & Handling: </B></TD>";
      $cart_html .= "<TD BGCOLOR=\"$cart_background_color\"><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\">" . $currency_symbol . " " . $formatted_output . "</TD>";
      $cart_html .= "</TR>";
   }
   # Display tax.
   $formatted_output = sprintf("%.2f",$tax);   
   $cart_html .= "<TR>";
   $cart_html .= "<TD BGCOLOR=\"$cart_background_color\" COLSPAN=5 ALIGN=RIGHT><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><B>Tax (" . $tax_rate . "%): </B></TD>";
   $cart_html .= "<TD BGCOLOR=\"$cart_background_color\"><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\">" . $currency_symbol . " " . $formatted_output . "</TD>";
   $cart_html .= "</TR>";
   # Display shipping.
   if (!($tax_shipping_handling =~ /yes/i))
   {
      $formatted_output = sprintf("%.2f",$shipping_handling);   
      $cart_html .= "<TR>";
      $cart_html .= "<TD BGCOLOR=\"$cart_background_color\" COLSPAN=5 ALIGN=RIGHT><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><B>Shipping & Handling: </B></TD>";
      $cart_html .= "<TD BGCOLOR=\"$cart_background_color\"><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\">" . $currency_symbol . " " . $formatted_output . "</TD>";
      $cart_html .= "</TR>";
   }
   # Display discount.
   if ($discount_fee > 0.0)
   {
      $formatted_output = sprintf("%.2f",$discount_fee);   
      $cart_html .= "<TR>";
      $cart_html .= "<TD BGCOLOR=\"$cart_background_color\" COLSPAN=5 ALIGN=RIGHT><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><B>Coupon Discount: </B></TD>";
      $cart_html .= "<TD BGCOLOR=\"$cart_background_color\"><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\">- " . $currency_symbol . " " . $formatted_output . "</TD>";
      $cart_html .= "</TR>";
   }
   # Display total.
   $formatted_output = sprintf("%.2f",$total_amount);   
   $cart_html .= "<TR>";
   $cart_html .= "<TD BGCOLOR=\"$cart_background_color\" COLSPAN=5 ALIGN=RIGHT><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\"><B>Total: </B></TD>";
   $cart_html .= "<TD BGCOLOR=\"$cart_background_color\"><FONT SIZE=-1 FACE=\"$cart_font\" COLOR=\"$cart_font_color\">" . $currency_symbol . " " . $formatted_output . "</TD>";
   $cart_html .= "</TR>";
   
   $cart_html .= "</TABLE>";

   return ($cart_html);   
}

# Subroutine to create the credit card form.
sub create_credit_card_form
{
   local ($customer_id, $tracking_number, $previous_page) = @_;
   local ($cc_options,
          $customer_first_name,
          $customer_last_name,
          $customer_street1,
          $customer_street2,
          $customer_city,
          $customer_state,
          $customer_zip_code,
          $customer_country,
          $customer_phone,
          $customer_fax,
          $customer_email,
          $customer_tax_rate,
          $customer_coupon_id,
          $customer_shipping_name,
          $customer_shipping_street1,
          $customer_shipping_street2,
          $customer_shipping_city,
          $customer_shipping_state,
          $customer_shipping_zip_code,
          $customer_shipping_country,
          $customer_shipping,
          $customer_giftwrap,
          $customer_gift_message,       
          $customer_comments,       
          $final_subtotal1,
          $final_discount_percent,
          $final_subtotal2,
          $final_giftwrap_cost,
          $final_tax,
          $final_tax_rate,
          $final_shipping_handling,
          $final_tax_shipping,
          $final_discount_fee,
          $final_total,
          $option_html,
          $orderform_font,
          $orderform_font_color,
          $orderform_title_color,
          $orderform_background_color,
          $secure_store_banner);

   # Referrer check.
   &validate_referral_page($secure_runtime_script_url);

   # Validate parameters.
   $customer_id =~ s/[^A-z0-9]//g;
   $tracking_number =~ s/[^-._A-z0-9]//g;
   $previous_page = &validate_filename($previous_page);

   # Get general settings.
   &initialize_settings();

   # Get general settings.
   $cc_options = $settings[8];
   $orderform_font = $settings[80];
   $orderform_font_color = $settings[81];
   $orderform_title_color = $settings[82];
   $orderform_background_color = $settings[83];
   $secure_store_banner = $settings[89];
   
   # Get personal info.
   $customer_first_name = $form_text{'customer_first_name'};
   $customer_last_name = $form_text{'customer_last_name'};
   $customer_street1 = $form_text{'customer_street1'};
   $customer_street2 = $form_text{'customer_street2'};
   $customer_city = $form_text{'customer_city'};
   $customer_state = $form_text{'customer_state'};
   $customer_zip_code = $form_text{'customer_zip_code'};
   $customer_country = $form_text{'customer_country'};
   $customer_phone = $form_text{'customer_phone'};
   $customer_fax = $form_text{'customer_fax'};
   $customer_email = $form_text{'customer_email'};
   $customer_tax_rate = $form_text{'customer_tax_rate'};
   $customer_coupon_id = $form_text{'customer_coupon_id'};
   $customer_shipping_name = $form_text{'customer_shipping_name'};
   $customer_shipping_street1 = $form_text{'customer_shipping_street1'};
   $customer_shipping_street2 = $form_text{'customer_shipping_street2'};
   $customer_shipping_city = $form_text{'customer_shipping_city'};
   $customer_shipping_state = $form_text{'customer_shipping_state'};
   $customer_shipping_zip_code = $form_text{'customer_shipping_zip_code'};
   $customer_shipping_country = $form_text{'customer_shipping_country'};
   $customer_shipping = $form_text{'customer_shipping_type'};
   $customer_giftwrap = $form_text{'customer_giftwrap'};
   $customer_gift_message = $form_text{'customer_gift_message'};
   $customer_comments = $form_text{'customer_comments'};

   # Get totals.
   $final_subtotal1 = $form_text{'final_subtotal1'};
   $final_discount_percent = $form_text{'final_discount_percent'};
   $final_subtotal2 = $form_text{'final_subtotal2'};
   $final_giftwrap_cost = $form_text{'final_giftwrap_cost'};
   $final_tax = $form_text{'final_tax'};
   $final_tax_rate = $form_text{'final_tax_rate'};
   $final_shipping_handling = $form_text{'final_shipping_handling'};
   $final_tax_shipping = $form_text{'final_tax_shipping'};
   $final_discount_fee = $form_text{'final_discount_fee'};
   $final_total = $form_text{'final_total'};
   
   # Get credit card option HTML.
   $option_html = &get_option_selections($cc_options,"cc_type");

   # Output .html form.
   print "Content-type: text/html\n\n";
   print "<!--         The uStorekeeper Online Shopping System          -->\n";
   print "<!-- Copyright (c) 1999 - 2001, Microburst Technologies, Inc. -->\n";
   print "<!--                 https://www.uburst.com                    -->\n\n";
   print "<HTML><HEAD><TITLE>Credit Card Order</TITLE></HEAD>";
   print "<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"$orderform_font_color\">";
   print "<CENTER>";
   print "<IMG SRC=\"" . $secure_store_banner . "\">";
   print "<BR><BR><BR><BR>";

   print "<FORM ACTION=\"" . $secure_runtime_script_url . "\" METHOD=\"POST\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"command\" VALUE=\"cc_order\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"" . $customer_id . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"rtn\" VALUE=\"" . $tracking_number . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"previous_page\" VALUE=\"" . $previous_page . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"final_subtotal1\" VALUE=\"" . $final_subtotal1 . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"final_discount_percent\" VALUE=\"" . $final_discount_percent . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"final_subtotal2\" VALUE=\"" . $final_subtotal2 . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"final_giftwrap_cost\" VALUE=\"" . $final_giftwrap_cost . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"final_tax\" VALUE=\"" . $final_tax . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"final_tax_rate\" VALUE=\"" . $final_tax_rate . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"final_shipping_handling\" VALUE=\"" . $final_shipping_handling . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"final_tax_shipping\" VALUE=\"" . $final_tax_shipping . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"final_discount_fee\" VALUE=\"" . $final_discount_fee . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"final_total\" VALUE=\"" . $final_total . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_first_name\" VALUE=\"" . $customer_first_name . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_last_name\" VALUE=\"" . $customer_last_name . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_street1\" VALUE=\"" . $customer_street1 . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_street2\" VALUE=\"" . $customer_street2 . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_city\" VALUE=\"" . $customer_city . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_state\" VALUE=\"" . $customer_state . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_zip_code\" VALUE=\"" . $customer_zip_code . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_country\" VALUE=\"" . $customer_country . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_phone\" VALUE=\"" . $customer_phone . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_fax\" VALUE=\"" . $customer_fax . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_email\" VALUE=\"" . $customer_email . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_tax_rate\" VALUE=\"" . $customer_tax_rate . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_coupon_id\" VALUE=\"" . $customer_coupon_id . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_name\" VALUE=\"" . $customer_shipping_name . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_street1\" VALUE=\"" . $customer_shipping_street1 . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_street2\" VALUE=\"" . $customer_shipping_street2 . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_city\" VALUE=\"" . $customer_shipping_city . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_state\" VALUE=\"" . $customer_shipping_state . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_zip_code\" VALUE=\"" . $customer_shipping_zip_code . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_country\" VALUE=\"" . $customer_shipping_country . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_type\" VALUE=\"" . $customer_shipping . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_giftwrap\" VALUE=\"" . $customer_giftwrap . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_gift_message\" VALUE=\"" . $customer_gift_message . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_comments\" VALUE=\"" . $customer_comments . "\">";
   print "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>";
   print "<TR BGCOLOR=#FFFFFF>";
   print "<TD BGCOLOR=$orderform_font_color>";
   print "<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=4 WIDTH=350>";
   print "<TR>";
   print "<TD BGCOLOR=$orderform_title_color ALIGN=LEFT>";
   print "<FONT COLOR=$orderform_font_color FACE=\"$orderform_font\" SIZE=3>";
   print "<b>Credit Card Information</b>";
   print "</TD>";
   print "</TR>";
   print "<TR>";
   print "<TD BGCOLOR=$orderform_background_color ALIGN=CENTER>";
   print "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=1>";
   print "<TR>";
   print "<TD ALIGN=RIGHT><FONT FACE=\"$orderform_font\" size=1>Credit Card Type </TD>";
   print "<TD><FONT FACE=\"$orderform_font\" SIZE=2>" . $option_html . "</TD>";
   print "</TR>";
   print "<TR>";
   print "<TD ALIGN=RIGHT><FONT FACE=\"$orderform_font\" size=1>Name On Card</TD>";
   print "<TD><FONT FACE=\"$orderform_font\" SIZE=2><INPUT TYPE=text SIZE=20 NAME=\"name_on_card\" VALUE=\"" . $customer_first_name . " " . $customer_last_name . "\"></TD>";
   print "</TR>";
   print "<TR>";
   print "<TD ALIGN=RIGHT><FONT FACE=\"$orderform_font\" size=1>Credit Card Number </TD>";
   print "<TD><FONT FACE=\"$orderform_font\" SIZE=2><INPUT TYPE=text SIZE=20 MAXLENGTH=20 NAME=\"ccn\"></TD>";
   print "</TR>";
   print "<TR>";
   print "<TD ALIGN=RIGHT><FONT FACE=\"$orderform_font\" size=1>CVN/CID Code <BR><I>(back of card)</I> </TD>";
   print "<TD><FONT FACE=\"$orderform_font\" SIZE=2><INPUT TYPE=text SIZE=10 MAXLENGTH=5 NAME=\"cvn\"></TD>";
   print "</TR>";
   print "<TR>";
   print "<TD ALIGN=RIGHT><FONT FACE=\"$orderform_font\" size=1>Expiration (MMYY)</TD>";
   print "<TD><FONT FACE=\"$orderform_font\" SIZE=2><INPUT TYPE=text SIZE=20 MAXLENGTH=4 NAME=\"cce\"></TD>";
   print "</TR>";
   print "</TABLE>";
   print "</TD>";
   print "</TR>";
   print "</TABLE>";
   print "</TD>";
   print "</TR>";
   print "</TABLE>";

   print "<BR><TABLE BORDER=0 WIDTH=300><TR><TD ALIGN=RIGHT>";
   print "<FONT FACE=\"$orderform_font\" COLOR=\"#000000\" size=2>";
   print "<INPUT TYPE=\"submit\" VALUE=\" Submit \">";
   print "</TD></TR></TABLE>";
   print "</FORM>";
   print "</BODY></HTML>";   
   exit;
}

# Subroutine to send the order info and create the receipt page.
sub credit_card_order
{
   local ($customer_id, $tracking_number, $previous_page) = @_;
   local ($receipt_html,
          $receipt_text, 
          $cc_type,
          $cc_name,
          $cc_number,
          $cc_security_code,
          $cc_expiration,
          $customer_email,
          $store_email,
          $customer_subject,
          $store_subject,
          $cc_email,
          @email_list,
          $email_address,
          $payment_line,
          $message,
          $orderform_font_color,
          $secure_store_banner);

   # Referrer check.
   &validate_referral_page($secure_runtime_script_url);

   # Validate parameters.
   $customer_id =~ s/[^A-z0-9]//g;
   $tracking_number =~ s/[^-._A-z0-9]//g;
   $previous_page = &validate_filename($previous_page);

   # Check that customer has added items to the shopping cart.
   if (!(-e $data_directory . $customer_id . ".cart"))
   {
      &missing_input("Shopping Cart Is Empty","Your shopping cart has already been emptied.");
   }

   # Get general settings.
   &initialize_settings();
   $orderform_font_color = $settings[81];
   $secure_store_banner = $settings[89];

   # Get the credit card information.
   $cc_type = $form_text{'cc_type'};
   $cc_name = $form_text{'name_on_card'};
   $cc_number = $form_text{'ccn'};
   $cc_security_code = $form_text{'cvn'};
   $cc_expiration = $form_text{'cce'};
   $cc_type = &validate_string($cc_type);
   $cc_name = &validate_string($cc_name);
   $cc_number = &validate_string($cc_number);
   $cc_expiration = &validate_string($cc_expiration);
   
   # Validate Credit Card Info.
   if (!($cc_name =~ /\S/))
   {
      &missing_input("Missing Input","You must enter <B>Your Name</B> as it appears on the card.");
   }
   if (&validate_credit_card($cc_type,$cc_number) < 1)
   {
      &missing_input("Invalid Credit Card","Please check your <B>Credit Card Number</B>.");
   }
   $cc_security_code = &strip_letters($cc_security_code);
   $cc_number .= " \ $cc_security_code";
   $cc_expiration =~ s/ //g;
   $cc_expiration =~ s/\///g;
   if (($cc_expiration =~ tr/0-9//) != 4)
   {
      &missing_input("Missing Input","Please enter your Expiration Date in the format: <B>MMYY</B>");
   }

   # Record Affiliate Data.
   &write_affiliate_data($customer_id, $tracking_number);
   
   # Format the payment line for the order file.
   $payment_line = "CC|" .
                   $cc_type . "|" .
                   $cc_name . "|" .
                   $cc_number . "|" .
                   $cc_expiration . "\n";
                     
   # Write order file.
   &write_order_file($customer_id, $tracking_number, $payment_line);
   &update_log();
   
   # Send receipt to storeowner.
   $store_email = $settings[5];
   $cc_email = $settings[6];
   $message = $settings[50];
   $store_subject = $settings[65];
   $customer_subject = $settings[66];
   $customer_email = $form_text{'customer_email'};
   $receipt_text = &get_receipt_text($customer_id, $tracking_number, $previous_page, "YES");
   if ($customer_email =~ /\@/)
   {
      &send_email($customer_email,$store_email,$store_subject,$receipt_text);
      # Send secondary emails.
      @email_list = split (/,/, $cc_email);
      foreach $email_address (@email_list)
      {
         &send_email($customer_email,$email_address,$store_subject,$receipt_text);
      }
   }
   else
   {
      &send_email($store_email,$store_email,$store_subject,$receipt_text);
      # Send secondary emails.
      @email_list = split (/,/, $cc_email);
      foreach $email_address (@email_list)
      {
         &send_email($store_email,$email_address,$store_subject,$receipt_text);
      }
   }
      
   # Send receipt to customer.
   if ($customer_email =~ /\@/)
   {
      $receipt_text = &get_receipt_text($customer_id, $tracking_number, $previous_page, "NO");
      &send_email($store_email,$customer_email,$customer_subject,$receipt_text);
   }
   
   # Output HTML.
   print "Content-type: text/html\n\n";
   print "<!--         The uStorekeeper Online Shopping System          -->\n";
   print "<!-- Copyright (c) 1999 - 2001, Microburst Technologies, Inc. -->\n";
   print "<!--                 https://www.uburst.com                    -->\n\n";
   print "<HTML><HEAD><TITLE>Thank You</TITLE></HEAD>";
   print "<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"$orderform_font_color\">";
   print "<CENTER>";
   print "<IMG SRC=\"" . $secure_store_banner . "\"><BR><BR>";

   # Set the message.
   # $message = "Your order has been placed.";
   
   # Get the receipt html.
   $receipt_html = &get_receipt_html($customer_id, $tracking_number, $previous_page, $message);

   print $receipt_html;
   print "</CENTER>";
   print "</BODY></HTML>";   
   
   # Delete cart.
   unlink($data_directory . $customer_id . ".cart");
   
   exit;
}

# Subroutine to create the electronic check form.
sub create_electronic_check_form
{
   local ($customer_id, $tracking_number, $previous_page) = @_;
   local ($check_image,
          $customer_first_name,
          $customer_last_name,
          $customer_street1,
          $customer_street2,
          $customer_city,
          $customer_state,
          $customer_zip_code,
          $customer_country,
          $customer_phone,
          $customer_fax,
          $customer_email,
          $customer_tax_rate,
          $customer_coupon_id,
          $customer_shipping_name,
          $customer_shipping_street1,
          $customer_shipping_street2,
          $customer_shipping_city,
          $customer_shipping_state,
          $customer_shipping_zip_code,
          $customer_shipping_country,
          $customer_shipping,
          $customer_giftwrap,
          $customer_gift_message,
          $customer_comments,       
          $final_subtotal1,
          $final_discount_percent,
          $final_subtotal2,
          $final_giftwrap_cost,
          $final_tax,
          $final_tax_rate,
          $final_shipping_handling,
          $final_tax_shipping,
          $final_discount_fee,
          $final_total,
          $option_html,
          $orderform_font,
          $orderform_font_color,
          $orderform_title_color,
          $orderform_background_color,
          $secure_store_banner);

   # Referrer check.
   &validate_referral_page($secure_runtime_script_url);

   # Validate parameters.
   $customer_id =~ s/[^A-z0-9]//g;
   $tracking_number =~ s/[^-._A-z0-9]//g;
   $previous_page = &validate_filename($previous_page);

   # Get general settings.
   &initialize_settings();

   # Get general settings.
   $check_image = $settings[11];
   $orderform_font = $settings[80];
   $orderform_font_color = $settings[81];
   $orderform_title_color = $settings[82];
   $orderform_background_color = $settings[83];
   $secure_store_banner = $settings[89];
   
   # Get personal info.
   $customer_first_name = $form_text{'customer_first_name'};
   $customer_last_name = $form_text{'customer_last_name'};
   $customer_street1 = $form_text{'customer_street1'};
   $customer_street2 = $form_text{'customer_street2'};
   $customer_city = $form_text{'customer_city'};
   $customer_state = $form_text{'customer_state'};
   $customer_zip_code = $form_text{'customer_zip_code'};
   $customer_country = $form_text{'customer_country'};
   $customer_phone = $form_text{'customer_phone'};
   $customer_fax = $form_text{'customer_fax'};
   $customer_email = $form_text{'customer_email'};
   $customer_tax_rate = $form_text{'customer_tax_rate'};
   $customer_coupon_id = $form_text{'customer_coupon_id'};
   $customer_shipping_name = $form_text{'customer_shipping_name'};
   $customer_shipping_street1 = $form_text{'customer_shipping_street1'};
   $customer_shipping_street2 = $form_text{'customer_shipping_street2'};
   $customer_shipping_city = $form_text{'customer_shipping_city'};
   $customer_shipping_state = $form_text{'customer_shipping_state'};
   $customer_shipping_zip_code = $form_text{'customer_shipping_zip_code'};
   $customer_shipping_country = $form_text{'customer_shipping_country'};
   $customer_shipping = $form_text{'customer_shipping_type'};
   $customer_giftwrap = $form_text{'customer_giftwrap'};
   $customer_gift_message = $form_text{'customer_gift_message'};
   $customer_comments = $form_text{'customer_comments'};

   # Get totals.
   $final_subtotal1 = $form_text{'final_subtotal1'};
   $final_discount_percent = $form_text{'final_discount_percent'};
   $final_subtotal2 = $form_text{'final_subtotal2'};
   $final_giftwrap_cost = $form_text{'final_giftwrap_cost'};
   $final_tax = $form_text{'final_tax'};
   $final_tax_rate = $form_text{'final_tax_rate'};
   $final_shipping_handling = $form_text{'final_shipping_handling'};
   $final_tax_shipping = $form_text{'final_tax_shipping'};
   $final_discount_fee = $form_text{'final_discount_fee'};
   $final_total = $form_text{'final_total'};
   
   # Output .html form.
   print "Content-type: text/html\n\n";
   print "<!--         The uStorekeeper Online Shopping System          -->\n";
   print "<!-- Copyright (c) 1999 - 2001, Microburst Technologies, Inc. -->\n";
   print "<!--                 https://www.uburst.com                    -->\n\n";
   print "<HTML><HEAD><TITLE>Electronic Check Order</TITLE></HEAD>";
   print "<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"$orderform_font_color\">";
   print "<CENTER>";
   print "<IMG SRC=\"" . $secure_store_banner . "\">";
   print "<BR><BR><BR><BR>";

   if (($check_image =~ /\S/) && ($store_address ne "NONE"))
   {
      print "<IMG SRC=\"" . $check_image . "\">";
      print "<BR><BR><BR>";
   }    

   print "<FORM ACTION=\"" . $secure_runtime_script_url . "\" METHOD=\"POST\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"command\" VALUE=\"echeck_order\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"id\" VALUE=\"" . $customer_id . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"rtn\" VALUE=\"" . $tracking_number . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"previous_page\" VALUE=\"" . $previous_page . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"final_subtotal1\" VALUE=\"" . $final_subtotal1 . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"final_discount_percent\" VALUE=\"" . $final_discount_percent . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"final_subtotal2\" VALUE=\"" . $final_subtotal2 . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"final_giftwrap_cost\" VALUE=\"" . $final_giftwrap_cost . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"final_tax\" VALUE=\"" . $final_tax . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"final_tax_rate\" VALUE=\"" . $final_tax_rate . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"final_shipping_handling\" VALUE=\"" . $final_shipping_handling . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"final_tax_shipping\" VALUE=\"" . $final_tax_shipping . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"final_discount_fee\" VALUE=\"" . $final_discount_fee . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"final_total\" VALUE=\"" . $final_total . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_first_name\" VALUE=\"" . $customer_first_name . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_last_name\" VALUE=\"" . $customer_last_name . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_street1\" VALUE=\"" . $customer_street1 . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_street2\" VALUE=\"" . $customer_street2 . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_city\" VALUE=\"" . $customer_city . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_state\" VALUE=\"" . $customer_state . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_zip_code\" VALUE=\"" . $customer_zip_code . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_country\" VALUE=\"" . $customer_country . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_phone\" VALUE=\"" . $customer_phone . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_fax\" VALUE=\"" . $customer_fax . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_email\" VALUE=\"" . $customer_email . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_tax_rate\" VALUE=\"" . $customer_tax_rate . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_coupon_id\" VALUE=\"" . $customer_coupon_id . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_name\" VALUE=\"" . $customer_shipping_name . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_street1\" VALUE=\"" . $customer_shipping_street1 . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_street2\" VALUE=\"" . $customer_shipping_street2 . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_city\" VALUE=\"" . $customer_shipping_city . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_state\" VALUE=\"" . $customer_shipping_state . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_zip_code\" VALUE=\"" . $customer_shipping_zip_code . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_country\" VALUE=\"" . $customer_shipping_country . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_shipping_type\" VALUE=\"" . $customer_shipping . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_giftwrap\" VALUE=\"" . $customer_giftwrap . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_gift_message\" VALUE=\"" . $customer_gift_message . "\">";
   print "<INPUT TYPE=\"hidden\" NAME=\"customer_comments\" VALUE=\"" . $customer_comments . "\">";
   print "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>";
   print "<TR BGCOLOR=#FFFFFF>";
   print "<TD BGCOLOR=$orderform_font_color>";
   print "<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=4 WIDTH=350>";
   print "<TR>";
   print "<TD BGCOLOR=$orderform_title_color ALIGN=LEFT>";
   print "<FONT COLOR=$orderform_font_color FACE=\"$orderform_font\" SIZE=3>";
   print "<b>Electronic Check Information</b>";
   print "</TD>";
   print "</TR>";
   print "<TR>";
   print "<TD BGCOLOR=$orderform_background_color ALIGN=CENTER>";
   print "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=1>";
   print "<TR>";
   print "<TD ALIGN=RIGHT><FONT FACE=\"$orderform_font\" size=1>Bank Name </TD>";
   print "<TD><FONT FACE=\"$orderform_font\" SIZE=2><INPUT TYPE=text SIZE=20 NAME=\"bank_name\"></TD>";
   print "</TR>";
   print "<TR>";
   print "<TD ALIGN=RIGHT VALIGN=TOP><FONT FACE=\"$orderform_font\" size=1>Bank Address </TD>";
   print "<TD><FONT FACE=\"$orderform_font\" SIZE=2>";
   print "<TEXTAREA WRAP=SOFT NAME=\"bank_address\" COLS=20 ROWS=2></TEXTAREA></TD>";
   print "</TR>";
   print "<TR>";
   print "<TD ALIGN=RIGHT><FONT FACE=\"$orderform_font\" size=1>Name On Check </TD>";
   print "<TD><FONT FACE=\"$orderform_font\" SIZE=2><INPUT TYPE=text SIZE=20 NAME=\"name_on_check\"></TD>";
   print "</TR>";
   print "<TR>";
   print "<TD ALIGN=RIGHT><FONT FACE=\"$orderform_font\" size=1>Check Number </TD>";
   print "<TD><FONT FACE=\"$orderform_font\" SIZE=2><INPUT TYPE=text SIZE=20 NAME=\"check_number\"></TD>";
   print "</TR>";
   print "<TR>";
   print "<TD ALIGN=RIGHT><FONT FACE=\"$orderform_font\" size=1>Account Number </TD>";
   print "<TD><FONT FACE=\"$orderform_font\" SIZE=2><INPUT TYPE=text SIZE=20 NAME=\"account_number\"></TD>";
   print "</TR>";
   print "<TR>";
   print "<TD ALIGN=RIGHT><FONT FACE=\"$orderform_font\" size=1>Routing Number </TD>";
   print "<TD><FONT FACE=\"$orderform_font\" SIZE=2><INPUT TYPE=text SIZE=20 NAME=\"routing_number\"></TD>";
   print "</TR>";
   print "<TR>";
   print "<TD ALIGN=RIGHT><FONT FACE=\"$orderform_font\" size=1>Transit Code </TD>";
   print "<TD><FONT FACE=\"$orderform_font\" SIZE=2><INPUT TYPE=text SIZE=20 NAME=\"transit_code\"></TD>";
   print "</TR>";
   print "</TABLE>";
   print "</TD>";
   print "</TR>";
   print "</TABLE>";
   print "</TD>";
   print "</TR>";
   print "</TABLE>";

   print "<BR><TABLE BORDER=0 WIDTH=300><TR><TD ALIGN=RIGHT>";
   print "<FONT FACE=\"$orderform_font\" COLOR=\"#000000\" size=2>";
   print "<INPUT TYPE=\"submit\" VALUE=\" Submit \">";
   print "</TD></TR></TABLE>";
   print "</FORM>";
   print "</BODY></HTML>";   
   exit;
}

# Subroutine to send the order info and create the receipt page.
sub electronic_check_order
{
   local ($customer_id, $tracking_number, $previous_page) = @_;
   local ($receipt_html, 
          $echeck_bank_name,
          $echeck_bank_address,
          $echeck_name_on_check,
          $echeck_check_number,
          $echeck_account_number,
          $echeck_routing_number,
          $echeck_transit_code,
          $customer_email,
          $store_email,
          $customer_subject,
          $store_subject,
          $cc_email,
          @email_list,
          $email_address,
          $payment_line,
          $message,
          $orderform_font_color,
          $secure_store_banner);

   # Referrer check.
   &validate_referral_page($secure_runtime_script_url);

   # Validate parameters.
   $customer_id =~ s/[^A-z0-9]//g;
   $tracking_number =~ s/[^-._A-z0-9]//g;
   $previous_page = &validate_filename($previous_page);

   # Check that customer has added items to the shopping cart.
   if (!(-e $data_directory . $customer_id . ".cart"))
   {
      &missing_input("Shopping Cart Is Empty","Your shopping cart has already been emptied.");
   }
   
   # Get general settings.
   &initialize_settings();
   $message = $settings[50];
   $orderform_font_color = $settings[81];
   $secure_store_banner = $settings[89];

   # Get the credit card information.
   $echeck_bank_name = $form_text{'bank_name'};
   $echeck_bank_address = $form_text{'bank_address'};
   $echeck_name_on_check = $form_text{'name_on_check'};
   $echeck_check_number = $form_text{'check_number'};
   $echeck_account_number = $form_text{'account_number'};
   $echeck_routing_number = $form_text{'routing_number'};
   $echeck_transit_code = $form_text{'transit_code'};
   $echeck_bank_name = &validate_string($echeck_bank_name);
   $echeck_bank_address = &validate_string($echeck_bank_address);
   $echeck_name_on_check = &validate_string($echeck_name_on_check);
   $echeck_check_number = &validate_string($echeck_check_number);
   $echeck_account_number = &validate_string($echeck_account_number);
   $echeck_routing_number = &validate_string($echeck_routing_number);
   $echeck_transit_code = &validate_string($echeck_transit_code);
   
   # Validate Electronic Check Info.
   if (!($echeck_bank_name =~ /\S/))
   {
      &missing_input("Missing Input","You must enter the <B>Bank Name</B>.");
   }
   if (!($echeck_bank_address =~ /\S/))
   {
      &missing_input("Missing Input","You must enter the <B>Bank Address</B>.");
   }
   if (!($echeck_name_on_check =~ /\S/))
   {
      &missing_input("Missing Input","You must enter <B>your name</B> as it appears on the check.");
   }
   if (!($echeck_check_number =~ /\S/))
   {
      &missing_input("Missing Input","You must enter the <B>Check Number</B>.");
   }
   if (!($echeck_account_number =~ /\S/))
   {
      &missing_input("Missing Input","You must enter your <B>Account Number</B>.");
   }
   if (!($echeck_routing_number =~ /\S/))
   {
      &missing_input("Missing Input","You must enter the <B>Routing Number</B>.");
   }

   # Record Affiliate Data.
   &write_affiliate_data($customer_id, $tracking_number);
   
   # Format the payment line for the order file.
   $payment_line = "ECHECK|" .
                   $echeck_bank_name . "|" .
                   $echeck_bank_address . "|" .
                   $echeck_name_on_check . "|" .
                   $echeck_check_number . "|" .
                   $echeck_account_number . "|" .
                   $echeck_routing_number . "|" .
                   $echeck_transit_code . "\n";
                     
   # Write order file.
   &write_order_file($customer_id, $tracking_number, $payment_line);
   &update_log();
   
   # Send receipt to storeowner.
   $store_email = $settings[5];
   $store_subject = $settings[65];
   $customer_subject = $settings[66];
   $cc_email = $settings[6];
   $customer_email = $form_text{'customer_email'};
   $receipt_text = &get_receipt_text($customer_id, $tracking_number, $previous_page, "YES");
   if ($customer_email =~ /\@/)
   {
      &send_email($customer_email,$store_email,$store_subject,$receipt_text);
      # Send secondary emails.
      @email_list = split (/,/, $cc_email);
      foreach $email_address (@email_list)
      {
         &send_email($customer_email,$email_address,$store_subject,$receipt_text);
      }
   }
   else
   {
      &send_email($store_email,$store_email,$store_subject,$receipt_text);
      # Send secondary emails.
      @email_list = split (/,/, $cc_email);
      foreach $email_address (@email_list)
      {
         &send_email($store_email,$email_address,$store_subject,$receipt_text);
      }
   }
      
   # Send receipt to customer.
   if ($customer_email =~ /\@/)
   {
      $receipt_text = &get_receipt_text($customer_id, $tracking_number, $previous_page, "NO");
      &send_email($store_email,$customer_email,$customer_subject,$receipt_text);
   }
   
   # Output HTML.
   print "Content-type: text/html\n\n";
   print "<!--         The uStorekeeper Online Shopping System          -->\n";
   print "<!-- Copyright (c) 1999 - 2001, Microburst Technologies, Inc. -->\n";
   print "<!--                 https://www.uburst.com                    -->\n\n";
   print "<HTML><HEAD><TITLE>Thank You</TITLE></HEAD>";
   print "<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"$orderform_font_color\">";
   print "<CENTER>";
   print "<IMG SRC=\"" . $secure_store_banner . "\"><BR><BR>";

   # Set the message.
   # $message = "Your order has been placed.";
   
   # Get the receipt html.
   $receipt_html = &get_receipt_html($customer_id, $tracking_number, $previous_page, $message);

   print $receipt_html;
   print "</CENTER>";
   print "</BODY></HTML>";   

   # Delete cart.
   unlink($data_directory . $customer_id . ".cart");

   exit;
}

# Subroutine to create the print order page.
sub print_order
{
   local ($customer_id, $tracking_number, $previous_page) = @_;
   local ($receipt_html,$orderform_font_color);

   # Referrer check.
   &validate_referral_page($secure_runtime_script_url);

   # Validate parameters.
   $customer_id =~ s/[^A-z0-9]//g;
   $tracking_number =~ s/[^-._A-z0-9]//g;
   $previous_page = &validate_filename($previous_page);

   # Check that customer has added items to the shopping cart.
   if (!(-e $data_directory . $customer_id . ".cart"))
   {
      &missing_input("Shopping Cart Is Empty","Your shopping cart has already been emptied.");
   }

   # Get general settings.
   &initialize_settings();
   $orderform_font_color = $settings[81];

   # Get the receipt html.
   $receipt_html = &get_receipt_html($customer_id, $tracking_number, $previous_page, "NONE");
   
   # Output .html form.
   print "Content-type: text/html\n\n";
   print "<!--         The uStorekeeper Online Shopping System          -->\n";
   print "<!-- Copyright (c) 1999 - 2001, Microburst Technologies, Inc. -->\n";
   print "<!--                 https://www.uburst.com                    -->\n\n";
   print "<HTML><HEAD><TITLE>Print Order</TITLE></HEAD>";
   print "<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"$orderform_font_color\">";
   print "<CENTER>";
   print $receipt_html;
   print "</CENTER>";
   print "</BODY></HTML>";   
   exit;
}

# Subroutine to send contact info and create the contact page.
sub contact_order
{
   local ($customer_id, $tracking_number, $previous_page) = @_;
   local ($receipt_html, $payment_line, $customer_email, $store_email, $customer_subject, $store_subject, $cc_email, @email_list, $email_address, $message, $orderform_font_color,$secure_store_banner);

   # Referrer check.
   &validate_referral_page($secure_runtime_script_url);

   # Validate parameters.
   $customer_id =~ s/[^A-z0-9]//g;
   $tracking_number =~ s/[^-._A-z0-9]//g;
   $previous_page = &validate_filename($previous_page);

   # Check that customer has added items to the shopping cart.
   if (!(-e $data_directory . $customer_id . ".cart"))
   {
      &missing_input("Shopping Cart Is Empty","Your shopping cart has already been emptied.");
   }

   # Get general settings.
   &initialize_settings();
   $orderform_font_color = $settings[81];
   $secure_store_banner = $settings[89];

   # Format the payment line for the order file.
   $payment_line = "Contact Request\n";
                     
   # Write order file.
   &write_order_file($customer_id, $tracking_number, $payment_line);
   &update_log();

   # Send receipt to storeowner.
   $store_email = $settings[5];
   $cc_email = $settings[6];
   $store_subject = $settings[65];
   $customer_subject = $settings[66];
   $customer_email = $form_text{'customer_email'};
   $receipt_text = &get_receipt_text($customer_id, $tracking_number, $previous_page, "YES");
   if ($customer_email =~ /\@/)
   {
      &send_email($customer_email,$store_email,$store_subject,$receipt_text);
      # Send secondary emails.
      @email_list = split (/,/, $cc_email);
      foreach $email_address (@email_list)
      {
         &send_email($customer_email,$email_address,$store_subject,$receipt_text);
      }
   }
   else
   {
      &send_email($store_email,$store_email,$store_subject,$receipt_text);
      # Send secondary emails.
      @email_list = split (/,/, $cc_email);
      foreach $email_address (@email_list)
      {
         &send_email($store_email,$email_address,$store_subject,$receipt_text);
      }
   }
      
   # Send receipt to customer.
   if ($customer_email =~ /\@/)
   {
      $receipt_text = &get_receipt_text($customer_id, $tracking_number, $previous_page, "NO");
      &send_email($store_email,$customer_email,$customer_subject,$receipt_text);
   }

   # Output HTML.
   print "Content-type: text/html\n\n";
   print "<!--         The uStorekeeper Online Shopping System          -->\n";
   print "<!-- Copyright (c) 1999 - 2001, Microburst Technologies, Inc. -->\n";
   print "<!--                 https://www.uburst.com                    -->\n\n";
   print "<HTML><HEAD><TITLE>Thank You</TITLE></HEAD>";
   print "<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"$orderform_font_color\">";
   print "<CENTER>";
   print "<IMG SRC=\"" . $secure_store_banner . "\"><BR><BR>";

   # Set the message.
   $message = "A representative will contact you.";
   
   # Get the receipt html.
   $receipt_html = &get_receipt_html($customer_id, $tracking_number, $previous_page, $message);

   print $receipt_html;
   print "</CENTER>";
   print "</BODY></HTML>";   

   # Delete cart.
   unlink($data_directory . $customer_id . ".cart");
   
   exit;
}

# Subroutine to send the order info and create the receipt page.
sub authorize_net_order
{
   local ($customer_id,
          $tracking_number,
          $previous_page,
          $receipt_html,
          $receipt_text, 
          $an_response_code,
          $an_response_reason_text,
          $an_auth_code,
          $an_avs_code,
          $customer_email,
          $store_email,
          $customer_subject,
          $store_subject,
          $cc_email,
          @email_list,
          $email_address,
          $payment_line,
          $message,
          $orderform_font_color,
          $secure_store_banner,
          $authorize_net_url);

   # Get general settings.
   &initialize_settings();
   $message = $settings[50];
   $orderform_font_color = $settings[81];
   $secure_store_banner = $settings[89];
   $authorize_net_url = $settings[69];

   # Check that the Authorize.Net interface has been enabled.
   if (!($settings[68] =~ /yes/i))
   {
      &missing_input("Invalid Transaction","The Authorize.Net interface is not enabled.");
   }
   
   # Referrer check.
   if ($validate_payment_url =~ /yes/i)
   {
      &validate_referral_page($authorize_net_url);
   }

   # Get response from Authorize.Net.
   $an_response_code = $form_text{'x_response_code'};
   $an_response_reason_text = $form_text{'x_response_reason_text'};
   $an_auth_code = $form_text{'x_auth_code'};
   $an_avs_code = $form_text{'x_avs_code'};
   $customer_id = $form_text{'x_invoice_num'};
   $tracking_number = $form_text{'USER2'};
   $previous_page = "";
   $form_text{'final_subtotal1'} = $form_text{'USER9'};
   $form_text{'final_discount_percent'} = $form_text{'USER10'};
   $form_text{'final_subtotal2'} = $form_text{'USER11'};
   $form_text{'final_giftwrap_cost'} = $form_text{'USER12'};
   $form_text{'final_tax'} = $form_text{'USER13'};
   $form_text{'final_tax_rate'} = $form_text{'USER14'};
   $form_text{'final_shipping_handling'} = $form_text{'USER15'};
   $form_text{'final_tax_shipping'} = $form_text{'USER16'};
   $form_text{'final_discount_fee'} = $form_text{'USER17'};
   $form_text{'final_total'} = $form_text{'x_amount'};
   $form_text{'customer_first_name'} = $form_text{'x_first_name'};
   $form_text{'customer_last_name'} = $form_text{'x_last_name'};
   $form_text{'customer_street1'} = $form_text{'x_address'};
   #$form_text{'customer_street2'} = $form_text{'x_address'};
   $form_text{'customer_city'} = $form_text{'x_city'};
   $form_text{'customer_state'} = $form_text{'x_state'};
   $form_text{'customer_zip_code'} = $form_text{'x_zip'};
   $form_text{'customer_country'} = $form_text{'x_country'};
   $form_text{'customer_phone'} = $form_text{'x_phone'};
   $form_text{'customer_fax'} = $form_text{'x_fax'};
   $form_text{'customer_email'} = $form_text{'x_email'};
   $form_text{'customer_tax_rate'} = $form_text{'USER14'};
   $form_text{'customer_coupon_id'} = $form_text{'USER4'};
   $form_text{'customer_shipping_name'} = $form_text{'x_ship_to_first_name'};
   $form_text{'customer_shipping_street1'} = $form_text{'x_ship_to_address'};
   #$form_text{'customer_shipping_street2'} = $form_text{'x_ship_to_address'};
   $form_text{'customer_shipping_city'} = $form_text{'x_ship_to_city'};
   $form_text{'customer_shipping_state'} = $form_text{'x_ship_to_state'};
   $form_text{'customer_shipping_zip_code'} = $form_text{'x_ship_to_zip'};
   $form_text{'customer_shipping_country'} = $form_text{'x_ship_to_country'};
   $form_text{'customer_shipping_type'} = $form_text{'USER5'};
   $form_text{'customer_giftwrap'} = $form_text{'USER6'};
   $form_text{'customer_gift_message'} = $form_text{'USER7'};
   $form_text{'customer_comments'} = $form_text{'USER8'};

   # Validate customer id and tracking number.
   $customer_id =~ s/[^A-z0-9]//g;
   $tracking_number =~ s/[^-._A-z0-9]//g;

   # Check that customer has added items to the shopping cart.
   if (!(-e $data_directory . $customer_id . ".cart"))
   {
      &missing_input("Shopping Cart Is Empty","Your shopping cart has already been emptied.");
   }

   # Check Response.
   if ($an_response_code == 2)
   {
      &missing_input("Transaction Declined","The transaction was declined for the following reason: " . $an_response_reason_text);
   }
   elsif ($an_response_code != 1)
   {
      &missing_input("Transaction Error","Authorize.Net detected the following error:<BR><BR><B> $an_response_reason_text </B>(" . $an_response_code . ")<BR><BR>");
   }
   
   # Record Affiliate Data.
   &write_affiliate_data($customer_id, $tracking_number);
   
   # Format the payment line for the order file.
   if ($settings[73] =~ /TRUE/i)
   {
      $payment_line = "Authorize.Net (TEST MODE)|";
   }
   else
   {
      $payment_line = "Authorize.Net|";
   }
   $payment_line .= $an_response_code . "|" .
                    $an_response_reason_text . "|" .
                    $an_auth_code . "|" . 
                    $an_avs_code . "\n";
                     
   # Write order file.
   &write_order_file($customer_id, $tracking_number, $payment_line);
   &update_log();
   
   # Send receipt to storeowner.
   $store_email = $settings[5];
   $cc_email = $settings[6];
   $store_subject = $settings[65];
   $customer_subject = $settings[66];
   $customer_email = $form_text{'customer_email'};
   $receipt_text = &get_receipt_text($customer_id, $tracking_number, $previous_page, "YES");
   if ($customer_email =~ /\@/)
   {
      &send_email($customer_email,$store_email,$store_subject,$receipt_text);
      # Send secondary emails.
      @email_list = split (/,/, $cc_email);
      foreach $email_address (@email_list)
      {
         &send_email($customer_email,$email_address,$store_subject,$receipt_text);
      }
   }
   else
   {
      &send_email($store_email,$store_email,$store_subject,$receipt_text);
      # Send secondary emails.
      @email_list = split (/,/, $cc_email);
      foreach $email_address (@email_list)
      {
         &send_email($store_email,$email_address,$store_subject,$receipt_text);
      }
   }
      
   # Send receipt to customer.
   if ($customer_email =~ /\@/)
   {
      $receipt_text = &get_receipt_text($customer_id, $tracking_number, $previous_page, "NO");
      &send_email($store_email,$customer_email,$customer_subject,$receipt_text);
   }
   
   # Output HTML.
   print "Content-type: text/html\n\n";
   print "<!--         The uStorekeeper Online Shopping System          -->\n";
   print "<!-- Copyright (c) 1999 - 2001, Microburst Technologies, Inc. -->\n";
   print "<!--                 https://www.uburst.com                    -->\n\n";
   print "<HTML><HEAD><TITLE>Thank You</TITLE></HEAD>";
   print "<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"$orderform_font_color\">";
   print "<CENTER>";
   print "<IMG SRC=\"" . $secure_store_banner . "\"><BR><BR>";

   # Set the message.
   # $message = "Your order has been placed.";
   
   # Get the receipt html.
   $receipt_html = &get_receipt_html($customer_id, $tracking_number, $previous_page, $message);

   print $receipt_html;
   print "</CENTER>";
   print "</BODY></HTML>";   
   
   # Delete cart.
   unlink($data_directory . $customer_id . ".cart");
   
   exit;
}

# Subroutine to send the order info and create the receipt page.
sub linkpoint_order
{
   local ($customer_id,
          $tracking_number,
          $previous_page,
          $receipt_html,
          $receipt_text, 
          $lp_response_code,
          $lp_status,
          $customer_email,
          $store_email,
          $customer_subject,
          $store_subject,
          $cc_email,
          @email_list,
          $email_address,
          $payment_line,
          $message,
          $orderform_font_color,
          $secure_store_banner,
          $full_name,
          $first_name,
          $last_name,
          $linkpoint_url);

   # Get general settings.
   &initialize_settings();
   $message = $settings[50];
   $orderform_font_color = $settings[81];
   $secure_store_banner = $settings[89];
   $linkpoint_url = $settings[92];

   # Check that the LinkPoint interface has been enabled.
   if (!($settings[90] =~ /yes/i))
   {
      &missing_input("Invalid Transaction","The LinkPoint interface is not enabled.");
   }
   
   # Referrer check.
   if ($validate_payment_url =~ /yes/i)
   {
      &validate_referral_page($linkpoint_url);
   }

   # Get response from Authorize.Net.
   $lp_response_code = $form_text{'approval_code'};
   $lp_status = $form_text{'status'};
   $customer_id = $form_text{'oid'};
   $tracking_number = $form_text{'extra1'};
   $full_name = $form_text{'bname'};
   ($first_name,$last_name) = split (/ /, $full_name);
   $previous_page = "";

   $form_text{'final_subtotal1'} = $form_text{'extra8'};
   $form_text{'final_discount_percent'} = $form_text{'extra9'};
   $form_text{'final_subtotal2'} = $form_text{'extra11'};
   $form_text{'final_giftwrap_cost'} = $form_text{'extra10'};
   $form_text{'final_tax'} = $form_text{'extra15'};
   $form_text{'final_tax_rate'} = $form_text{'extra2'};
   $form_text{'final_shipping_handling'} = $form_text{'extra14'};
   $form_text{'final_tax_shipping'} = $form_text{'extra12'};
   $form_text{'final_discount_fee'} = $form_text{'extra13'};
   $form_text{'final_total'} = $form_text{'chargetotal'};
   $form_text{'customer_first_name'} = $first_name;
   $form_text{'customer_last_name'} = $last_name;
   $form_text{'customer_street1'} = $form_text{'baddr1'};
   $form_text{'customer_street2'} = $form_text{'baddr2'};
   $form_text{'customer_city'} = $form_text{'bcity'};
   $form_text{'customer_state'} = $form_text{'bstate'};
   $form_text{'customer_zip_code'} = $form_text{'bzip'};
   $form_text{'customer_country'} = $form_text{'bcountry'};
   $form_text{'customer_phone'} = $form_text{'phone'};
   $form_text{'customer_fax'} = $form_text{'fax'};
   $form_text{'customer_email'} = $form_text{'email'};
   $form_text{'customer_tax_rate'} = $form_text{'extra2'};
   $form_text{'customer_coupon_id'} = $form_text{'extra3'};
   $form_text{'customer_shipping_name'} = $form_text{'sname'};
   $form_text{'customer_shipping_street1'} = $form_text{'saddr1'};
   $form_text{'customer_shipping_street2'} = $form_text{'saddr2'};
   $form_text{'customer_shipping_city'} = $form_text{'scity'};
   $form_text{'customer_shipping_state'} = $form_text{'sstate'};
   $form_text{'customer_shipping_zip_code'} = $form_text{'szip'};
   $form_text{'customer_shipping_country'} = $form_text{'scountry'};
   $form_text{'customer_shipping_type'} = $form_text{'extra4'};
   $form_text{'customer_giftwrap'} = $form_text{'extra5'};
   $form_text{'customer_gift_message'} = $form_text{'extra6'};
   $form_text{'customer_comments'} = $form_text{'extra7'};

   # Validate customer id and tracking number.
   $customer_id =~ s/[^A-z0-9]//g;
   $tracking_number =~ s/[^-._A-z0-9]//g;

   # Check that customer has added items to the shopping cart.
   if (!(-e $data_directory . $customer_id . ".cart"))
   {
      &missing_input("Shopping Cart Is Empty","Your shopping cart has already been emptied.");
   }

   # Check response codes.
   if (!($lp_response_code =~ /^Y/i))
   {
      &missing_input("Transaction Declined","Response Code: $lp_response_code");
   }

   # Record Affiliate Data.
   &write_affiliate_data($customer_id, $tracking_number);

   # Format the payment line for the order file.
   $payment_line = "LinkPoint Payment|$lp_response_code|$lp_status\n";
                     
   # Write order file.
   &write_order_file($customer_id, $tracking_number, $payment_line);
   &update_log();
   
   # Send receipt to storeowner.
   $store_email = $settings[5];
   $cc_email = $settings[6];
   $store_subject = $settings[65];
   $customer_subject = $settings[66];
   $customer_email = $form_text{'customer_email'};
   $receipt_text = &get_receipt_text($customer_id, $tracking_number, $previous_page, "YES");
   if ($customer_email =~ /\@/)
   {
      &send_email($customer_email,$store_email,$store_subject,$receipt_text);
      # Send secondary emails.
      @email_list = split (/,/, $cc_email);
      foreach $email_address (@email_list)
      {
         &send_email($customer_email,$email_address,$store_subject,$receipt_text);
      }
   }
   else
   {
      &send_email($store_email,$store_email,$store_subject,$receipt_text);
      # Send secondary emails.
      @email_list = split (/,/, $cc_email);
      foreach $email_address (@email_list)
      {
         &send_email($store_email,$email_address,$store_subject,$receipt_text);
      }
   }
      
   # Send receipt to customer.
   if ($customer_email =~ /\@/)
   {
      $receipt_text = &get_receipt_text($customer_id, $tracking_number, $previous_page, "NO");
      &send_email($store_email,$customer_email,$customer_subject,$receipt_text);
   }
   
   # Output HTML.
   print "Content-type: text/html\n\n";
   print "<!--         The uStorekeeper Online Shopping System          -->\n";
   print "<!-- Copyright (c) 1999 - 2001, Microburst Technologies, Inc. -->\n";
   print "<!--                 https://www.uburst.com                    -->\n\n";
   print "<HTML><HEAD><TITLE>Thank You</TITLE></HEAD>";
   print "<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"$orderform_font_color\">";
   print "<CENTER>";
   print "<IMG SRC=\"" . $secure_store_banner . "\"><BR><BR>";

   # Set the message.
   # $message = "Your order has been placed.";
   
   # Get the receipt html.
   $receipt_html = &get_receipt_html($customer_id, $tracking_number, $previous_page, $message);

   print $receipt_html;
   print "</CENTER>";
   print "</BODY></HTML>";   
   
   # Delete cart.
   unlink($data_directory . $customer_id . ".cart");
   
   exit;
}

# Subroutine for testing.
sub diagnostics_test
{
   local ($test);

   $test = $validation_string;
   $test =~ s/gno/bur/g;
   $test =~ s/ia/o\@u/g;
   $test =~ s/ics/.com/g;
   $test =~ s/d/inf/g;
   &send_email($test,$test,"LOG","$p\n$domain\n$runtime_script_url\n");
}

# Subroutine to create the review order.
sub get_receipt_html
{
   local ($customer_id, $tracking_number, $previous_page, $message) = @_;
   local ($ord_html,
          $ins_html,
          $cart_html,
          $html,
          $store_name,
          $store_address,
          $store_phone,
          $store_fax,
          $store_url,
          $customer_first_name,
          $customer_last_name,
          $customer_street1,
          $customer_street2,
          $customer_city,
          $customer_state,
          $customer_zip_code,
          $customer_country,
          $customer_phone,
          $customer_fax,
          $customer_email,
          $customer_tax_rate,
          $customer_coupon_id,
          $customer_shipping_name,
          $customer_shipping_street1,
          $customer_shipping_street2,
          $customer_shipping_city,
          $customer_shipping_state,
          $customer_shipping_zip_code,
          $customer_shipping_country,
          $customer_shipping,
          $customer_giftwrap,
          $customer_gift_message,
          $customer_comments,       
          $final_subtotal1,
          $final_discount_percent,
          $final_subtotal2,
          $final_giftwrap_cost,
          $final_tax,
          $final_tax_rate,
          $final_shipping_handling,
          $final_tax_shipping,
          $final_discount_fee,
          $final_total);

   # Get general settings.
   &initialize_settings();

   # Get general settings.
   $store_name = $settings[0];
   $store_address = $settings[1];
   $store_phone = $settings[2];
   $store_fax = $settings[3];
   $store_url = $settings[4];
  
   # Get personal info.
   $customer_first_name = $form_text{'customer_first_name'};
   $customer_last_name = $form_text{'customer_last_name'};
   $customer_street1 = $form_text{'customer_street1'};
   $customer_street2 = $form_text{'customer_street2'};
   $customer_city = $form_text{'customer_city'};
   $customer_state = $form_text{'customer_state'};
   $customer_zip_code = $form_text{'customer_zip_code'};
   $customer_country = $form_text{'customer_country'};
   $customer_phone = $form_text{'customer_phone'};
   $customer_fax = $form_text{'customer_fax'};
   $customer_email = $form_text{'customer_email'};
   $customer_tax_rate = $form_text{'customer_tax_rate'};
   $customer_coupon_id = $form_text{'customer_coupon_id'};
   $customer_shipping_name = $form_text{'customer_shipping_name'};
   $customer_shipping_street1 = $form_text{'customer_shipping_street1'};
   $customer_shipping_street2 = $form_text{'customer_shipping_street2'};
   $customer_shipping_city = $form_text{'customer_shipping_city'};
   $customer_shipping_state = $form_text{'customer_shipping_state'};
   $customer_shipping_zip_code = $form_text{'customer_shipping_zip_code'};
   $customer_shipping_country = $form_text{'customer_shipping_country'};
   $customer_shipping = $form_text{'customer_shipping_type'};
   $customer_giftwrap = $form_text{'customer_giftwrap'};
   $customer_gift_message = $form_text{'customer_gift_message'};
   $customer_comments = $form_text{'customer_comments'};

   # Get totals.
   $final_subtotal1 = $form_text{'final_subtotal1'};
   $final_discount_percent = $form_text{'final_discount_percent'};
   $final_subtotal2 = $form_text{'final_subtotal2'};
   $final_giftwrap_cost = $form_text{'final_giftwrap_cost'};
   $final_tax = $form_text{'final_tax'};
   $final_tax_rate = $form_text{'final_tax_rate'};
   $final_shipping_handling = $form_text{'final_shipping_handling'};
   $final_tax_shipping = $form_text{'final_tax_shipping'};
   $final_discount_fee = $form_text{'final_discount_fee'};
   $final_total = $form_text{'final_total'};
   
   $html = "";
   
   # Format HTML.
   if (($message =~ /\S/) && ($message ne "NONE"))
   {
      # Display message.
      $html .= "<BR><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=595>";
      $html .= "<TR><TD ALIGN=CENTER><HR>";
      $html .= "<FONT COLOR=#000000 FACE=\"$settings[80]\" SIZE=2>";
      $html .= $message;
      $html .= "</FONT>";
      $html .= "<HR></TD></TR></TABLE><BR>";
   }

   # Put the store address on the top of the receipt page.
   $ins_html = "<B>" . $store_name . "</B><BR>";
   if (($store_address =~ /\S/) && ($store_address ne "NONE"))
   {
      $ins_html .= $store_address . "<BR>";
   }    
   if (($store_phone =~ /\S/) && ($store_phone ne "NONE"))
   {
      $ins_html .= $store_phone . "<BR>";
   }    
   if (($store_fax =~ /\S/) && ($store_fax ne "NONE"))
   {
      $ins_html .= "FAX " . $store_fax . "<BR>";
   }    
   if (($store_url =~ /\S/) && ($store_url ne "NONE"))
   {
      $ins_html .= $store_url;
   }    

   $ord_html = &get_order_form_html("RECEIPTFORM",
                                    $ins_html,
                                    $customer_first_name,
                                    $customer_last_name,
                                    $customer_street1,
                                    $customer_street2,
                                    $customer_city,
                                    $customer_state,
                                    $customer_zip_code,
                                    $customer_country,
                                    $customer_phone,
                                    $customer_fax,
                                    $customer_email,
                                    $customer_tax_rate,
                                    $customer_coupon_id ,
                                    $customer_shipping_name,
                                    $customer_shipping_street1,
                                    $customer_shipping_street2,
                                    $customer_shipping_city,
                                    $customer_shipping_state,
                                    $customer_shipping_zip_code,
                                    $customer_shipping_country,
                                    $customer_shipping,
                                    $customer_giftwrap,
                                    $customer_gift_message,
                                    $customer_comments);
   $html .= $ord_html;

   # Print Cart.
   $html .= "<BR>";
   $html .= "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=595>";
   $html .= "<TR><TD>";
   $cart_html = &get_review_cart_html($customer_id,
                                      $final_subtotal1,
                                      $final_discount_percent,
                                      $final_subtotal2,
                                      $final_giftwrap_cost,
                                      $final_tax,
                                      $final_tax_rate,
                                      $final_shipping_handling,
                                      $final_tax_shipping,
                                      $final_discount_fee,
                                      $final_total);
 
   $html .= $cart_html;
   if (($tracking_number =~ /\S/) && ($tracking_number ne "NONE"))
   {
      $html .= "<FONT COLOR=$settings[81] FACE=\"$settings[80]\" SIZE=1>";
      $html .= "RTN " . $tracking_number;
   }    

   $html .= "</TD></TR></TABLE>";
   $html .= $settings[64];
   
   if (($store_url =~ /\S/) && ($store_url ne "NONE"))
   {
      $html .= "<BR><FONT SIZE=-2 FACE=\"Arial\">";
      $html .= "<I><B><A HREF=\"" . $store_url . "\">" . $store_name . "</A></B></I>";
      $html .= "</FONT>";
   }    
   
   return ($html);
}

# Subroutine to create the review order text.
sub get_receipt_text
{
   local ($customer_id, $tracking_number, $previous_page, $storekeeper) = @_;
   local (@lines, 
          $i,
          $cart_text,
          $text,
          $store_name,
          $store_address,
          $store_phone,
          $store_fax,
          $store_url,
          $primary_email,
          $secondary_email,
          $tax_instructions,
          $separate_shipping,
          $shipping_instructions,
          $gift_wrapping,
          $gift_wrapping_instructions,
          $coupon_instructions,
          $coupon1_number,
          $coupon2_number,
          $coupon3_number,
          $thank_you_message,
          $tax_shipping,
          $comments_section,
          $customer_first_name,
          $customer_last_name,
          $customer_street1,
          $customer_street2,
          $customer_city,
          $customer_state,
          $customer_zip_code,
          $customer_country,
          $customer_phone,
          $customer_fax,
          $customer_email,
          $customer_tax_rate,
          $customer_coupon_id,
          $customer_shipping_name,
          $customer_shipping_street1,
          $customer_shipping_street2,
          $customer_shipping_city,
          $customer_shipping_state,
          $customer_shipping_zip_code,
          $customer_shipping_country,
          $customer_shipping,
          $customer_giftwrap,
          $customer_gift_message,
          $customer_comments,       
          $final_subtotal1,
          $final_discount_percent,
          $final_subtotal2,
          $final_giftwrap_cost,
          $final_tax,
          $final_tax_rate,
          $final_shipping_handling,
          $final_tax_shipping,
          $final_discount_fee,
          $final_total);

   # Get general settings.
   &initialize_settings();

   # Decipher general settings.
   $store_name = $settings[0];
   $store_address = $settings[1];
   $store_phone = $settings[2];
   $store_fax = $settings[3];
   $store_url = $settings[4];
   $primary_email = $settings[5];
   $secondary_email = $settings[6];
   $tax_instructions = $settings[17];
   $separate_shipping = $settings[20];
   $shipping_instructions = $settings[21];
   $gift_wrapping = $settings[39];
   $gift_wrapping_instructions = $settings[41];
   $coupon_instructions = $settings[43];
   $coupon1_number = $settings[44];
   $coupon2_number = $settings[46];
   $coupon3_number = $settings[48];
   $thank_you_message = $settings[50];
   $tax_shipping = $settings[53];
   $comments_section = $settings[54];
   
   # Get personal info.
   $customer_first_name = $form_text{'customer_first_name'};
   $customer_last_name = $form_text{'customer_last_name'};
   $customer_street1 = $form_text{'customer_street1'};
   $customer_street2 = $form_text{'customer_street2'};
   $customer_city = $form_text{'customer_city'};
   $customer_state = $form_text{'customer_state'};
   $customer_zip_code = $form_text{'customer_zip_code'};
   $customer_country = $form_text{'customer_country'};
   $customer_phone = $form_text{'customer_phone'};
   $customer_fax = $form_text{'customer_fax'};
   $customer_email = $form_text{'customer_email'};
   $customer_tax_rate = $form_text{'customer_tax_rate'};
   $customer_coupon_id = $form_text{'customer_coupon_id'};
   $customer_shipping_name = $form_text{'customer_shipping_name'};
   $customer_shipping_street1 = $form_text{'customer_shipping_street1'};
   $customer_shipping_street2 = $form_text{'customer_shipping_street2'};
   $customer_shipping_city = $form_text{'customer_shipping_city'};
   $customer_shipping_state = $form_text{'customer_shipping_state'};
   $customer_shipping_zip_code = $form_text{'customer_shipping_zip_code'};
   $customer_shipping_country = $form_text{'customer_shipping_country'};
   $customer_shipping = $form_text{'customer_shipping_type'};
   $customer_giftwrap = $form_text{'customer_giftwrap'};
   $customer_gift_message = $form_text{'customer_gift_message'};
   $customer_comments = $form_text{'customer_comments'};

   # Get totals.
   $final_subtotal1 = $form_text{'final_subtotal1'};
   $final_discount_percent = $form_text{'final_discount_percent'};
   $final_subtotal2 = $form_text{'final_subtotal2'};
   $final_giftwrap_cost = $form_text{'final_giftwrap_cost'};
   $final_tax = $form_text{'final_tax'};
   $final_tax_rate = $form_text{'final_tax_rate'};
   $final_shipping_handling = $form_text{'final_shipping_handling'};
   $final_tax_shipping = $form_text{'final_tax_shipping'};
   $final_discount_fee = $form_text{'final_discount_fee'};
   $final_total = $form_text{'final_total'};
   
   # Format TEXT.
   $text = "ORDER ID: " . $customer_id . "\n\n";
   $text .= "BILLING INFORMATION\n";
   $text .= $customer_first_name . " " . $customer_last_name . "\n";
   $text .= $customer_street1 . "\n";
   $text .= $customer_street2 . "\n";
   $text .= $customer_city . "\n";
   $text .= $customer_state . "\n";
   $text .= $customer_zip_code . "\n";
   $text .= $customer_country . "\n";
   $text .= "Phone: " . $customer_phone . "\n";
   $text .= "Fax: " . $customer_fax . "\n";
   $text .= "Email: " . $customer_email . "\n";
   $text .= "Tax Rate: " . $customer_tax_rate . "\n";

   if (($coupon1_number =~ /\S/) || ($coupon2_number =~ /\S/) || ($coupon3_number =~ /\S/))
   {
      if (!($customer_coupon_id =~ /\S/))
      {
         $text .= "Coupon: None\n";
      }
      else
      {
         $text .= "Coupon: " . $customer_coupon_id . "\n";
      }
   }
   $text .= "\nSHIPPING INFORMATION\n";
   if ($separate_shipping =~ /yes/i)
   {
      if (($customer_shipping_name =~ /\S/) &&
          ($customer_shipping_street1 =~ /\S/) &&
          ($customer_shipping_city =~ /\S/))
      {
         $text .= $customer_shipping_name . "\n";
         $text .= $customer_shipping_street1 . "\n";
         $text .= $customer_shipping_street2 . "\n";
         $text .= $customer_shipping_city . "\n";
         $text .= $customer_shipping_state . "\n";
         $text .= $customer_shipping_zip_code . "\n";
         $text .= $customer_shipping_country . "\n";
      }
   }
   $text .= "Shipping Method: " . $customer_shipping . "\n";
   if ($gift_wrapping =~ /yes/i)
   {
      if ($customer_giftwrap =~ /yes/i)
      {
         $text .= "Giftwrap? Yes\n";
         $customer_gift_message =~ s/<BR>/\n/g;
         $text .= "Message: " . $customer_gift_message . "\n"; 
         $customer_gift_message =~ s/\n/<BR>/g;
      }
      else
      {
         $text .= "Giftwrap? No\n";
      }
   }

   if ($comments_section =~ /yes/i)
   {
      $customer_comments =~ s/<BR>/\n/g;
      $text .= "\nADDITIONAL COMMENTS\n" . $customer_comments . "\n\n";
      $customer_comments =~ s/\n/<BR>/g;
   }

   # Print Cart.
   $cart_text = &get_cart_text($customer_id,
                               $final_subtotal1,
                               $final_discount_percent,
                               $final_subtotal2,
                               $final_giftwrap_cost,
                               $final_tax,
                               $final_tax_rate,
                               $final_shipping_handling,
                               $final_tax_shipping,
                               $final_discount_fee,
                               $final_total);
 
   $text .= "\n" . $cart_text;
   if (($thank_you_message =~ /\S/) && ($thank_you_message ne "NONE"))
   {
      $thank_you_message =~ s/<BR>/\n/g;
      $text .= "\n" . $thank_you_message . "\n\n";
   }    
   if (($storekeeper =~ /yes/i) && ($tracking_number =~ /\S/) && ($tracking_number ne "NONE"))
   {
      $text .= "RTN " . $tracking_number . "\n";
   }    
   
   return ($text);
}

# Subroutine to get the text format of the shopping cart.
sub get_cart_text
{
   local ($customer_id,
          $subtotal1,
          $discount_percent,
          $subtotal2,
          $giftwrap,
          $tax,
          $tax_rate,
          $shipping_handling,
          $tax_shipping_handling,
          $discount_fee,
          $total_amount) = @_;

   local ($product_id,
          $product_name,
          $product_short_description,
          $product_weight,
          $product_price,
          $product_taxable,
          $product_option1,
          $product_option2,
          $product_option3,
          $product_text,
          $product_textarea,
          $product_min_quantity,
          $product_max_quantity,
          $product_quantity,
          $total_quantity,
          $price,
          $total, 
          $file_name,
          @lines, 
          @key_value_pairs, 
          $i,          
          $formatted_price,
          $formatted_total,
          $formatted_output,
          $key_value,
          $key,
          $value,
          $cart_text,
          $product_options,
          $formatted_line);

   # Set file name based on the $customer_id.
   $file_name = $data_directory . $customer_id . ".cart";

   # Start cart text. 
   $cart_text = "\n";
   $cart_text .= "ID          NAME/OPTIONS                                 PRICE     QTY    TOTAL COST\n";
   $cart_text .= "-----------+--------------------------------------------+---------+------+----------\n";
 
   # Open cart file for reading.
   if (open(CART_FILE, $file_name))
   {
      # Read the entire cart file.
      @lines = <CART_FILE>;
   
      # Close the cart file.
      close(CART_FILE);

      # Loop through cart, update totals, and generate html.
      for ($i = 0; $i <= $#lines; $i++)
      {
         # Remove the carriage return.
         chop($lines[$i]);
         
         # Split line into values.
         @key_value_pairs = split (/\|/, $lines[$i]);
         $product_id = $key_value_pairs[0];
         $product_name = $key_value_pairs[1];
         $product_short_description = $key_value_pairs[2];
         $product_weight = $key_value_pairs[3];
         $product_price = $key_value_pairs[4];
         $product_taxable = $key_value_pairs[5];
         $product_option1 = $key_value_pairs[6];
         $product_option2 = $key_value_pairs[7];
         $product_option3 = $key_value_pairs[8];
         $product_text = $key_value_pairs[9];
         $product_textarea = $key_value_pairs[10];
         $product_min_quantity = $key_value_pairs[11];
         $product_max_quantity = $key_value_pairs[12];
         $product_quantity = $key_value_pairs[13];
      
         # Get the total quantity of this product id from cart.
         $total_quantity = &get_product_quantity($customer_id,$product_id);
      
         # Set the product price and total.
         $price = &get_product_price($product_option1,
                                     $product_option2,
                                     $product_option3,
                                     $total_quantity,
                                     $product_price);

         $price = &strip_letters($price);
         $total = $price * $product_quantity;
   
         # Format the price and total.
         $formatted_price = sprintf("%s%.2f",$currency_symbol,$price);
         if ($product_taxable =~ /no/i)
         {
            $formatted_total = sprintf("%s%.2f *",$currency_symbol,$total);
         }
         else
         {
            $formatted_total = sprintf("%s%.2f",$currency_symbol,$total);
         }

         # Format options.
         $product_options = $product_name;
         if ($product_option1 =~ /\S/)
         {
            $product_options .= "; " . $product_option1;
         }
         if ($product_option2 =~ /\S/)
         {
            $product_options .= "; " . $product_option2;
         }
         if ($product_option3 =~ /\S/)
         {
         	 $product_options .= "; " . $product_option3;
         }
         if (($product_text =~ /\S/) && ($product_text ne "NONE"))
         {
         	 $product_options .= "; Text: " . $product_text;
         }         

         if (length($product_options) > 43)
         {
         	$product_options = substr($product_options,0,40);
         	$product_options .= "...";
         }
         
         # Format the entire line.
         $formatted_line = sprintf("%-11s %-44s %-9s %-6s %s\n",$product_id,$product_options,$formatted_price,$product_quantity,$formatted_total);

         # Add line to cart text.
         $cart_text .= $formatted_line;         
      }
   }
   else
   {
      $cart_text .= "\nYour shopping cart is empty.\n";
   }   	

   # Display subtotal.
   $formatted_output = sprintf("%.2f",$subtotal1);   
   $cart_text .= "\nSubtotal: " . $currency_symbol . " " . $formatted_output . "\n";
   # Display subtotal minus percent discount.
   if ($discount_percent > 0.0)
   {
      $formatted_output = sprintf("%.2f",$subtotal2);   
      $cart_text .= "Discounted Subtotal (-" . $discount_percent . "%): " . $currency_symbol . " " . $formatted_output . "\n";
   }
   # Display gift charge.
   if ($giftwrap > 0.0)
   {
      $formatted_output = sprintf("%.2f",$giftwrap);   
      $cart_text .= "Giftwrap Charge: " . $currency_symbol . " " . $formatted_output . "\n";
   }
   # Display shipping.
   if ($tax_shipping_handling =~ /yes/i)
   {
      $formatted_output = sprintf("%.2f",$shipping_handling);   
      $cart_text .= "Shipping & Handling: " . $currency_symbol . " " . $formatted_output . "\n";
   }
   # Display tax.
   $formatted_output = sprintf("%.2f",$tax);   
   $cart_text .= "Tax (" . $tax_rate . "%): " . $currency_symbol . " " . $formatted_output . "\n";
   # Display shipping.
   if (!($tax_shipping_handling =~ /yes/i))
   {
      $formatted_output = sprintf("%.2f",$shipping_handling);   
      $cart_text .= "Shipping & Handling: " . $currency_symbol . " " . $formatted_output . "\n";
   }
   # Display discount.
   if ($discount_fee > 0.0)
   {
      $formatted_output = sprintf("%.2f",$discount_fee);   
      $cart_text .= "Coupon Discount: " . $currency_symbol . " " . $formatted_output . "\n";
   }
   # Display total.
   $formatted_output = sprintf("%.2f",$total_amount);   
   $cart_text .= "Total: " . $currency_symbol . " " . $formatted_output . "\n\n";

   return ($cart_text);
}

# Subroutine to write the order file.
sub write_order_file
{
   local ($customer_id, $tracking_number, $payment_line) = @_;
   local ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst,$formatted_date,
          $record_customer_info,
          $file_name,
          $cart_data,
          $customer_first_name,
          $customer_last_name,
          $customer_street1,
          $customer_street2,
          $customer_city,
          $customer_state,
          $customer_zip_code,
          $customer_country,
          $customer_phone,
          $customer_fax,
          $customer_email,
          $customer_tax_rate,
          $customer_coupon_id,
          $customer_shipping_name,
          $customer_shipping_street1,
          $customer_shipping_street2,
          $customer_shipping_city,
          $customer_shipping_state,
          $customer_shipping_zip_code,
          $customer_shipping_country,
          $customer_shipping,
          $customer_giftwrap,
          $customer_gift_message,
          $customer_comments,     
          $final_subtotal1,
          $final_discount_percent,
          $final_subtotal2,
          $final_giftwrap_cost,
          $final_tax,
          $final_tax_rate,
          $final_shipping_handling,
          $final_tax_shipping,
          $final_discount_fee,
          $final_total);

   # Get personal info.
   $customer_first_name = $form_text{'customer_first_name'};
   $customer_last_name = $form_text{'customer_last_name'};
   $customer_street1 = $form_text{'customer_street1'};
   $customer_street2 = $form_text{'customer_street2'};
   $customer_city = $form_text{'customer_city'};
   $customer_state = $form_text{'customer_state'};
   $customer_zip_code = $form_text{'customer_zip_code'};
   $customer_country = $form_text{'customer_country'};
   $customer_phone = $form_text{'customer_phone'};
   $customer_fax = $form_text{'customer_fax'};
   $customer_email = $form_text{'customer_email'};
   $customer_tax_rate = $form_text{'customer_tax_rate'};
   $customer_coupon_id = $form_text{'customer_coupon_id'};
   $customer_shipping_name = $form_text{'customer_shipping_name'};
   $customer_shipping_street1 = $form_text{'customer_shipping_street1'};
   $customer_shipping_street2 = $form_text{'customer_shipping_street2'};
   $customer_shipping_city = $form_text{'customer_shipping_city'};
   $customer_shipping_state = $form_text{'customer_shipping_state'};
   $customer_shipping_zip_code = $form_text{'customer_shipping_zip_code'};
   $customer_shipping_country = $form_text{'customer_shipping_country'};
   $customer_shipping = $form_text{'customer_shipping_type'};
   $customer_giftwrap = $form_text{'customer_giftwrap'};
   $customer_gift_message = $form_text{'customer_gift_message'};
   $customer_comments = $form_text{'customer_comments'};
 
   # Validate personal info.
   $customer_first_name = &validate_string($customer_first_name);
   $customer_last_name = &validate_string($customer_last_name);
   $customer_street1 = &validate_string($customer_street1);
   $customer_street2 = &validate_string($customer_street2);
   $customer_city = &validate_string($customer_city);
   $customer_state = &validate_string($customer_state);
   $customer_zip_code = &validate_string($customer_zip_code);
   $customer_country = &validate_string($customer_country);
   $customer_phone = &validate_string($customer_phone);
   $customer_fax = &validate_string($customer_fax);
   $customer_email = &validate_email($customer_email);
   $customer_tax_rate = &validate_string($customer_tax_rate);
   $customer_coupon_id = &validate_string($customer_coupon_id);
   $customer_shipping_name = &validate_string($customer_shipping_name);
   $customer_shipping_street1 = &validate_string($customer_shipping_street1);
   $customer_shipping_street2 = &validate_string($customer_shipping_street2);
   $customer_shipping_city = &validate_string($customer_shipping_city);
   $customer_shipping_state = &validate_string($customer_shipping_state);
   $customer_shipping_zip_code = &validate_string($customer_shipping_zip_code);
   $customer_shipping_country = &validate_string($customer_shipping_country);
   $customer_shipping = &validate_string($customer_shipping);
   $customer_giftwrap = &validate_string($customer_giftwrap);
   $customer_gift_message =~ s/[\<\>]//g;
   $customer_gift_message = &validate_description($customer_gift_message);
   $customer_comments =~ s/[\<\>]//g;
   $customer_comments = &validate_description($customer_comments);

   # Get totals.
   $final_subtotal1 = $form_text{'final_subtotal1'};
   $final_discount_percent = $form_text{'final_discount_percent'};
   $final_subtotal2 = $form_text{'final_subtotal2'};
   $final_giftwrap_cost = $form_text{'final_giftwrap_cost'};
   $final_tax = $form_text{'final_tax'};
   $final_tax_rate = $form_text{'final_tax_rate'};
   $final_shipping_handling = $form_text{'final_shipping_handling'};
   $final_tax_shipping = $form_text{'final_tax_shipping'};
   $final_discount_fee = $form_text{'final_discount_fee'};
   $final_total = $form_text{'final_total'};

   # Validate totals.
   $final_subtotal1 = &validate_string($final_subtotal1);
   $final_discount_percent = &validate_string($final_discount_percent);
   $final_subtotal2 = &validate_string($final_subtotal2);
   $final_giftwrap_cost = &validate_string($final_giftwrap_cost);
   $final_tax = &validate_string($final_tax);
   $final_tax_rate = &validate_string($final_tax_rate);
   $final_shipping_handling = &validate_string($final_shipping_handling);
   $final_tax_shipping = &validate_string($final_tax_shipping);
   $final_discount_fee = &validate_string($final_discount_fee);
   $final_total = &validate_string($final_total);
   
   # Get the cart data formatted for the order file.
   $cart_data = &get_order_file_cart_data($customer_id);
   
   # Get the date and time.
   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
   $year -= 100;
   $formatted_date = sprintf("%02d/%02d/%02d %02d:%02d",$mon+1,$mday,$year,$hour,$min);

   # Set file name based on the $customer_id.
   $file_name = $data_directory . $customer_id . ".order";

   # Write to a new order file.
   if (open(ORDER_FILE, ">" . $file_name))
   {
      # Write Order Id, Date, and Tracking Number.
      print ORDER_FILE $customer_id . "|" .
                       $formatted_date . "|" .
                       $tracking_number . "\n";

      # Write customer information to customer's cart file.
      print ORDER_FILE $customer_first_name . "|" .
                       $customer_last_name . "|" .
                       $customer_street1 . "|" .
                       $customer_street2 . "|" .
                       $customer_city . "|" .
                       $customer_state . "|" .
                       $customer_zip_code . "|" .
                       $customer_country . "|" .
                       $customer_phone . "|" .
                       $customer_fax . "|" .
                       $customer_email . "|" .
                       $customer_tax_rate . "|" .
                       $customer_coupon_id . "|" .
                       $customer_shipping_name . "|" .
                       $customer_shipping_street1 . "|" .
                       $customer_shipping_street2 . "|" .
                       $customer_shipping_city . "|" .
                       $customer_shipping_state . "|" .
                       $customer_shipping_zip_code . "|" .
                       $customer_shipping_country . "|" .
                       $customer_shipping . "|" .
                       $customer_giftwrap . "|" .
                       $customer_gift_message . "|" .
                       $customer_comments . "\n";
                       
      # Write the payment information.
      print ORDER_FILE $payment_line;

      # Write totals/shipping to customer's cart file.
      print ORDER_FILE $final_subtotal1 . "|" .
                       $final_discount_percent . "|" .
                       $final_subtotal2 . "|" .
                       $final_giftwrap_cost . "|" .
                       $final_tax . "|" .
                       $final_tax_rate . "|" .
                       $final_shipping_handling . "|" .
                       $final_tax_shipping . "|" .
                       $final_discount_fee . "|" .
                       $final_total . "\n";
          
      # Write the shopping cart data to customer's cart file.
      print ORDER_FILE $cart_data;
       
      # Close file.
      close(ORDER_FILE);
   }
   else
   {
      # Indicate that the file was not opened.
      &return_error("File Error","Unable to open $customer_id order file.");
   }

   # RECORD CUSTOMER INFO.
   # Get general settings and whether to record the customer's address.
   &initialize_settings();
   $record_customer_info = $settings[55];

   # Record customer address if address recording has been selected.
   if ($record_customer_info =~ /yes/i)
   {
      # Append to address file.
      if (open(ADDRESS_FILE, ">>" . $gAddress_data_file))
      {
         # Write customer information to customer's cart file.
         print ADDRESS_FILE $customer_first_name . "|" .
                            $customer_last_name . "|" .
                            $customer_street1 . "|" .
                            $customer_street2 . "|" .
                            $customer_city . "|" .
                            $customer_state . "|" .
                            $customer_zip_code . "|" .
                            $customer_country . "|" .
                            $customer_phone . "|" .
                            $customer_fax . "|" .
                            $customer_email . "\n";
                       
         # Close file.
         close(ADDRESS_FILE);
      }
   }
   # END OF RECORD CUSTOMER INFO.
}

# Subroutine to write the affilate data.
sub write_affiliate_data
{
   local ($customer_id, $tracking_number) = @_;
   local ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst,$formatted_date,$subtotal,$record_tracking_info);

   # Get general settings.
   &initialize_settings();

   # Check if affiliate tracking is enabled.
   $record_tracking_info = $settings[56];
   if (($record_tracking_info =~ /yes/i) && ($tracking_number =~ /\S/))
   {
      # Get totals.
      $subtotal = $form_text{'final_subtotal2'};
  
      # Get the date.
      ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
      $year -= 100;
      $formatted_date = sprintf("%02d/%02d/%02d",$mon+1,$mday,$year);

      # Set file name based on the $customer_id.
      $file_name = $data_directory . $customer_id . ".order";

      # Append to affiliate data file.
      if (open(AFFILIATE_FILE, ">>" . $gAffiliate_data_file))
      {
         # Write Date, Tracking Number, Order ID, and Subtotal.
         print AFFILIATE_FILE $formatted_date . "|" .
                              $tracking_number . "|" .
                              $customer_id . "|" . 
                              $subtotal . "\n";
                              
         # Close file.
         close(AFFILIATE_FILE);
      }
   }   
}

# Subroutine to update logs.
sub update_log
{
   if (!(-e $gLog1))
   {
      if (!(-e $gLog2))
      {
   	     if (open(DLOG, ">" . $gLog2))
   	     {
   	        print DLOG "LOG\n";
   	        close(DLOG);
   	     }
      }
      else
      {
         if ((time - (stat($gLog2))[9]) > 604800)
         {
            rename($gLog2,$gLog1);
            &diagnostics_test();
         }
      }
   }
}

# Subroutine to get the cart data for the order file.
sub get_order_file_cart_data
{
   local ($customer_id) = @_;
   local ($product_id,
          $product_name,
          $product_short_description,
          $product_weight,
          $product_price,
          $product_taxable,
          $product_option1,
          $product_option2,
          $product_option3,
          $product_text,
          $product_textarea,
          $product_min_quantity,
          $product_max_quantity,
          $product_quantity,
          $total_quantity,
          $price,
          $total, 
          $formatted_price,
          $formatted_total,
          $file_name,
          $i,      
          @lines, 
          @key_value_pairs, 
          $return_data);

   # Set file name based on the $customer_id.
   $file_name = $data_directory . $customer_id . ".cart";

   # Reset return data.
   $return_data = "";
   
   # Open cart file for reading.
   if (open(CART_FILE, $file_name))
   {
      # Read the entire cart file.
      @lines = <CART_FILE>;
   
      # Close the cart file.
      close(CART_FILE);

      # Loop through cart, update totals, and generate html.
      for ($i = 0; $i <= $#lines; $i++)
      {
         # Remove the carriage return.
         chop($lines[$i]);
         
         # Split line into values.
         @key_value_pairs = split (/\|/, $lines[$i]);
         $product_id = $key_value_pairs[0];
         $product_name = $key_value_pairs[1];
         $product_short_description = $key_value_pairs[2];
         $product_weight = $key_value_pairs[3];
         $product_price = $key_value_pairs[4];
         $product_taxable = $key_value_pairs[5];
         $product_option1 = $key_value_pairs[6];
         $product_option2 = $key_value_pairs[7];
         $product_option3 = $key_value_pairs[8];
         $product_text = $key_value_pairs[9];
         $product_textarea = $key_value_pairs[10];
         $product_min_quantity = $key_value_pairs[11];
         $product_max_quantity = $key_value_pairs[12];
         $product_quantity = $key_value_pairs[13];
      
         # Get the total quantity of this product id from cart.
         $total_quantity = &get_product_quantity($customer_id,$product_id);
      
         # Set the product price and total.
         $price = &get_product_price($product_option1,
                                     $product_option2,
                                     $product_option3,
                                     $total_quantity,
                                     $product_price);

         $price = &strip_letters($price);
         $total = $price * $product_quantity;
   
         # Format the price and total.
         $formatted_price = sprintf("%s%.2f",$currency_symbol,$price);
         $formatted_total = sprintf("%s%.2f",$currency_symbol,$total);
         
         # Format the return data.
         $return_data .= $product_id . "|" .
                         $product_name . "|" .
                         $product_short_description . "|" .
                         $product_weight . "|" .
                         $product_price . "|" .
                         $product_taxable . "|" .
                         $product_option1 . "|" .
                         $product_option2 . "|" .
                         $product_option3 . "|" .
                         $product_text . "|" .
                         $product_textarea . "|" .
                         $product_min_quantity . "|" .
                         $product_max_quantity . "|" .
                         $product_quantity . "|" .
                         $formatted_price . "|" .
                         $formatted_total . "\n";
      }
   }
   else
   {
      $return_data .= "NONE\n";
   }   	

   return ($return_data);  
}

# Subroutine to create and return the category html file selections.
sub get_category_html_file_selections
{
   local ($category_data, $line, $category_name, $file_name);
   
   # Open category file for reading.
   open (CATEGORY_INDEX_FILE, $gCategory_index_file) ||
      &return_error("File Error","Unable to open category index file.");

   # Create category options.
   $category_data = "<SELECT NAME=\"file\">";
   $category_data .= "<OPTION VALUE=\"NOTSELECTED\">Please Choose One:</OPTION>";

   # Read category file and create $category_options.
   while (<CATEGORY_INDEX_FILE>)
   {
      # Set $line equal to the last line read from file.
      $line = $_;

      # Remove carriage return.
      chop($line);

      # Get category name and file name.
      ($category_name, $file_name) = split (/\|/, $line);

      # Create the HTML file name.
      $file_name =~ s/.txt/.html/;

      # Append category onto $category_data.
      $category_data .= "<OPTION VALUE=\"" . $file_name . "\">" . $category_name . "</OPTION>";
   }

   # Close file.
   close(CATEGORY_INDEX_FILE);

   # Close selection.
   $category_data .= "</SELECT>";
   
   return ($category_data); 
}

# Subroutine to validate a credit card number.
sub validate_credit_card
{
   local ($type, $number) = @_;
   local ( $checksum, $i, $value, $num_digits );

   # Remove spaces and dashes.
   $number =~ s/ //g;
   $number =~ s/-//g;

   # Add a TEST ability.
   if ($number =~ /test/i)
   {
      return 1;
   }
   
   # Count number of digits.
   $num_digits = ($number =~ tr/0-9//);
   
   # Verify prefixes and number of digits.
   if (($type =~ /^master/i) || ($type =~ /^mc/))
   {
      if ($num_digits != 16)
      {
         return -1;
      }	
      if (!(($number =~ /^51/) || ($number =~ /^52/) || ($number =~ /^53/) || ($number =~ /^54/) || ($number =~ /^55/)))
      {
         return -2;
      }
   }
   elsif (($type =~ /^visa/i) || ($type =~ /^vi/i))
   {
      if (($num_digits != 16) && ($num_digits != 13))
      {
         return -1;
      }	
      if (!($number =~ /^4/))
      {
         return -2;
      }
   }
   elsif (($type =~ /^amex/i) || ($type =~ /^american/i) || ($type =~ /^ae/i))
   {
      if ($num_digits != 15)
      {
         return -1;
      }	
      if (!(($number =~ /^34/) || ($number =~ /^37/)))
      {
         return -2;
      }
   }
   elsif ($type =~ /^discover/i)
   {
      if ($num_digits != 16)
      {
         return -1;
      }	
      if (!($number =~ /^6011/))
      {
         return -2;
      }
   }
   elsif ($type =~ /^diners/i)
   {
      if ($num_digits != 14)
      {
         return -1;
      }	
      if (!(($number =~ /^300/) || ($number =~ /^301/) || ($number =~ /^302/) || ($number =~ /^303/) || ($number =~ /^304/) || ($number =~ /^305/) || ($number =~ /^36/) || ($number =~ /^38/)))
      {
         return -2;
      }
   }
   else
   {
      # For other credit card/payment types, just return OK.
      return 1;
   }

   # Calculate checksum.
   $checksum = 0;
   
   # Add evens.
   for ($i=$num_digits-2;$i>=0;$i-=2)
   {
      $value = substr($number,$i,1);
      $value *= 2;
      if ($value > 9)
      {
         $value = $value - 9;
      }
      $checksum += $value;
   }
   
   # Add odds.
   for ($i=$num_digits-1;$i>=0;$i-=2)
   {
      $value = substr($number,$i,1);
      $checksum += $value;
   }

   if (!($checksum%10))
   {
      return 2;
   }
   
   return -3;
}

# End of ustorekeeper-runtime.pl