// This code is under copyright and cannot be included in any other book,
// publication, or educational product without permission from O'Reilly &
// Associates. No warranty is attached; we cannot take responsibility for errors
// or fitness for use.
// This is the script that allows the to search and browse wines, and
// to select wines to add to their shopping cart
require_once "../includes/template.inc";
require_once "../includes/winestore.inc";
set_error_handler("customHandler");
session_start();
// Takes <form> heading, instructions, action, formVars name, and
// formErrors name as parameters
$template = new winestoreFormTemplate("Search", "Choose regions and wine types to browse.", S_SEARCH, "searchFormVars", NULL, "GET");
$connection = DB::connect($dsn, true);
if (DB::isError($connection)) {
    trigger_error($connection->getMessage(), E_USER_ERROR);
}
// Create the drop-down search widgets for the page
// Load the regions from the region table
$regionResult = $connection->query("SELECT * FROM region");
if (DB::isError($regionResult)) {
    trigger_error($regionResult->getMessage(), E_USER_ERROR);
}
// Load the wine types from the wine_type table
$wineTypeResult = $connection->query("SELECT * FROM wine_type");
if (DB::isError($wineTypeResult)) {
    trigger_error($wineTypeResult->getMessage(), E_USER_ERROR);
}
$template->selectWidget("region_name", "Region name:", "region_name", $regionResult);
$template->selectWidget("wine_type", "Wine type:", "wine_type", $wineTypeResult);
$template->showWinestore(NO_CART, B_HOME | B_SHOW_CART | B_LOGINLOGOUT);
    foreach ($row as $variable => $value) {
        $_SESSION["custFormVars"]["{$variable}"] = $value;
    }
}
// Load the titles from the title table
$titleResult = $connection->query("SELECT * FROM titles");
if (DB::isError($titleResult)) {
    trigger_error($titleResult->getMessage(), E_USER_ERROR);
}
// Load the countries from the country table
$countryResult = $connection->query("SELECT * FROM countries");
if (DB::isError($countryResult)) {
    trigger_error($countryResult->getMessage(), E_USER_ERROR);
}
// Create widgets for each of the customer fields
$template->selectWidget("title_id", "Title:", "title", $titleResult);
$template->mandatoryWidget("firstname", "First name:", 50);
$template->mandatoryWidget("surname", "Surname:", 50);
$template->optionalWidget("initial", "Middle initial:", 1);
$template->mandatoryWidget("address", "Address:", 50);
$template->mandatoryWidget("city", "City:", 50);
$template->optionalWidget("state", "State:", 20);
$template->mandatoryWidget("zipcode", "Zip code:", 10);
$template->selectWidget("country_id", "Country:", "country", $countryResult);
$template->optionalWidget("phone", "Telephone:", 15);
$template->mandatoryWidget("birth_date", "Date of Birth (dd/mm/yyyy):", 10);
// Only show the username/email and password widgets to new users
if (!isset($_SESSION["loginUsername"])) {
    $template->mandatoryWidget("loginUsername", "Email/username:"******"loginPassword", "Password:", 15);
}