function showAddAnswerPost(postId, level) {
    var place = $("<tr><td>&nbsp;</td><td style=\"padding-left: " + (50 * (level + 1)) + "px\" id=\"addAnswerPost\">Подождите минутку...</td></tr>");
    $("#post" + postId).after(place);
    $.ajax({
        type: "GET",
        url: "/addAnswerPost.action",
        success: function (addPostContent) {
            $("#addAnswerPost").html(addPostContent);
            $("#addPostCancel").bind("click", function () {
                place.remove();
            });
            $("#addPostSave").bind("click", function () {
                $("#addPostCancel").remove();
                $("#addPostSave").attr("disabled", "disabled");
                $("#addPostSave").val("Минутку...");
                $.ajax({
                    dataType: "json",
                    type: "POST",
                    url: "/addAnswerPost.action",
                    data: {
                        execute: true,
                        text: $("#addPostText").val(),
                        postId: postId
                    },
                    cache: false,
                    success: function (post) {
                        if ($(".parentPost" + postId + ":last").length == 0) {
                            $("#post" + postId).after(
                                    "<tr><td>" + post.created + "</td><td style=\"padding-left: "
                                            + (50 * (level + 1)) + "px\">" + $("#addPostText").val() + "</td></tr>");
                        } else {
                            $(".parentPost" + postId + ":last").after(
                                    "<tr><td>" + post.created + "</td><td style=\"padding-left: "
                                            + (50 * (level + 1)) + "px\">" + $("#addPostText").val() + "</td></tr>");
                        }
                        place.remove();
                    }
                });
            });
        }
    });
}

// ---------------------------------------------------------------------------------------------------------------------

function subscribe() {
    $("#subscriptionEmail").attr("disabled", "disabled");
    $("#subscriptionButton").attr("disabled", "disabled");
    $("#subscriptionButton").val("Подождите...");
    $.ajax({
        cache: false,
        type: "post",
        data: {email: $("#subscriptionEmail").val()},
        dataType: "json",
        url: "/subscribe.action",
        success: function (ok) {
            if (ok) {
                $("#subscriptionBlock").html("Спасибо.");
            } else {
                $("#subscriptionButton").val("Неверный адрес, еще раз");
                $("#subscriptionEmail").attr("disabled", "");
                $("#subscriptionButton").attr("disabled", "");
            }
        }
    });
}

// ---------------------------------------------------------------------------------------------------------------------

function removePost(id, ask) {
    if (confirm("Вы действительно хотите удалить пост?")) {
        $("#post" + id + "Operation").attr("oldHtml", $("#post" + id + "Operation").html());
        $("#post" + id + "Operation").html("Удаляю...");
        $.ajax({
            type: "GET",
            url: "/removePost.action",
            data: {id: id},
            cache: false,
            success: function (ok) {
                if (ok) {
                    if (ask) {
                        window.location = $("#goToTopic").attr("href");
                    } else {
                        $("#post" + id).remove();
                    }
                } else {
                    $("#post" + id + "Operation").html($("#post" + id + "Operation").attr("oldHtml"));
                }
            }
        });
    }
}

// ---------------------------------------------------------------------------------------------------------------------

function editPost(id) {
    var postText = $("#post" + id + "Text");
    postText.attr("oldHtml", postText.html());
    var postOperation = $("#post" + id + "Operation");
    postOperation.attr("oldHtml", postOperation.html());
    postOperation.hide();
    $.ajax({
        type: "get",
        url: "/viewEditPost.action",
        data: {id: id},
        cache: false,
        success: function (html) {
            postText.html(html);
            $("#editPost" + id + "Cancel").bind("click", function () {
                postText.html(postText.attr("oldHtml"));
                postOperation.show();
            });
            var editPostSave = $("#editPost" + id + "Save");
            editPostSave.bind("click", function () {
                var editPostText = $("#editPost" + id + "Text");
                editPostText.attr("disabled", "disabled");
                $("#editPost" + id + "Cancel").remove();
                editPostSave.attr("disabled", "disabled");
                editPostSave.val("Минутку...");
                $.ajax({
                    type: "post",
                    url: "/editPost.action",
                    dataType: "json",
                    cache: false,
                    data: {
                        id: id,
                        text: editPostText.val()
                    },
                    success: function (ok) {
                        if (ok) {
                            postText.html(editPostText.val());
                        } else {
                            postText.html(postText.attr("oldHtml"));
                        }
                        postOperation.show();
                    }
                });
            });
        }
    });
}

// ---------------------------------------------------------------------------------------------------------------------

function removeGroup(groupId, language) {
    if (confirm("Вы действительно хотите удалить категорию?")) {
        $.ajax({
            type: "GET",
            url: "/removeGroup.action",
            data: {language: language, groupId: groupId},
            cache: false,
            success: function () {
                window.location = "/pages/store/" + language;
            }
        });
    }
}

// ---------------------------------------------------------------------------------------------------------------------

function removeProduct(productIndex, productId) {
    var productName = $("#product" + productIndex + "Name").html();
    if (!confirm("Вы действительно хотите удалить " + productName + "?")) {
        return;
    }

    $.ajax({
        type: "GET",
        url: "/removeProduct.action",
        data: {productId: productId},
        success: function (ok) {
            if (ok) {
                $("#product" + productIndex).remove();
            }
        }
    });
}

// ---------------------------------------------------------------------------------------------------------------------

function insertCode() {
    var text = $("#addPostText");
    text.val(text.val() + "<code></code>");
}

// ---------------------------------------------------------------------------------------------------------------------

function selectShowGroup() {
    var selectShowGroup = document.getElementById("selectShowGroup");
    var selectShowGroupId = -1;
    if (selectShowGroup.selectedIndex > -1) {
        selectShowGroupId = selectShowGroup.options[selectShowGroup.selectedIndex].value;
    }
    var i = 0;
    var currentProductGroupId = document.getElementById("product" + i + "GroupId");
    while (currentProductGroupId) {
        if (selectShowGroupId < 0 || currentProductGroupId.value == selectShowGroupId) {
            document.getElementById("product" + i).style.display = "block";
        } else {
            document.getElementById("product" + i).style.display = "none";
        }
        i++;
        currentProductGroupId = document.getElementById("product" + i + "GroupId");
    }
}

// ---------------------------------------------------------------------------------------------------------------------

function onLoadViewAccounts() {
    $("#accounts tr:odd").attr("class", "lightLine");
    $("#accounts tr:even").attr("class", "generalLine");
}

// ---------------------------------------------------------------------------------------------------------------------

function focusOnLoginEmail() {
    var loginEmail = $("#loginEmail");
    if (!loginEmail.attr("alredyFocused")) {
        $("#loginEmail").val("");
        $("#loginEmail").attr("alredyFocused", true);
    }
}

// ---------------------------------------------------------------------------------------------------------------------

function showAddAskPost(topicId) {
    // if add ask post textarea alredy open
    if (document.getElementById("addPostText")) {
        $("#addPostText").get(0).focus();
        return;
    }

    var place = document.createElement("div");
    $("#beforeAddAskPost").after(place);
    $(place).html("Подождите минутку...");
    $.ajax({
        type: "GET",
        url: "/addAskPost.action",
        success: function (addPostContent) {
            $(place).html(addPostContent);
            $("#addPostText").get(0).focus();
            $("#addPostCancel").bind("click", function () {
                $(place).remove();
            });
            $("#addPostSave").bind("click", function () {
                if ($("#addPostText").val().length == 0) {
                    $(place).remove();
                } else {
                    $("#addPostSave").attr("disabled", "disabled");
                    $("#addPostCancel").remove();
                    $("#addPostSave").val("Минутку...");
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: "/addAskPost.action",
                        data: {
                            execute: true,
                            text: $("#addPostText").val(),
                            topicId: topicId
                        },
                        cache: false,
                        success: function (post) {
                            var postRow = $("#posts").get(0).insertRow(1);
                            var postTextCell = postRow.insertCell(0);
                            postTextCell.innerHTML = "<a href=\"/pages/askPost/ru/"
                                    + post.url + "\">" + $("#addPostText").val() + "</a>";
                            var postDetailAnswerCount = postRow.insertCell(1);
                            postDetailAnswerCount.innerHTML = "0";
                            postDetailAnswerCount.align = "center";
                            postRow.insertCell(2).innerHTML = post.created;
                            postRow.insertCell(3).innerHTML = "";
                            $(place).remove();
                        }
                    });
                }
            });
        }
    });
}