$versions{'page_display_store.pl'} = '06.6.00.0000';

#######################################################################
#
# AgoraCart and all associated files, except where noted, are
# Copyright 2001 to Present jointly by K-Factor Technologies, Inc.
# and by C E Mayo (aka Mister Ed) at AgoraCart.com & K-Factor.net
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This copyright notice may not be removed or altered in any way.
#
#######################################################################

#
#
# Contains the subroutines/functions for serving non-catalog
# store pages including static pages, store headers & footers.
#
#

#######################################################################
#                     display_page Subroutine
#######################################################################
# used to filter HTML pages through the script and display them in the browser.
#
# display_page takes  four arguments: the file or
# directory which failed, the section in the code in which
# the erroneous call was made, the current file name and
# line number, and is called with the following syntax:
#
# file_open_error("file.name", "ROUTINE", __FILE__, __LINE__);
#
# (notice the two special Perl variables __FILE__, which
# equals the current filename, and __LINE__ which equals
# the current line number).

sub display_page {
    local ( $page, $routine, $file, $line ) = @_;
    local ( $the_file, $canonical_piece, $loaded_page_content, $agorascript_ok2check ) = '';
    local ( $href_fields, $hidden_fields );
    local $canonical_url = qq|$sc_store_url?|;
    my $page_found = 0;
    $sc_page_open_error =~ s/\[\[errormessage\]\]/$agora_metapage_load_message01/;
    $sc_page_title = 'AgoraCart [[agoracgi_ver]]'; # default just in case

    $href_fields      = make_href_fields();
    $hidden_fields    = make_hidden_fields();
    $cart_id_for_html = '[[ZZZ]]'; # temp token - internal use only

    # the subroutine begins by opening the requested file for
    # reading, exiting with file_open_error if there is a problem.

    # use managed content pages
    if ( ( $form_data{'cartlink'} ) && ( $sc_legacy_cartlinks_use ne 'yes' ) ) {
        my $page_location = "$sc_store_menu_pages_path/$cartlink" . '.pl';
        my $page_content_file = "$sc_store_menu_pages_path/$cartlink" . '.inc';
        $page_location =~ s/\.htm[l]*//i;
        $page_content_file =~ s/\.htm[l]*//i;
        if ( -e $page_location ) {
          require_supporting_libraries( __FILE__, __LINE__, "$page_location" );
          if ( ( $sc_page_content eq '' ) && ( -e $page_content_file ) ) {
              open( MYFILE, "$page_content_file" );
              read( MYFILE, $sc_page_content, 256000 );
              close(MYFILE);
              $agorascript_ok2check = 1;
          }

          # assigns page frame for cartlinks
          if ( $use_seo_toggle eq 'yes' && $sc_seo_friendly =~ /yes/i ) {
              $canonical_piece = 'cl/' . $cartlink;
          }
          else {
              $canonical_piece = 'cartlink=' . $cartlink;
          }
          if ( $form_data{'customlink'} ) {
              $the_file = $sc_html_framework{'Custom Link Page'};
          }
          else {
              $the_file = $sc_html_framework{'CMS Link Page'};
          }
          $page_found = 1;
        }

    }
    # use older full html page style
    elsif ( ( $form_data{'cartlink'} ) && ( $sc_legacy_cartlinks_use eq 'yes' ) ) {
        if ( -e $page_location ) {
          open( PAGE, "<$sc_store_menu_pages_path/$cartlink" );
          $page_found = 1;
          &display_page_legacy;
        }
    }
    # use managed content pages
    elsif ( ( $form_data{'page'} ) && ( $sc_legacy_html_pages_use ne 'yes' ) ) {
        my $page_location = $page;
        my $page_content_file = $page;
        $page_location =~ s/\.htm[l]*/\.pl/i;
        $page_content_file =~ s/\.htm[l]*/\.inc/i;
        if ( -e $page_location ) {
          require_supporting_libraries( __FILE__, __LINE__, "$page_location" );
          if ( ( $sc_page_content eq '' ) && ( -e $page_content_file ) ) {
              open( MYFILE, "$page_content_file" );
              read( MYFILE, $sc_page_content, 256000 );
              close(MYFILE);
              $agorascript_ok2check = 1;
          }
          if ( $use_seo_toggle eq 'yes'  && $sc_seo_friendly =~ /yes/i ) {
              $canonical_piece = 'page/' . $page;
          }
          else {
              $canonical_piece = 'page=' . $page;
          }
          # assigns page frame for product pages
          if ( $form_data{'custompage'} ) {
              $the_file = $sc_html_framework{'Custom Page'};
          }
          else {
              $the_file = $sc_html_framework{'CMS Page'};
          }
          $page_found = 1;
        }
    }
    # use older full html page style
    elsif ( ( $form_data{'page'} ) && ( $sc_legacy_html_pages_use eq 'yes' ) ) {
        if ( -e $page ) {
          open( PAGE, "<$page" );
          $page_found = 1;
          &display_page_legacy;
        }
    }
    elsif ( ( $sc_legacy_indexpage_use ne 'yes' ) && ( $routine eq 'Output Frontpage' ) ) {
        my $page_location = $page;
        my $page_content_file = $page;
        $page_location =~ s/\.htm[l]*/\.pl/i;
        $page_content_file =~ s/\.htm[l]*/\.inc/i;
        if ( -e $page_location ) {
          require_supporting_libraries( __FILE__, __LINE__, "$page_location" );
          if ( ( $sc_page_content eq '' ) && ( -e $page_content_file ) ) {
              open( MYFILE, "$page_content_file" );
              read( MYFILE, $sc_page_content, 256000 );
              close(MYFILE);
              $agorascript_ok2check = 1;
          }
          # assigns page frame for the store's main index page, if newer 7.x + format
          $the_file = $sc_html_framework{'CMS Page'};
          $page_found = 1;
        }
    }
    # use older full html page style
    else {
        if ( -e $page ) {
          open( PAGE, "<$page" );
          $page_found = 1;
          &display_page_legacy;
        }
    }

    if ( ( $sc_page_is_active ne 'no' ) && ( $page_found ) ) {
        $canonical_url .= $canonical_piece;
        my $og_image_metatag_string = '';

        if ( $sc_page_meta_og_image_url && $sc_page_meta_og_image_height && $sc_page_meta_og_image_width ) {
            $og_image_metatag_string = $sc_html_framework{'og_image_meta'};
            $og_image_metatag_string =~ s/\[\[og_image_url\]\]/$sc_page_meta_og_image_url/i;
            $og_image_metatag_string =~ s/\[\[og_image_secure_url\]\]/$sc_page_meta_og_image_secure_url/i;
            $og_image_metatag_string =~ s/\[\[og_image_mime\]\]/$sc_page_meta_og_image_mime/i;
            $og_image_metatag_string =~ s/\[\[og_image_width\]\]/$sc_page_meta_og_image_width/i;
            $og_image_metatag_string =~ s/\[\[og_image_height\]\]/$sc_page_meta_og_image_height/i;
            $og_image_metatag_string =~ s/\[\[og_image_alt\]\]/$sc_page_meta_og_image_alt/i;
        }
        &print_agora_http_headers();
        if ( $agorascript_ok2check ) {
            $sc_page_content = agorascript( $sc_page_content, 'pre', "From Loaded HTML Page Content", __FILE__, __LINE__ );
        }
        $the_file =~ s/\[\[title\]\]/$sc_page_title/gi;
        $the_file =~ s/\[\[metaDescription\]\]/$sc_page_meta_description/gi;
        $the_file =~ s/\[\[metaKeywords\]\]/$sc_page_meta_keywords/gi;
        $the_file =~ s/\[\[head_charset\]\]/$sc_header_charset/i;
        $the_file =~ s/\[\[robots_meta\]\]/$sc_robot_meta_tags/ig;
        $the_file =~ s/\[\[canonicalURL\]\]/$canonical_url/gi;
        $the_file =~ s/\[\[site_name\]\]/$sc_schema_site_name/i;
        $the_file =~ s/\[\[image_og\]\]/$og_image_metatag_string/i;
        $the_file = script_and_substitute( $the_file, $page );
        print $the_file;
    }
    else { # error page
    # NEED TO FIX THIS FOR 404 REDIRECT
        $sc_header_status = '404 Not Found';
        &print_agora_http_headers();
        standard_page_header("$sc_error_text01");
        StoreHeader();
        print $sc_page_open_error;
        StoreFooter();
    }
    call_exit();
}

#######################################################################
# display older style pages
# completes process called from display_page subroutine

sub display_page_legacy {

    # It reads in the file one line at a time.
    while (<PAGE>)  {
        $the_file .= $_;

        # Check to see if the add_to_cart_button button
        # has been clicked.  if so, it means that we have just
        # added an item and are returning to the display of the
        # product page.  In this case, we will sneak in an addition
        # confirmation message right after the <FORM> tag line.
        if (   ( $form_data{'add_to_cart_button'} )
            && ( $sc_allow_sneak_in_message =~ /yes/i )
            && ( $sc_shall_i_let_client_know_item_added =~ /yes/i ) )
        {
            if ( $_ =~ /<form/i ) {
                $the_file .= "$sc_item_ordered_message";
            }
        }
    }
    close(PAGE);
}

#######################################################################
#                       StoreHeader Subroutine
#######################################################################

sub StoreHeader {
    print GetStoreHeader();
}

sub GetStoreHeader {
    my $temp_header_thingy = "$sc_store_header_file";

    # dynamic store header by category name, if exists
    # otherwise use default.  Added by Mister Ed Jan 11, 2005
    if (   ( $sc_use_category_name_as_header =~ /yes/i )
        && ( $form_data{'order_form_button.x'} eq '' )
        && ( $form_data{'dc'} eq '' ) )
    {
        my $myproduct = "$form_data{'product'}";
        my $mysubproduct = "$form_data{'product'}";

        # if using subcats, add subcat name to header template name.
        if ( $sc_use_database_subcats =~ /yes/i && $form_data{"$sc_subcat_index_field"} ) {
            $mysubproduct .= '_' . $form_data{"$sc_subcat_index_field"};
        }

        if ( ( $form_data{'cartlink'} || $form_data{'page'} ) && ( $sc_static_page_header_file ) ) {
            if ( -f "$sc_prod_cat_templates_dir/$sc_static_page_header_file" ) {
                $temp_header_thingy = "$sc_prod_cat_templates_dir/$sc_static_page_header_file";
            }
        }
        else {
            $myproduct =~ s/ /\_/g;
            $mysubproduct =~ s/ /\_/g;
            if ( -f "$sc_prod_cat_templates_dir/header-$mysubproduct.tpl" ) {
                $temp_header_thingy = "$sc_prod_cat_templates_dir/header-$mysubproduct.tpl";
            }
            elsif ( -f "$sc_prod_cat_templates_dir/header-$myproduct.tpl" ) {
                $temp_header_thingy = "$sc_prod_cat_templates_dir/header-$myproduct.tpl";
            }
        }

    }

    return StoreHeaderFooter( "$temp_header_thingy", 'Store Header' );
}

#######################################################################
#                       StoreFooter Subroutine                        #
#######################################################################

sub StoreFooter {
    print GetStoreFooter();
}

sub GetStoreFooter {

    my $temp_footer_thingy = "$sc_store_footer_file";

    # dynamic store footer by category name, if exists
    # otherwise use default.  Added by Mister Ed Jan 11, 2005
    if (   ( $sc_use_category_name_as_header =~ /yes/i )
        && ( $form_data{'order_form_button.x'} eq '' )
        && ( $form_data{'order_form_button'} ne 1 )
        && ( $form_data{'dc'} eq '' ) )
    {
        my $myproduct = "$form_data{'product'}";
        my $mysubproduct = "$form_data{'product'}";

        # if using subcats, add subcat name to footer template name.
        if ( $sc_use_database_subcats =~ /yes/i && $form_data{"$sc_subcat_index_field"} ) {
          $mysubproduct .= '_' . $form_data{"$sc_subcat_index_field"};
        }

        if ( ( $form_data{'cartlink'} || $form_data{'page'} ) && ( $sc_static_page_footer_file ) ) {
            if ( -f "$sc_prod_cat_templates_dir/$sc_static_page_footer_file" ) {
                $temp_footer_thingy = "$sc_prod_cat_templates_dir/$sc_static_page_footer_file";
            }
        }
        else {
            $myproduct =~ s/ /\_/g;
            $mysubproduct =~ s/ /\_/g;
            if ( -f "$sc_prod_cat_templates_dir/footer-$mysubproduct.tpl" ) {
                $temp_footer_thingy = "$sc_prod_cat_templates_dir/footer-$mysubproduct.tpl";
            }
            elsif ( -f "$sc_prod_cat_templates_dir/footer-$myproduct.tpl" ) {
                $temp_footer_thingy = "$sc_prod_cat_templates_dir/footer-$myproduct.tpl";
            }
        }
    }

    return StoreHeaderFooter( "$temp_footer_thingy", 'Store Footer' );
}
#######################################################################
#                    CheckoutStoreHeader Subroutine
#######################################################################

sub CheckoutStoreHeader {
    print GetCheckoutStoreHeader();
}

sub GetCheckoutStoreHeader {
    return StoreHeaderFooter( "$sc_checkout_store_header_file",'Checkout Store Header' );
}

#######################################################################
#                    CheckoutStoreFooter Subroutine
#######################################################################
sub CheckoutStoreFooter {
    print GetCheckoutStoreFooter();
}

sub GetCheckoutStoreFooter {
    return StoreHeaderFooter( "$sc_checkout_store_footer_file",'Checkout Store Footer' );
}

#######################################################################
#                       StoreHeaderFooter Subroutine
#######################################################################

sub StoreHeaderFooter {
    local ( $zfile, $ztitle ) = @_;
    local ( $the_file, $very_first_part, $junk ) = q{};
    local ($item_ordered_message) = q{};
    local ($myproduct);

    $myproduct = $form_data{'product'};
    if ( $sc_convert_product_token_underlines ) {
        $myproduct =~ s/\-/$sc_convert_product_token_dashes/g;
        $myproduct =~ s/\_/$sc_convert_product_token_underlines/g;
    }

    open( MYFILE, "$zfile" );
    read( MYFILE, $the_file, 204800 );
    close(MYFILE);

    ( $very_first_part, $the_file, $junk ) = split( /<h3>--$cut_here_div--<\/h3>/i, $the_file, 3 );
    if ( $the_file eq '' ) {
        $the_file = $very_first_part;
    }

    $href_fields      = make_href_fields();
    $hidden_fields    = make_hidden_fields();
    $cart_id_for_html = '[[ZZZ]]';

    if (   ( $form_data{'add_to_cart_button'} )
        && ( $sc_shall_i_let_client_know_item_added =~ /yes/i ) )
    {
        $item_ordered_message = $sc_item_ordered_msg_token;
    }

    $the_file = agorascript( $the_file, 'pre', "$ztitle", __FILE, __LINE__ );

    # normal operation - v5.9 and above
    $the_file =~ s/\[\[item_ordered_msg\]\]/$item_ordered_message/g;
    $the_file =~ s/\[\[StepOneURL\]\]/$sc_stepone_order_script_url/ig;
    $the_file =~ s/\[\[ScriptURL\]\]/$sc_main_script_url/ig;
    $the_file =~ s/\[\[gateway_username\]\]/$sc_gateway_username/g;
    if ( $sc_global_bot_tracker eq '1' ) {
        $the_file =~ s/cart_id=//ig;
        $the_file =~ s/\[\[cart_id\]\]//ig;
    }
    else {
        $the_file =~ s/\[\[cart_id\]\]/$cart_id_for_html/ig;
    }
    $the_file =~ s/\[\[href_fields\]\]/$href_fields/g;
    $the_file =~ s/\[\[make_hidden_fields\]\]/$hidden_fields/g;
    $the_file =~ s/\[\[ppinc\]\]/$form_data{'ppinc'}/g;
    $the_file =~ s/\[\[maxp\]\]/$form_data{'maxp'}/g;
    $the_file =~ s/\[\[page\]\]/$page/g;
    $the_file =~ s/\[\[cartlink\]\]/$cartlink/g;
    $the_file =~ s/\[\[date\]\]/$date/g;
    $the_file =~ s/\[\[product\]\]/$myproduct/g;
    $the_file =~ s/\[\[p_id\]\]/$form_data{'p_id'}/g;
    $the_file =~ s/\[\[keywords\]\]/$keywords/g;
    $the_file =~ s/\[\[next\]\]/$form_data{'next'}/g;
    $the_file =~ s/\[\[exact_match\]\]/$form_data{'exact_match'}/g;
    $the_file =~ s/\[\[exact_case\]\]/$form_data{'exact_case'}/g;
    $the_file =~ s/\[\[URLofImages\]\]/$URL_of_images_directory/g;
    $the_file =~ s/\[\[affiliate\]\]/$form_data{'affiliate'}/g;
    $the_file =~ s/\[\[member\]\]/$form_data{'member'}/g;
    $the_file =~ s/\[\[TemplateName\]\]/$sc_headerTemplateRoot/ig;
    $the_file =~ s/\[\[BaseURL\]\]/$sc_store_base_URL/ig;
    $the_file =~ s/\[\[sslBaseURL\]\]/$sc_SSL_base_URL/ig;
    $the_file =~ s/\[\[sc_money_symbol\]\]/$sc_money_symbol/ig;
    $the_file =~ s/\[\[JS_Links\]\]/$sc_template_javascript_includes/ig;
    $the_file =~ s/\[\[back2cart\]\]/$agora_checkoutPage_back2cart_link/ig;
    $the_file =~ s/\[\[contine_shopping\]\]/$agora_cart_footer_continue/ig;
    $the_file =~ s/\[\[viewcart\]\]/$agora_productPage_viewcart_link/ig;
    $the_file =~ s/\[\[checkout\]\]/$agora_productPage_checkout_link/ig;
    $the_file =~ s/\[\[restartcheckout\]\]/$agora_productPage_restartcheckout_link/ig;
    $the_file =~ s/\[\[storeFullURL\]\]/$sc_store_url/ig;
    $the_file =~ s/\[\[subcatIndexField\]\]/$sc_subcat_index_field/ig;
    $the_file =~ s/\[\[PriceRangeSearch\]\]/$agora_price_range_search_text_for_product_field/ig;
    $the_file =~ s/\[\[searchButton\]\]/$agora_price_range_search_text_for_button/ig;
    $the_file =~ s/\[\[searchBetween\]\]/$agora_price_range_search_between_ranges_text/ig;

    if ( $sc_global_bot_tracker ne '1' ) {
        while ( $the_file =~ /\[\[ZZZ\]\]/ ) {
            $the_file =~ s/\[\[ZZZ\]\]/$cart_id/;
        }
    }

    $the_file = agorascript( $the_file, 'post', "$ztitle", __FILE, __LINE__ );

    return $the_file;

}

#######################################################################
#                    standard_page_header Subroutine
#######################################################################
# Syntax: standard_page_header("TITLE");

sub standard_page_header {
    local ($type_of_page)  = @_;
    local ($hidden_fields) = make_hidden_fields();
    local $header = $sc_html_framework{'Standard Page Header'};
    $header  =~ s/\[\[title\]\]/$type_of_page/i;
    $header  =~ s/\[\[head_info\]\]/$sc_standard_head_info/ig;
    $header =~ s/\[\[head_charset\]\]/$sc_header_charset/i;
    $header  =~ s/\[\[doc_type\]\]/$sc_doctype/ig;
    $header  =~ s/\[\[robots_meta\]\]/$sc_robot_meta_tags/ig;
    $header =~ s/\[\[metaDescription\]\]/$sc_page_meta_description/i;
    $header =~ s/\[\[metaKeywords\]\]/$sc_page_meta_keywords/i;

    codehook('standard_page_header');

    print $header;
}

#######################################################################
#                     capture_STDOUT Subroutine
#######################################################################
# This subroutine redirects STDOUT to a file

sub capture_STDOUT {
    $capture_STDOUT++;
    $CAPTURE_SAVE[$capture_STDOUT] = select();    #current value of STDOUT;

    open( MYSTDOUT, ">$sc_capture_path$capture_STDOUT" );
    select(MYSTDOUT);
    $CAPTURE_CURRENT[$capture_STDOUT] = MYSTDOUT;
}
#######################################################################
#                    uncapture_STDOUT Subroutine
#######################################################################
# This subroutine un-redirects STDOUT to a file, returns that file's
# contents in a string.

sub uncapture_STDOUT {
    my ($contents) = q{};

    if ( $capture_STDOUT < 1 ) {    #not already doing it, nada to return
        return '';
    }
    select( $CAPTURE_SAVE[$capture_STDOUT] );    #previous value of STDOUT;
    close( $CAPTURE_CURRENT[$capture_STDOUT] );
    {
        open( MYFILE, "$sc_capture_path$capture_STDOUT" );  #now need to read it
        local $/ = undef;
        $contents = <MYFILE>;
        close(MYFILE);
    }
    unlink("$sc_capture_path$capture_STDOUT");    #and need to remove it
    $capture_STDOUT = $capture_STDOUT - 1;
    return "$contents";

}

#######################################################################
#                       agorascript Subroutine
#######################################################################
# This subroutine runs <!--agorascript scripts

sub agorascript {
    local ( $str, $type, $zhtml, $zfile, $zline ) = @_;
    local ( $script_start, $script_start_short, $part1, $part1a, $part2 ) = q{};
    my $iterations = 0;
    local ( $err_code, $return_val, $ztype ) = q{};
    local ($on_agorascript_error) = 'warn exit ';

    if ( $type ) {
        $ztype = "-$type";
    }
    else {
        $ztype = q{};
    }
    $script_start_short = "agorascript$ztype";
    $script_start       = "<!--agorascript$ztype";
    $script_end         = '-->';

    # delete any agorascript continuation markers
    $str =~ s/<!--AGSC//ig;
    $str =~ s/AGSC-->//ig;

    $str =~ s/<!--  agorascript/<!-- agorascript/g;    # so either works OK
    $str =~ s/<!-- agorascript/<!--agorascript/g;      # so either works OK
    $str =~ s/<!--agorascript$ztype\n/<!--agorascript$ztype /g;    # -->

    $part1 = $str;
    while ( ( $iterations < 25 ) && ( $str =~ /${script_start_short} / ) ) {
        $iterations++;
        my $kount = 0;
        ( $part1, $part2 ) = split( /${script_start} /, $str, 2 );
        while ( $part2 ) {
            ( $the_script, $part2 ) = split( /$script_end/, $part2, 2 );

            # Taint mode needs to be cleared ... potential security hole here is
            # small, as we only do this routine if we read the file in from our
            # own directory of files we create ourselves.
            # however, the eval itself will still fail if an unsafe operation
            # is attempted with a tainted variable.
            $the_script =~ /([^\xFF]*)/;    # should I make this ^M???
            $the_script = $1;

            # now run the script
            if ( $sc_use_agorascript =~ /yes/i ) {
                $return_val = eval($the_script);
                $err_code   = $@;
            }
            if ( $err_code ) {        #script died, error of some kind
                if ( $on_agorascript_error =~ /warn/i ) {    # error log
                    update_error_log(
                        "agorascript$ztype #$iterations.$kount $sc_error_text01"
                          . "($zhtml) $err_code ",
                        $zfile, $zline
                    );
                }
                if ( $on_agorascript_error =~ /screen/i ) {    # error to STDOUT
                    print
"<CENTER><DIV ALIGN=LEFT><TABLE WIDTH=550><TR><TD><PRE>\n";
                    print "AGORASCRIPT $agora_error_notice_title01:\n";
                    print "agorascript-$type $sc_error_text03 #$kount\n";
                    print "$sc_error_text02: $zhtml\n";
                    print "$sc_error_text01: $err_code\n";
                    print "</PRE></TD></TR></TABLE></DIV></CENTER>\n";
                }
                if ( $on_agorascript_error =~ /exit/i ) {      # error log
                    file_open_error();
                    call_exit();
                }
                if ( $on_agorascript_error =~ /discard/i ) {    # error log
                    $return_val = q{};    # discard the erroneous results
                }
            }
            $part1 .= $return_val;       # got here, so add the info
            $kount++;
            ( $part1a, $part2 ) = split( /$script_start/, $part2, 2 );
            $part1 .= $part1a;
        }    # end of inner while
        $str = $part1;
        $str =~ s/<!--  agorascript-/<!-- agorascript-/g;   # so either works OK
        $str =~ s/<!-- agorascript-/<!--agorascript-/g;     # so either works OK
    }    # end of outter while

    return $part1;

}

#######################################################################
#                    make_hidden_fields Subroutine
#######################################################################
# make_hidden_fields is used to generate the hidden fields
# necessary for maintaining state.  It takes no arguments
# and is called with the following syntax:
#
# make_hidden_fields();

sub make_hidden_fields {

    local ( $hidden, $db_query_row, $temp, $db_form_field , $special_stuff) = q{};

    &xssKiller;

    # $hidden is defined initially as containing the cart_id
    # and page hidden tags which are necessry state variables
    # on EVERY page in the cart.
    #
    # The script then goes through  checking to see which
    # optional state variables it has received as incoming
    # form data.  For each of those, it adds a hidden input
    # tag.

    if ( $sc_running_an_SSI_store =~ /yes/i ) {

        $special_stuff .= '  <input type="hidden" name="SSI" value="1">' . "\n";
        $special_stuff .= '  <input type="hidden" name="crtmod" value="';
        $special_stuff .= make_random_chars() . "\">\n";

    }

    $hidden = qq!$special_stuff!;

    if ( $sc_global_bot_tracker eq '1' ) {
        $hidden .= qq!<input type="hidden" name="page" value="$form_data{'page'}">!;
    }
    else {
        $hidden .= qq!<input type="hidden" name="cart_id" value="$cart_id">
     <input type="hidden" name="page" value="$form_data{'page'}">!;
    }

    if ( $form_data{'keywords'} ) {
        $temp = $form_data{'keywords'};
        $temp =~ s/\0/ /g;       #multi values should be sep. by blanks
        $temp =~ s/(\s+)/ /g;    #de-multi-blank it
        $temp =~ s/(^\s)//g;     #remove leading blank, if there
        $hidden .= qq!<input type="hidden" name="keywords" value="$temp">!;
    }

    if ( $form_data{'cartlink'} ) {
        $hidden .= qq!<input type="hidden" name="cartlink" value="$form_data{'cartlink'}">!;
    }

    if ( $form_data{'next'} ) {
        $hidden .= qq!<input type="hidden" name="next" value="$form_data{'next'}">!;
    }

    if ( $form_data{'ppinc'} ) {
        $hidden .= qq!<input type="hidden" name="ppinc" value="$form_data{'ppinc'}">!;
    }

    if ( $form_data{'maxp'} ) {
        $hidden .= qq!<input type="hidden" name="maxp" value="$form_data{'maxp'}">!;
    }

    if ( $form_data{'exact_match'} ) {
        $hidden .= qq!<input type="hidden" name="exact_match" value="$form_data{'exact_match'}">!;
    }

    if ( $form_data{'case_sensitive'} ) {
        $hidden .= qq!<input type="hidden" name="case_sensitive" value="$form_data{'case_sensitive'}">!;
    }

    # Added by Mister Ed June 20, 2005
    if ( $form_data{'affiliate'} ) {
        $hidden .= qq!<input type="hidden" name="affiliate" value="$form_data{'affiliate'}">!;
    }

    # Added by Mister Ed June 20, 2005
    if ( $form_data{'member'} ) {
        $hidden .= qq!<input type="hidden" name="member" value="$form_data{'member'}">!;
    }

    # Added by Mister Ed June 20, 2005
    if ( $form_data{'pricing'} ) {
        $hidden .= qq!<input type="hidden" name="pricing" value="$form_data{'pricing'}">!;
    }

    foreach $db_query_row (@sc_db_query_criteria) {
        $db_form_field = ( split( /\|/, $db_query_row ) )[0];
        if ( $form_data{$db_form_field} && $db_form_field ne 'keywords' )
        {
            $hidden .= qq!<input type="hidden" name="$db_form_field" value="$form_data{$db_form_field}">!;
        }
    }

    # Added to maintain custom header - added by sb Oct 08, 2008
    if ( $form_data{'hdr'} ) {
        $hidden .= qq!
<input type="hidden" name="hdr" value="$form_data{'hdr'}">!;
    }

    codehook('make_hidden_fields_bot');

    return ($hidden);
}

#######################################################################
#                     make_href_fields Subroutine
#######################################################################
# make_href_fields is used to generate the href fields
# necessary for maintaining state.  It takes no arguments
# and is called with the following syntax:
#
# make_href_fields();

sub make_href_fields {

    local ( $use_seo_toggle ) = @_;
    local ( $href, $db_query_row, $db_form_field, $temp ) = q{};

    # $hidden is defined initially as containing the cart_id
    # and page hidden tags which are necessry state variables
    # on EVERY page in the cart.
    #
    # The script then goes through  checking to see which
    # optional state variables it has received as incoming
    # form data.  For each of those, it adds a hidden input
    # tag.
    &xssKiller;

    my $temp_first_amper_count = 1;
    my $cat_sep = '-';

    if ( $sc_seo_friendly_cats_have_dashes =~ /yes/i && $sc_seo_friendly =~ /yes/i ) {
        $cat_sep = ','
    }

    if ( $form_data{'maxp'} ) {
        $use_seo_toggle = 'no';
        if ( $temp_first_amper_count > 1 ) {
            $href .= "&amp;maxp=$form_data{'maxp'}";
        }
        else {
            $href .= "maxp=$form_data{'maxp'}";
            $temp_first_amper_count++;
        }
    }

    if ( $form_data{'case_sensitive'} ) {
        $use_seo_toggle = 'no';
        if ( $temp_first_amper_count > 1 ) {
            $href .= "&amp;case_sensitive=$form_data{'case_sensitive'}";
        }
        else {
            $href .= "case_sensitive=$form_data{'case_sensitive'}";
            $temp_first_amper_count++;
        }
    }

    # Added by Mister Ed June 20, 2005
    if ( $form_data{'affiliate'} ) {
        $use_seo_toggle = 'no';
        if ( $temp_first_amper_count > 1 ) {
            $href .= "&amp;affiliate=$form_data{'affiliate'}";
        }
        else {
            $href .= "affiliate=$form_data{'affiliate'}";
            $temp_first_amper_count++;
        }
    }

    # Added by Mister Ed June 20, 2005
    if ( $form_data{'member'} ) {
        $use_seo_toggle = 'no';
        if ( $temp_first_amper_count > 1 ) {
            $href .= "&amp;member=$form_data{'member'}";
        }
        else {
            $href .= "member=$form_data{'member'}";
            $temp_first_amper_count++;
        }
    }

    # Added by Mister Ed June 20, 2005
    if ( $form_data{'pricing'} ) {
        $use_seo_toggle = 'no';
        if ( $temp_first_amper_count > 1 ) {
            $href .= "&amp;pricing=$form_data{'pricing'}";
        }
        else {
            $href .= "pricing=$form_data{'pricing'}";
            $temp_first_amper_count++;
        }
    }

    foreach $db_query_row (@sc_db_query_criteria) {
        $db_form_field = ( split( /\|/, $db_query_row ) )[0];
        if ( $form_data{$db_form_field} && $db_form_field ne 'keywords' && $db_form_field ne 'product' && $db_form_field ne "$sc_catlev2" && $db_form_field ne "$sc_catlev3" && $db_form_field ne "$sc_catlev4")
        {
            $use_seo_toggle = 'no';
            if ( $temp_first_amper_count > 1 ) {
                $href .= "&amp;$db_form_field="
                  . str_encode( $form_data{$db_form_field} );
            }
            else {
                $href .=
                  "$db_form_field=" . str_encode( $form_data{$db_form_field} );
                $temp_first_amper_count++;
            }
        }

    }

    if ( $form_data{'product'} ) {
        if ( $use_seo_toggle eq 'yes'  && $sc_seo_friendly =~ /yes/i ) {
            if ( $form_data{'product'} eq '.' ) { $form_data{'product'} = '_'; }
            $href .= "$form_data{'productURL'}";
            $use_seo_toggle = 'yes';
        }
        elsif ( $temp_first_amper_count > 1 ) {
            $href .= "&amp;product=$form_data{'productURL'}";
        }
        else {
            $href .= "product=$form_data{'productURL'}";
            $temp_first_amper_count++;
        }
    }

    if ( $form_data{$sc_catlev2} ) {
        $form_data{$sc_catlev2} =~ s/ /\-/g;
        if ( $use_seo_toggle eq 'yes'  && $sc_seo_friendly =~ /yes/i ) {
            $href .= $cat_sep . $form_data{$sc_catlev2};
            $use_seo_toggle = 'yes';
        }
        elsif ( $temp_first_amper_count > 1 ) {
            $href .= '&amp;' . $sc_catlev2 . '=' . str_encode( $form_data{$sc_catlev2} );
        }
        else {
            $href .= "$sc_catlev2" . '=' . str_encode( $form_data{$sc_catlev2} );
            $temp_first_amper_count++;
        }
    }

    if ( $form_data{$sc_catlev3} ) {
        $form_data{$sc_catlev3} =~ s/ /\-/g;
        if ( $use_seo_toggle eq 'yes'  && $sc_seo_friendly =~ /yes/i ) {
            $href .= $cat_sep . $form_data{$sc_catlev3};
            $use_seo_toggle = 'yes';
        }
        elsif ( $temp_first_amper_count > 1 ) {
            $href .= '&amp;' . $sc_catlev3 . '=' . str_encode( $form_data{$sc_catlev3} );
        }
        else {
            $href .= "$sc_catlev3" . '=' . str_encode( $form_data{$sc_catlev3} );
            $temp_first_amper_count++;
        }
    }

    if ( $form_data{$sc_catlev4} ) {
        $form_data{$sc_catlev4} =~ s/ /\-/g;
        if ( $use_seo_toggle eq 'yes'  && $sc_seo_friendly =~ /yes/i ) {
            $href .= $cat_sep . $form_data{$sc_catlev4};
            $use_seo_toggle = 'yes';
        }
        elsif ( $temp_first_amper_count > 1 ) {
            $href .= '&amp;' . $sc_catlev4 . '=' . str_encode( $form_data{$sc_catlev4} );
        }
        else {
            $href .= "$sc_catlev4" . '=' . str_encode( $form_data{$sc_catlev4} );
            $temp_first_amper_count++;
        }
    }

    if ( $form_data{'keywords'} ) {
        $temp = $form_data{'keywords'};
        $temp = str_encode($temp);
        if ( $use_seo_toggle eq 'yes'  && $sc_seo_friendly =~ /yes/i ) {
            $href .= '-k-' . $temp;
            $use_seo_toggle = 'yes';
        }
        elsif ( $temp_first_amper_count > 1 ) {
            $href .= "&amp;keywords=$temp";
        }
        else {
            $href .= "keywords=$temp";
            $temp_first_amper_count++;
        }
    }

    if ( $form_data{'next'} ) {
        if ( $use_seo_toggle eq 'yes'  && $sc_seo_friendly =~ /yes/i ) {
            $href .= '-n' . $form_data{'next'};
            $use_seo_toggle = 'yes';
        }
        elsif ( $temp_first_amper_count > 1 ) {
            $href .= "&amp;next=$form_data{'next'}";
        }
        else {
            $href .= "next=$form_data{'next'}";
            $temp_first_amper_count++;
        }
    }

    if ( $form_data{'ppinc'}) {
         if ( $use_seo_toggle eq 'yes'  && $sc_seo_friendly =~ /yes/i ) {
            $href .= '-i-' . $form_data{'ppinc'};
            $use_seo_toggle = 'yes';
        }
        elsif ( $temp_first_amper_count > 1 ) {
            $href .= '&amp;ppinc=' . &str_encode( $form_data{'ppinc'} );
        }
        else {
            $href .= 'ppinc=' . &str_encode( $form_data{'ppinc'} );
            $temp_first_amper_count++;
        }
    }

    if ( $form_data{'xm'} && $use_seo_toggle ne 'yes' ) {
        if ( $temp_first_amper_count > 1 ) {
            $href .= "&amp;xm=$form_data{'xm'}";
        }
        else {
            $href .= "xm=$form_data{'xm'}";
            $temp_first_amper_count++;
        }
    }

    if ( $form_data{'page'} && $use_seo_toggle ne 'yes' ) {
        if ( $temp_first_amper_count > 1 ) {
            $href .= '&amp;page=' . str_encode( $form_data{'page'} );
        }
        else {
            $href .= 'page=' . str_encode( $form_data{'page'} );
            $temp_first_amper_count++;
        }
    }

    if ( ( $sc_global_bot_tracker ne '1' ) && ( $sc_seo_friendly ne 'yes' || $use_seo_toggle ne 'yes' ) ) {
        if ( $temp_first_amper_count > 1 ) {
            $href .= '&amp;cart_id=' . str_encode( $cart_id );
        }
        else {
            $href .= 'cart_id=' . str_encode($cart_id);
            $temp_first_amper_count++;
        }
    }

    # Added by Mister Ed Nov 15, 2008
    if ( $form_data{'hdr'} && $use_seo_toggle ne 'yes' ) {
        if ( $temp_first_amper_count > 1 ) {
            $href .= "&amp;hdr=$form_data{'hdr'}";
        }
        else {
            $href .= "hdr=$form_data{'hdr'}";
            $temp_first_amper_count++;
        }
    }

    codehook('make_href_fields_bot');

    return ($href);
}

#######################################################################

1;
