﻿function OnChangeMagazineYear(selectObj, idLanguage) {
    $.ajax({
        type: "GET",
        url: "/_modules/magazine/get_numbers.ashx?year=" + selectObj.options[selectObj.selectedIndex].value + "&language=" + idLanguage + "&u=" + Date.parse(new Date()),
        dataType: "json",
        success: function(data) {
            var numberSelect = $("#NumberSelect").get(0);
            numberSelect.enabled = false;
            numberSelect.options.length = 0;

            for (var i = 0; i < data.length; i++) {
                numberOption = new Option();
                numberOption.value = data[i].id_magazine;
                numberOption.text = data[i].name + " " + data[i].number;
                numberSelect.options.add(numberOption);
            }
            numberSelect.enabled = true;
        },
        error: function() {
            alert("Произошла ошибка на вызываемой странице.\nПодгрузка данных не может быть осуществлена.");
        }
    });
}


function window_open(title, href, target, width, height) {
    width = parseInt(width, 10);
    height = parseInt(height, 10);
    var wind_img = window.open('', target, 'width=' + (height > 800 ? width + 16 : width) + ',height=' + (height > 800 ? 800 : height) + (height > 800 ? ',scrollbars=yes' : '') + ',resizable=yes');
    wind_img.focus();
    wind_img.document.open();
    wind_img.document.write('<html><head><META http-equiv="Content-Type" content="text/html; charset=windows-1251">' + (title ? '<title>' + title + '</title>' : '<title>Большое изображение</title>') + '</head>');
    wind_img.document.write('<body leftmargin="0" rightmargin="0" topmargin="0" bottommargin="0" marginwidth="0" marginheight="0"><img src="' + href + '" border="0" width="' + width + '" height="' + height + '" title="' + title + '" alt="' + title + '">');
    wind_img.document.write('</body></html>');
    wind_img.document.close();
    return false;
}

var PrintView = {

    _printIndex: -1,
    _printLink: null,
    enabled: false,

    enable: function() {
        if (this.enabled) return;
        if (this._printLink == null) {
            this._printLink = document.createElement("link");
            this._printLink.setAttribute("rel", "stylesheet");
            this._printLink.setAttribute("href", "/_css/print.css");
            document.body.appendChild(this._printLink);
            this._printIndex = document.styleSheets.length - 1;
        }
        this.enabled = true;
        for (var i = 0; i < document.styleSheets.length; ++i) {
            if (i == this._printIndex)
                document.styleSheets[i].disabled = false;
            else
                document.styleSheets[i].disabled = true;
        }
    }
  ,
    disable: function() {
        if (this._printLink == null || !this.enabled) return;
        this.enabled = false;
        for (var i = 0; i < document.styleSheets.length; ++i) {
            if (i == this._printIndex)
                document.styleSheets[i].disabled = true;
            else
                document.styleSheets[i].disabled = false;
        }
    }
  ,
    autoPrint: function() {
        if (document.location.search.indexOf("?printv") >= 0 || document.location.search.indexOf("&printv") >= 0) {
            this.run();
        }
    }
  ,
    run: function() {
        this.enable();
        window.print();
    }
  ,
    openWindow: function() {
        var url = document.location.href;
        if (url.indexOf('?') >= 0) url += "&printv";
        else url += "?printv";
        var pwin = window.open(url, 'printWindow');
    }
}

jQuery(function() { PrintView.autoPrint(); });

function CalculatorFieldActive(obj) {
    var ch_list = (obj) ? $(obj) : $("input[name='cf_checked']]");
    for (var i = 0; i < ch_list.length; i++) {
        var ch_obj = ch_list[i];
        var tr_obj = ch_obj.parentNode.parentNode;
        tr_obj.className = (ch_obj.checked) ? "cf_enabled" : "cf_disabled";
    }
    UpdateSummary();
}

function CountChange(id) {

    //получаем количество
    var input_obj = document.CalculatorForm["cf" + id + "_value"];
    var count = parseInt(input_obj.value, 10);
    if ((count <= 0) || (isNaN(count))) count = 1;

    input_obj.value = count;

    //обновляем строку

    var ch_obj = $("input[name='cf_checked'][value='" + id + "']").get(0);
    ch_obj.checked = true;
    CalculatorFieldActive(ch_obj);

    //получаем цену
    var input_price_obj = document.CalculatorForm["cf" + id + "_price"];
    var price = parseInt(input_price_obj.value, 10);
    if (price <= 0) price = 1;

    //обновляем строку
    $("span#cf" + id + "_cost").text((isNaN(count) || isNaN(price)) ? "-" : count * price);

    UpdateSummary();
}

//обновляем итог
function UpdateSummary() {
    var span_list = $("span[id$='_cost']");
    var summary = 0;
    for (var i = 0; i < span_list.length; i++) {
        var span_obj = span_list[i];
        var tr_obj = span_obj.parentNode.parentNode;
        if (tr_obj.className != "cf_disabled") summary += parseInt(span_obj.innerHTML);
    }
    $("span#total").text(isNaN(summary) ? "-" : summary);
}

$(function() {
    UpdateSummary();
    //if (!CheckSelectedChGroup('ch_fmi_active_') {}
});



function change_password_input_visibility(id, changePasswordStr, cancelStr) {
    var password_input = $("#" + id).get(0);
    var tr_password_copy = $("#tr_password_copy_" + id).get(0);
    var change_password_link = $("#link_change_password_" + id).get(0);

    var display = "";

    if (change_password_link.innerHTML == changePasswordStr) {
        change_password_link.innerHTML = cancelStr;
    }
    else {
        display = "none"
        change_password_link.innerHTML = changePasswordStr;
    }

    password_input.style.display = display;
    tr_password_copy.style.display = display;
}

function change_post_edit_mode(id, edit) {
    if (edit) {
        $("[id^='post_" + id + "']").hide();
        $("[id^='edit_post_" + id + "']").show();
    }
    else {
        $("[id^='post_" + id + "']").show();
        $("[id^='edit_post_" + id + "']").hide();
    }
}

function edit_forum_post(id) {
    change_post_edit_mode(id, true);
}

function cancel_edit_forum_post(id) {
    change_post_edit_mode(id, false);
}

function delete_forum_post(id, confirm_message) {
    var answer = confirm(confirm_message)
    if (answer) {
        var post_form = get_forum_post_form(id);
        post_form["form_action"].value = "delete";
        post_form.submit();
    }
}

function save_forum_post(id) {
    submit_forum_post_form(id);
}

function submit_forum_post_form(id) {
    var post_form = get_forum_post_form(id);
    post_form.submit();
}

function get_forum_post_form(id) {
    return $("#forum_post_form_" + id).get(0);
}



function change_topic_edit_mode(id, edit) {
    if (edit) {
        $("[id^='topic_" + id + "']").hide();
        $("[id^='edit_topic_" + id + "']").show();
    }
    else {
        $("[id^='topic_" + id + "']").show();
        $("[id^='edit_topic_" + id + "']").hide();
    }
}

function edit_forum_topic(id) {
    change_topic_edit_mode(id, true);
}

function cancel_edit_forum_topic(id) {
    change_topic_edit_mode(id, false);
}

function delete_forum_topic(id, confirm_message) {
    var answer = confirm(confirm_message)
    if (answer) {
        var topic_form = get_forum_topic_form(id);
        topic_form["form_action"].value = "delete";
        topic_form.submit();
    }
}

function save_forum_topic(id) {
    submit_forum_topic_form(id);
}

function submit_forum_topic_form(id) {
    var topic_form = get_forum_topic_form(id);
    topic_form.submit();
}

function get_forum_topic_form(id) {
    return $("#forum_topic_form_" + id).get(0);
}


function reply_to_post(formName, idPost, title) {
    var form = document.forms[formName];
    form["id_parent"].value = idPost;
    form["name"].value = "RE: " + title;
}



function quote_post(formName, idPost, username, txt) {
    var form = document.forms[formName];
    //form["id_parent"] = idPost;
    form["txt"].innerHTML += username + ": " + txt;
}