#!/usr/bin/perl
#	version 3.030806,	coded by MrRat - http://www.mrrat.com,  GPL licensed - http://www.opensource.org/licenses/gpl-license.html
#	you can make a donation at http://s1.amazon.com/exec/varzea/pay/T3M26803DZOCMK
#	This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#use strict;
my (%FORM, %MY_variables, %associate_ids);

#
#  required changes:

#  Check the perl line at the top and make sure it points to the proper location.
#  Place this cgi in your cgi-bin directory and make it executable (chmod 755).
#  Put your Amazon.com affiliate id below.
$associate_ids{us} = [ "freewarfrommrrat" ];

#
#  basic options:

#  set the locale: "us", "uk", "de"
$FORM{locale} = "us";

#  If you are going to use locales othen than "us" you need to put those associate ids below.
$associate_ids{uk} = [ "mrratcom-21" ];
$associate_ids{de} = [ "ari04" ];


#  cache settings - file location and number of requests to cache
my $use_cache = "yes";
my $cache_file = "apf_xml_cache";
my $cache_max_size = "2000";


# setup initial variables
my (%chat_modes, %lookup_store, %store_to_browse, %all_store_to_browse, %all_catalog_to_mode, %catalog_to_mode, %sort_hash, %sort_hash_by_mode, %offering_type );
my ($debug, $debug_state, %AWS_variables, $session, %template_html, $nav_menu_type, $html);
my ($url_mode, $url_options, $see_url_options, %lookup_browse_id, $query, $my_marketplace_text, $sellerprofile_max, $amazon_server, $money_symbol, $details_max, $menu_length );
$MY_variables{script_name} = $ENV{SCRIPT_NAME};
if ($MY_variables{script_name} =~ /test.cgi/) { $debug_state = "on"; $cache_file = "apf_xml_cache.test"; } elsif (-e "ratlog.cgi") { require "ratlog.cgi"; }
$FORM{page_num} = 1;
$FORM{sort_type} = "+titlerank";
$FORM{max_results} = 50;
$MY_variables{products_html} .= qq[<SCRIPT>function noImageCheck(objImg) { if (objImg.width == 1) { objImg.src = "http://g-images.amazon.com/images/G/01/books/icons/books-no-image.gif"; } } </SCRIPT>\n];
get_url_input();
if ($FORM{search_type} eq "FeaturedProducts") { $FORM{item_id} = join(",", @featured_products); }
if ($FORM{mode} =~ /node:/) { $FORM{mode} =~ s/node:([^:]+):(.*)/$1/; $FORM{node} = $2; }
if (!$FORM{link_templates}) { $FORM{link_templates} = $FORM{templates}; }
$debug .= qq[mode = $FORM{mode} | id = $FORM{node} | search = $FORM{input_string}\n];
load_language(\@language_templates);
initialize_hashes();
load_templates();
START_PROCESSING_LABEL:
calculate_initial_variables();

# build the products_html
if ($FORM{cart_action}) {
	shopping_cart();
} elsif ( ($FORM{mode} and ($FORM{input_string} or $FORM{node}) and ($FORM{mode} ne "blended")) or $FORM{item_id} =~ /,/ or $FORM{search_type} =~ /(?:ThirdPartyNew|Used|Collectible|Refurbished|ListManiaSearch|SimilaritySearch|WishlistSearch)/) {
	# get products
	my $this_xml_url="http://$amazon_server.amazon.com/onca/xml3?t=$MY_variables{associate_id}&dev-t=D1KQJBNTALRLQH&$query&f=xml&sort=$FORM{sort_type}&locale=$FORM{locale}";
	$debug .= "xml_url=$this_xml_url\n";
	if ($FORM{search_type} =~ /(?:ThirdPartyNew|Used|Collectible|Refurbished|ListManiaSearch|SimilaritySearch|WishlistSearch)/) { $this_xml_url="http://$amazon_server.amazon.com/onca/xml3?t=$MY_variables{associate_id}&dev-t=D1KQJBNTALRLQH&$query&f=xml&locale=$FORM{locale}"; }
	my $xml_result = get_url($this_xml_url);
	my $level_1 = parse_xml_into_hashes_of_arrays($xml_result);
	if ($AWS_variables{TotalResults} eq 1) {
		${${$level_1}{Details}}[0] =~ /<Asin>([^<]+)<\/Asin>/;
		delete $MY_variables{header}; delete $FORM{mode}; delete $FORM{input_mode}; delete $FORM{node}; $FORM{search_type} = "AsinSearch"; $FORM{item_id} = "$1";
		goto START_PROCESSING_LABEL;	# yes i know goto is lame, bite me
	} else {
		assign_variables("products",$level_1);
	}
} elsif ($FORM{search_type} =~ /(?:AsinSearch|CustomerReviews)/) {
	# get item by ASIN/ISBN
	my $this_xml_url = "http://$amazon_server.amazon.com/onca/xml3?t=$MY_variables{associate_id}&dev-t=D1KQJBNTALRLQH&AsinSearch=$FORM{item_id}&type=heavy&f=xml&locale=$FORM{locale}";
	$debug .= "xml_url=$this_xml_url\n";
	my $xml_result = get_url($this_xml_url);
	my $level_1 = parse_xml_into_hashes_of_arrays($xml_result);
	if ($FORM{search_type} eq "CustomerReviews") {
		assign_variables("customer_reviews",$level_1);
	} else {
		assign_variables("item",$level_1);
	}
	$MY_variables{store} = $lookup_store{$catalog_to_mode{$AWS_variables{Catalog}}}; $MY_variables{subject} = $AWS_variables{ProductName};
} elsif ($FORM{search_type} eq "UpcSearch") {
	# get item by UPC
	my $this_xml_url = "http://$amazon_server.amazon.com/onca/xml3?t=$MY_variables{associate_id}&dev-t=D1KQJBNTALRLQH&UpcSearch=$FORM{item_id}&mode=$FORM{mode}&type=heavy&f=xml&locale=$FORM{locale}";
	$debug .= "xml_url=$this_xml_url\n";
	my $xml_result = get_url($this_xml_url);
	my $level_1 = parse_xml_into_hashes_of_arrays($xml_result);
	assign_variables("item",$level_1);
	$MY_variables{store} = $AWS_variables{Catalog}; $MY_variables{subject} = $AWS_variables{ProductName};
} elsif ($FORM{mode} eq "blended") {
	# blended search
	my $this_xml_url = "http://$amazon_server.amazon.com/onca/xml3?t=$MY_variables{associate_id}&dev-t=D1KQJBNTALRLQH&BlendedSearch=$FORM{input_string}&type=lite&f=xml&locale=$FORM{locale}";
	$debug .= "xml_url=$this_xml_url\n";
	my $xml_result = get_url($this_xml_url);
	my $level_1 = parse_xml_into_hashes_of_arrays($xml_result);
	blended_search($level_1);
} elsif ($FORM{search_type} eq "image") {
	# get large image
	my $this_xml_url="http://$amazon_server.amazon.com/onca/xml3?t=$MY_variables{associate_id}&dev-t=D1KQJBNTALRLQH&AsinSearch=$FORM{item_id}&type=lite&f=xml&locale=$FORM{locale}";
	$debug .= "xml_url=$this_xml_url\n";
	my $xml_result = get_url($this_xml_url);
	$xml_result =~ s/<([^>]+)>([^<]+)<\/\1>/$AWS_variables{$1} = $2;/gsie;
	$MY_variables{store} = $AWS_variables{Catalog}; $MY_variables{subject} = $AWS_variables{ProductName};
	$MY_variables{products_html} .= set_html("larger_image");
} elsif ($FORM{search_type} eq "SellerSearch") {
	# get SellerProfile
	my $this_xml_url="http://$amazon_server.amazon.com/onca/xml3?t=$MY_variables{associate_id}&dev-t=D1KQJBNTALRLQH&SellerProfile=$FORM{input_string}&type=lite&f=xml&locale=$FORM{locale}";
	$debug .= "xml_url=$this_xml_url\n";
	my $xml_result = get_url($this_xml_url);
	my $level_1 = parse_xml_into_hashes_of_arrays($xml_result);
	assign_variables("sellerprofile",$level_1);
	$MY_variables{store} = $AWS_variables{Catalog}; $MY_variables{subject} = $AWS_variables{ProductName};
	# get SellerSearch
	my $this_xml_url="http://$amazon_server.amazon.com/onca/xml3?t=$MY_variables{associate_id}&dev-t=D1KQJBNTALRLQH&$query&f=xml&sort=$FORM{sort_type}&locale=$FORM{locale}";
	$debug .= "xml_url=$this_xml_url\n";
	my $xml_result = get_url($this_xml_url);
	my $level_1 = parse_xml_into_hashes_of_arrays($xml_result);
	assign_variables("sellersearch",$level_1);
} elsif ($FORM{search_type} eq "ExchangeSearch") {
	# get ExchangeSearch
	my $this_xml_url="http://$amazon_server.amazon.com/onca/xml3?t=$MY_variables{associate_id}&dev-t=D1KQJBNTALRLQH&$query&f=xml&sort=$FORM{sort_type}&locale=$FORM{locale}";
	$debug .= "xml_url=$this_xml_url\n";
	my $exchange_xml_result = get_url($this_xml_url);
	$exchange_xml_result =~ s/<ExchangeSellerId>([^<]+)<\/ExchangeSellerId>/$AWS_variables{ExchangeSellerId} = $1/e; 
	# get SellerProfile
	my $this_xml_url="http://$amazon_server.amazon.com/onca/xml3?t=$MY_variables{associate_id}&dev-t=D1KQJBNTALRLQH&SellerProfile=$AWS_variables{ExchangeSellerId}&type=lite&f=xml&locale=$FORM{locale}";
	$debug .= "xml_url=$this_xml_url\n";
	my $xml_result = get_url($this_xml_url);
	my $level_1 = parse_xml_into_hashes_of_arrays($xml_result);
	assign_variables("sellerprofile",$level_1);
	# get ExchangeSearch
	my $xml_result = $exchange_xml_result;
	my $level_1 = parse_xml_into_hashes_of_arrays($xml_result);
	assign_variables("sellersearch",$level_1);
} elsif ($FORM{search_type} eq "AllSearches") {
	assign_variables("AllSearches");
} elsif ($FORM{search_type} eq "power_search_info") {
	$MY_variables{store} = "PowerSearch"; $MY_variables{subject} = "Details";
	$MY_variables{products_html} .= set_html("power_search_info");
} elsif ($FORM{mode}) {
	# display the categories for the mode
	$nav_menu_type = "modes";
	$MY_variables{subject} = $language_text{header_text5};
	$MY_variables{header} = qq[<A Href="$MY_variables{script_name}?mode=none$url_options">$language_text{header_text1}</A> > $MY_variables{store}<BR>];
	$MY_variables{products_html} = load_browse_table(\%{$FORM{mode}}, "mode=$FORM{mode}&node=");
	if (exists($chat_modes{$FORM{mode}})) {
		$MY_variables{chat_link} = qq[<A Href="#" onClick="childwin = window.open('http://www.amazon.com/exec/obidos/redirect?tag=$MY_variables{associate_id}&path=dt/assoc/tg/chat/age-verification/-/$chat_modes{$FORM{mode}}/true/].${$FORM{mode}}{"Top Selling"}.qq[/ref=br_chat_cc_av_e/', '', 'width=392,height=490'); childwin.opener = window;">Chat about $lookup_store{$FORM{mode}}</a><BR>];
	}
} else {
	# display the modes
	$nav_menu_type = "none";
	$MY_variables{subject} = $language_text{header_text1}; $MY_variables{store} = $language_text{header_text4};
	$MY_variables{products_html} = load_browse_table(\%store_to_browse, "mode=");
}

# build the page
if ($FORM{search_type} eq "SimilaritySearch") { $MY_variables{header} = qq[$language_text{my_similar_products_text1} $language_text{button_text4}]; }
elsif ($FORM{search_type} eq "ListManiaSearch") { $MY_variables{header} = "$AWS_variables{ListName}"; }
elsif (!$MY_variables{header}) { $MY_variables{header} = qq[$MY_variables{store} : $MY_variables{subject}]; }
if ($MY_variables{error_msg}) { $MY_variables{products_html} .= qq[<FONT Color="red" Size="+1">$MY_variables{error_msg}</FONT><BR>]; }
if ($debug_state eq "on") { $MY_variables{debug_html} = $debug; }
build_search_box();
if (!$AWS_variables{HMAC}) { build_international_links(); }
$html .= set_html("page");
$html .= qq[<CENTER><FONT Size="1"><A Href="http://www.mrrat.com/scripts.html" Target="_new">script by MrRat</A>];
if ($MY_variables{associate_id} =~ /(freewarfrommrrat|mrratcom-21)/) { $html .= "<BR>A portion of each sale goes to benefit MrRat.com, thank you."; }
$html .= qq[</FONT><BR></CENTER>];
# send the page to the browser
print "Content-type: text/html; charset=utf-8\n\n";
print $html;
exit;

#	the end - subs below

sub get_url_input {
	my ($form_pair,$form_name,$form_value,$item);
	for $form_pair (split(/&/, $ENV{QUERY_STRING})) {
		$form_pair =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		# remove all dangerous characters listed in WWW Security FAQ - http://www.w3.org/Security/Faq/
		# except characters ( ) * which are required for power searches
		# i know this doesn't untaint but can't figure out how to untaint accented characters without the overhead of locale
		$form_pair =~ s/[\&|\;|\`|\\|\"|\||\?|\~|\<|\>|\^|\[|\]|\{|\}|\$]/ /g;
		($form_name, $form_value) = split(/=/, $form_pair);
		if ($form_name eq $form_value) { $form_value = ""; }
		$FORM{$form_name} = $form_value;
	}
	if (%FORM) {
		if ($FORM{input_mode}) { $FORM{mode} = $FORM{input_mode}; }
		if ($FORM{input_templates}) { $FORM{templates} = $FORM{input_templates}; }
		if ($FORM{input_link_templates}) { $FORM{link_templates} = $FORM{input_link_templates}; }
		if ($FORM{input_language}) { $FORM{language} = $FORM{input_language}; }
		if ($FORM{input_sort}) { $FORM{sort_type} = $FORM{input_sort}; }
		if ($FORM{sort_type}) { $FORM{sort_type} =~ s/\s/+/g; }
		if ($FORM{input_max}) { $FORM{max_results} = $FORM{input_max}; }
		if ($FORM{input_link_max}) { $FORM{link_max_results} = $FORM{input_link_max}; }
		if ($FORM{input_locale}) { $FORM{locale} = $FORM{input_locale}; }
		if ($FORM{input_id}) { $FORM{node} = $FORM{input_id}; }
		if ($FORM{input_item}) { $FORM{item_id} = $FORM{input_item}; }
		if ($FORM{input_page}) { $FORM{page_num} = $FORM{input_page}; }
		if ($FORM{input_search_type}) { $FORM{search_type} = $FORM{input_search_type}; }
		if ($FORM{input_pricerange}) { $FORM{pricerange} = $FORM{input_pricerange}; }
		if ($FORM{input_keywords}) { $FORM{keywords} = $FORM{input_keywords}; }
		if ($FORM{input_cart_action}) { $FORM{cart_action} = $FORM{input_cart_action}; }
		if ($FORM{input_cart_item}) { $FORM{cart_item} = $FORM{input_cart_item}; }
	}
}

sub load_language {
	my $temp_input = shift;
	my @temp_array = @$temp_input;
	# start default language
	if ($FORM{locale} eq "uk") {
		$amazon_site = "Amazon.co.uk";
	} elsif ($FORM{locale} eq "de") {
		$amazon_site = "Amazon.de";
	} else {
		$amazon_site = "Amazon.com";
	}
	$language_text{header_text1} = "Bestsellers";
	$language_text{header_text2} = "Items";
	$language_text{header_text3} = "List";
	$language_text{header_text4} = "All Products";
	$language_text{header_text5} = "Categories";
	$language_text{my_artists_text1} = "by:";
	$language_text{my_artists_text2} = "starring:";
	$language_text{my_artists_text3} = "from:";
	$language_text{my_artists_text4} = "directed by:";
	$language_text{my_prices_text1} = "List Price:";
	$language_text{my_prices_text2} = $amazon_site . "'s Price:";
	$language_text{my_prices_text3} = "You Save:";
	$language_text{my_prices_text4} = $amazon_site . " prices subject to change.";
	$language_text{my_prices_text5} = "price not available";
	$language_text{button_text1} = "Buy from " . $amazon_site;
	$language_text{button_text2} = "Add to " . $amazon_site . " Wishlist";
	$language_text{button_text3} = "Add to " . $amazon_site . " Wedding Registry";
	$language_text{button_text4} = "Search";
	$language_text{button_text5} = "Add to " . $amazon_site . " Baby Registry";
	$language_text{button_text6} = "Sort";
	$language_text{button_text7} = "Change Quantities";
	$language_text{button_text8} = "Delete";
	$language_text{button_text9} = "Empty Shopping Cart";
	$language_text{button_text10} = "Add to Shopping Cart";
	$language_text{button_text11} = "See More Details";
	$language_text{my_marketplace1} = "Price:";
	$language_text{my_marketplace2} = "Used";
	$language_text{my_marketplace3} = "Collectible";
	$language_text{my_marketplace4} = "Third Party New";
	$language_text{my_marketplace5} = "Refurbished";
	$language_text{my_features_text1} = "Features:";
	$language_text{my_similar_products_text1} = "Similar Items:";
	$language_text{my_accessories_text1} = "Accessories:";
	$language_text{my_comments_text1} = "Rating:";
	$language_text{my_comments_text2} = "Read More";
	$language_text{my_descriptors_text1} = "Release Date:";
	$language_text{my_descriptors_text2} = "Theatrical Date:";
	$language_text{my_descriptors_text3} = "Media:";
	$language_text{my_descriptors_text4} = "Number of Media:";
	$language_text{my_descriptors_text5} = "MPAA Rating:";
	$language_text{my_descriptors_text6} = "Platform:";
	$language_text{my_descriptors_text7} = "ESRB Rating:";
	$language_text{my_descriptors_text8} = "Age Group:";
	$language_text{my_descriptors_text9} = "Sales Rank:";
	$language_text{my_descriptors_text10} = "Encoding:";
	$language_text{my_descriptors_text11} = "Tracks:";
	$language_text{my_descriptors_text12} = "Reading Level:";
	$language_text{my_descriptors_text13} = "Manufacturer:";
	$language_text{my_browse_list_text1} = "Browse:";
	$language_text{see_text1} = "Next &gt;";
	$language_text{see_text2} = "&lt; Previous";
	$language_text{see_text3} = "Back";
	$language_text{see_text4} = "page";
	$language_text{see_text5} = "of";
	$language_text{see_text6} = "see more";
	$language_text{association_text1} = "In association with " . $amazon_site;
	$language_text{availability_text1} = "Availability:";
	$language_text{availability_text2} = "Not currently available for purchase.";
	$language_text{see_larger_image_text1} = "See Larger Image";
	$language_text{customer_reviews_text1} = "Customer Reviews";
	$language_text{average_rating_text1} = "Average Rating: ";
	$language_text{average_rating_text2} = "none";
	$language_text{similar_text1} = "Featured Listmania! List";
	$language_text{thirdparty_text1} = "Seller:";
	$language_text{thirdparty_text2} = "Condition:";
	$language_text{thirdparty_text3} = "Condition Type:";
	$language_text{thirdparty_text4} = "From:";
	$language_text{thirdparty_text5} = "Comments:";
	$language_text{thirdparty_text6} = "Quantity:";
	$language_text{thirdparty_text7} = "Offering Type:";
	$language_text{thirdparty_text8} = "End Date:";
	$language_text{thirdparty_text9} = "Total Feedback:";
	$language_text{thirdparty_text10} = "Cancelled Auctions:";
	$language_text{thirdparty_text11} = "Date:";
	$language_text{cart_text1} = "Shopping Cart";
	$language_text{cart_text2} = "There is nothing in your shopping cart.";
	$language_text{cart_text3} = "View Shopping Cart or Checkout";
	$language_text{locale_text1} = "<FONT Size=-1>International Store:</FONT>";
	$language_text{locale_text2} = "<FONT Size=-1>USA</FONT>";
	$language_text{locale_text3} = "<FONT Size=-1>United Kingdom</FONT>";
	$language_text{locale_text4} = "<FONT Size=-1>Germany</FONT>";
	$language_text{locale_text5} = "<FONT Size=-1>Japan</FONT>";
	$language_text{sort_text1} = "Featured Items";
	$language_text{sort_text2} = "Bestselling";
	$language_text{sort_text3} = "Average Customer Review";
	$language_text{sort_text4} = "Price (Low to High)";
	$language_text{sort_text5} = "Price (High to Low)";
	$language_text{sort_text6} = "Publication Date";
	$language_text{sort_text7} = "Alphabetical (A-Z)";
	$language_text{sort_text8} = "Alphabetical (Z-A)";
	$language_text{sort_text9} = "Manufacturer (A-Z)";
	$language_text{sort_text10} = "Manufacturer (Z-A)";
	$language_text{sort_text11} = "Artist Name";
	$language_text{sort_text12} = "Original Release Date";
	$language_text{searchbox_text1} = "All Products";
	$language_text{searchbox_text2} = "Only within this Category";
	$language_text{searchbox_text3} = "Search Options";
	$language_text{searchbox_text4} = "Search by"; 
	$language_text{searchbox_text5} = "Item";
	$language_text{searchbox_text6} = "Actor/Actress";
	$language_text{searchbox_text7} = "Search String";
	$language_text{searchbox_text8} = "Sort by";
	$language_text{searchbox_text9} = "Artist/Musician";
	$language_text{searchbox_text10} = "Author"; 
	$language_text{searchbox_text11} = "BrowseNode"; 
	$language_text{searchbox_text12} = "Browse Id Number";   
	$language_text{searchbox_text13} = "Product Mode";
	$language_text{searchbox_text14} = "Price Range";
	$language_text{searchbox_text15} = "Director"; 
	$language_text{searchbox_text16} = "Listmania";
	$language_text{searchbox_text17} = "Manufacturer";
	$language_text{searchbox_text18} = "Power Search";
	$language_text{searchbox_text19} = "Similar Products";
	$language_text{searchbox_text20} = "third-party ASIN"; 
	$language_text{searchbox_text21} = "Third-party exchange ID";
	$language_text{searchbox_text22} = "Third-party Seller ID";
	$language_text{searchbox_text23} = "UPC";
	$language_text{searchbox_text24} = "WishList id";
	# end default language
	my $temp_num = $FORM{language} - 1;
	if ($FORM{language} and -e $temp_array[$temp_num]) {
		require $temp_array[$temp_num];
	} elsif ($FORM{locale} eq "de" and -e "apf_de.language") {
		require "apf_de.language";
	}
}

sub initialize_hashes {
	my %us_store_to_browse = ( "Baby" => "baby", "Books" => "books", "Classical Music" => "classical", "DVD" => "dvd", "Electronics" => "electronics", "Kitchen & Housewares" => "kitchen", "Magazines" => "magazines", "Outdoor Living" => "garden", "Popular Music" => "music", "Computers" => "pc_hardware", "Camera & Photo" => "photo", "Software" => "software", "Tools & Hardware" => "universal", "Toys & Games" => "toys", "Video" => "vhs", "Computer & Video Games" => "videogames" );
	my %uk_store_to_browse = ( "Books" => "books_uk", "Popular Music" => "music", "Classical Music" => "classical", "DVD" => "dvd_uk", "Video" => "vhs_uk", "Electronics" => "electronics_uk", "Kitchen & Housewares" => "kitchen_uk", "Software" => "software_uk", "Computer & Video Games" => "video_games_uk", "Toys & Games" => "toys_uk" );
	my %de_store_to_browse = ( "B&uuml;cher" => "books_de", "Musik" => "pop_music_de", "Musik (Klassische)" => "classical_de", "DVD" => "dvd_de", "Video" => "vhs_de", "Elektronik & Foto" => "ce_de", "K&uuml;che & Haushalt" => "kitchen_de", "Software" => "software_de", "Computer & Video-Spiele" => "video_games_de", "Zeitschriften" => "magazines_de", "Englische  B&uuml;cher" => "books_de_intl_us");
	%all_store_to_browse = ( "us" => \%us_store_to_browse, "uk" => \%uk_store_to_browse, "de" => \%de_store_to_browse );
	my %us_catalog_to_mode = ( "Apparel" => "apparel", "Baby Product" => "baby", "Book" => "books", "DVD" => "dvd", "Electronics" => "electronics", "Home Improvement" => "universal", "Kitchen" => "kitchen", "Lawn &amp; Patio" => "garden", "Magazine" => "magazines", "Magazines" => "magazines", "Music" => "music", "Personal Computer" => "pc_hardware", "Software" => "software", "Toy" => "toys", "Video" => "vhs", "Video Games" => "videogames" );
	my %uk_catalog_to_mode = ( "Book" => "books_uk", "Music" => "music", "DVD" => "dvd_uk", "Video" => "vhs_uk", "Electronics" => "electronics_uk", "Kitchen" => "kitchen_uk", "Software" => "software_uk", "Video Games" => "video_games_uk", "Toy" => "toys_uk" );
	my %de_catalog_to_mode = ( "Book" => "books_de", "Music" => "pop_music_de", "DVD" => "dvd_de", "Video" => "vhs_de", "Electronics" => "ce_de", "Kitchen" => "kitchen_de", "Software" => "software_de", "Video Games" => "video_games_de", "Magazine" => "magazines_de" );
	%all_catalog_to_mode = ( "us" => \%us_catalog_to_mode, "uk" => \%uk_catalog_to_mode, "de" => \%de_catalog_to_mode );
	if ($FORM{locale} eq "uk") {
		%books_uk = ( "Top Selling" => 1025612, "Address Books, Journals & More" => 507848, "Art, Architecture & Photography" => 91, "Audio Cassettes" => 51, "Audio CDs" => 267859, "Biography" => 67, "Business, Finance & Law" => 68, "Children's Books" => 69, "Comics & Graphic Novels" => 274081, "Computers & Internet" => 71, "Crime, Thrillers & Mystery" => 72, "e-Books" => 637262, "Fiction" => 62, "Food & Drink" => 66, "Gay & Lesbian" => 275835, "Health, Family & Lifestyle" => 74, "History" => 65, "Home & Garden" => 64, "Horror" => 63, "Mind, Body & Spirit" => 61, "Music, Stage & Screen" => 73, "Poetry, Drama & Criticism" => 275389, "Reference & Languages" => 59, "Religion & Spirituality" => 58, "Romance" => 88, "Science Fiction & Fantasy" => 56, "Science & Nature" => 57, "Scientific, Technical & Medical" => 564334, "Society, Politics & Philosophy" => 60, "Sports, Hobbies & Games" => 55, "Travel & Holiday" => 83, "Young Adult" => 52 );
		%classical = ( "Top Selling" => 229817, "Classical for Beginners" => 508574, "Composers" => 506476, "Compilations" => 720862, "Occasions" => 508572, "Solo Instrumental" => 505660, "Chamber Music" => 505630, "Orchestral" => 505586, "Ballet & Dance" => 505622 );
		%dvd_uk = ( "Top Selling" => 573406, "Action & Adventure" => 501778, "Art House & International" => 501796, "Children's DVD" => 501858, "Classics" => 501976, "Comedy" => 501866, "Drama" => 501872, "DVD Special Offers" => 655852, "Horror & Suspense" => 501880, "Music" => 501888, "Musicals & Classical" => 1108824, "Science Fiction & Fantasy" => 501912, "Sports & Fitness" => 501940, "Television & Documentary" => 501958 );
		%electronics_uk = ( "Top Selling" => 560800, "Computer Peripherals" => 561904, "Gadgets" => 3108311, "Handhelds & PDAs" => 560802, "Home Entertainment" => 560858, "Phones" => 560820, "Photography" => 560834, "Portable Audio" => 560884 );
		%kitchen_uk = ( "Top Selling" => 3147411, "Cookware" => 3147471, "Cutlery & Knives" => 3147481, "Home Appliances" => 3147451, "Kitchen Accessories & Gadgets" => 3147491, "Kitchen Appliances" => 3147441, "Personal Care" => 3147461 );
		%music = ( "Top Selling" => 694208, "The PopShop" => 694266, "Adult Contemporary" => 694228, "Dance Pop" => 694296, "Greatest Hits" => 694212, "Live Albums" => 694214, "Disco" => 694308, "Pop R&B" => 694216 );
		%software_uk = ( "Top Selling" => 1025614, "Business" => 600014, "Children's Fun & Learning" => 300473, "Education & Reference" => 300436, "Graphics" => 600136, "Home Computing" => 599892, "Lifestyle & Hobbies" => 300479, "Linux" => 300543, "Macintosh" => 300530, "Programming & Web Development" => 600188, "Video & Music" => 600236 );
		%toys_uk = ( "Top Selling" => 595314, "Arts & Crafts" => 595346, "Baby & Pre-school Toys" => 595446, "Cars" => 1161582, "Collectables" => 1095918, "Development & Learning" => 595334, "Dolls & Accessories" => 595376, "Dressing Up & Make-believe" => 595398, "Electronic Toys" => 1193380, "Experiences" => 3052531, "Games & Puzzles" => 595426, "Outdoor Kit" => 1025554, "Playhouses & Tents" => 595406, "Playsets" => 1095914, "Ride-ons" => 1095910, "Skateboards" => 3059981, "Trains" => 595826, "Vehicles" => 1095916, "Wooden Toys" => 1025552 );
		%vhs_uk = ( "Top Selling" => 573400, "Action & Adventure" => 283921, "Art House & International" => 283922, "Children's Videos" => 283928, "Classic Films" => 283923, "Comedy" => 283924, "Drama" => 283925, "Fitness Corner" => 283995, "Horror & Suspense" => 283927, "Music & Performing Arts" => 283929, "Science Fiction & Fantasy" => 283930, "Special Interest" => 283931, "Sports" => 283932, "Television & Documentary" => 283933, "Video Special Offers" => 655858 );
		%video_games_uk = ( "Top Selling" => 1025616, "Children's PC & Video Games" => 501128, "Game Boy Advance" => 573612, "GameCube" => 660286, "Game Hardware" => 506846, "PC Games" => 300729, "PlayStation 2" => 526776, "PSone" => 300839, "Xbox" => 660202, "Other Platforms" => 725726 );
		%store_to_browse = %{$all_store_to_browse{uk}};
		%catalog_to_mode = %{$all_catalog_to_mode{uk}};
	} elsif ($FORM{locale} eq "de") {
		%books_de = ( "Belletristik" => 117, "B&ouml;rse & Geld" => 120, "Business & Karriere" => 403434, "Computer & Internet" => 124, "Fachb&uuml;cher" => 288100, "Film, Kultur & Comics" => 548400, "Geist & Wissen" => 119, "Kinder & Jugend B&uuml;cher" => 280652, "Kochen & Lifestyle" => 122, "Krimi & Thriller" => 287480, "Lernen & Nachschlagen" => 403432, "Musiknoten" => 1199902, "Naturwissenschaften & Technik" => 121, "Politik, Biografien & Geschichte" => 143, "Ratgeber" => 536302, "Reise & Sport" => 298002, "Science Fiction, Horror & Fantasy" => 142, "H&ouml;rb&uuml;cher" => 300259 );
		%books_de_intl_us =  ( "Arts & Photography" => 1, "Biographies & Memoirs" => 2, "Business & Investing" => 3, "Children's Books" => 4, "Computers & Internet" => 5, "Cooking, Food & Wine" => 6, "Engineering" => 13643, "Entertainment" => 86, "Gay & Lesbian" => 301889, "Health, Mind & Body" => 10, "History" => 9, "Home & Garden" => 48, "Horror" => 49, "Law" => 10777, "Literature & Fiction" => 17, "Medicine" => 13996, "Mystery & Thrillers" => 18, "Nonfiction" => 53, "Outdoors & Nature" => 290060, "Parenting & Families" => 20, "Professional & Technical" => 173507, "Reference" => 21, "Religion & Spirituality" => 22, "Romance" => 23, "Science" => 75, "Science Fiction & Fantasy" => 25, "Sports" => 26, "Teens" => 28, "Travel" => 27 );
		%classical_de = ( "Klassik" => 255966, "Popul&auml;re Klassik" => 518124, "Komponisten" => 256029, "Interpreten" => 513528, "Gattungen" => 550142, "Preiswerte Klassik" => 518342 );
		%dvd_de = ( "Action, Thriller & Horror" => 289093, "Kinder & Familie" => 290505, "Kom&ouml;die & Drama" => 290800, "Musik-DVDs" => 289312, "Originalfassungen" => 526774, "Science Fiction & Fantasy" => 548404, "TV & Dokumentationen" => 1027630 );
		%ce_de = ( "Computerzubeh&ouml;r" => 700962, "Heimkino & Video" => 761254, "HiFi & Audio" => 571760, "Kamera & Foto" => 571860, "PC & Notebook" => 3034831, "PDA & Organizer" => 569606, "Handy & Telefon" => 571954, "Tragbare Ger&auml;te" => 571712, "Zubeh&ouml;r" => 569866 );
		%kitchen_de = ( "Elektrische K&uuml;chenger&auml;te" => 3169321, "Elektrowerkzeug" => 3527241, "Geschirr, Besteck & Gl&auml;ser" => 3310821, "Haushaltsger&auml;te" => 3169211, "K&ouml;rperpflege & Bad" => 3169021, "K&uuml;chenger&auml;te & Kochgeschirr" => 3311201, "Tisch- & Wohnaccessoires" => 3312031, "Wohnen, Garten & Freizeit" => 3312261 );
		%pop_music_de = ( "Alternative" => 400118, "Comedy & Kabarett" => 255953, "Dance & Electronic" => 255883, "Diverses" => 255952, "Hard & Heavy" => 264898, "Jazz & Blues" => 255917, "Kindermusik & H&ouml;rspiele" => 255956, "Pop" => 264875, "Rap & HipHop" => 255902, "R&B & Soul" => 255895, "Reggae & Ska" => 464356, "Rock" => 264886, "Schlager & Oldies" => 264910, "Songwriter, Folk & Country" => 573044, "Soundtrack & Musical" => 264918, "Weltmusik" => 265502, "Box-Sets" => 1038400, "DVD-Audio" => 772670, "Limited Editions" => 914906, "Maxi-CDs" => 915202, "Super-Audio-CDs" => 914900, "TV-Werbung & Compilations" => 510728 );
		%software_de = ( "Betriebssysteme & Tools" => 1064170, "Bildung & Wissenschaft" => 408270, "B&uuml;ro & Gesch&auml;ft" => 408288, "Interessen & Unterhaltung" => 408308, "Kinder & Familie" => 408290, "Multimedia & Internet" => 408292, "PDAs & Handhelds" => 505578, "Programmierung & Webdesign" => 408306, "Macintosh" => 910084 );
		%magazines_de = ( "Auto & Motorrad" => 1196118, "Computer, Internet & Technik" => 1198616, "Erotik" => 1198618, "Familie, Hobby & Wohnen" => 1198620, "Jugend & Comics" => 1198622, "Mode, Lifestyle & Leute" => 1198628, "Musikzeitschriften" => 1199630, "Reise, Sport & Kultur" => 1198626, "TV & Kino" => 1198630, "Wirtschaft & Politik" => 1198624, "Internationale Presse" => 1198632, "Fachzeitschriften" => 1198634 );
		%vhs_de = ( "Action, Thriller & Science Fiction" => 284262, "Kinder & Familie" => 284264, "Kom&ouml;die & Drama" => 284263, "Musik, TV & Dokumentation" => 284265, "Originalfassungen" => 284267 );
		%video_games_de = ( "Computerspiele" => 301129, "Game Boy" => 301014, "Game Boy Advance" => 569128, "GameCube" => 639320, "Nintendo 64" => 301037, "PlayStation" => 300993, "PlayStation 2" => 516838, "Sega Dreamcast" => 301027, "Sega Saturn" => 1198490, "Xbox" => 575708, "Zubeh&ouml;r &amp; Hardware f&uuml;r PC-Spiele" => 643172, "Zubeh&ouml;r &amp; Hardware f&uuml;r Game Boy" => 301026, "Zubeh&ouml;r &amp; Hardware f&uuml;r Game Boy advanced" => 600590, "Zubeh&ouml;r &amp; Hardware f&uuml;r GameCube" => 901948, "Zubeh&ouml;r &amp; Hardware f&uuml;r Nintendo 64" => 408342, "Zubeh&ouml;r &amp; Hardware f&uuml;r PlayStation" => 403624, "Zubeh&ouml;r &amp; Hardware f&uuml;r PlayStation 2" => 516856, "Zubeh&ouml;r &amp; Hardware f&uuml;r Sega Dreamcast" => 301036, "Zubeh&ouml;r &amp; Hardware f&uuml;r Xbox" => 749286 );
		%store_to_browse = %{$all_store_to_browse{de}};
		%catalog_to_mode = %{$all_catalog_to_mode{de}};
	} else {
		%apparel = ( "Girls" => 1040664, "Boys" => 1040666, "Infants & Toddlers" => 1044500, "Women - Sleepware" => 1044462 );
		%baby = ( "Top Selling" => 540988, Outlet => 735502, "Activity" => 542460, "Bath & Potty" => 541586, "Feeding" => 541564, "Gear" => 541558, "Health & Safety" => 541580, "Layette" => 738468, "Nursery" => 541572, "Backpacks & Carriers" => 542456, "Car Seats" => 541560, "Strollers" => 541562, "Travel Systems" => 542442, "Playards" => 542468, "Bedding" => 541574, "Furniture" => 541576, "Breast-feeding" => 541568, "Bottle Feeding" => 541566, "Solid Feeding" => 541570, "Highchairs" => 542302, "Play Centers" => 548050, "Swings & Bouncers" => 542470 );
		%books = ( Outlet => 733804, "Arts & Photography" => 1, "Biographies & Memoirs" => 2, "Business & Investing" => 3, "Children's Books" => 4, "Computers & Internet" => 5, "Cooking, Food & Wine" => 6, "Engineering" => 13643, "Entertainment" => 86, "Gay & Lesbian" => 301889, "Health, Mind & Body" => 10, "History" => 9, "Home & Garden" => 48, "Horror" => 49, "Law" => 10777, "Literature & Fiction" => 17, "Medicine" => 13996, "Mystery & Thrillers" => 18, "Nonfiction" => 53, "Outdoors & Nature" => 290060, "Parenting & Families" => 20, "Professional & Technical" => 173507, "Reference" => 21, "Religion & Spirituality" => 22, "Romance" => 23, "Science" => 75, "Science Fiction & Fantasy" => 25, "Sports" => 26, "Teens" => 28, "Travel" => 27 );
		%classical = ( "Top Selling" => 85, "Ballets & Dances" => 5260, "Chamber Music" => 5318, "Featured Composers, A-Z" => 5338, "Featured Performers, A-Z" => 38472, "Forms & Genres" => 36632, "Historical Periods" => 36712, "Instruments" => 38374, "Sacred & Religious" => 63654, "Symphonies" => 63681 );
		%dvd = ( "Top Selling" => 404276, Outlet => 734006, "Action & Adventure" => 163296, "African American Cinema" => 538708, "Animation" => 712256, "Anime & Manga" => 517956, "Art House & International" => 163313, "Classics" => 163345, "Comedy" => 163357, "Cult Movies" => 466674, "Documentary" => 508532, "Drama" => 163379, "Gay & Lesbian" => 301667, "Horror" => 163396, "Kids & Family" => 163414, "Military & War" => 586156, "Musicals & Performing Arts" => 508528, "Music Video & Concerts" => 163420, "Mystery & Suspense" => 512030, "Science Fiction & Fantasy" => 163431, "Special Interests" => 163448, "Television" => 163450, "Westerns" => 163312, "Boxed Sets" => 501230 );
		%electronics = ( "Top Selling" => 493964, Outlet => 734050, "Accessories & Supplies" => 281407, "Audio & Video" => 1065836, "Computer Add-Ons" => 172455, "Gadgets" => 172517, "GPS & Navigation" => 172526, "Handhelds & PDAs" => 172594, "Office Electronics" => 172574, "Printers" => 172635, "Telephones" => 172606, "DVD Players" => 172514, "VCRs & DVRs" => 172669, "TV & HDTV" => 172659, "MP3 & Digital Audio" => 172630, "Car Accessories" => 226184, "Clocks & Clock Radios" => 509280, "Home Audio" => 172531, "Home Office" => 172574, "Home Video" => 172592, "Portable Audio & Video" => 172623 );
		%garden = ( "Top Selling" => 468250, Outlet => 734346, "Backyard Birding" => 553632, "Gifts" => 553648, "Grills & Fryers" => 553760, "Heating & Lighting" => 553778, "Sports & Lawn Games" => 892984, "Outdoor D&#233;cor" => 553788, "Patio Furniture" => 553824, "Pest Control" => 553844, "Lawn/Garden Tools" => 915484, "Camping" => 892986 );
		%kitchen = ( "Top Selling" => 491864, Outlet => 734070, "Baking" => 289668, "Bar Tools & Glasses" => 289728, "Coffee, Tea & Espresso" => 289742, "Cookware" => 289814, "Cutlery" => 289851, "Housewares" => 510080, "Small Appliances" => 289913, "Tableware" => 289891, "Gadgets" => 289754, "Grills & Fryers" => 553760 );
		%magazines = ( "Top Selling" => 599872, "Arts & Crafts" => 602314, "Automotive" => 602316, "Business & Finance" => 602320, "Computer & Internet" => 602324, "Electronics & Audio" => 602326, "Family & Parenting" => 602330, "Fashion & Style" => 602332, "Food & Gourmet" => 602334, "Games & Hobbies" => 602336, "Health & Fitness" => 602340, "Home & Garden" => 602344, "Men's Interest" => 602352, "Music" => 602354, "News & Politics" => 602358, "Science & Nature" => 602364, "Travel & Regional" => 602370, "Women's Interest" => 602372, "Pets" => 602360, "Children's" => 602322, "Entertainment" => 602328 );
		%music = ( "Top Selling" => 301668, Outlet => 734368, "Alternative Rock" => 30, "Blues" => 31, "Broadway & Vocalists" => 265640, "Children's" => 173425, "Christian & Gospel" => 173429, "Classic Rock" => 67204, "Country" => 16, "Dance & DJ" => 7, "Folk" => 32, "Hard Rock & Metal" => 67207, "International" => 33, "Latin" => 289122, "Miscellaneous" => 35, "New Age" => 36, "Opera & Vocal" => 84, "Pop" => 37, "Rap & Hip-Hop" => 38, "R&B" => 39, "Rock" => 40, "Soundtracks" => 42, "Indie" => 266023, "Jazz" => 34 );
		%pc_hardware = ( "Top Selling" => 565118, "Desktops" => 565098, "Notebooks" => 565108, "AMD" => 602286, "Apple" => 565124, "HP" => 565120, "IBM" => 603128, "Intel" => 565122, "Sony" => 565126, "Toshiba" => 598398, "Desktops" => 565098, "Accessories & Supplies" => 281407, "Refurbished" => 896918, "Handhelds & PDAs" => 172594, "Monitors" => 172509, "Printers" => 172635 );
		%photo = ( "Top Selling" => 508048, Outlet => 733952, "Accessories" => 172435, "Binoculars" => 499320, "Camcorders" => 172421, "Film Cameras" => 499106, "Projectors" => 525462, "Surveillance Systems" => 524136, "Digital Cameras" => 281052 );
		%software = ( "Top Selling" => 491286, Outlet => 735326, "Business & Office" => 229535, "Children's Software" => 229548, "Graphics" => 229614, "Home & Hobbies" => 229624, "Language & Travel" => 497026, "Linux" => 290562, "Networking" => 229637, "Operating Systems" => 229653, "Personal Finance" => 229540, "Programming" => 229667, "Software for Handhelds" => 229663, "Utilities" => 229672, "Video & Music" => 497022 );
		%toys = ( "Top Selling" => 491290, Outlet => 735410, "Action Figures" => 171662, "Arts & Crafts" => 171859, "Building Sets, Blocks & Models" => 171814, "Dolls" => 171569, "Electronics" => 720366, "Games" => 171689, "Sports & Outdoor Play" => 171960, "Stuffed Animals" => 171992, "Learning Toys" => 585496, "Bikes" => 569472, "Furniture" => 172790, "Puzzles" => 171744, "Vehicles" => 171600 );
		%universal = ( "Top Selling" => 468240, Outlet => 735342, "Automotive Supplies" => 553294, "Electrical" => 495266, "Hand Tools" => 551238, "Hardware" => 511228, "Heating & Cooling" => 495346, "Job Site Equipment" => 551240, "Lawn & Garden Tools" => 551242, "Lighting" => 495224, "Power Tools" => 551236, "Accessories" => 552262, "Air Tools" => 552684, "Cordless Tools" => 552738, "Sanders" => 552876, "Saws"=> 552894, "Painting" => 228899 );
		%vhs = ( "Top Selling" => 404274, Outlet => 735448, "Action & Adventure" => 141, "African American Cinema" => 301597, "Animation" => 712260, "Anime & Manga" => 281300, "Art House & International" => 126, "Classics" => 127, "Comedy" => 128, "Cult Movies" => 162482, "Drama" => 129, "Gay & Lesbian" => 301665, "Horror" => 131, "Kids & Family" => 132, "Military & War" => 586154, "Musicals & Performing Arts" => 508526, "Music Video & Concerts" => 133, "Mystery & Suspense" => 512026, "Science Fiction & Fantasy" => 144, "Special Interests" => 135, "Sports" => 169798, "Television" => 136, "Westerns" => 139725, "Boxed Sets" => 501232 );
		%videogames = ( "Top Selling" => 471280, Outlet => 733954, "Game Boy" => 229783, "Game Boy Advance" => 541020, "GameCube" => 541022, "Mac Games" => 229647, "More Systems" => 294940, "Nintendo 64" => 229763, "PC Games" => 229575, "PlayStation" => 229773, "PlayStation2" => 301712, "Sega Dreamcast" => 229793, "Xbox" => 537504, "Linux" => 290573, "NEOGEO" => 541018, "NES" => 566458, "Palm" => 498176, "Sega Game Gear" => 294942, "Sega Genesis" => 294943, "Sega Saturn" => 294944, "Super NES" => 294945 );
		%store_to_browse = %{$all_store_to_browse{us}};
		%catalog_to_mode = %{$all_catalog_to_mode{us}};
	}
	%sort_hash = ( "+pmrank" => $language_text{sort_text1}, "+salesrank" => $language_text{sort_text2}, "+reviewrank" => $language_text{sort_text3}, "+pricerank" => $language_text{sort_text4}, "+inverse-pricerank" => $language_text{sort_text5}, "+price" => $language_text{sort_text4}, "-price" => $language_text{sort_text5}, "+daterank" => $language_text{sort_text6}, "+titlerank" => $language_text{sort_text7}, "-titlerank" => $language_text{sort_text8}, "+manufactrank" => $language_text{sort_text9}, "-manufactrank" => $language_text{sort_text10}, "+artistrank" => $language_text{sort_text11}, "+orig-rel-date" => $language_text{sort_text12} );
	%sort_hash_by_mode = (
		"all" => ["+titlerank","+salesrank","+pmrank"],
		"books" => ["+pmrank","+salesrank","+reviewrank","+pricerank","+inverse-pricerank","+daterank","+titlerank","-titlerank"],
		"software" => ["+pmrank","+salesrank","+titlerank","-price","+price"],
		"garden" => ["+pmrank","+salesrank","+titlerank","-titlerank","+manufactrank","-manufactrank","+price","-price"],
		"universal" => ["+pmrank","+salesrank","+titlerank","-titlerank","+manufactrank","-manufactrank","+price","-price"],
		"photo" => ["+pmrank","+salesrank","+titlerank","-titlerank"],
		"pc_hardware" => ["+pmrank","+salesrank","+titlerank","-titlerank"],
		"videogames" => ["+pmrank","+salesrank","+price","-price","+titlerank"],
		"music" => ["+pmrank","+salesrank","+artistrank","+orig-rel-date","+titlerank"], 
		"classical" => ["+pmrank","+salesrank","+artistrank","+orig-rel-date","+titlerank"], 
		"office_products" => ["+pmrank","+salesrank","+reviewrank","+price","-price","+titlerank","-titlerank"],
		"electronics" => ["+pmrank","+salesrank","+reviewrank","+titlerank"],
		"kitchen" => ["+pmrank","+salesrank","+titlerank","-titlerank","+manufactrank","-manufactrank","+price","-price"],
	);
	%offering_type = ( new => "ThirdPartyNew", used => "Used", collectible => "Collectible", refurbished => "Refurbished" );
	%chat_modes = ( photo => 502394, electronics => 172282, videogames => 468642, books => 283155, music => 5174, dvd => 130, toys => 171280 );
}

sub calculate_initial_variables {
	my @associate_ids = @{$associate_ids{$FORM{locale}}};
	if ($FORM{templates} and @associate_ids[$FORM{templates}-1]) { $MY_variables{associate_id} = @associate_ids[$FORM{templates}-1]; }
	else { $MY_variables{associate_id} = @associate_ids[0]; }
	if ($FORM{locale} eq "uk") {
		$MY_variables{in_association} = qq[<A Href="http://www.amazon.co.uk/exec/obidos/redirect-home?site=amazon&tag=$MY_variables{associate_id}"><FONT Size="2">$language_text{association_text1}</FONT></A><BR>];
		$money_symbol = "&#163;";
		$amazon_server = "xml-eu";
	} elsif ($FORM{locale} eq "de") {
		$MY_variables{in_association} = qq[<A Href="http://www.amazon.de/exec/obidos/redirect-home?site=home&tag=$MY_variables{associate_id}"><FONT Size="2">$language_text{association_text1}</FONT></A><BR>];
		$money_symbol = "EUR ";
		$amazon_server = "xml-eu";
	} else {
		$MY_variables{in_association} = qq[<A Href="http://www.amazon.com/exec/obidos/redirect-home/$MY_variables{associate_id}?dev-t=D1KQJBNTALRLQH"><FONT Size="2">$language_text{association_text1}</FONT></A><BR>];
		$money_symbol = "&#36;";
		$amazon_server = "xml";
	}
	if ($FORM{mode} eq "none") { $FORM{mode} = ""; }
	if ($FORM{input_string} eq "none") { $FORM{input_string} = ""; }
	if (!$FORM{link_max_results} and $FORM{max_results} != 50) { $FORM{link_max_results} = $FORM{max_results}; }
	$ENV{'HTTP_COOKIE'} =~ s/apfcart=([^,]+),([^;]+)/$AWS_variables{CartId} = $1; $AWS_variables{HMAC} = $2;/e;
	$AWS_variables{HMAC} =~ s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
	if ($AWS_variables{CartId} and $AWS_variables{HMAC}) { $session = qq[&CartId=$AWS_variables{CartId}&Hmac=$AWS_variables{HMAC}]; }
	%lookup_store = reverse %store_to_browse;
	$MY_variables{store} = $lookup_store{$FORM{mode}};
	($url_mode = $FORM{mode}) =~ s/_/-/g;
	$FORM{input_string} =~ s/\s/\+/g;
	$MY_variables{see_back} = qq[<A Href="javascript:history.go(-1)"  onMouseOver="self.status=document.referrer;return true">$language_text{see_text3}</A>];
	# url, form, & see options
	$url_options = ""; $MY_variables{form_options} = ""; $see_url_options = "";
	if ($FORM{link_templates} or $FORM{templates}) {
		$url_options .= "&templates=$FORM{link_templates}";
		$MY_variables{form_options} .= qq[<INPUT Type="hidden" Name="templates" Value="$FORM{link_templates}">];
	}
	if ($FORM{language}) {
		$url_options .= "&language=$FORM{language}";
		$MY_variables{form_options} .= qq[<INPUT Type="hidden" Name="language" Value="$FORM{language}">];
	}
	if ($FORM{locale}) {
		$url_options .= "&locale=$FORM{locale}";
		$MY_variables{form_options} .= qq[<INPUT Type="hidden" Name="locale" Value="$FORM{locale}">];
	}
	if ($FORM{link_max_results}) {
		$url_options .= "&max_results=$FORM{link_max_results}";
		$MY_variables{form_options} .= qq[<INPUT Type="hidden" Name="max_results" Value="$FORM{link_max_results}">];
	}
	if ($FORM{search_type}) { $see_url_options .= "&search_type=$FORM{search_type}"; }
	if ($FORM{input_string}) { $see_url_options .= "&input_string=$FORM{input_string}"; }
	if ($FORM{node}) { $see_url_options .= "&node=$FORM{node}"; }
	if ($FORM{item_id}) { $see_url_options .= "&item_id=$FORM{item_id}"; }
	if ($FORM{sort_type} ne "+salesrank") { $see_url_options .= "&sort_type=$FORM{sort_type}"; }
	if ($FORM{pricerange}) { $see_url_options .= "&pricerange=$FORM{pricerange}"; }
	if ($FORM{keywords}) { $see_url_options .= "&keywords=$FORM{keywords}"; }
	#
	if ($session and !$FORM{cart_action} and $FORM{locale} eq "us") { $MY_variables{shopping_cart_link} = qq[<DIV Style="$cart_text3_style">&nbsp;<A Href="$MY_variables{script_name}?cart_action=get$url_options">$language_text{cart_text3}</A>&nbsp;</DIV>]; }
	if ($FORM{node}) {
		%lookup_browse_id = reverse %{$FORM{mode}};
		$query = "BrowseNodeSearch=" . $FORM{node} . "&mode=$url_mode&type=lite&page=$FORM{page_num}";
		if ($FORM{input_string} and !$FORM{keywords}) { $FORM{keywords} = $FORM{input_string}; }
		$MY_variables{subject} = $lookup_browse_id{$FORM{node}};
		$MY_variables{header} = qq[<A Href="$MY_variables{script_name}?mode=none$url_options">$language_text{header_text1}</A> > <A Href="$MY_variables{script_name}?mode=$FORM{mode}$url_options">$MY_variables{store}</A> > $MY_variables{subject}<BR>];
	} elsif ($FORM{input_string}) {
		if ($FORM{search_type} =~ /(?:SimilaritySearch|ListManiaSearch|SellerProfile|SellerSearch|WishlistSearch|ExchangeSearch)/) {
			if ($FORM{search_type} eq "SellerSearch") { $query = "SellerSearch=$FORM{input_string}&type=heavy&offerstatus=open&page=$FORM{page_num}"; }
			else { $query = "$FORM{search_type}=$FORM{input_string}&type=lite&page=$FORM{page_num}"; }
		} else {
			if ($FORM{search_type} =~ /(AuthorSearch|ArtistSearch|ActorSearch|ManufacturerSearch|DirectorSearch|PowerSearch)/) {
				$query = "$FORM{search_type}=";
			} else {
				$query = "KeywordSearch=";
			}
			(my $search_url = $FORM{input_string}) =~ s/\+/%20/g;
			($MY_variables{subject} = $FORM{input_string}) =~ s/\+/ /g;
			$query .= $search_url . "&mode=$url_mode&type=lite&page=$FORM{page_num}";
		}
	} elsif ($FORM{search_type} =~ /(ThirdPartyNew|Used|Collectible|Refurbished)/) {
		$query = "AsinSearch=$FORM{item_id}&type=heavy&offer=$FORM{search_type}&offerpage=$FORM{page_num}";
		my %marketplace_text_hash = ( Used => $language_text{my_marketplace2}, Collectible => $language_text{my_marketplace3}, ThirdPartyNew => $language_text{my_marketplace4}, Refurbished => $language_text{my_marketplace5} );
		$my_marketplace_text = $marketplace_text_hash{$FORM{search_type}};
	} elsif ($FORM{item_id} =~ /,/) {
		$MY_variables{store} = $language_text{header_text2}; $MY_variables{subject} = $language_text{header_text3};
		$query = "AsinSearch=" . $FORM{item_id} . "&type=lite&page=$FORM{page_num}";
	}
	if ($FORM{pricerange}) { $query .= qq[&price=$FORM{pricerange}]; }
	if ($FORM{keywords}) { $query .= qq[&keywords=$FORM{keywords}]; }
}

sub build_search_box {
	my ($search_box_allsearches, $search_subject);
	if ($show_search_options eq "yes") { $search_box_allsearches = qq[<BR><A Href="$MY_variables{script_name}?search_type=AllSearches$url_options"><FONT Size="-1">$language_text{searchbox_text3}</FONT></A>]; }
	$MY_variables{search_box} = qq[<FORM Method="get" Action="$MY_variables{script_name}">];
	$MY_variables{search_box} .= $MY_variables{form_options};
	$MY_variables{search_box} .= qq[<INPUT Type="text" Name="input_string" Size="20"];
	if ($FORM{input_string}) { ($search_subject = $FORM{input_string}) =~ s/\+/ /g; $MY_variables{search_box} .= qq[ Value="$search_subject" ]; }
	$MY_variables{search_box} .= qq[><SELECT Name="mode">];
	if ($FORM{node}) { $MY_variables{search_box} .= qq[<OPTION Value="node:$FORM{mode}:$FORM{node}" Selected>$language_text{searchbox_text2}]; }
	$MY_variables{search_box} .= qq[<OPTION Value="blended"];
	if (!$FORM{mode}) { $MY_variables{search_box} .= qq[ Selected]; }
	$MY_variables{search_box} .= qq[>$language_text{searchbox_text1}];
	foreach my $key (sort keys %store_to_browse) {
		my $value = $store_to_browse{$key};
		$MY_variables{search_box} .= qq[<OPTION Value="$value"];
		if ($value eq $FORM{mode} and !$FORM{node}) { $MY_variables{search_box} .= qq[ Selected]; }
		$MY_variables{search_box} .= qq[>$key];
	}
	$MY_variables{search_box} .= qq[</SELECT><INPUT Style="$MY_variables{submit_button_style}" Type="submit" Value="$language_text{button_text4}">$search_box_allsearches</FORM>];
}

sub load_templates {
	my $temp_num = $FORM{templates} - 1; if ($temp_num < 0) { $temp_num = 0; }
	my %template_name_refs = ( page => \@page_templates, products => \@products_templates, item => \@item_templates, larger_image => \@larger_image_templates, sellersearch => \@sellersearch_templates, sellerprofile => \@sellerprofile_templates, thirdparty_products => \@thirdparty_products_templates, browse_menu => \@browse_menu_templates, nav_menu => \@nav_menu_templates, featured_product => \@featured_product_templates, blended => \@blended_templates, shopping_cart => \@shopping_cart_templates, customer_reviews => \@customer_reviews_templates); 
	for my $key (keys %template_name_refs) {
		my $template_file_name = ${$template_name_refs{$key}}[$temp_num];
		if ( -e $template_file_name ) {
			open(FILEHANDLE,"<$template_file_name");
			while (<FILEHANDLE>) { $template_html{$key} = $template_html{$key} . $_; }
			close (FILEHANDLE);
		}
	}
}

sub get_url {
	my $value = $_[0]; my $skip = $_[1];
	my (%xml_cache,$xml_result,$dbm_error);
	my $cache_expire = time() - 3600; my $cache_time = $value . "_time";
	if ($use_cache eq "yes") {
		eval 'use Fcntl'; $dbm_error = $@;
		eval 'use DB_File'; $dbm_error .= $@;
		if (!$dbm_error) {
			if (-s $cache_file > (7000 * $cache_max_size)) { unlink $cache_file; }
			eval 'tie(%xml_cache,"DB_File",$cache_file,O_RDONLY)'; $dbm_error = $@;
		}
	}
	if ($xml_cache{$value} and $xml_cache{$cache_time} > $cache_expire) {
		$xml_result = $xml_cache{$value};
		$debug .= "cache status = cached request size " . length($xml_result) . ", total requests cached " . keys(%xml_cache) . "\n";
		untie %xml_cache;
	} else {
		untie %xml_cache;
		eval 'use LWP::Simple qw($ua get)'; if ($@) { print "Content-type: text/html\n\n"; print "Unable to use LWP::Simple and this script cannot function without it.\n"; exit; }
		my $ua = new LWP::UserAgent;
		$ua->timeout(30);
		$xml_result = get($value);
		$xml_result =~ s/\&lt;/</gi;
		$xml_result =~ s/\&amp;/&/gi;
		if (!$xml_result and $skip ne "skip_ok") { $MY_variables{error_msg} = "Sorry, we are currently unable to process your request in a timely manner.<BR>Please try again later.\n"; }
		if ($use_cache eq "yes" and !$dbm_error) {
			my $count=0;
			open(LOCK, ">$cache_file.lock");
			until (flock(LOCK,2) or $count > 50) {
				sleep .10;
				++$count;
			}
			if ($count > 50) { $dbm_error = "lock failed"; }
			if (!$dbm_error) {
				eval 'tie(%xml_cache,"DB_File",$cache_file,O_CREAT|O_RDWR)'; $dbm_error = $@;
				if (keys(%xml_cache) > $cache_max_size) { undef %xml_cache; $debug .= "cleared cache\n"; }
			}
		}
		$xml_cache{$value} = $xml_result;
		$xml_cache{$cache_time} = time;
		untie %xml_cache;
		close(LOCK);
		$debug .= "cache status = not cached request size " . length($xml_result) . "\n";
	}
	$xml_result =~ s/<ErrorMsg>([^<]+)<\/ErrorMsg>/if ($skip ne "skip_ok") { $MY_variables{error_msg} .= $1; }/esi;
	return $xml_result;
}

sub parse_xml_into_hashes_of_arrays {
	my $xml_result = shift;
	my (%temp_level_0, $temp_level_1);
	$xml_result =~ s/<([^>]+?)(?:\s[^>]+)?>(.*?)<\/\1>/push @{$temp_level_0{$1}}, $2;/gsie;
	$temp_level_1 = process_hashes_of_arrays(\%temp_level_0);
	delete $AWS_variables{ProductInfo};
	$AWS_variables{TotalResults} = ${${$temp_level_1}{TotalResults}}[0];
	$AWS_variables{TotalPages} = ${${$temp_level_1}{TotalPages}}[0];
	return $temp_level_1;
}

sub process_hashes_of_arrays {
	my ($entry_hash, %exit_hash) = @_;
	for my $key (keys %$entry_hash) {
		if (!${${$entry_hash}{$key}}[1]) {
			$AWS_variables{$key} = ${${$entry_hash}{$key}}[0];
			$AWS_variables{$key} =~ s/"/&quot;/g;
			$AWS_variables{$key} =~ s/'/&#39;/g;
		}
 		foreach my $item (@{${$entry_hash}{$key}}) {
			$item =~ s/<([^>]+?)(\s[^>]+)?>(.*?)<\/\1>/my $temp3; if ($2 and $1 eq "Details") { $temp3 = $3 . "<DetailsURL>$2<\/DetailsURL>"; } else { $temp3 = $3; } push @{$exit_hash{$1}}, $temp3;/gsie;
		}
	}
	return \%exit_hash;
}

sub blended_search {
	my $level_1 = shift;
	my $i_blended = 0;
	foreach my $item_blended (@{${$level_1}{ProductLine}}) {
		my %temp_hash_blended;
		push @{$temp_hash_blended{ProductLine}}, $item_blended;
		my $level_2_blended = process_hashes_of_arrays(\%temp_hash_blended);
		if (${$level_2_blended}{Mode}[0] eq "apparel") { next; }
		my $level_3_blended = process_hashes_of_arrays($level_2_blended);
		${$level_2_blended}{Mode}[0] =~ s/-/_/g;
		$MY_variables{blended_mode} = $lookup_store{${$level_2_blended}{Mode}[0]};
		$MY_variables{blended_mode_totalresults} = ${$level_3_blended}{TotalResults}[0];
		$MY_variables{blended_mode_pages} = ${$level_3_blended}{TotalPages}[0];
		$MY_variables{blended_see_more_url} = qq[$MY_variables{script_name}?mode=${$level_2_blended}{Mode}[0]&input_string=$FORM{input_string}$url_options];
		$MY_variables{products_html} .= set_html("blended");
		assign_variables("products",$level_3_blended);
	}
	$MY_variables{store} = "$language_text{searchbox_text1}";
}

sub assign_variables {
	my ($whose_variables, $temp_input) = @_;
	my @Details;
	if (@{${$temp_input}{Details}}) {
		@Details = @{${$temp_input}{Details}};
	}
	my $i = 0;
	my ($more_form_options);
	Details_loop: foreach my $item (@Details) {
		my (%temp_hash, @descriptors);
		my @AWS_deletekeys = (DetailsURL,Catalog,ReleaseDate,Manufacturer,Availability,ListPrice,OurPrice,UsedPrice);
		delete @AWS_variables{@AWS_deletekeys};
		my @MY_deletekeys = (our_price,list_price,discount,our_value,list_value,my_artists);
		delete @MY_variables{@MY_deletekeys};
		$details_max = $#Details + 1;
		push @{$temp_hash{Details}}, $item;
		my $level_2 = process_hashes_of_arrays(\%temp_hash);
		my $level_3 = process_hashes_of_arrays($level_2);
		if ($AWS_variables{DetailsURL}) {
			$AWS_variables{DetailsURL} =~ s/(?:\surl=|&quot;)//g;
			$MY_variables{in_association} = qq[<A Href="$AWS_variables{DetailsURL}"><FONT Size="2">$language_text{association_text1}</FONT></A><BR>];
		}
		if ($FORM{node}) {
			if (exists($chat_modes{$FORM{mode}})) {
				$MY_variables{chat_link} = qq[<A Href="#" onClick="childwin = window.open('http://www.amazon.com/exec/obidos/redirect?tag=$MY_variables{associate_id}&path=dt/assoc/tg/chat/age-verification/-/$chat_modes{$FORM{mode}}/true/$FORM{node}/ref=br_chat_cc_av_e/', '', 'width=392,height=490'); childwin.opener = window;">Chat about $lookup_browse_id{$FORM{node}}</a><BR>];
			}
		}
		if ($catalog_to_mode{$AWS_variables{Catalog}} and !$FORM{mode}) { $FORM{mode} = $catalog_to_mode{$AWS_variables{Catalog}}; }
		$more_form_options = qq[<INPUT Type="hidden" Name="mode" Value="$FORM{mode}">];
		if ($FORM{search_type}) { $more_form_options .= qq[<INPUT Type="hidden" Name="search_type" Value="$FORM{search_type}">]; }
		if ($FORM{input_string}) { $more_form_options .= qq[<INPUT Type="hidden" Name="input_string" Value="$FORM{input_string}">]; }
		if ($FORM{node}) { $more_form_options .= qq[<INPUT Type="hidden" Name="node" Value="$FORM{node}">]; }
		if ($FORM{item_id}) { $more_form_options .= qq[<INPUT Type="hidden" Name="item_id" Value="$FORM{item_id}">]; }
		if ($FORM{pricerange}) { $more_form_options .= qq[<INPUT Type="hidden" Name="pricerange" Value="$FORM{pricerange}">]; }
		if ($FORM{keywords}) { $more_form_options .= qq[<INPUT Type="hidden" Name="keywords" Value="$FORM{keywords}">]; }
		# start my_artists
		if ($FORM{mode} =~ /^(books|books_uk|books_de|books_de_intl_us)$/i) {
			$MY_variables{my_artists} = comma_separate_list(${$level_3}{Author},$language_text{my_artists_text1},"AuthorSearch");
		} elsif ($FORM{mode} =~ /^(music|classical|pop_music_de|classical_de)$/i) {
			$MY_variables{my_artists} = comma_separate_list(${$level_3}{Artist},$language_text{my_artists_text1},"ArtistSearch");
		} elsif ($FORM{mode} =~ /^(dvd|vhs|video|dvd_uk|vhs_uk|dvd_de|vhs_de)$/i) {
			$MY_variables{my_artists} = comma_separate_list(${$level_3}{Actor},$language_text{my_artists_text2},"ActorSearch");
			if (${$level_3}{Director}) { $MY_variables{my_artists} .= "<BR>" . comma_separate_list(${$level_3}{Director},$language_text{my_artists_text4},"DirectorSearch"); }
		}
		if ( (!$MY_variables{my_artists}) and ($AWS_variables{Manufacturer}) ) {
			if ($FORM{mode} =~ /^(electronics|kitchen|videogames|software|photo|pc_hardware|electronics_uk|video_games_uk|software_uk|ce_de|kitchen_de|video_games_de|software_de)$/) {
				(my $search_manufacturer_string = $AWS_variables{Manufacturer}) =~ s/ /+/g;
				$MY_variables{my_artists} = qq[$language_text{my_artists_text3} <A Href="$MY_variables{script_name}?mode=$FORM{mode}&search_type=ManufacturerSearch&input_string=$search_manufacturer_string$url_options">$AWS_variables{Manufacturer}</A>];
			} else {
				$MY_variables{my_artists} = qq[$language_text{my_artists_text3} $AWS_variables{Manufacturer}];
			}
		}
		# end my_artists
		# start my_comments
		my $temp_loop = 1;
		foreach my $item (@{${$level_3}{CustomerReview}}) {
			my (%temp_hash,$temp_hash1,$temp_rounded_rating,$temp_rating_display,$shortened_comment);
			push @{$temp_hash{CustomerReview}}, $item;
			$temp_hash1 = process_hashes_of_arrays(\%temp_hash);
			($temp_rounded_rating = ${$temp_hash1}{Rating}[0]) =~ s/(\.\d+)//;
			if ($1 >= .25 and $1 < .75) { $temp_rounded_rating .= "-5"; } elsif ($1 >= .75) { $temp_rounded_rating++; $temp_rounded_rating .= "-0"; } else { $temp_rounded_rating .= "-0"; }
			$temp_rating_display = qq[<IMG Alt="${$temp_hash1}{Rating}[0] out of 5 stars" Src="http://g-images.amazon.com/images/G/01/x-locale/common/customer-reviews/stars-$temp_rounded_rating.gif">];
			if ($FORM{mode} eq "toys") { $temp_rating_display = ${$temp_hash1}{Rating}[0]};
			if (!${$temp_hash1}{Rating}[0]) { $temp_rating_display = $language_text{average_rating_text2}; }
			if ($MY_variables{my_comments}) { $MY_variables{my_comments} .= qq[<BR><HR Width="75%"><BR>\n]; }
			$MY_variables{my_comments} .= qq[<A Name="review_$temp_loop">$language_text{my_comments_text1}</A> $temp_rating_display - <B>${$temp_hash1}{Summary}[0]</B><BR>\n${$temp_hash1}{Comment}[0]<BR>\n];
			$shortened_comment = ${$temp_hash1}{Comment}[0];
			$shortened_comment =~ s/<([^\s|>]+)\s[^>]+>(.*?)<\/\1>/$2/gis;
			if ($review_length) {
				while (substr($shortened_comment,$review_length,1) !~ /[\s|-]/ and $review_length < length($shortened_comment)) { $review_length++ }
				$shortened_comment = substr($shortened_comment,0,$review_length);
				if ($review_length < length(${$temp_hash1}{Comment}[0])) { $shortened_comment .= qq[ ... <A Href="$MY_variables{script_name}?search_type=CustomerReviews&item_id=$AWS_variables{Asin}$url_options#review_$temp_loop">$language_text{my_comments_text2}</A>]; }
			}
			if ($MY_variables{my_shortened_comments}) { $MY_variables{my_shortened_comments} .= qq[<BR><HR Width="75%"><BR>\n]; }
			$MY_variables{my_shortened_comments} .= qq[$language_text{my_comments_text1} $temp_rating_display - <B>${$temp_hash1}{Summary}[0]</B><BR>\n$shortened_comment<BR>\n];
			if ($temp_loop eq 1) {
				$MY_variables{first_review_rating_display} = $temp_rating_display; $MY_variables{first_review_summary} = ${$temp_hash1}{Summary}[0]; $MY_variables{first_review_comment} = ${$temp_hash1}{Comment}[0]; $MY_variables{first_review_shortened_comment} = $shortened_comment;
			}
			$temp_loop++;
		}
		# end my_comments
		# start my_features
		if (${$level_3}{Feature}) {
			$MY_variables{my_features} = qq[<B>$language_text{my_features_text1}</B><BR>\n<LI>];
			$MY_variables{my_features} .= join("<BR>\n<LI>", @{${$level_3}{Feature}});
			$MY_variables{my_features} .= qq[<BR><BR>\n];
		}
		# end my_features
		# start my_browse_list
		if (${$level_3}{BrowseNode}) {
			my $my_temp_browse_list;
			foreach my $key1 (keys %store_to_browse) {
				foreach my $key2 (keys %{$store_to_browse{$key1}}) {
					foreach my $item (@{${$level_3}{BrowseNode}}) {
						my (%temp_hash,$temp_hash1,$temp_hash2,$lc_browse_name);
						push @{$temp_hash{BrowseNode}}, $item;
						$temp_hash1 = process_hashes_of_arrays(\%temp_hash);
						$lc_browse_name = lc ${$temp_hash1}{BrowseName}[0];
						next if $lc_browse_name eq "top selling";
						if ($lc_browse_name eq lc $key2) {
							if ($my_temp_browse_list) {
								$my_temp_browse_list .= qq[, <A Href="$MY_variables{script_name}?mode=$store_to_browse{$key1}&node=${$store_to_browse{$key1}}{$key2}$url_options">$key1:${$temp_hash1}{BrowseName}[0]</A>];
									} else {
								$my_temp_browse_list = qq[<B>$language_text{my_browse_list_text1}</B> <A Href="$MY_variables{script_name}?mode=$store_to_browse{$key1}&node=${$store_to_browse{$key1}}{$key2}$url_options">$key1:${$temp_hash1}{BrowseName}[0]</A>];
							}
						}
					}
				}
			}
			if ($my_temp_browse_list) { $MY_variables{my_browse_list} = $my_temp_browse_list . qq[<BR><BR>]; }
		}
		# end my_browse_list
		# start my_prices
		if ($FORM{locale} eq "us") {
			if ($AWS_variables{UsedPrice} and $AWS_variables{UsedPrice} ne "\$0.") { $MY_variables{my_used} = qq[<A Href="$MY_variables{script_name}?item_id=$AWS_variables{Asin}&search_type=Used$url_options">$language_text{my_marketplace2} $language_text{my_marketplace1} $AWS_variables{UsedPrice}</A><BR>]; }
			if ($AWS_variables{CollectiblePrice} and $AWS_variables{CollectiblePrice} ne "\$0.") { $MY_variables{my_collectible} = qq[<A Href="$MY_variables{script_name}?item_id=$AWS_variables{Asin}&search_type=Collectible$url_options">$language_text{my_marketplace3} $language_text{my_marketplace1} $AWS_variables{CollectiblePrice}</A><BR>]; }
			if ($AWS_variables{ThirdPartyNewPrice} and $AWS_variables{ThirdPartyNewPrice} ne "\$0.") { $MY_variables{my_thirdpartynew} = qq[<A Href="$MY_variables{script_name}?item_id=$AWS_variables{Asin}&search_type=ThirdPartyNew$url_options">$language_text{my_marketplace4} $language_text{my_marketplace1} $AWS_variables{ThirdPartyNewPrice}</A><BR>]; }
			if ($AWS_variables{RefurbishedPrice} and $AWS_variables{RefurbishedPrice} ne "\$0.") { $MY_variables{my_refurbished} = qq[<A Href="$MY_variables{script_name}?item_id=$AWS_variables{Asin}&search_type=Refurbished$url_options">$language_text{my_marketplace5} $language_text{my_marketplace1} $AWS_variables{RefurbishedPrice}</A><BR>]; }
		}
		if ($AWS_variables{OurPrice}) {
			$MY_variables{our_value} = $AWS_variables{OurPrice};
			if ($FORM{locale} eq "de") {
				$MY_variables{our_value} =~ s/\.//g; $MY_variables{our_value} =~ s/,/./;
			} else {
				$MY_variables{our_value} =~ s/,//g;
			}
			$MY_variables{our_value} =~ s/^(?:(?:\$|&Acirc;&#163;|EUR )(\d+(?:\.\d*)?|\.\d+))?.*$/$1/;
			$MY_variables{our_price} = qq[<B>$language_text{my_prices_text2} <FONT Color="#990000">$AWS_variables{OurPrice}</FONT></B><BR>];
		} else {
			$MY_variables{our_price} = qq[<B>$language_text{my_prices_text2} <I>$language_text{my_prices_text5}</I></B><BR>];
		}
		if ($AWS_variables{ListPrice} and ($AWS_variables{OurPrice} ne $AWS_variables{ListPrice}) ) {
			$MY_variables{list_value} = $AWS_variables{ListPrice};
			if ($FORM{locale} eq "de") {
				$MY_variables{list_value} =~ s/\.//g; $MY_variables{list_value} =~ s/,/./;
			} else {
				$MY_variables{list_value} =~ s/,//g;
			}
			$MY_variables{list_value} =~ s/^(?:(?:\$|&Acirc;&#163;|EUR )(\d+(?:\.\d*)?|\.\d+))?.*$/$1/;
			$MY_variables{list_price} = qq[<B>$language_text{my_prices_text1} </B><STRIKE>$AWS_variables{ListPrice}</STRIKE><BR>];
		}
		if ( ($MY_variables{our_value} and $MY_variables{list_value}) and ($MY_variables{our_value} < $MY_variables{list_value}) ) {
			my $discount_amount = $money_symbol . (sprintf "%.2f", ($MY_variables{list_value} - $MY_variables{our_value}));
			if ($FORM{locale} eq "de") { $discount_amount =~ s/\./,/g; }
			my $discount_percent = (sprintf "%2.f", (100 - ($MY_variables{our_value} / $MY_variables{list_value})*100)) . "%";
			$MY_variables{discount} = qq[<B>$language_text{my_prices_text3}</B> <FONT Color="#990000">$discount_amount ($discount_percent)</FONT><BR>];
		}
		$MY_variables{my_prices} = $MY_variables{list_price} . $MY_variables{our_price} . $MY_variables{discount} . qq[<FONT Size="1">$language_text{my_prices_text4}</FONT><BR>];
		# end my_prices
		# start my_descriptors
		if ($AWS_variables{SalesRank}) { push (@descriptors, qq[<B>$language_text{my_descriptors_text9}</B> $AWS_variables{SalesRank}]); }
		if ($AWS_variables{ReleaseDate}) { push (@descriptors, qq[<B>$language_text{my_descriptors_text1}</B> $AWS_variables{ReleaseDate}]); }
		if ($AWS_variables{Media} and $AWS_variables{Media} ne $AWS_variables{Catalog}) { push (@descriptors, qq[<B>$language_text{my_descriptors_text3}</B> $AWS_variables{Media}]); }
		if ($AWS_variables{NumMedia} and $AWS_variables{NumMedia} > 1) { push (@descriptors, qq[<B>$language_text{my_descriptors_text4}</B> $AWS_variables{NumMedia}]); }
		if ($AWS_variables{Encoding}) { push (@descriptors, qq[<B>$language_text{my_descriptors_text10}</B> $AWS_variables{Encoding}]); }
		if ($AWS_variables{TheatricalReleaseDate}) { push (@descriptors, qq[<B>$language_text{my_descriptors_text2}</B> $AWS_variables{TheatricalReleaseDate}]); }
		if ($AWS_variables{MpaaRating}) { push (@descriptors, qq[<B>$language_text{my_descriptors_text5}</B> $AWS_variables{MpaaRating}]); }
		if ($AWS_variables{EsrbRating}) { push (@descriptors, qq[<B>$language_text{my_descriptors_text7}</B> $AWS_variables{EsrbRating}]); }
		if ($AWS_variables{ReadingLevel}) { push (@descriptors, qq[<B>$language_text{my_descriptors_text12}</B> $AWS_variables{ReadingLevel}]); }
		if ($AWS_variables{AgeGroup}) { push (@descriptors, qq[<B>$language_text{my_descriptors_text8}</B> $AWS_variables{AgeGroup}]); }
		if ($AWS_variables{Manufacturer} and $MY_variables{my_artists} !~ /\Q$AWS_variables{Manufacturer}\E/) { push (@descriptors, qq[<B>$language_text{my_descriptors_text13}</B> $AWS_variables{Manufacturer}]); }
		$MY_variables{my_platforms} = join(", ", @{${$level_3}{Platform}});
		if ($MY_variables{my_platforms}) { push (@descriptors, qq[<B>$language_text{my_descriptors_text6}</B> $MY_variables{my_platforms}]); }
		$MY_variables{my_descriptors} = join("; ", @descriptors);
		if ($MY_variables{my_descriptors}) { $MY_variables{my_descriptors} .= "<BR><BR>"; }
		# end my_descriptors
		# start my_ThirdPartyProductDetails
		if ($my_marketplace_text) {
			my $ii;
			foreach my $item (@{${$level_3}{ThirdPartyProductDetails}}) {
				my (%temp_hash,$temp_hash1,$temp_hash2);
				push @{$temp_hash{ThirdPartyProductDetails}}, $item;
				$temp_hash1 = process_hashes_of_arrays(\%temp_hash);
				$temp_hash2 = process_hashes_of_arrays($temp_hash1);
				if (!$AWS_variables{OfferingPrice}) { next; }
				if (!$AWS_variables{SellerRating}) { $AWS_variables{SellerRating} = "$language_text{average_rating_text2}"; }
				$MY_variables{sellersearch_url} = "$MY_variables{script_name}?search_type=SellerSearch&input_string=$AWS_variables{SellerId}$url_options";
				$MY_variables{ThirdPartyProduct_buy_button} = qq[<INPUT Type="hidden" Name="exchange.$AWS_variables{ExchangeId}.$AWS_variables{Asin}.$AWS_variables{SellerId}" Value="1">];
				$MY_variables{buy_button} = initialize_buttons("buy");
				$MY_variables{my_ThirdPartyProductDetails} .= set_html("thirdparty_products",$ii);
				$ii++
			}
			$MY_variables{store} = $my_marketplace_text; $MY_variables{subject} = $AWS_variables{ProductName};
		}
		# end my_ThirdPartyProductDetails
		#start sort_box
		if (($FORM{input_string} or $FORM{node} or $FORM{search_type} =~ /AuthorSearch|ArtistSearch|ActorSearch|DirectorSearch|ManufacturerSearch|PowerSearch/) and $AWS_variables{TotalResults} > 1) {
			my (@temp_sort_array,$sort_box_options);
			if ( @{$sort_hash_by_mode{$FORM{mode}}} ) {
				@temp_sort_array = @{$sort_hash_by_mode{$FORM{mode}}};
			} else {
				@temp_sort_array = @{$sort_hash_by_mode{"all"}};
			}
			foreach my $key1 (keys %sort_hash) {
				if (grep { $_ eq $key1 } @temp_sort_array) {
					$sort_box_options .= qq[<OPTION Value="$key1"];
					if ($key1 eq $FORM{sort_type}) { $sort_box_options .= " Selected"; }
					$sort_box_options .= qq[>$sort_hash{$key1}];
				}
			}
			my $sort_form_options = $MY_variables{form_options} . $more_form_options;
			$MY_variables{sort_box} = qq[<FORM Method="get" Action="$MY_variables{script_name}"><SELECT Name="sort_type">$sort_box_options</SELECT>$sort_form_options<INPUT Style="$MY_variables{submit_button_style}" Type="submit" Value=" $language_text{button_text6} "></FORM>];
		}
		#end sort_box
		if (${$level_3}{AvgCustomerRating} and ${$level_3}{AvgCustomerRating}[0] != 0) {
			$AWS_variables{AvgCustomerRating} = sprintf "%.2f", ${$level_3}{AvgCustomerRating}[0];
			(my $temp_rounded_rating = $AWS_variables{AvgCustomerRating}) =~ s/(\.\d+)//;
			if ($1 >= .25 and $1 < .75) { $temp_rounded_rating .= "-5"; } elsif ($1 >= .75) { $temp_rounded_rating++; $temp_rounded_rating .= "-0"; } else { $temp_rounded_rating .= "-0"; }
			$MY_variables{my_avg_rating_display} = qq[<IMG Alt="$AWS_variables{AvgCustomerRating} out of 5 stars" Src="http://g-images.amazon.com/images/G/01/x-locale/common/customer-reviews/stars-$temp_rounded_rating.gif">];
		} else {
			$MY_variables{my_avg_rating_display} = $AWS_variables{AvgCustomerRating} = $language_text{average_rating_text2};
		}
		if ($AWS_variables{Tracks}) {
			foreach my $item (@{${$level_3}{Track}}) {
				$MY_variables{my_tracks} .= qq[<LI>$item<BR>];
			}
			$MY_variables{my_tracks} = qq[<B>$language_text{my_descriptors_text11}</B><BR>$MY_variables{my_tracks}<BR>];
		}
		if (${$level_3}{Product}) {
			if (${$level_3}{ListId}) { $MY_variables{my_listmania} = qq[<LI><A Href="$MY_variables{script_name}?search_type=ListManiaSearch&input_string=${${$level_3}{ListId}}[rand @{${$level_3}{ListId}}]$url_options">$language_text{similar_text1}</A>]; }
			my $temp_similar_products = get_extra_product_links(${$level_3}{Product});
			if ($temp_similar_products) {
				my $temp_similar_products_images;
				$MY_variables{my_similar_products} = qq[<B>$language_text{my_similar_products_text1}</A></B><BR>$MY_variables{my_listmania}$temp_similar_products<LI><A Href="$MY_variables{script_name}?search_type=SimilaritySearch&input_string=$AWS_variables{Asin}$url_options">$language_text{see_text6}</A><BR>];
				foreach my $item (@{${$level_3}{Product}}) { $temp_similar_products_images .= qq[<A Href="$MY_variables{script_name}?item_id=$item&search_type=AsinSearch$url_options"><IMG Border="0" Src="http://images.amazon.com/images/P/$item.01.THUMBZZZ.jpg" onLoad="noImageCheck(this);"></A>&nbsp;]; } 
				$MY_variables{my_similar_products_images} = qq[<B>$language_text{my_similar_products_text1}</A></B><BR>$temp_similar_products_images<A Href="$MY_variables{script_name}?search_type=SimilaritySearch&input_string=$AWS_variables{Asin}$url_options">$language_text{see_text6}</A><BR>];
			}
		}
		if (${$level_3}{Accessory}) {
			my $temp_accessories = get_extra_product_links(${$level_3}{Accessory});
			if ($temp_accessories) {
				my $temp_accessories_images;
				$MY_variables{my_accessories} = qq[<B>$language_text{my_accessories_text1}</B><BR>$temp_accessories<BR>];
				foreach my $item (@{${$level_3}{Accessory}}) { $temp_accessories_images .= qq[<A Href="$MY_variables{script_name}?item_id=$item&search_type=AsinSearch$url_options"><IMG Border="0" Src="http://images.amazon.com/images/P/$item.01.THUMBZZZ.jpg" onLoad="noImageCheck(this);"></A>&nbsp;]; } 
				$MY_variables{my_accessories_images} = qq[<B>$language_text{my_accessories_text1}</A></B><BR>$temp_accessories_images<BR>];
			}
		}
		if (!$AWS_variables{Availability}) { $AWS_variables{Availability} = "unknown"; }
		$MY_variables{my_availability} = qq[<B>$language_text{availability_text1}</B> $AWS_variables{Availability}<BR>];
		$MY_variables{old_product_url} = "http://www.$amazon_site/exec/obidos/ASIN/$AWS_variables{Asin}/ref=nosim/$MY_variables{associate_id}?dev-t=D1KQJBNTALRLQH";
		$MY_variables{pda_item_url} = "http://www.amazon.com/exec/obidos/redirect?tag=$MY_variables{associate_id}&creative=D1KQJBNTALRLQH&camp=2025&link_code=xm2&path=dt/upda-1.0-anywhere/tg/aa/upda/item/-/$AWS_variables{Asin}";
		$MY_variables{rim_item_url} = "http://www.amazon.com/exec/obidos/redirect?tag=$MY_variables{associate_id}&creative=D1KQJBNTALRLQH&camp=2025&link_code=xm2&path=dt/upda-1.0-i/tg/aa/upda/item/-/$AWS_variables{Asin}";
		$MY_variables{my_large_image_url} = "$MY_variables{script_name}?item_id=$AWS_variables{Asin}&search_type=image$url_options";
		$MY_variables{item_url} = "$MY_variables{script_name}?item_id=$AWS_variables{Asin}&search_type=AsinSearch$url_options";
		$MY_variables{buy_button} = initialize_buttons("buy");
		$MY_variables{oneclick_button} = initialize_buttons("oneclick");
		$MY_variables{shopping_cart_button} = initialize_buttons("cart");
		$MY_variables{wishlist_button} = initialize_buttons("wishlist");
		$MY_variables{wedding_button} = initialize_buttons("wedding");
		$MY_variables{baby_button} = initialize_buttons("baby");

		$MY_variables{products_html} .= set_html($whose_variables,$i) . $MY_variables{my_ThirdPartyProductDetails};
		if ($i >= $FORM{max_results} -1) { last Details_loop; }
		$i++;
	}
	if ($whose_variables eq "sellerprofile") {
		my $level_2 = process_hashes_of_arrays($temp_input);
		my $level_3 = process_hashes_of_arrays($level_2);
		my $ii ="0";
		if (!$AWS_variables{NumberOfCanceledAuctions}) { $AWS_variables{NumberOfCanceledAuctions} = $language_text{average_rating_text2}; }
		$sellerprofile_max = $#{${$level_3}{Feedback}} + 1;
		foreach my $item (@{${$level_3}{Feedback}}) {
			my (%temp_hash,$temp_hash1,$temp_hash2);
			push @{$temp_hash{ListingProductDetails}}, $item;
			$temp_hash1 = process_hashes_of_arrays(\%temp_hash);
			$temp_hash2 = process_hashes_of_arrays($temp_hash1);
			$MY_variables{sellerprofile_html} .= set_html("sellerprofile",$ii);
			$ii++
		}
	} elsif ($whose_variables eq "sellersearch") {
		my $level_2 = process_hashes_of_arrays($temp_input);
		my $level_3 = process_hashes_of_arrays($level_2);
		$MY_variables{store} = $AWS_variables{SellerNickname}; $MY_variables{subject} = $language_text{button_text4};
		$MY_variables{products_html} .= $MY_variables{sellerprofile_html};
		if ($search_type eq "ExchangeSearch") { ${$level_3}{ListingProductDetails} = ${$temp_input}{ListingProductDetails}; }
		foreach my $item (@{${$level_3}{ListingProductDetails}}) {
			my (%temp_hash,$temp_hash1,$temp_hash2);
			push @{$temp_hash{ListingProductDetails}}, $item;
			$temp_hash1 = process_hashes_of_arrays(\%temp_hash);
			$temp_hash2 = process_hashes_of_arrays($temp_hash1);
			if (!$AWS_variables{ExchangeSellerRating}) { $AWS_variables{ExchangeSellerRating} = "$language_text{average_rating_text2}"; }
			$AWS_variables{ImageUrlSmall} = "http://images.amazon.com/images/P/$AWS_variables{ExchangeAsin}.01.THUMBZZZ.jpg";
			$MY_variables{item_url} = "$MY_variables{script_name}?search_type=$offering_type{$AWS_variables{ExchangeOfferingType}}&item_id=$AWS_variables{ExchangeAsin}$url_options";
			$MY_variables{my_exchangesellerrating} = "<CENTER><B>$language_text{my_comments_text1} $AWS_variables{ExchangeSellerRating}</B></CENTER>";
			$MY_variables{ThirdPartyProduct_buy_button} = qq[<INPUT Type="hidden" Name="exchange.$AWS_variables{ExchangeId}.$AWS_variables{ExchangeAsin}.$AWS_variables{ExchangeSellerId}" Value="1">];
			$MY_variables{buy_button} = initialize_buttons("buy");
			$MY_variables{products_html} .= set_html("sellersearch",$i);
			if ($i >= $FORM{max_results} -1) { return; }
			$i++;
		}
	} elsif ($whose_variables eq "AllSearches") {
		initialize_all_search_boxes();
		$MY_variables{products_html} .= set_html("AllSearches",$i);
	}
	# start see_more
	if ($AWS_variables{TotalResults} or $AWS_variables{NumberOfOfferings}) {
		$AWS_variables{TotalResults} = $AWS_variables{TotalResults} + $AWS_variables{NumberOfOfferings};
		if (!$AWS_variables{TotalPages}) {
			$AWS_variables{TotalPages} = int(($AWS_variables{TotalResults} + 9)/10);
			if ($AWS_variables{TotalPages} == 0) { $AWS_variables{TotalPages} = 1; }
		}
		$MY_variables{see_total} = qq[$language_text{see_text4} $FORM{page_num} $language_text{see_text5}&nbsp; $AWS_variables{TotalPages}];
		my $see_form_options = $MY_variables{form_options} . $more_form_options;
		$see_form_options .= qq[<INPUT Type="hidden" Name="sort_type" Value="$FORM{sort_type}">];
		if ($FORM{page_num} < $AWS_variables{TotalPages}) {
			my $next_page = $FORM{page_num} + 1;
			$see_form_options .= qq[<INPUT Type="hidden" Name="page_num" Value="$next_page">];
			$MY_variables{see_next} = qq[<A HREF="$ENV{SCRIPT_NAME}?mode=$url_mode&page_num=$next_page$see_url_options$url_options"><B>Next</B></A>];
		}
		if ($FORM{page_num} > 1) {
			my $prev_page = $FORM{page_num} - 1;
			$see_form_options .= qq[<INPUT Type="hidden" Name="page_num" Value="$prev_page">];
			$MY_variables{see_prev} = qq[<FORM Action="$MY_variables{script_name}" Method="GET">$see_form_options<INPUT Style="$MY_variables{submit_button_style};" Type="submit" Value="$language_text{see_text2}"></FORM>];
		}
		my ($page_num_low,$page_num_high);
		$MY_variables{see_index} = "";
		if ($AWS_variables{TotalPages} != "1") {
			if ($FORM{page_num} - 5 > 0) { $page_num_low = $FORM{page_num} - 5; } else { $page_num_low = "1"; }
			if ($page_num_low + 10 <= $AWS_variables{TotalPages}) { $page_num_high = $page_num_low + 10; } else { $page_num_high = $AWS_variables{TotalPages}; }
			for (my $loop_index = $page_num_low; $loop_index <= $page_num_high; $loop_index++) {
				if ($loop_index == $FORM{page_num}) {
					$MY_variables{see_index} .= qq[&nbsp;$loop_index&nbsp;];
				} else {
					$MY_variables{see_index} .= qq[&nbsp;<A Href="$MY_variables{script_name}?mode=$FORM{mode}&page_num=$loop_index$see_url_options$url_options">$loop_index</A>&nbsp;];
				}
			}
		}
	}
	#end see_more
}

sub shopping_cart {
	my ($this_xml_url, $xml_result);
	if (!$MY_variables{associate_id}) { $html = "<H1>No associate_id found!</H1>"; }
	$use_cache = "no";
	$debug .= "cook=$ENV{'HTTP_COOKIE'}\n";
	$MY_variables{store} = "Shopping"; $MY_variables{subject} = "Cart"; $MY_variables{header} = $language_text{cart_text1};
	if ($FORM{cart_action} eq "get") {
		if (!$session) { $MY_variables{products_html} = $language_text{cart_text2}; return; }
		$this_xml_url = "http://$amazon_server.amazon.com/onca/xml3?ShoppingCart=get&f=xml&t=$MY_variables{associate_id}&dev-t=D1KQJBNTALRLQH$session&locale=$FORM{locale}";
		$debug .= "xml_url=$this_xml_url\n";
	} elsif ($FORM{cart_action} eq "clear") {
		if (!$session) { $MY_variables{products_html} = $language_text{cart_text2}; return; }
		print "Set-Cookie: apfcart=empty; expires=Sun 01-Jan-01 01:01:01 GMT;\n";
		my $temp_result = get_url("http://$amazon_server.amazon.com/onca/xml3?ShoppingCart=clear&f=xml&t=$MY_variables{associate_id}&dev-t=D1KQJBNTALRLQH$session&locale=$FORM{locale}");
		$MY_variables{products_html} = $language_text{cart_text2};
		return;
	} elsif ($FORM{cart_action} eq "modify") {
		my $cart_delete_item;
		$ENV{QUERY_STRING} =~ s/cart_delete_item=([^&]+)/$cart_delete_item = $1;/e;
		$this_xml_url = "http://$amazon_server.amazon.com/onca/xml3?ShoppingCart=modify&f=xml&t=$MY_variables{associate_id}&dev-t=D1KQJBNTALRLQH&Item.$cart_delete_item=0$session&locale=$FORM{locale}";
		$debug .= "xml_url=$this_xml_url\n";
	} elsif ($FORM{cart_action} eq "add") {
		my ( %item_hash, $item_key, $items_quantities );
		$ENV{QUERY_STRING} =~ s/cart_item_([^=]+)=([^&]+)/if ($2 > 0) { $item_hash{$1} = $item_hash{$1} + $2; }/ge;
		foreach $item_key (keys %item_hash) {
			$items_quantities .= "&Asin.$item_key=$item_hash{$item_key}";
		}
		$this_xml_url = "http://$amazon_server.amazon.com/onca/xml3?ShoppingCart=add&f=xml&t=$MY_variables{associate_id}&dev-t=D1KQJBNTALRLQH$items_quantities$session&locale=$FORM{locale}";
		$debug .= "xml_url=$this_xml_url\n";
	} elsif ($FORM{cart_action} eq "replace") {
		if (!$session) { $MY_variables{products_html} = $language_text{cart_text2}; return; }
		my ( $temp_result, %item_hash, $item_key, $items_quantities, $i );
		for (my $temp_i = 1; $temp_i <= 3; $temp_i++) {
			delete $MY_variables{error_msg};
			$temp_result = get_url("http://$amazon_server.amazon.com/onca/xml3?ShoppingCart=clear&f=xml&t=$MY_variables{associate_id}&dev-t=D1KQJBNTALRLQH$session&locale=$FORM{locale}");
			$xml_result = get_url($this_xml_url);
			$debug .= "xml_url=$this_xml_url\n";
			if ($xml_result =~ /ShoppingCart/) { $MY_variables{error_msg} = ""; last; }
		}
		sleep 1;
		$ENV{QUERY_STRING} =~ s/cart_item_([^=]+)=([^&]+)/if ($2 > 0) { $item_hash{$1} = $item_hash{$1} + $2; }/ge;
		foreach $item_key (keys %item_hash) {
			$items_quantities .= "&Asin.$item_key=$item_hash{$item_key}";
		}
		$this_xml_url = "http://$amazon_server.amazon.com/onca/xml3?ShoppingCart=add&f=xml&t=$MY_variables{associate_id}&dev-t=D1KQJBNTALRLQH$items_quantities$session&locale=$FORM{locale}";
		$debug .= "xml_url=$this_xml_url\n $items_quantities - $MY_variables{error_msg} - $AWS_variables{ErrorMsg}\n\n";
		if (!$items_quantities) { delete $MY_variables{error_msg}; $FORM{cart_action} = "clear"; shopping_cart(); return; }
	}
	if ($this_xml_url) { 
		for (my $temp_i = 1; $temp_i <= 3; $temp_i++) {
			$MY_variables{error_msg} = "";
			$xml_result = get_url($this_xml_url);
			if ($xml_result =~ /ShoppingCart/) { $MY_variables{error_msg} = ""; last; }
		}
	}
	my $level_1 = parse_xml_into_hashes_of_arrays($xml_result);
	my $level_2 = process_hashes_of_arrays($level_1);
	my $level_3 = process_hashes_of_arrays($level_2);
	my $i = 0;
	if (!$session and $FORM{cart_action} eq "add") { print "Set-Cookie: apfcart=$AWS_variables{CartId},$AWS_variables{HMAC};\n"; }
	$MY_variables{total_cart_items} = $#{${$level_3}{Item}} + 1;
	my $whose_variables = "shopping_cart";
	$MY_variables{continue_shopping} = qq[<A Href="javascript:history.go(-1)"  onMouseOver="self.status=document.referrer;return true"><IMG Alt="Continue Shopping" Border="0" Src="http://g-images.amazon.com/images/G/01/detail/continue-shopping-02.gif"></A>];
	$MY_variables{checkout} = qq[<A Href="$AWS_variables{PurchaseUrl}"><IMG Alt="Buy from Amazon.com" Border="0" Src="http://g-images.amazon.com/images/G/01/associates/add-to-cart.gif"></A>];
	foreach my $single_item (@{${$level_3}{Item}}) {
		my (%temp_hash,$temp_hash1,$temp_hash2);
		push @{$temp_hash{Item}}, $single_item;
		$temp_hash1 = process_hashes_of_arrays(\%temp_hash);
		$temp_hash2 = process_hashes_of_arrays($temp_hash1);
		($MY_variables{our_value} = $AWS_variables{OurPrice}) =~ s/(?:\$|&Acirc;&#163;|,|USD )//g;
		$MY_variables{our_value_item_total} = $MY_variables{our_value} * $AWS_variables{Quantity};
		$MY_variables{my_cart_item_price} = $money_symbol . (sprintf "%.2f", $MY_variables{our_value_item_total});
		$MY_variables{cart_value_total} += $MY_variables{our_value_item_total};
		$MY_variables{cart_price_total} = $money_symbol . (sprintf "%.2f", $MY_variables{cart_value_total});
		$MY_variables{cart_qty_total} += $AWS_variables{Quantity};
		$MY_variables{products_html} .= set_html($whose_variables,$i);
		$i++
	}
	if (!$MY_variables{cart_qty_total} and !$MY_variables{error_msg} and !$AWS_variables{ErrorMsg}) { $MY_variables{products_html} = $language_text{cart_text2}; }
}

sub initialize_all_search_boxes {
	$MY_variables{header} = $language_text{searchbox_text3};
	my ($searchbox_start,$searchbox_end,$searchbox_modes,$searchbox_string,$searchbox_item,$searchbox_id,$searchbox_offering_types,%searchbox_types,$searchbox_sort,$searchbox_name,$searchbox_pricerange);
	$searchbox_start = qq[<FORM Method="get" Action="$MY_variables{script_name}"><TABLE Width="100%">];
	$searchbox_end = qq[<TR><TD Align="right" Colspan="2"><INPUT Style="$MY_variables{submit_button_style}" Type="submit" Value="$language_text{button_text4}"></TD></TR>\n</TABLE></FORM>];
	foreach my $key (sort keys %store_to_browse) { $searchbox_modes .= qq[<OPTION Value="$store_to_browse{$key}"]; if ($key eq "Books") { $searchbox_modes .= " Selected"; } $searchbox_modes .= qq[>$key]; }
	$searchbox_modes = qq[<TR><TD>$language_text{searchbox_text13}: </TD><TD Align="right"><SELECT Name="mode">$searchbox_modes</SELECT></TD></TR>];
	$searchbox_string = qq[<TR><TD>$language_text{searchbox_text7}: </TD><TD Align="right"><INPUT Type="text" Name="input_string" Size="20"></TD></TR>];
	$searchbox_item = qq[<TR><TD>$language_text{searchbox_text5}: </TD><TD Align="right"><INPUT Type="text" Name="item_id" Size="20"></TD></TR>];
	$searchbox_id = qq[<TR><TD>$language_text{searchbox_text12}: </TD><TD Align="right"><INPUT Type="text" Name="node" Size="20"></TD></TR>];
	$searchbox_pricerange = qq[<TR><TD>$language_text{searchbox_text14}: </TD><TD Align="right"><INPUT Type="text" Name="pricerange" Size="20"></TD></TR>];
	foreach my $key (sort keys %offering_type) { $searchbox_offering_types .= qq[<OPTION Value="$offering_type{$key}"]; if ($offering_type{$key} eq "Used") { $searchbox_offering_types .= " Selected"; } $searchbox_offering_types .= qq[>$offering_type{$key}]; }
	$searchbox_offering_types = qq[<TR><TD>$language_text{thirdparty_text7} </TD><TD Align="right"><SELECT Name="search_type">$searchbox_offering_types</SELECT></TD></TR>];
	%searchbox_types = (
		"ASIN/ISBN" => ["","$searchbox_item"],
		$language_text{searchbox_text6} => ["","$searchbox_string","ActorSearch","dvd","sort"],
		$language_text{searchbox_text9} => ["","$searchbox_string","ArtistSearch","music","sort"],
		$language_text{searchbox_text10} => ["","$searchbox_string","AuthorSearch","books","sort"],
		$language_text{searchbox_text11} => ["","$searchbox_string\n$searchbox_id\n$searchbox_modes\n$searchbox_pricerange","","","sort"],
		$language_text{searchbox_text15} => ["","$searchbox_string","DirectorSearch","dvd","sort"],
		$language_text{searchbox_text16} => ["","$searchbox_string","ListManiaSearch"],
		$language_text{searchbox_text17} => ["","$searchbox_string\n$searchbox_modes","ManufacturerSearch","","sort"],
		$language_text{searchbox_text18} => ["<BR><A Href=$MY_variables{script_name}?search_type=power_search_info$url_options>Details</A>","$searchbox_string","PowerSearch","books","sort"],
		$language_text{searchbox_text19} => ["","$searchbox_string","SimilaritySearch"],
		$language_text{searchbox_text20} => ["","$searchbox_item\n$searchbox_offering_types"],
		$language_text{searchbox_text21} => ["","$searchbox_string","ExchangeSearch"],
		$language_text{searchbox_text22} => ["","$searchbox_string","SellerSearch"],
		$language_text{searchbox_text23} => ["","$searchbox_item\n$searchbox_modes","UpcSearch"],
		$language_text{searchbox_text24} => ["","$searchbox_string","WishlistSearch"],
	);
	my $lap="0"; $MY_variables{all_searchboxes} = qq[<TABLE Border="6" Cellpadding="10" Width="100%">\n];
	foreach my $key (sort keys %searchbox_types) {
		$lap++;
		if (${$searchbox_types{$key}}[2]) { ${$searchbox_types{$key}}[1] .= qq[\n<INPUT Type="hidden" Name="search_type" Value="${$searchbox_types{$key}}[2]">]; }
		if (${$searchbox_types{$key}}[3]) { ${$searchbox_types{$key}}[1] .= qq[\n<INPUT Type="hidden" Name="mode" Value="${$searchbox_types{$key}}[3]">]; }
		if (${$searchbox_types{$key}}[4]) {
			my (@temp_sort_array,$searchbox_sort);
			if ( ${$searchbox_types{$key}}[3] and @{$sort_hash_by_mode{${$searchbox_types{$key}}[3]}} ) {
				@temp_sort_array = @{$sort_hash_by_mode{${$searchbox_types{$key}}[3]}};
			} else {
				@temp_sort_array = @{$sort_hash_by_mode{"all"}};
			}
			foreach my $key1 (keys %sort_hash) {
				if (grep { $_ eq $key1 } @temp_sort_array) {
					$searchbox_sort .= qq[<OPTION Value="$key1"];
					if ($key1 eq $FORM{sort_type}) { $searchbox_sort .= " Selected"; }
					$searchbox_sort .= qq[>$sort_hash{$key1}];
				}
			}
			${$searchbox_types{$key}}[1] .= qq[<TR><TD>$language_text{searchbox_text8}: </TD><TD Align="right"><SELECT Name="sort_type">$searchbox_sort</SELECT></TD></TR>];
		}
		$searchbox_name = $key . "_searchbox";
		${$searchbox_name} = qq|$searchbox_start\n<TR><TD Align="center" Colspan="2"><B>$key${$searchbox_types{$key}}[0]</TD></TR>\n$MY_variables{form_options}\n${$searchbox_types{$key}}[1]\n$searchbox_end\n|;
		if ($lap == 1) { $MY_variables{all_searchboxes} .= "<TR>\n"; }
		$MY_variables{all_searchboxes} .= qq[<TD Align="center" Width="50%">\n${$searchbox_name}</TD>];
		if ($lap == 2) {
			$MY_variables{all_searchboxes} .= "</TR>\n\n";
			$lap = 0;
		}
	}
	if ($lap > 0) { $MY_variables{all_searchboxes} .= "</TR>\n\n"; }
	$MY_variables{all_searchboxes} .= "</TABLE>\n";
}

sub comma_separate_list {
	my($temp_array_ref,$temp_list_start,$temp_search_type,$temp_list,$temp_search_string,$item) = @_;
	foreach $item (@{$temp_array_ref}) {
		if ($temp_list) { $temp_list .= ", "; } else { $temp_list = "$temp_list_start "; }
		($temp_search_string = $item) =~ s/ /+/g;
		$temp_list .= qq[<A Href="$MY_variables{script_name}?mode=$FORM{mode}&search_type=$temp_search_type&input_string=$temp_search_string$url_options">$item</A>];
	}
	return $temp_list;
}

sub initialize_buttons {
	my $value = $_[0];
	my ($asin_buy_button,$temp_button);
	if (!$MY_variables{associate_id}) { $html = "<H1>No associate_id found!</H1>"; }
	if ($MY_variables{ThirdPartyProduct_buy_button}) {
		$asin_buy_button = $MY_variables{ThirdPartyProduct_buy_button};
		if ($value eq "oneclick" or $value eq "cart") { $value = "buy"; }
	} else {
		$asin_buy_button = qq[<INPUT Type="hidden" Name="asin.$AWS_variables{Asin}" Value="1">];
	}
	if ($value eq "wishlist") { $temp_button = qq[<FORM Method="POST" Action="http://www.amazon.com/o/dt/assoc/handle-buy-box=$AWS_variables{Asin}" Target="amazon">$asin_buy_button<INPUT Type="hidden" Name="tag-value" Value="$MY_variables{associate_id}"><INPUT Type="hidden" Name="tag_value" Value="$MY_variables{associate_id}"><INPUT Type="hidden" Name="dev-tag-value" Value="D1KQJBNTALRLQH"><INPUT Style="$MY_variables{submit_button_style}" type="submit" name="submit.add-to-registry.wishlist" value="$language_text{button_text2}"></FORM>]; }
	elsif ($value eq "wedding") { $temp_button = qq[<FORM Method="POST" Action="http://www.amazon.com/o/dt/assoc/handle-buy-box=$AWS_variables{Asin}" Target="amazon">$asin_buy_button<INPUT Type="hidden" Name="tag-value" Value="$MY_variables{associate_id}"><INPUT Type="hidden" Name="tag_value" Value="$MY_variables{associate_id}"><INPUT Type="hidden" Name="dev-tag-value" Value="D1KQJBNTALRLQH"><INPUT Style="$MY_variables{submit_button_style}" type="submit" name="submit.add-to-registry.wedding" value="$language_text{button_text3}"></FORM>]; }
	elsif ($value eq "baby") { $temp_button = qq[<FORM Method="POST" Action="http://www.amazon.com/o/dt/assoc/handle-buy-box=$AWS_variables{Asin}" Target="amazon">$asin_buy_button<INPUT Type="hidden" Name="tag-value" Value="$MY_variables{associate_id}"><INPUT Type="hidden" Name="tag_value" Value="$MY_variables{associate_id}"><INPUT Type="hidden" Name="dev-tag-value" Value="D1KQJBNTALRLQH"><INPUT Style="$MY_variables{submit_button_style}" type="submit" name="submit.add-to-registry.babyreg" value="$language_text{button_text5}"></FORM>]; }
	elsif ($AWS_variables{Availability} =~ /(?:not available|discontinued|out of print|unable|jetzt gebraucht vorbestellen)/i or ($FORM{locale} eq "us" and $value eq "cart" and $AWS_variables{Availability} !~ /Usually ships/)) {
		$MY_variables{my_availability} = $language_text{availability_text2};
		$temp_button = qq[<FORM Method="get" Action="$AWS_variables{DetailsURL}"><INPUT Style="$MY_variables{submit_button_style}" Type="submit" Value=" $language_text{button_text11} "></FORM>];

	} else {
		if ($value eq "buy") { $temp_button = qq[<FORM Method="POST" Action="http://www.amazon.com/exec/obidos/dt/assoc/handle-buy-box=$AWS_variables{Asin}" Target="amazon">$asin_buy_button<INPUT Type="hidden" Name="tag-value" Value="$MY_variables{associate_id}"><INPUT Type="hidden" Name="tag_value" Value="$MY_variables{associate_id}"><INPUT Type="hidden" Name="dev-tag-value" Value="D1KQJBNTALRLQH"><INPUT Style="$MY_variables{submit_button_style}" Type="submit" Name="submit.add-to-cart" Value="$language_text{button_text1}"></FORM>]; }
		if ($value eq "oneclick") { $temp_button = qq[<script language="JavaScript">function popUp(URL,NAME) { amznwin=window.open(URL,NAME,'location=yes,scrollbars=yes,status=yes,toolbar=yes,resizable=yes,width=380,height=450,screenX=10,screenY=10,top=10,left=10'); amznwin.focus(); } document.open(); document.write("<a href=javascript:popUp('http://buybox.amazon.com/exec/obidos/redirect?tag=$MY_variables{associate_id}&link_code=qcb&creative=23424&camp=2025&path=/dt/assoc/tg/aa/xml/assoc/-/$AWS_variables{Asin}/$MY_variables{associate_id}/ref=ac_bb1_,_amazon')><img src=http://rcm-images.amazon.com/images/G/01/associates/remote-buy-box/buy1.gif border=0 alt='Buy from Amazon.com' ></a>"); document.close(); </script> <noscript><form method="POST" action="http://buybox.amazon.com/o/dt/assoc/handle-buy-box=$AWS_variables{Asin}"><input type="hidden" name="asin.$AWS_variables{Asin}" value="1"><input type="hidden" name="tag-value" value="$MY_variables{associate_id}"><input type="hidden" name="tag_value" value="$MY_variables{associate_id}"><INPUT Type="hidden" Name="dev-tag-value" Value="D1KQJBNTALRLQH"><input type="image" name="submit.add-to-cart" value="Buy from Amazon.com" border="0" alt="Buy from Amazon.com" src="http://rcm-images.amazon.com/images/G/01/associates/add-to-cart.gif"></form></noscript>]; }
		if ($value eq "cart") { $temp_button = qq[<FORM Action="$MY_variables{script_name}" Method="GET">$MY_variables{form_options}<INPUT Type="hidden" Name="cart_action" Value="add"><INPUT Type="hidden" Name="cart_item_$AWS_variables{Asin}" Value="1"><INPUT Style="$MY_variables{submit_button_style}" Type="submit" Value="$language_text{button_text10}"></FORM>]; }
		if ($FORM{locale} eq "uk") { $temp_button = qq[<FORM Method="POST" Action="http://www.amazon.co.uk/exec/obidos/dt/assoc/handle-buy-box=$AWS_variables{Asin}" Target="amazon">$asin_buy_button<INPUT Type="hidden" Name="tag-value" Value="$MY_variables{associate_id}"><INPUT Type="hidden" Name="tag_value" Value="$MY_variables{associate_id}"><INPUT Type="hidden" Name="dev-tag-value" Value="D1KQJBNTALRLQH"><INPUT Style="$MY_variables{submit_button_style}" Type="submit" Name="submit.add-to-cart" Value="$language_text{button_text1}"></FORM>]; }
		if ($FORM{locale} eq "de") { $temp_button = qq[<form method="POST" action="http://www.amazon.de/exec/obidos/dt/assoc/handle-buy-box=$AWS_variables{Asin}" Target="amazon">$asin_buy_button<input type="hidden" name="tag-value" value="$MY_variables{associate_id}"><input type="hidden" name="tag_value" value="$MY_variables{associate_id}"><input type="hidden" name="dev-tag-value" value="D1KQJBNTALRLQH"><INPUT Style="$MY_variables{submit_button_style}" Type="submit" Name="submit.add-to-cart" Value="$language_text{button_text1}"></FORM>]; }
	}
	return $temp_button;
}

sub get_extra_product_links {
	my $value = $_[0];
	my ($temp_asins,$temp_xml_result,@temp_xml_feed,$my_temp);
	$temp_asins = join(",", @{$value});
	$temp_xml_result = get_url("http://$amazon_server.amazon.com/onca/xml3?t=$MY_variables{associate_id}&dev-t=D1KQJBNTALRLQH&AsinSearch=$temp_asins&type=lite&f=xml&locale=$FORM{locale}","skip_ok");
	push @temp_xml_feed, ($temp_xml_result =~ /<Details(?:\s[^>]+)?>(.*?)<\/Details>/gsi);
	for my $ii (0 .. $#temp_xml_feed) {
		my ($temp_asin,$temp_product_name);
		@temp_xml_feed[$ii] =~ s/<Asin>([^<]+)<\/Asin>/$temp_asin = $1/sie;
		@temp_xml_feed[$ii] =~ s/<ProductName>([^<]+)<\/ProductName>/$temp_product_name = $1/sie;
		$my_temp .= qq[<LI><A Href="$MY_variables{script_name}?item_id=$temp_asin&search_type=AsinSearch$url_options">$temp_product_name</A><BR>\n];
	}
	return $my_temp;
}

sub build_international_links {
	my $int_options = $see_url_options . $url_options;
	my %international_sites = ( "us" => $language_text{locale_text2}, "uk" => $language_text{locale_text3}, "de" => $language_text{locale_text4} );
	if ($int_options =~ /&search_type=(?:ListManiaSearch|SimilaritySearch|WishlistSearch|SellerSearch)/) { $FORM{mode} = ""; $int_options = $url_options; }
	$int_options =~ s/&search_type=(?:ThirdPartyNew|Used|Collectible|Refurbished)/&search_type=AsinSearch/;
	$int_options =~ s/&search_type=AsinSearch/&input_string=$AWS_variables{ProductName}/;
	delete($international_sites{$FORM{locale}});
	foreach my $int_key (keys %international_sites) {
		my $int_mode = ${$all_store_to_browse{$int_key}}{$lookup_store{$FORM{mode}}}; # i dare you to figure that out :P
		if ($FORM{mode} eq "blended") { $int_mode = $FORM{mode}; }
		if (!$int_mode and $FORM{mode}) { my $temp_mode = $FORM{mode}; $FORM{mode} = ""; $int_options = $url_options; $FORM{mode} = $temp_mode; }
		if ($MY_variables{international_links}) { $MY_variables{international_links} .= ", "; } else { $MY_variables{international_links} = "$language_text{locale_text1} "; }
		$int_options =~ s/(?:&locale=[^&]+|&node=[^&]+)//g;
		$MY_variables{international_links} .= qq[<A Href="$MY_variables{script_name}?mode=$int_mode$int_options&locale=$int_key">$international_sites{$int_key}</A>];
	}
}

sub build_featured_product {
	my $featured_input = shift;
	my ($featured_asin);
	if ($featured_input =~ ",") { @featured_products = split(/,/,$featured_input); $featured_input = "random"; }
	if ($featured_input =~ /browse\+(\d+)\+mode\+(.+)/) {
		my $query = "BrowseNodeSearch=$1&mode=$2&type=lite&page=$FORM{page_num}";
		my $this_xml_url="http://$amazon_server.amazon.com/onca/xml3?t=$MY_variables{associate_id}&dev-t=D1KQJBNTALRLQH&$query&f=xml&sort=$FORM{sort_type}&locale=$FORM{locale}";
		$debug .= "xml_url=$this_xml_url\n";
		my $xml_result = get_url($this_xml_url);
		my $level_1 = parse_xml_into_hashes_of_arrays($xml_result);
		my $featured_asin_xml = ${${$level_1}{Details}}[rand @{${$level_1}{Details}}];
		# now this next line is an impressive piece of poo
		${${$level_1}{Details}}[rand @{${$level_1}{Details}}] =~ s/<Asin>([^<]+)<\/Asin>/$featured_asin = $1/e;
	} elsif ($featured_input eq "random") {
		if (@featured_products) { $featured_asin = $featured_products[rand @featured_products]; } else { return; }
	} else {
		$featured_asin = $featured_input;
	}
	my $this_xml_url = "http://$amazon_server.amazon.com/onca/xml3?t=$MY_variables{associate_id}&dev-t=D1KQJBNTALRLQH&AsinSearch=$featured_asin&type=lite&f=xml&locale=$FORM{locale}";
	$debug .= "xml_url=$this_xml_url\n";
	my $xml_result = get_url($this_xml_url,"skip_ok");
	if (!$xml_result) { return; }
	my $level_1 = parse_xml_into_hashes_of_arrays($xml_result);
	my $level_2 = process_hashes_of_arrays($level_1);
	$MY_variables{featured_ProductName} = ${$level_2}{ProductName}[0];
	$MY_variables{my_featured_item_url} = "$MY_variables{script_name}?item_id=${$level_2}{Asin}[0]&search_type=AsinSearch$url_options";
	$MY_variables{featured_ImageUrlSmall} = ${$level_2}{ImageUrlSmall}[0];
	$MY_variables{featured_OurPrice} = ${$level_2}{OurPrice}[0];
	return set_html("featured_product");
}

sub load_browse_table {
	my ($input1, $input2, $input3) = @_;
	my ($page_listing);
	my $i = 0;
	$menu_length = keys(%{$input1});
	foreach my $key (sort keys %{$input1}) {
		$MY_variables{browse_menu_url} = qq[$MY_variables{script_name}?$input2${$input1}{$key}$url_options];
		$MY_variables{browse_menu_name} = $key;
		if ($input3 eq "nav_menu") {
			$page_listing .= set_html("nav_menu",$i);
		} else {
			$page_listing .= set_html("browse_menu",$i);
		}
		$i++;
	}
	return $page_listing;
}

sub apf_commands {
	my ($command_string, $lap, $whose_variables) = @_;
	$lap = $lap + 1;
	my (%COMMAND, $command_name, $command_value);
	$command_string =~ /<!--apf([^!]+?)!([^!]*?)!-->/s;
	my $command_commands = $1; my $command_html = $2;
	for my $command_pair (split(/&/, $command_commands)) {
		($command_name, $command_value) = split(/=/, $command_pair);
		if ($command_value eq "last") {
			if ($whose_variables eq "sellerprofile") { $command_value = $sellerprofile_max; }
			elsif ($whose_variables eq "shopping_cart") { $command_value = $MY_variables{total_cart_items}; }
			elsif ($whose_variables eq "products") { $command_value = $details_max; }
			elsif ($whose_variables =~ "_menu") { $command_value = $menu_length; }
			else { $command_value = $FORM{max_results}; }
		}
		$COMMAND{$command_name} = $command_value;
	}
	if ($COMMAND{apf_repeat} and !$COMMAND{apf_start}) { $COMMAND{apf_start} = 1; }
	if ($COMMAND{apf_start}) {
		if ($COMMAND{apf_start} == $lap) { return $command_html; }
		if ($COMMAND{apf_repeat}) {
			my $test_lap = ($lap - $COMMAND{apf_start})/$COMMAND{apf_repeat};
			if ($test_lap == int $test_lap) { return $command_html;	}
		}
	}
	if ($COMMAND{apf_end} == $lap) { return $command_html; }
	elsif ($COMMAND{apf_include}) {
		if ($COMMAND{apf_include} eq "nav_menu") {
			if ($FORM{mode} and !$nav_menu_type) {
				$MY_variables{nav_menu_html} = load_browse_table(\%{$FORM{mode}}, "mode=$FORM{mode}&node=", "nav_menu");
			} elsif ($nav_menu_type ne "none") {
				$MY_variables{nav_menu_html} = load_browse_table(\%store_to_browse, "mode=", "nav_menu");
			}
			if ($MY_variables{nav_menu_html}) { return $command_html; }
		} else {
			my $include_variable;
			open(FILEHANDLE,"<$COMMAND{apf_include}");
			while (<FILEHANDLE>) { $include_variable = $include_variable . $_; }
			close (FILEHANDLE);
			return $include_variable;
		}
	} elsif ($COMMAND{apf_featured_product}) {
		my $my_featured_product = build_featured_product($COMMAND{apf_featured_product});
		if ($my_featured_product) { return $my_featured_product; }
	} elsif ($COMMAND{apf_show_vars}) {
		my $temp_html;
		if ($COMMAND{apf_show_vars} ne "AWS") {
			$temp_html = "<BR><FONT Size=+2>MY_variables:</FONT><BR><BR>";
			foreach my $key (sort keys %MY_variables) {
				(my $encoded = $MY_variables{$key}) =~ s/</&lt;/g;
				$temp_html .= "<B>%%$key%%</B> => $encoded<BR><BR>";
			}
			$temp_html .= "<BR><BR>";
		}
		if ($COMMAND{apf_show_vars} ne "MY") {
			$temp_html .= "<BR><FONT Size=+2>AWS_variables:</FONT><BR><BR>";
			foreach my $key (sort keys %AWS_variables) {
				(my $encoded = $AWS_variables{$key}) =~ s/</&lt;/g;
				$temp_html .= "<B>%%$key%%</B> => $encoded<BR><BR>";
			}
			$temp_html .= "<BR><BR>";
		}
		$MY_variables{products_html} .= $temp_html . "<BR><BR>";
	}
	return;
}

sub set_html {
	my ($which, $lap) = @_;
	my $temp_result;
	if ($template_html{$which}) {
		$temp_result = $template_html{$which};
	} else {

		if ($which eq "page") {

			$temp_result = qq[
<!--\n%%debug_html%%\n-->
<HTML><HEAD>
<TITLE>%%store%% : %%subject%%</TITLE>
<TABLE Valign="middle" Width="100%"><TR><TD Align="center">
<H1>If my machine ends up rooted you're fucking dead, Rich</h1>
<H1>%%header%%</H1>
%%in_association%%
%%search_box%%
</TD>
</TR></TABLE>
<CENTER><TABLE Bgcolor="#00659C" Cellpadding="0" Cellspacing="0" Width="100%"><TR><TD Width="33%"><DIV Style="font-size: 6pt">&nbsp;</DIV></TD><TD Align="center" Width="33%"><DIV Style="font-size: 6pt"><NOBR>%%shopping_cart_link%%</NOBR></DIV></TD><TD Width="33%"><DIV Style="font-size: 6pt">&nbsp;</DIV></TD></TR></TABLE></CENTER>
<BR>
<CENTER>%%sort_box%%</CENTER>
%%products_html%%
<TABLE Width="100%"><TR>
<TD Align="left" Width="33%">%%see_prev%%</TD>
<TD Align="center" Width="34%"><FONT Size="-1">%%see_total%%<BR>%%see_index%%</FONT></TD>
<TD Align="right" Width="33%">%%see_next%%</TD>
</TR></TABLE>
<TABLE Bgcolor="#00659C" Cellpadding="0" Cellspacing="0" Width="100%"><TR><TD Align="center"><DIV Style="font-size: 6pt">&nbsp;</DIV></TD></TR></TABLE>
			];

		} elsif ($which eq "products") {

			$temp_result = qq[
%%Asin%% %%ProductName%%<BR>
			];

		} elsif ($which eq "item") {

			$temp_result = qq[
			];

		} elsif ($which eq "larger_image") {

			$temp_result = qq[
			];

		} elsif ($which eq "sellersearch") {

			$temp_result = qq[
			];

		} elsif ($which eq "sellerprofile") {

			$temp_result = qq[
			];

		} elsif ($which eq "thirdparty_products") {

			$temp_result = qq[
			];

		} elsif ($which eq "browse_menu") {

			$temp_result = qq[
<!--apf &apf_start=1! <TABLE Bgcolor="#EEEECC" Cellpadding="5" Width="100%"> !-->
<!--apf &apf_start=1&apf_repeat=3! <TR> !-->
<TD Align="center" Width="33%"><B><A Href="%%browse_menu_url%%">%%browse_menu_name%%</A></B></TD>
<!--apf &apf_start=3&apf_end=last&apf_repeat=3! </TR> !-->
<!--apf &apf_start=last! </TABLE> !-->
			];

		} elsif ($which eq "nav_menu") {

			$temp_result = qq[
<TR><TD><B><A Href="%%browse_menu_url%%">%%browse_menu_name%%</A></B></TD></TR>
			];

		} elsif ($which eq "featured_product") {

			$temp_result = qq[
			];

		} elsif ($which eq "AllSearches") {

			$temp_result = qq[
%%all_searchboxes%%
			];

		} elsif ($which eq "power_search_info") {

			$temp_result = qq[
A power search is used to retrieve book information using a complex query.<BR><BR>The syntax for a power search is shown below:
<UL><LI>author: ambrose<BR><LI>author: ambrose and binding: (abridged or large print) and pubdate: after 11-1996<BR><LI>subject: history and (<st1:country-region><st1:place>spain</st1:place></st1:country-region> or <st1:country-region><st1:place>mexico</st1:place></st1:country-region>) and not military and language: spanish<BR><LI>(subject: marketing and author: kotler) or (publisher: harper and subject: "high technology")<BR><LI>keywords: "high tech*" and not fiction and pubdate: during 1999<BR><LI>isbn: 0446394319 or 0306806819 or 1567993850<BR></UL><BR>
You can use the following properties of a book in your search:
<UL><LI>Title<BR><LI>Subject<BR><LI>Author<BR><LI>Keyword<BR><LI>ISBN<BR><LI>Publisher<BR><LI>Language<BR><LI>Publication date (pubdate)<BR></UL>
You can use the words <I>and</I>, <I>or</I>, and <I>not</I> to link different parts of a book description. You can also group properties together using parentheses if that seems necessary to convey the precise meaning of your description.<BR><BR><BR>
Power Search allows searching against a variety of fields. You can use any combination of the following keywords to match against specific product fields:
<UL><LI>asin:<BR><LI>author:<BR><LI>author-exact:<BR><LI>author-begins:<BR><LI>keywords:<BR><LI>keywords-begin:<BR><LI>language:<BR><LI>publisher:<BR><LI>subject:<BR><LI>subject-words-begin:<BR><LI>subject-begins:<BR><LI>title:<BR><LI>title-words-begin:<BR><LI>title-begins:<BR></UL>
You can combine them with <I>and</I>, <I>or</I>, and <I>not</I>, and you can group them with parentheses.<BR>
			];

		} elsif ($which eq "shopping_cart") {

			$temp_result = qq[
			];

		} elsif ($which eq "blended") {

			$temp_result = qq[
<BR><B Style="color: #CC6600; font-size: large;">%%blended_mode%%:</B> <A Href="%%blended_see_more_url%%">See all %%blended_mode_totalresults%% results...</A><BR>
			];

		} elsif ($which eq "customer_reviews") {
			$temp_result = qq[
%%my_comments%%
<BR><BR><CENTER>%%see_back%%</CENTER>
			];
		}
	}
	$MY_variables{result_number} = $lap + 1 + (($FORM{page_num} - 1) * 10);
	$temp_result =~ s/(<!--apf[^!]+?![^!]*?!-->)/apf_commands($1,$lap,$which);/egs;
	$temp_result =~ s/%%(\w+)%%/if ($AWS_variables{$1}) { $AWS_variables{$1}; } elsif ($MY_variables{$1}) { $MY_variables{$1}; } else { $language_text{$1}; }/ge;
	return $temp_result;
}
