function cw_check_user_field_build_profile($profile, $fields, $profile_fields)
{
    global $customer_id;
    // Map of data trasition from section/field to profile field
    $map = array('basic' => array('email' => '', 'password' => '', 'ssn' => '', 'tax_number' => 'additional_info', 'language' => '', 'company' => 'additional_info', 'company_type' => 'additional_info', 'membership_id' => ''), 'customer_info' => array('birthday' => 'additional_info', 'birthday_place' => 'additional_info', 'sex' => 'additional_info', 'married' => 'additional_info', 'nationality' => 'additional_info'), 'customer_company' => array('employees' => 'additional_info', 'foundation' => 'additional_info', 'foundation_place' => 'additional_info', 'employees' => 'additional_info'), 'mailing_list' => '', 'commerciale' => array('company_id' => 'additional_info', 'can_change_company_id' => 'additional_info', 'parent_customer_id' => 'salesman_info', 'department' => 'additional_info', 'division_id' => 'additional_info', 'contact_date' => 'additional_info', 'salesman_customer_id' => 'relations', 'warehouse_customer_id' => 'relations', 'doc_prefix' => 'additional_info', 'order_entering_format' => 'additional_info'), 'administration' => array('special_tax' => 'additional_info', 'tax_id' => 'additional_info', 'payment_note' => 'additional_info', 'tax_exempt' => 'additional_info', 'separate_invoices' => 'additional_info', 'department' => 'additional_info'), 'shipping' => array('shipping_operated' => 'additional_info', 'shipment_paid' => 'additional_info', 'shipping_company_to' => 'additional_info', 'shipping_company_from' => 'additional_info', 'cod_delivery_type' => 'additional_info', 'special_tax' => 'additional_info'));
    if (AREA_TYPE == 'A') {
        $map['basic'] = array_merge($map['basic'], array('status' => '', 'change_password' => '', 'status_note' => 'additional_info'));
        $profile_fields['basic']['change_password']['is_avail'] = true;
        $profile_fields['basic']['status_note']['is_avail'] = true;
    }
    foreach ($map as $section => $all_fields) {
        if (!isset($fields[$section])) {
            continue;
        }
        if (empty($all_fields)) {
            $profile[$section] = $fields[$section];
            continue;
        }
        if (is_scalar($all_fields)) {
            $profile[$all_fields] = $fields[$section];
            continue;
        }
        foreach ($all_fields as $field => $to) {
            if ($profile_fields[$section][$field]['is_avail']) {
                if (empty($to)) {
                    $profile[$field] = $fields[$section][$field];
                } else {
                    $profile[$to][$field] = $fields[$section][$field];
                }
            }
        }
    }
    foreach (cw_user_address_array($fields['address']) as $address_id => $address) {
        $profile['addresses'][$address_id] = cw_user_process_address($address);
    }
    if ($profile_fields['basic']['password']['is_avail']) {
        $profile['password2'] = $fields['basic']['password'];
    }
    foreach ($profile_fields as $section => $pf) {
        foreach ($pf as $field) {
            if (!empty($field['field_id']) && $field['type'] != 'D' && $field['name'] != 'address') {
                $profile['custom_fields'][$field['field_id']] = $fields[$section][$field['field_id']];
            }
        }
    }
    return $profile;
}
function cw_user_get_default_address()
{
    global $config;
    $address = array('country' => $config['General']['default_country'], 'state' => $config['General']['default_state'], 'zipcode' => $config['General']['default_zipcode'], 'city' => $config['General']['default_city']);
    return cw_user_process_address($address);
}
function cw_doc_prepare_user_information($user_info, $current_information = array())
{
    if (!is_array($user_info['additional_info'])) {
        $user_info['additional_info'] = array();
    }
    $current_information = array_merge($current_information, $user_info['additional_info']);
    $current_information['membership_id'] = $user_info['membership_id'];
    $current_information['email'] = $user_info['email'];
    if (!is_array($user_info['main_address'])) {
        $user_info['main_address'] = array();
    }
    if (!is_array($current_information['main_address'])) {
        $current_information['main_address'] = array();
    }
    if (!is_array($user_info['current_address'])) {
        $user_info['current_address'] = array();
    }
    if (!is_array($current_information['main_address'])) {
        $current_information['main_address'] = array();
    }
    unset($user_info['main_address']['address_id']);
    unset($user_info['current_address']['address_id']);
    $current_information['main_address'] = array_merge($current_information['main_address'], $user_info['main_address']);
    if ($user_info['current_address']['same_as_main']) {
        $current_information['current_address'] = $current_information['main_address'];
        $current_information['current_address']['same_as_main'] = 1;
    } else {
        $current_information['current_address'] = array_merge($current_information['current_address'], $user_info['current_address']);
        $current_information['current_address']['same_as_main'] = true;
        foreach ($current_information['main_address'] as $k => $v) {
            if ($v != $current_information['current_address'][$k]) {
                $current_information['current_address']['same_as_main'] = false;
                break;
            }
        }
    }
    $current_information['main_address']['customer_id'] = 0;
    $current_information['current_address']['customer_id'] = 0;
    $current_information['usertype'] = $user_info['usertype'];
    $current_information['main_address'] = cw_user_process_address($current_information['main_address']);
    $current_information['current_address'] = cw_user_process_address($current_information['current_address']);
    return cw_stripslashes($current_information);
}