function addOnclickToDatatableRows()
{
    if (document.getElementById && document.createTextNode) {
        var tables = document.getElementsByTagName('table');
        for (var i = 0; i < tables.length; i++)
        {
            if (tables[i].id.indexOf("ClickableTable") > 0)
            {
                var tableId = tables[i].id;
                var trs = document.getElementById(tableId).getElementsByTagName('tbody')[0]
                        .getElementsByTagName('tr');
                for (var j = 0; j < trs.length; j++) {
                    trs[j].onclick = new Function("clickRow('" + tableId + "', this)");
                }
            }
        }
    }
}

function clickRow(tableId, tr)
{
    var rowIndex = tr.rowIndex - 1;
    link = tr.getElementsByTagName('a')[0];
    if (!link.onclick || (link.onclick()!=false))
        location.href=link.href;
    //document.getElementById(tableId + ':' + rowIndex + ':rowClickLink').onclick();
}

$(document).ready(function()
{
    addOnclickToDatatableRows();
    $("input[type=submit]").addClass("btn");
    $("input[id*=buttonEssaysSearch]").click(function() {
        $("#loading").css("display", "block");
    });
    $("#rightPanel").css("height", $("#content").height());
    $("#loading").css("display", "none");
});

/*
 Requests browser to add specified or current (if no specified one) page to bookmarks.
 Displays error message if bookmark could not be added (browser not supported)
 */
function addBookmark(url, title)
{
    if (!url) url = location.href;
    if (!title) title = document.title;

    // Gecko (Firefox, Netscape, Konqueror, ...)
    if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) {
        window.sidebar.addPanel(title, url, "");
    }
    // IE4+
    else if (typeof window.external == "object") {
        window.external.AddFavorite(url, title);
    }
    // Opera7+
    else if (window.opera && document.createElement && document.createElement('A')) {
        var a = document.createElement('A');
        a.setAttribute('rel', 'sidebar');
        a.setAttribute('href', url);
        a.setAttribute('title', title);
        a.click();
    }
    // browser not supported
    else {
        alert("Извините, Ваш браузер не поддерживает автоматическое добавление закладок. Пожалуйста, для добавления закладки обратитесь к документации Вашего браузера.");
    }
}

function buildFaqQuestionsList()
{
    var questionsDiv = document.getElementById("questions");

    var _divs = $(".entry"); // jquery selector - selects all elements with class "entry"
    for (var i = 0; i < _divs.length; i++) {
        var _entryDiv = _divs[i];
        var _bookmark = _entryDiv.getElementsByTagName("a")[0];
        var name = _bookmark.getAttribute("name");
        var _questionSpan = _entryDiv.getElementsByTagName("span")[0];
        var question = _questionSpan.firstChild.data;

        var link = document.createElement("a");
        link.setAttribute("href", "#" + name);
        var linkText = document.createTextNode(question);
        link.appendChild(linkText)
        var newline = document.createElement("br");
        questionsDiv.appendChild(link);
        questionsDiv.appendChild(newline);
    }
}
