x_session_register('user_bonuses_logged_userid');
// Do not update bonuses for current user if
// - it is already stored in session
// - AND stored for current logged_userid
// - AND update is not forced by config
if (!empty($user_bonuses) && $logged_userid == $user_bonuses_logged_userid && !PS_FORCE_USER_BONUSES) {
    return true;
}
x_load('cart');
if (empty($logged_userid) && $config["General"]["apply_default_country"] == "Y") {
    # Use the default address
    $userinfo["b_country"] = $userinfo["s_country"] = $config["General"]["default_country"];
    $userinfo["b_state"] = $userinfo["s_state"] = $config["General"]["default_state"];
    $userinfo["b_zipcode"] = $userinfo["s_zipcode"] = $config["General"]["default_zipcode"];
    $userinfo["b_city"] = $userinfo["s_city"] = $config["General"]["default_city"];
    $userinfo["b_countryname"] = $userinfo["s_countryname"] = cw_get_country($userinfo["s_country"]);
    $userinfo["b_statename"] = $userinfo["s_statename"] = cw_get_state($userinfo["s_state"], $userinfo["s_country"]);
}
if (!empty($logged_userid)) {
    $userinfo = cw_userinfo($logged_userid, $current_area);
}
$where_statement = '';
$join_statement = '';
# Multidomain addon integration
if (!empty($domain_info)) {
    $join_statement = " LEFT JOIN {$tables['domain_bonuses']} ON {$tables['domain_bonuses']}.bonusid = b.bonusid AND {$tables['domain_bonuses']}.domainid = {$domain_info['domainid']}";
    $where_statement = " AND  {$tables['domain_bonuses']}.bonusid IS NOT NULL";
}
# / Multidomain addon integration
$user_bonuses = cw_query_hash("SELECT b.bonusid, b.*, IF(bl.bonus_name IS NOT NULL,bl.bonus_name,b.bonus_name) as bonus_name, bl.bonus_desc as bonus_desc, IF(bi.id IS NULL,'','Y') as is_image FROM {$tables['bonuses']} b LEFT JOIN {$tables['images_PS']} bi ON bi.id=b.bonusid LEFT JOIN {$tables['bonuses_lng']} bl ON b.bonusid=bl.bonusid AND bl.code='{$shop_language}' {$join_statement} WHERE b.bonus_active='Y' and b.start_date<UNIX_TIMESTAMP() and b.end_date>UNIX_TIMESTAMP() AND b.pid=0 {$where_statement} ORDER BY " . (empty($cat) ? 'pos' : 'priority'), 'bonusid', 0, 0);
if (!empty($user_bonuses)) {
 $zones = cw_query("SELECT {$tables['zones']}.* FROM {$tables['zones']} WHERE 1 {$zones_condition} ORDER BY {$tables['zones']}.zone_name");
 if (!empty($zones)) {
     #
     # Gather the additional information on each zone (for notes field)
     #
     foreach ($zones as $k => $zone) {
         if (!empty($zone['zone_cache'])) {
             $zone_cache_array = explode("-", $zone['zone_cache']);
             for ($i = 0; $i < count($zone_cache_array); $i++) {
                 if (preg_match("/^([\\w])([0-9]+)\$/", $zone_cache_array[$i], $match)) {
                     $zones[$k]['elements'][$i]['element_type'] = $match[1];
                     $zones[$k]['elements'][$i]['counter'] = $match[2];
                     if ($match[2] == 1) {
                         $_element = cw_query_first_cell("SELECT field FROM {$tables['zone_element']} WHERE zone_id='{$zone['zone_id']}' AND field_type='{$match['1']}' LIMIT 1");
                         if ($match[1] == "C") {
                             $element_name = cw_get_country($_element);
                         } elseif ($match[1] == "S") {
                             $element_name = cw_get_state(substr($_element, strpos($_element, "_") + 1), substr($_element, 0, strpos($_element, "_")));
                         } elseif ($match[1] == "G") {
                             $element_name = cw_get_state(substr($_element, strpos($_element, "_") + 1), substr($_element, 0, strpos($_element, "_")));
                         } else {
                             $element_name = $_element;
                         }
                         $zones[$k]['elements'][$i]['element_name'] = $element_name;
                     }
                 }
             }
             usort($zones[$k]['elements'], "sort_zone_elements");
         }
     }
 }
function cw_warehouse_get_info($warehouse)
{
    global $tables;
    $info['warehouse_info'] = cw_query_first("select * from {$tables['customers']} where customer_id='{$warehouse}'");
    $info['warehouse_info']['country_name'] = cw_get_country($info['warehouse_info']['b_country']);
    $info['warehouse_info']['state_name'] = cw_get_state($info['warehouse_info']['b_country'], $info['warehouse_info']['b_state']);
    return $info;
}
function cw_user_process_address($address)
{
    if ($address) {
        $address['titleid'] = cw_user_detect_title(addslashes($address['title']));
        $address['title'] = cw_user_get_title($address['titleid']);
        $address['statename'] = cw_get_state($address['state'], $address['country']);
        $address['countryname'] = cw_get_country($address['country'], null, true);
        if ($config['General']['use_counties'] == 'Y') {
            $address['countyname'] = cw_get_county($address['county']);
        }
        if (empty($address['custom_fields'])) {
            $address['custom_fields'] = cw_user_get_custom_fields($address['customer_id'], $address['address_id'], 'A');
        }
    }
    return $address;
}
     # Send via Email
     #
     $fill_error = $fill_error || empty($recipient_email);
     $giftcert = array("purchaser" => stripslashes($purchaser), "recipient" => stripslashes($recipient), "message" => stripslashes($message), "amount" => $amount, "send_via" => $send_via, "recipient_email" => $recipient_email);
 } else {
     #
     # Send via Postal Mail
     #
     $has_states = cw_query_first_cell("SELECT display_states FROM {$tables['map_countries']} WHERE code = '" . $recipient_country . "'") == 'Y';
     $fill_error = $fill_error || empty($recipient_firstname) || empty($recipient_lastname) || empty($recipient_address) || empty($recipient_city) || empty($recipient_zipcode) || empty($recipient_state) && $has_states || empty($recipient_country) || empty($recipient_county) && $has_states && $config['General']['use_counties'] == "Y";
     if (empty($gc_template) || $config['Gift_Certificates']['allow_customer_select_tpl'] != 'Y') {
         $gc_template = $config['Gift_Certificates']['default_giftcert_template'];
     } else {
         $gc_template = stripslashes($gc_template);
     }
     $giftcert = array("purchaser" => stripslashes($purchaser), "recipient" => stripslashes($recipient), "message" => stripslashes($message), "amount" => $amount, "send_via" => $send_via, "recipient_firstname" => stripslashes($recipient_firstname), "recipient_lastname" => stripslashes($recipient_lastname), "recipient_address" => stripslashes($recipient_address), "recipient_city" => stripslashes($recipient_city), "recipient_zipcode" => $recipient_zipcode, "recipient_county" => $recipient_county, "recipient_countyname" => cw_get_county($recipient_county), "recipient_state" => $recipient_state, "recipient_statename" => cw_get_state($recipient_state, $recipient_country), "recipient_country" => $recipient_country, "recipient_countryname" => cw_get_country($recipient_country), "recipient_phone" => $recipient_phone, "tpl_file" => $gc_template);
 }
 #
 # If gcindex is empty - add
 # overwise - update
 #
 if (!$fill_error && !$amount_error) {
     if (!empty($addons['Gift_Certificates']) && $action == "addgc2wl") {
         cw_include('addons/Wishlist/wishlist.php');
     }
     if ($mode == "preview") {
         $smarty->assign('giftcerts', array($giftcert));
         header("Content-Type: text/html");
         header("Content-Disposition: inline; filename=giftcertificates.html");
         $_tmp_smarty_debug = $smarty->debugging;
         $smarty->debugging = false;
function ps_get_zones()
{
    global $tables;
    $is_shipping = 1;
    $zones_condition = ' and is_shipping = 1';
    $zones = cw_query("SELECT {$tables['zones']}.* FROM {$tables['zones']} WHERE 1 {$zones_condition} ORDER BY {$tables['zones']}.zone_name");
    if (empty($zones) || !is_array($zones)) {
        return array();
    }
    if (!empty($zones)) {
        foreach ($zones as $k => $zone) {
            if (!empty($zone['zone_cache'])) {
                $zone_cache_array = explode("-", $zone['zone_cache']);
                for ($i = 0; $i < count($zone_cache_array); $i++) {
                    if (preg_match("/^([\\w])([0-9]+)\$/", $zone_cache_array[$i], $match)) {
                        $zones[$k]['elements'][$i]['element_type'] = $match[1];
                        $zones[$k]['elements'][$i]['counter'] = $match[2];
                        if ($match[2] == 1) {
                            $_element = cw_query_first_cell("SELECT field FROM {$tables['zone_element']} WHERE zone_id='{$zone['zone_id']}' AND field_type='{$match['1']}' LIMIT 1");
                            if ($match[1] == "C") {
                                $element_name = cw_get_country($_element);
                            } elseif ($match[1] == "S") {
                                $element_name = cw_get_state(substr($_element, strpos($_element, "_") + 1), substr($_element, 0, strpos($_element, "_")));
                            } elseif ($match[1] == "G") {
                                $element_name = cw_get_state(substr($_element, strpos($_element, "_") + 1), substr($_element, 0, strpos($_element, "_")));
                            } else {
                                $element_name = $_element;
                            }
                            $zones[$k]['elements'][$i]['element_name'] = $element_name;
                        }
                    }
                }
                usort($zones[$k]['elements'], "sort_zone_elements");
            }
        }
    }
    return $zones;
}
    }
} elseif ($action == 'print') {
    $giftcerts = false;
    $gc_ids = $gc_ids_p;
    if (!empty($gc_ids) && is_array($gc_ids)) {
        $tpl_cond = !empty($tpl_file) ? " AND tpl_file='{$tpl_file}'" : '';
        $giftcerts = cw_query("SELECT *, add_date FROM {$tables['giftcerts']} WHERE gc_id IN ('" . implode("','", array_keys($gc_ids)) . "') " . $tpl_cond);
    }
    if (empty($giftcerts) || !is_array($giftcerts)) {
        $top_message['type'] = 'W';
        $top_message['content'] = cw_get_langvar_by_name("msg_adm_warn_gc_sel");
        cw_header_location('index.php?target=giftcerts');
    }
    foreach ($giftcerts as $k => $v) {
        $giftcerts[$k]['recipient_statename'] = cw_get_state($v['recipient_state'], $v['recipient_country']);
        $giftcerts[$k]['recipient_countryname'] = cw_get_country($v['recipient_country']);
    }
    $smarty->assign('giftcerts', $giftcerts);
    header("Content-Type: text/html");
    header("Content-Disposition: inline; filename=giftcertificates.html");
    $_tmp_smarty_debug = $smarty->debugging;
    $smarty->debugging = false;
    if (!empty($tpl_file)) {
        $css_file = preg_replace('!\\.tpl$!', '.css', $tpl_file);
        if ($css_file != $tpl_file) {
            $smarty->assign('css_file', $css_file);
        }
    }
    cw_display("addons/estore_gift/admin/gc_admin_print.tpl", $smarty);
    $smarty->debugging = $_tmp_smarty_debug;
    exit;
}
if ($all_languages[$shop_language]['text_direction']) {
    $smarty->assign('reading_direction_tag', ' dir="RTL"');
} else {
    $smarty->assign('reading_direction_tag', '');
}
$smarty->assign('all_languages', $all_languages);
$smarty->assign('shop_language', $shop_language);
$smarty->assign('all_languages_cnt', sizeof($all_languages));
if (empty($edited_language)) {
    $edited_language = $shop_language;
}
$smarty->assign('edited_language', $edited_language);
$smarty->assign('shop_company', $shop_company);
if ($current_area != 'Y' && $config['Company']['country']) {
    $config['Company']['country_name'] = cw_get_country($config['Company']['country']);
    $config['Company']['state_name'] = cw_get_state($config['Company']['state'], $config['Company']['country']);
    $config['Company']['country_has_states'] = cw_query_first_cell("SELECT display_states FROM {$tables['map_countries']} WHERE code = '" . $config['Company']['country'] . "'") == 'Y';
}
# kornev, define the lng links
$_tmp = @parse_url($REQUEST_URI);
parse_str($_tmp['query'], $merge_arr);
if (!is_array($merge_arr)) {
    $merge_arr = array();
}
foreach (array('sl', 'redirect', 'language', 'area') as $k) {
    unset($merge_arr[$k]);
}
$lng_urls = array();
foreach ($all_languages as $code => $tmp) {
    /*
     }
 }
 if (!$fillerror && is_array($additional_fields)) {
     foreach ($additional_fields as $k => $v) {
         $additional_fields[$k]['value'] = stripslashes($_POST['additional_values'][$v['field_id']]);
         if (empty($_POST['additional_values'][$v['field_id']]) && $v['required'] == 'Y' && $v['avail'] == 'Y') {
             $fillerror = true;
         }
     }
 }
 if (!$fillerror) {
     $fillerror = empty($subject) || empty($body);
 }
 if (!$fillerror && !$antibot_err) {
     $contact['b_statename'] = cw_get_state($contact['b_state'], $contact['b_country']);
     $contact['b_countryname'] = cw_get_country($contact['b_country']);
     if ($config['General']['use_counties'] == "Y") {
         $contact['b_countyname'] = cw_get_county($contact['b_county']);
     }
     $contact = cw_stripslashes($contact);
     $smarty->assign('contact', $contact);
     $smarty->assign('profile_fields', $profile_fields);
     $smarty->assign('is_areas', $is_areas);
     $smarty->assign('additional_fields', $additional_fields);
     cw_call('cw_send_mail', array($contact['email'], $config['Company']['support_department'], 'mail/contactus/subj.tpl', 'mail/contactus/body.tpl', $config['default_admin_language']));
     $top_message = array('content' => cw_get_langvar_by_name('txt_contact_us_sent'), 'type' => 'I');
     cw_header_location(cw_call('cw_core_get_html_page_url', array(array("var" => "help", "section" => "contactus", 'delimiter' => '&'))));
 } else {
     cw_unset($_POST, 'additional_values');
     $userinfo = $_POST;
     $userinfo['customer_id'] = $userinfo['uname'];