}
    cw_header_location("index.php?target={$target}&mode=fields&js_tab=addition_section");
}
if ($action == 'delete_sections') {
    foreach ($update as $k => $v) {
        if ($v['del'] == 'Y') {
            cw_profile_fields_delete_section($k);
        }
    }
    cw_header_location("index.php?target={$target}&mode=fields&js_tab=addition_section");
}
$membership_titles = array();
$usertypes_array = array();
$usertypes_array[$user_type] = '';
if (in_array($user_type, array('C'))) {
    $customer_memberships = cw_user_get_memberships($user_type);
    if (is_array($customer_memberships)) {
        foreach ($customer_memberships as $val) {
            $usertypes_array[$user_type . '_' . $val['membership_id']] = '';
            $membership_titles[$user_type . '_' . $val['membership_id']] = $val['membership'];
        }
    }
}
$smarty->assign('membership_titles', $membership_titles);
$smarty->assign('customer_memberships', $customer_memberships);
$smarty->assign('customer_memberships_count', count($customer_memberships));
# kornev, sort and add the sections if required.
if ($mode == 'fields') {
    list($sections, $fields, $additional_fields) = cw_profile_fields_get_sections('U', true, '', false, $edited_language);
} else {
    list($sections, $fields, $additional_fields) = cw_profile_fields_get_sections('U');
                    $message['countries'][$val['code']] = true;
                }
            }
            $smarty->assign('message', $message);
            $smarty->assign('messageid', $messageid);
        }
        $messages = cw_query("SELECT * FROM {$tables['newsletter']} WHERE list_id='{$list_id}'");
        $smarty->assign('messages', $messages);
        $smarty->assign('main', 'management');
    } else {
        $smarty->assign('main', 'details');
        $list['usertype'] = 'C';
        $smarty->assign('list', $list);
        // default values of new list
    }
    $smarty->assign('memberships', cw_user_get_memberships(array('C', 'R')));
    $location[] = array($list['name'], '');
} else {
    $lists = cw_query("SELECT * FROM {$tables['newslists']} WHERE lngcode='{$current_language}' {$salesman_condition}");
    $smarty->assign('lists', $lists);
    $smarty->assign('main', 'lists_select');
}
if (cw_session_is_registered('nwslt_object')) {
    $nwslt_object =& cw_session_register('nwslt_object');
    if (is_array($nwslt_object)) {
        foreach ($nwslt_object as $k => $v) {
            $smarty->assign($k, $v);
        }
    }
    cw_session_unregister("nwslt_object");
}
        }
        $valid_statuses = cw_query_column("select code from {$tables['order_statuses']} where inventory_decreasing=1");
        foreach ($users as $k => $v) {
            $users[$k]['orders'] = cw_query_first_cell("SELECT count(d.doc_id) FROM {$tables['docs_user_info']} dui, {$tables['docs']} d WHERE dui.customer_id={$v['customer_id']} AND dui.doc_info_id=d.doc_info_id AND d.type='O' and d.status in ('" . implode("','", $valid_statuses) . "')");
        }
        $smarty->assign('users', $users);
    }
    $smarty->assign('mode', $mode);
}
$predefined_lng_variables[] = 'lbl_search_user_' . $usertype;
if (is_array($search_data['users'][$usertype]['address']['state'])) {
    $search_data['users'][$usertype]['address']['state'] = json_encode($search_data['users'][$usertype]['address']['state']);
}
$smarty->assign('js_tab', $search_data['users'][$usertype]['js_tab']);
$smarty->assign('mode', $mode);
$smarty->assign('payment_methods', cw_func_call('cw_payment_search', array('data' => array('type' => 1, 'active' => 1))));
$smarty->assign('memberships', cw_user_get_memberships($usertype == 'C' ? array('C', 'R') : $usertype));
$smarty->assign('employees', cw_user_get_short_list('E'));
$smarty->assign('sales_managers', cw_user_get_short_list('B'));
$smarty->assign('countries', cw_map_get_countries());
$smarty->assign('states', cw_map_get_states());
$smarty->assign('saved_user_search', cw_query("select * from {$tables['saved_search']} where type='C' order by name, ss_id"));
$smarty->assign('search_prefilled', $search_data['users'][$usertype]);
$smarty->assign('current_search_type', $usertype);
$smarty->assign('current_loaded_search_id', $current_loaded_search_id);
$smarty->assign('current_loaded_search_name', cw_query_first_cell("select name from {$tables['saved_search']} where type='C' and ss_id='{$current_loaded_search_id}'"));
if ($is_ajax) {
    global $ajax_blocks;
    $ajax_blocks[] = array('id' => 'search_result', 'template' => 'main/users/search_results.tpl');
    $ajax_blocks[] = array('id' => 'script', 'template' => 'js/navigation.js');
}
        $smarty->assign('messages', $messages);
    }
    $location[] = array(cw_get_langvar_by_name("lbl_messages"), "");
}
if (cw_session_is_registered("nwslt_object")) {
    $nwslt_object =& cw_session_register("nwslt_object");
    if (is_array($nwslt_object)) {
        foreach ($nwslt_object as $k => $v) {
            $smarty->assign($k, $v);
        }
    }
    cw_session_unregister("nwslt_object");
}
if (!empty($targetlist)) {
    $targetlistname = cw_query_first_cell("SELECT name FROM {$tables['newslists']} WHERE list_id='{$targetlist}'");
    $smarty->assign('targetlistname', $targetlistname);
    $smarty->assign('targetlist', $targetlist);
}
$lists = cw_query("SELECT * FROM {$tables['newslists']} WHERE lngcode='{$current_language}' {$salesman_condition}");
$smarty->assign('lists', $lists);
$smarty->assign('action', $action);
$smarty->assign('mode', $mode);
cw_load('user');
$memberships = cw_user_get_memberships(array('C', 'R'));
//cw_query("select * from $tables[memberships] where area='C' order by orderby");
$memberships[] = array('membership' => cw_get_langvar_by_name("lbl_retail_level"), 'membership_id' => 0);
$smarty->assign('memberships', $memberships);
$smarty->assign('flag_show_as_news', $flag_show_as_news);
$smarty->assign('current_script', $current_script);
cw_load('map');
$smarty->assign('countries', cw_map_get_countries());
$smarty->assign('cart_giftcerts', $aom_orders[$doc_id]['giftcerts']);
# user information
cw_load('map', 'profile_fields');
$smarty->assign('countries', cw_map_get_countries());
if (!$aom_orders[$doc_id]['userinfo']['usertype']) {
    $aom_orders[$doc_id]['userinfo']['usertype'] = cw_doc_get_defaulttype($aom_orders[$doc_id]['type']);
}
$fields_area = cw_profile_fields_get_area($aom_orders[$doc_id]['userinfo']['customer_id'], $aom_orders[$doc_id]['userinfo']['membership_id'], 0, $aom_orders[$doc_id]['userinfo']['usertype'], true);
list($profile_sections, $profile_fields, $additional_fields) = cw_profile_fields_get_sections('U', true, $fields_area);
$smarty->assign('profile_sections', $profile_sections);
$smarty->assign('profile_fields', $profile_fields);
$smarty->assign('additional_fields', $additional_fields);
$smarty->assign('cart_customer', $aom_orders[$doc_id]['userinfo']);
$smarty->assign('customer', $doc_data['userinfo']);
$mem_usertype = $aom_orders[$doc_id]['userinfo']['usertype'];
$smarty->assign('memberships', cw_user_get_memberships($mem_usertype));
if ($real_taxes == "Y") {
    global $current_area, $customer_id, $user_account;
    $_saved_data = compact("current_area", "customer_id", "user_account");
    $current_area = $current_area == 'G' ? 'G' : 'C';
    $customer_id = $aom_orders[$doc_id]['userinfo']['customer_id'];
    $user_account = $aom_orders[$doc_id]['userinfo'];
}
$smarty->assign('payment_methods', cw_func_call('cw_payment_search', array('data' => array('type' => 1))));
//$smarty->assign('shipping', cw_func_call('cw_shipping_search', array('data' => array('active' => 1))));
//$smarty->assign('salesmen', cw_user_get_salesmans_for_register());
$smarty->assign('cod_types', cw_shipping_get_shipping_cod_types($aom_orders[$doc_id]['info']['shipping_id']));
$config['Appearance']['allow_update_quantity_in_cart'] = "N";
$smarty->assign('default_use_only_ean', 'Y');
$smarty->assign('order', $aom_orders[$doc_id]);
$smarty->assign('orig_order', $doc_data);