function cw_user_get_title($titleid, $code = false)
{
    global $tables, $current_language;
    if (empty($titleid)) {
        return false;
    }
    $title = cw_get_languages_alt("title_" . $titleid, $code);
    if (empty($title)) {
        $title = cw_query_first_cell("select title from {$tables['titles']} where titleid = '{$titleid}'");
    }
    return $title;
}
            }
            $count_surveys++;
            if (cw_check_survey_filling($sid)) {
                $surveys[$sid]['is_filled'] = true;
                $count_filled++;
            }
            if (!empty($filled_surveys) && in_array($sid, $filled_surveys) && $v['publish_results'] == 'Y') {
                $id = array_search($sid, $filled_surveys);
                $rid = cw_query_first_cell("SELECT survey_result_id FROM {$tables['survey_results']} WHERE survey_result_id = '{$id}' AND survey_id = '{$sid}'");
                if (!empty($rid)) {
                    $surveys[$sid]['is_view_results'] = true;
                } else {
                    cw_unset($filled_surveys, $id);
                }
            }
            $surveys[$sid]['survey'] = cw_get_languages_alt("survey_name_" . $sid, false, true);
        }
        if (!empty($surveys)) {
            $smarty->assign('surveys', $surveys);
            $smarty->assign('count_surveys', $count_surveys);
            $smarty->assign('count_filled', $count_filled);
            $smarty->assign('count_unfilled', $count_surveys - $count_filled);
        }
    }
    $location[] = array(cw_get_langvar_by_name("lbl_survey_surveys"));
    $smarty->assign('current_main_dir', 'addons');
    $smarty->assign('current_section_dir', 'survey');
    $smarty->assign('main', 'customer_surveys');
}
if (!empty($antibot_err)) {
    $smarty->assign('antibot_err', $antibot_err);
            cw_membership_update("tax_rate", $rate_id, $membership_ids, "rate_id");
        } else {
            $top_message['content'] = cw_get_langvar_by_name("msg_err_tax_rate_add");
            $top_message['type'] = "E";
        }
        $top_message['anchor'] = "rates";
        $redirect_to = "&tax_id={$tax_id}";
    }
    cw_header_location('index.php?target=' . $target . $redirect_to);
}
if ($mode == "add" || !empty($tax_id)) {
    $location[] = array(cw_get_langvar_by_name('lbl_taxes'), 'index.php?target=' . $target);
    $location[] = array(cw_get_langvar_by_name('lbl_tax_details'), '');
    if (!empty($tax_id)) {
        $tax_details = cw_query_first("SELECT * FROM {$tables['taxes']} WHERE tax_id='{$tax_id}'");
        $tax_details['tax_display_name'] = cw_get_languages_alt('tax_' . $tax_id);
    }
    if (empty($tax_details)) {
        $mode = "add";
        if (cw_session_is_registered("tmp_tax_details")) {
            $tmp_tax_details =& cw_session_register("tmp_tax_details");
            $tax_details = $tmp_tax_details;
            cw_session_unregister("tmp_tax_details");
        }
    } else {
        $tax_rates = cw_query("SELECT {$tables['tax_rates']}.*, {$tables['zones']}.zone_name FROM {$tables['tax_rates']} LEFT JOIN {$tables['zones']} ON {$tables['tax_rates']}.zone_id={$tables['zones']}.zone_id WHERE {$tables['tax_rates']}.tax_id='{$tax_id}' ORDER BY {$tables['zones']}.zone_name, {$tables['tax_rates']}.rate_value");
        $tmp = cw_user_get_memberships(array('C', 'R'));
        if (!empty($tax_rates)) {
            foreach ($tax_rates as $k => $v) {
                $keys = cw_query_column("SELECT membership_id FROM {$tables['tax_rate_memberships']} WHERE rate_id = '{$v['rate_id']}'");
                if (!empty($tmp) && !empty($keys)) {
    $smarty->assign('navigation', $navigation);
    if ($total_items > 0) {
        $smarty->assign('maillist', cw_query("SELECT * FROM {$tables['survey_maillist']} WHERE survey_id = '{$survey_id}' ORDER BY date DESC LIMIT {$navigation['first_page']}, {$navigation['objects_per_page']}"));
    }
    # Get news lists
    if (!empty($addons['news'])) {
        $news_list = cw_query("SELECT {$tables['newslists']}.list_id, {$tables['newslists']}.name FROM {$tables['newslists']}, {$tables['newslist_subscription']} WHERE {$tables['newslists']}.list_id = {$tables['newslist_subscription']}.list_id AND {$tables['newslists']}.lngcode = '" . $current_language . "' GROUP BY {$tables['newslists']}.list_id");
        if (!empty($news_list)) {
            $smarty->assign('news_list', $news_list);
        }
    }
    # Get another surveys
    $another_surveys = cw_query_hash("SELECT {$tables['surveys']}.survey_id FROM {$tables['surveys']}, {$tables['survey_maillist']} WHERE {$tables['surveys']}.survey_id != '{$survey_id}' AND {$tables['surveys']}.survey_id = {$tables['survey_maillist']}.survey_id GROUP BY {$tables['surveys']}.survey_id", "survey_id", false);
    if (!empty($another_surveys)) {
        foreach ($another_surveys as $sid => $v) {
            $another_surveys[$sid] = cw_get_languages_alt("survey_name_" . $sid);
        }
        $smarty->assign('another_surveys', $another_surveys);
    }
}
if ($js_tab == 'stats' || $js_tab == "instances") {
    $smarty->assign('filter', $filter_surveys[$survey_id]);
    $smarty->assign('is_filter', !empty($filter_surveys[$survey_id]['date_from']) && !empty($filter_surveys[$survey_id]['date_to']));
    $smarty->assign('now', time());
    $smarty->assign('prev_month', mktime(date("H"), date("i"), date("s"), date("m") - 1, date("d"), date("Y")));
}
if ($action == 'printable') {
    if (is_array($survey['questions'])) {
        foreach ($survey['questions'] as $_questionid => $_question) {
            if (is_array($_question['answers'])) {
                foreach ($_question['answers'] as $_answer_id => $_answer) {
    cw_array2insert("titles", $add);
    # Update title(s)
} elseif ($action == "update" && !empty($data)) {
    foreach ($data as $id => $v) {
        $v['active'] = $v['active'];
        cw_languages_alt_insert("title_" . $id, $v['title'], $current_language);
        if ($current_language != $config['default_admin_language']) {
            unset($v['title']);
        }
        cw_array2update("titles", $v, "titleid = '{$id}'");
    }
    # Delete title(s)
} elseif ($action == "delete" && !empty($ids)) {
    $string = "titleid IN ('" . implode("','", $ids) . "')";
    db_query("DELETE FROM {$tables['titles']} WHERE " . $string);
    db_query("DELETE FROM {$tables['languages_alt']} WHERE name IN ('title_" . implode("','title_", $ids) . "')");
}
if (!empty($action)) {
    cw_header_location("index.php?target=titles");
}
$titles = cw_query("SELECT * FROM {$tables['titles']} ORDER BY orderby, title");
if (!empty($titles)) {
    foreach ($titles as $k => $v) {
        $name = cw_get_languages_alt("title_" . $v['titleid']);
        if (!empty($name)) {
            $titles[$k]['title'] = $name;
        }
    }
    $smarty->assign('titles', $titles);
}
$smarty->assign('main', 'titles');
function cw_get_product_tax_rates($product, $user_info, $include_in_any_case = false, $special_taxes = false)
{
    global $tables, $config;
    static $saved_tax_rates = array();
    static $saved_tax = array();
    # Define input data
    $is_array = true;
    if (is_int($product)) {
        $is_array = false;
        $_product = array($product => array('product_id' => $product));
    } elseif (isset($product['product_id'])) {
        $is_array = false;
        $_product = array($product['product_id'] => $product);
    } else {
        $_product = array();
        foreach ($product as $k => $p) {
            $_product[$p['product_id']] = $p;
        }
    }
    unset($product);
    $zone_account = $user_info;
    $company_id = $user_info['company_id'];
    if (isset($saved_tax[$company_id])) {
        return $saved_tax[$company_id];
    }
    if ($special_taxes) {
        $_taxes = cw_func_call('cw_taxes_search', array('product_id' => $product['product_id'], 'data' => array('active' => 1, 'use_info' => 1)));
    } else {
        $_taxes = cw_func_call('cw_taxes_search', array('product_id' => $product['product_id'], 'data' => array('active' => 1)));
    }
    $taxes = array();
    if (is_array($_taxes)) {
        foreach ($_taxes as $k => $v) {
            if ($v['address_type'] == 'O') {
                $v['address_type_real'] = 'O';
                $v['address_type'] = 'B';
                $taxes[] = $v;
                $v['address_type'] = 'S';
            }
            $taxes[] = $v;
        }
    }
    if (empty($taxes) || !is_array($taxes)) {
        return array();
    }
    # Define available customer zones
    $tax_rates = $address_zones = $_tax_names = array();
    $display_including_tax = null;
    if ($include_in_any_case) {
        $display_including_tax = 1;
    }
    foreach ($taxes as $k => $v) {
        $_tax_names["tax_" . $v['tax_id']] = true;
        if ($v['address_type_real'] == 'O') {
            $taxes[$k]['tax_name'] = $v['tax_name'] . '_' . $v['address_type'];
        }
        if (isset($display_including_tax)) {
            $taxes[$k]['display_including_tax'] = $display_including_tax;
        }
    }
    # Get tax names
    $_tax_names = cw_get_languages_alt(array_keys($_tax_names));
    if ($config['Taxes']['enable_user_tax_exemption'] == "Y") {
        #
        # Get the 'tax_exempt' feature of customer
        #
        static $_customer_tax_exempt;
        if (empty($_customer_tax_exempt)) {
            $_customer_tax_exempt = cw_query_first_cell("select tax_exempt from {$tables['customers_customer_info']} where customer_id='{$use_customer_id}'");
        }
        if ($_customer_tax_exempt == "Y") {
            $tax_rate['skip'] = true;
        }
    } else {
        $_customer_tax_exempt = "";
    }
    foreach ($_product as $product_id => $product) {
        if ($product['free_tax'] == 'Y') {
            // || !is_array($_taxes[$product_id]) || empty($_taxes[$product_id]))
            continue;
        }
        # kornev, because all of the products are related with admin and we should get the appropriate settings
        $admin_warehouse = cw_get_default_account_for_zones();
        # Generate tax rates array
        foreach ($taxes as $k => $v) {
            $product['warehouse'] = $admin_warehouse;
            if (!isset($address_zones[$product['warehouse']][$v['address_type']])) {
                $address = $v['address_type'] == 'B' ? $zone_account['main_address'] : $zone_account['current_address'];
                $address_zones[$product['warehouse']][$v['address_type']] = array_keys(cw_call('cw_cart_get_zones', array('address' => $address)));
            }
            $zones = $address_zones[$product['warehouse']][$v['address_type']];
            $tax_rate = array();
            # kornev. one rate can be repeated twice. the "both" address mode.
            if (!empty($zones) && is_array($zones)) {
                foreach ($zones as $zone_id) {
                    $saved_key = $v['tax_id'] . '_' . $v['address_type'] . '_' . $zone_id . '_' . $membership_id;
                    if (isset($saved_tax_rates[$saved_key])) {
                        $tax_rate = $saved_tax_rates[$saved_key];
                    } else {
                        $tax_rate = cw_query_first($sql = "SELECT {$tables['tax_rates']}.tax_id, {$tables['tax_rates']}.formula, {$tables['tax_rates']}.rate_value, {$tables['tax_rates']}.rate_type FROM {$tables['tax_rates']} LEFT JOIN {$tables['tax_rate_memberships']} ON {$tables['tax_rate_memberships']}.rate_id = {$tables['tax_rates']}.rate_id WHERE {$tables['tax_rates']}.tax_id = '{$v['tax_id']}' {$warehouse_condition} AND {$tables['tax_rates']}.zone_id = '{$zone_id}' AND ({$tables['tax_rate_memberships']}.membership_id = '{$membership_id}' OR {$tables['tax_rate_memberships']}.membership_id IS NULL) ORDER BY {$tables['tax_rate_memberships']}.membership_id DESC LIMIT 1");
                        if ($tax_rate) {
                            $tax_rate['address_type'] = $v['address_type'];
                            $tax_rate['address_type_real'] = $v['address_type_real'];
                        }
                        $saved_tax_rates[$saved_key] = $tax_rate;
                    }
                    if (!empty($tax_rate)) {
                        break;
                    }
                }
            }
            if (empty($tax_rate) || $_customer_tax_exempt == "Y") {
                if (!$v['price_includes_tax'] != "Y") {
                    continue;
                }
                $tax_rate = cw_query_first("SELECT {$tables['tax_rates']}.tax_id, {$tables['tax_rates']}.formula, {$tables['tax_rates']}.rate_value, {$tables['tax_rates']}.rate_type FROM {$tables['tax_rates']} LEFT JOIN {$tables['tax_rate_memberships']} ON {$tables['tax_rate_memberships']}.rate_id = {$tables['tax_rates']}.rate_id WHERE {$tables['tax_rates']}.tax_id='{$v['tax_id']}' {$warehouse_condition} AND ({$tables['tax_rate_memberships']}.membership_id = '{$membership_id}' OR {$tables['tax_rate_memberships']}.membership_id IS NULL) ORDER BY {$tables['tax_rates']}.rate_value DESC LIMIT 1");
                $tax_rate['skip'] = true;
            }
            if (empty($tax_rate['formula'])) {
                $tax_rate['formula'] = $v['formula'];
            }
            $tax_rate['rate_value'] *= 1;
            $tax_rate['tax_display_name'] = isset($_tax_names["tax_" . $v['tax_id']]) ? $_tax_names["tax_" . $v['tax_id']] . ($v['address_type_real'] == 'O' ? $v['address_type'] == 'B' ? ' ' . cw_get_langvar_by_name('lbl_tax_billing') : ' ' . cw_get_langvar_by_name('lbl_tax_shipping') : '') : $v['tax_name'];
            if ($is_array) {
                $tax_rates[$product_id][$v['tax_name']] = cw_array_merge($v, $tax_rate);
            } else {
                $tax_rates[$v['tax_name']] = cw_array_merge($v, $tax_rate);
            }
        }
    }
    $saved_tax[$company_id] = $tax_rates;
    return $tax_rates;
}