function display_rs(rs_data) {
    var rs_str = '';
    var rs_str_top = '<span style="white-space: nowrap;">' + language_popular_categories + '&nbsp;</span>&nbsp;&nbsp;';
    var bottom_rs_str = '<div class="popular_cat" style="padding-top: 10px;"><span style="white-space:nowrap; color:#000000;">' + language_related_searches + '&nbsp;</span>&nbsp;';
    var top_rs_str = '';
    var bottom_rs_limit = 8;
    var total_rs = rs_data.length;
    var max_related_searches = (rs_data.length < 9) ? rs_data.length : 9;
    
    
    for (var i = 0; i < max_related_searches; i++) {
        rs_link = '<a class="rs" href="' + rs_data[i].href_url + '" title="' + rs_data[i].term + '">' +  rs_data[i].term + '</a>';
        rs_link_bottom = '<a class="popular_cat" href="' + rs_data[i].href_url + '" title="' + rs_data[i].term + '">' +  rs_data[i].term + '</a>';
        
        if (i == 0){
            rs_str += '<li class="rs" style="border-top: #cccccc 1px solid;">' + rs_link + '</li>'; 
        }
        else if (i == (max_related_searches)-1){
        
            if (!force_portal){
             rs_str += '<li class="rs" style="border-bottom: #cccccc 1px solid;">' + rs_link + '</li>';
            }
           
            else{
              rs_str += '<li class="rs" style="border-bottom: none;">' + rs_link + '</li>';
            }        
        }
        else{
            rs_str += '<li class="rs">' + rs_link + '</li>';
        }
        

        if (i < 3){
        rs_link_top = '<a class= "popular_cat_head" href="' + rs_data[i].href_url + '" title="' + rs_data[i].term + '">' +  rs_data[i].term + '</a>';
        
            if (i == 2){        
                rs_str_top += '<span style="white-space: nowrap;">' + rs_link_top + '</span>';
            }
            else{
                rs_str_top += '<span style="white-space: nowrap; border-right: #ffffff 1px solid;">' + rs_link_top + '&nbsp;&nbsp;</span>&nbsp;&nbsp;';
            }
        
        }
        
      if (i < bottom_rs_limit) {
            if(i < total_rs - 1 && i < bottom_rs_limit - 1 && i != 4) bottom_rs_space = "-";
            else bottom_rs_space = "";
            bottom_rs_str += '<span style="margin: 0 8px; white-space: nowrap;">' + rs_link_bottom + '</span>' + bottom_rs_space;
            if (i == 4 || i == 7)
                bottom_rs_str += '</div><div class="popular_cat" style="padding-top: 10px;">';
        }
    }
    
    document.getElementById('rs_container').innerHTML = '<div class="rs_top"><h2 class="rs">' + language_related_searches + '</h2></div><div class="rs"><ul class="rs">' + rs_str + '</ul></div>';
    document.getElementById('rs_container_portal').innerHTML = '<div class="rs_top"><h2 class="rs">' + language_related_searches + '</h2></div><div class="rs_portal"><ul class="rs">' + rs_str + '</ul></div>'; 
    document.getElementById('top_rs_container').innerHTML = rs_str_top;
    document.getElementById('bottom_rs_container').innerHTML = bottom_rs_str;
}

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

function display_portal(pq, pc_array) {
    var lpq = pq.replace(/&tk=[^&]*(?:&|$)/, '&');
    lpq = lpq.replace(/&$/, '');
    var pc_string = '';
    var cur_row = 1;
    var cur_col = 1;
    
    col_items = new Array(3, 3, 4);
    for(var i=0, items=0; i<col_items.length; items+=col_items[i++]);

    var start_cat = '<div style="width:194px;overflow:hidden;">\n';
    var end_cat = '</div>\n';
    
    // popular categories
    for (var i = 0; i < items; i++) {
        if (pc_array[i] && pc_array[i][0]) {
            var pc_href = lpq + '&aq=' + encodeURIComponent(pc_array[i][0]) + '&tk=' + token;
            var pc_link = '<a class="ads_hl_2click" href="' + pc_href + '">' + pc_array[i][0] + '</a>';
        } else {
            pc_link = '&nbsp;';
        }

        if (i > 0) {
            pc_string += '</td>';
            if (cur_col % col_items.length == 0) {
                pc_string += '</tr><tr>';
                cur_row++;
                cur_col = 0;
            }
            cur_col++;
            // add blank cells where necessary
            if (cur_row > col_items[cur_col - 1]) {
                while (cur_row > col_items[cur_col - 1]) {
                    pc_string += '<td>&nbsp;</td>';
                    cur_col++;
                }
            }    

            pc_string += '<td class="ads_2click">';
        }

        pc_string += start_cat + pc_link + end_cat;

        // add blank cells where necessary
        while (cur_row > col_items[cur_col] && cur_col < col_items.length) {
            pc_string += '</td><td>&nbsp;';
            cur_col++;
        }
    }

    // display
    document.getElementById("pc_container").innerHTML = '<table cellpadding="0" cellspacing="0" border="0"><tr><td class="ads_2click">'+pc_string+'</td></tr></table>';
    document.getElementById("portal").style.display = 'block';
    document.getElementById("ads").style.display = 'none';
    document.getElementById("search_box2_container").style.display = 'none';
}


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

    // display rs
    display_rs(rs_data);

    // ads
    if(!force_portal) {
        display_oneclick();        
    }
    //portal
    else {
        display_portal(pq, pc_array);
    }
}

// content display call
display_content();
