function display_rs(rs_data) {
    top_rs_limit = 5;
    bottom_rs_limit = 5;

    top_rs_count = (rs_data.length > top_rs_limit) ? top_rs_limit : rs_data.length;
    bottom_rs_count = (rs_data.length > top_rs_count + bottom_rs_limit) ? bottom_rs_limit : rs_data.length - top_rs_count;

    top_width = Math.floor(100 / top_rs_count);
    bottom_width = Math.floor(100 / (bottom_rs_count + 1));
    border_right = 'box-border ';

    var top_rs_str = '<table cellpadding="0" cellspacing="0" border="0" width="100%"><tr>';
    var bottom_rs_str = '<table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td class="popular-categories-text" width="' + bottom_width + '%">' + language_related_searches + ':</td>';

    for (var i = 0; i < rs_data.length; i++) {
        rs_link = '<a href="' + rs_data[i].href_url + '">' +  rs_data[i].term + '</a>';

        if (i < top_rs_count) {
            top_rs_str += '<td width="' + top_width + '%" class="';
            if (i < top_rs_count - 1)
                top_rs_str += border_right;
            top_rs_str += 'ac">' + rs_link + '</td>';
        }

        if (i >= top_rs_count && i < top_rs_count + bottom_rs_count) {
            bottom_rs_str += '<td width="' + bottom_width + '%" class="';
            if (i < top_rs_count + bottom_rs_count - 1)
                bottom_rs_str += border_right;
            bottom_rs_str +=  'ac">' + rs_link + '</td>';
        }
    }

    document.getElementById('top_rs_container').innerHTML = top_rs_str + '</tr></table>';
    document.getElementById('bottom_rs_container').innerHTML += bottom_rs_str + '</tr></table>';
}

function set_sb_tokens() {
    if(token) {
        document.forms.frm_search.ltk.value = token;
    }
    if(search_token) {
        document.forms.frm_search.stk.value = search_token;
    }
}

function display_portal(rs_data) {
    var lpq = pq.replace(/&tk=[^&]*(?:&|$)/, '&');
    lpq = lpq.replace(/&$/, '');

    total_rs = (rs_data.length > 24) ? 24 : rs_data.length;
    rs_per_col = Math.floor(total_rs / 4);

    total_additional_rs = total_rs % 4;
    next_rs_cnt = rs_per_col + ((total_additional_rs > 0) ? 1 : 0);

    rs_string = '';
    for (var i = 0; i < total_rs; i++) {
        if (i == next_rs_cnt || i == 0) {
            // close last opened rs box
            if (i > 0) {
                rs_string += '</div></div>\n';
                next_rs_cnt += rs_per_col + ((--total_additional_rs > 0) ? 1 : 0);
            }

            // add border if not last box
            if (i < total_rs - rs_per_col)
                border = ' box-border';
            else
                border = '';

            // open new rs box
            rs_string += '<div class="box' + border + '"><div class="related2">' + language_related_searches + '</div><div class="related-links">\n';
        }
        rs_string += '<a href="' + rs_data[i].href_url + '">' +  rs_data[i].term + '</a>';
    }
    if (i > 0)
        rs_string += '</div></div><div class="clearlr"></div>';

    // display
    document.getElementById("rs_container").innerHTML = rs_string;
    document.getElementById("portal").style.display = 'block';
    document.getElementById("ads").style.display = 'none';
}

function display_content() {
    // set tokens for SB
    set_sb_tokens();

    // display rs
    display_rs(rs_data);

    // ads
    if(!force_portal) {
        $('#left_column').html(left_column);
        $('#right_column').html(right_column);
        display_oneclick();
    }
    //portal
    else {
        $('#portal_row').html(portal_row);
        display_portal(rs_data);
    }
}

// content display call
display_content();
