$top_message['content'] = cw_get_langvar_by_name("txt_survey_is_already_filling_ip");
            } elseif ($check_res == 3) {
                $top_message['content'] = cw_get_langvar_by_name("txt_survey_is_already_filling_customer_id");
            }
            cw_header_location("index.php?target=survey");
        }
        $smarty->assign('survey', $survey);
        $smarty->assign('survey_key', $survey_key);
    } else {
        $top_message = array("content" => cw_get_langvar_by_name("txt_survey_is_invalid_customer_warning"), "type" => "E");
        cw_header_location("index.php?target=survey");
    }
    $location[] = array($survey['survey']);
    $smarty->assign('main', "survey");
} elseif ($mode == 'view' && !empty($survey_id)) {
    $survey = cw_get_survey_results($survey_id);
    if (empty($survey) || !$survey['valid']) {
        $top_messsage = array('content' => cw_get_langvar_by_name('txt_survey_is_invalid_customer_warning'), 'type' => 'E');
        cw_header_location("index.php?target={$target}");
    }
    if (empty($filled_surveys) || !in_array($survey['survey_id'], $filled_surveys) || $survey['publish_results'] != 'Y' || $survey['count'] == 0) {
        cw_header_location('index.php?target=' . $target);
    }
    # Check count of available and unfilled surveys
    $avail_unfilled_surveys = cw_get_surveys_ids(true);
    if (!empty($avail_unfilled_surveys)) {
        $smarty->assign('avail_unfilled_surveys', true);
    }
    $smarty->assign('survey', $survey);
    $location[] = array(cw_get_langvar_by_name('lbl_survey_surveys'), 'index.php?target=' . $target);
    $location[] = array(cw_get_langvar_by_name('lbl_survey_results'));
} elseif ($action == 'filter' && ($js_tab == 'stats' || $js_tab == 'instances') && !empty($survey_id)) {
    # Update the survey filter
    if (!empty($StartMonth) && !empty($StartDay) && !empty($StartYear) && !empty($EndMonth) && !empty($EndDay) && !empty($EndYear)) {
        $filter_surveys[$survey_id] = array('date_from' => mktime(0, 0, 0, $StartMonth, $StartDay, $StartYear), 'date_to' => mktime(23, 59, 59, $EndMonth, $EndDay, $EndYear));
    }
    cw_header_location("index.php?target={$target}&survey_id={$survey_id}&js_tab={$js_tab}");
} elseif ($action == 'reset_filter' && ($js_tab == 'stats' || $js_tab == 'instances') && !empty($survey_id)) {
    # Reset the survey filter
    if (isset($filter_surveys[$survey_id])) {
        unset($filter_surveys[$survey_id]);
    }
    cw_header_location("index.php?target={$target}&survey_id={$survey_id}&js_tab={$js_tab}");
}
$surveys_count = cw_query_first_cell("SELECT COUNT(*) FROM {$tables['surveys']}");
$survey = cw_get_survey($survey_id, $edited_language);
$survey = cw_get_survey_results($survey_id, true, $filter_surveys[$survey_id]);
if (empty($survey)) {
    if ($mode == 'preview') {
        cw_close_window();
    }
    $top_message = array('type' => 'E', 'content' => cw_get_langvar_by_name('txt_survey_not_found'));
    cw_header_location("index.php?target={$targets}");
}
if ($js_tab == "maillist") {
    # Get maillist (with pagination)
    $total_items = cw_query_first_cell("SELECT COUNT(survey_id) FROM {$tables['survey_maillist']} WHERE survey_id = '{$survey_id}'");
    $navigation = cw_core_get_navigation($target, $total_items, $page);
    $navigation['script'] = "index.php?target={$target}&js_tab=maillist&survey_id=" . $survey_id;
    $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']}"));