﻿//Включаем после загрузки
function pageLoad(sender, args) {
    $addHandler(document, "keydown", OnKeyPress);  // добавили обработчик Escape
    //Sys.WebForms.PageRequestManager.getInstance().add_endRequest(ShowAfterAlert);  // функция вызывается после асинхронного вызова 
    // если видима панель сообщений с определенным стилем, включаем анимацию
    $("div.fullscreen").each(function(index) {
        if (alertWindow && !alertWindow.closed)  { alertWindow.close(); }
        var alertFeatures = '';  // 'height=130, width=350, left=337, top=319, screenx=337, screeny=319, location=no, menubar=no, scrollbars=no, status=no, toolbar=no';
        var alertHtml='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >';
        alertHtml += '<head><title>Сообщение сайта</title><link href="Styles/Site.css" rel="stylesheet" type="text/css" /><link rel="shortcut icon" href="Styles/site.ico" type="image/x-icon"/></head><body style="background-color: #FFFFFF; ">';
        alertHtml += this.innerHTML;
        alertHtml +='</body></html>';
        var alertWindow = window.open('', 'alertWindow', alertFeatures, true);
        alertWindow.document.write(alertHtml);
        alertWindow.document.close();
        alertWindow.focus();
    });
    if ($("div.attention") != null) {
        window.setTimeout('$("div.attention").hide()', 4000);
    };
    if ($("div.message") != null) {
        window.setTimeout('$("div.message").hide()', 3000);
    };
    if ($("#editmodalloadprogress") != null) { window.setTimeout('$("#editmodalloadprogress").hide()', 10); };
   
    // Сообщение об ошибке, если запрос на сервер неудачен ------------------ действует только для запросов к веб-службам
    $("#Warning").ajaxError(function (event, request, settings) {
        $("#Warning").css({ "border-color": "#CC9900",
            "background-color": "#FFFF99",
            "color": "#996600"
        });
        $(this).append("<img alt='Внимание' class='icon' src='Styles/attention.png' /> Произошла ошибка при запросе страницы " + settings.url + "<br>");
        window.setTimeout('$("#Warning").hide()', 3000);
        $(this).fadeIn();
    });

//    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(onEndRequest);
}
//По кнопке Escape закрываем всплывающие окна
function OnKeyPress(args) {
 if (args.keyCode == Sys.UI.Key.esc) {
     if ($find("Warning") != null) { $find("Warning").hide(); };
     if ($find("backgroundPopup") != null) { $find("backgroundPopup").hide(); };
     if ($find("LoginModalPopupExtender") != null) { $find("LoginModalPopupExtender").hide(); };
     if ($find("EditModalPopupExtender") != null) { $find("EditModalPopupExtender").hide(); };
     if ($find("PreorderModalPopupExtender") != null) { $find("PreorderModalPopupExtender").hide(); };
     if ($find("ShowItemModalPopupExtender") != null) { $find("ShowItemModalPopupExtender").hide(); };
 }
}
// сообщение со страницы
function showPopup(message) {
    if ($find("Warning") != null) {
        $find("Warning").html("<img alt='Сообщение' class='icon' src='Styles/message.png' />" + message);
        window.setTimeout('$find("Warning").hide()', 3000);
    }
}
// Отправка сообщения с паролем на электронную почту
function SendPassToMail() {
    $.post("WebService.asmx/SendPassToMail",
           { login: document.getElementById("LoginTextBox").value },
           function (data) {
               var message = data.lastChild.nodeTypedValue.split("@"); // вначале нужно расшифровать xml: data.lastChild.nodeTypedValue
               showPopup(message[0]);
               return;
           }
     );
}

//// Функция для принудительного асинхронного вызова
//function doPostBackAsync(eventName, eventArgs) {
//    var prm = Sys.WebForms.PageRequestManager.getInstance();
//    if (!Array.contains(prm._asyncPostBackControlIDs, eventName)) {
//        prm._asyncPostBackControlIDs.push(eventName);
//    }
//    if (!Array.contains(prm._asyncPostBackControlClientIDs, eventName)) {
//        prm._asyncPostBackControlClientIDs.push(eventName);
//    }
//    __doPostBack(eventName, eventArgs);
//}
//// сообщение с сервера  - убираем из-за непрерывных вызовов при асинхронных вызовах
//function ShowAfterAlert(sender, args) {
//    // Выводим или прячем окно сообщений при вызове ajax страницы
//    $.post("WebService.asmx/ShowMessage",
//          { clientid: document.getElementById("clientTextBox").value },
//                function (data) {
//                    // вначале нужно расшифровать xml: data.lastChild.nodeTypedValue
//                    var apos = String.fromCharCode(34);
//                    var message = data.lastChild.nodeTypedValue.split("@");
//                    if (message[2] == "refresh") {
//                        window.location = window.location;
//                        return;
//                    };
//                    switch (message[1]) {
//                        case "fullscreen":
//                            $("#Warning").html(message[0]);
//                            $("#Warning").css({ "top": 0,
//                                "left": 0,
//                                "width": document.documentElement.clientWidth - 20,
//                                "height": document.documentElement.clientHeight * 5,
//                                "background-color": "#FFFFFF",
//                                "border-style": "none"
//                            });
//                            $("body").css({ "background-color": "#FFFFFF" });
//                            $("#Warning").fadeIn();
//                            break;
//                        case "popup":
//                            $("#Warning").html("<a onclick='$(" + apos + "#Warning" + apos + ").hide(); $(" + apos + "#backgroundPopup" + apos + ").hide();'><img alt='Закрыть' src='Styles/delete.png' /></a><img alt='Внимание!' class='icon' src='Styles/attentionhigh.png' />" + message[0] + "");
//                            $("#backgroundPopup").css({ "opacity": "0.7" });
//                            $("#Warning").css({ "position": "absolute",
//                                "border-color": "#FF0000",
//                                "background-color": "#FFCCCC",
//                                "color": "#790000"
//                            });
//                            $("#backgroundPopup").fadeIn();
//                            $("#Warning").fadeIn();
//                            break;
//                        case "attention":
//                            $("#Warning").html("<img alt='Внимание' class='icon' src='Styles/attention.png' />" + message[0]);
//                            $("#Warning").css({ "border-color": "#CC9900",
//                                "background-color": "#FFFF99",
//                                "color": "#996600"
//                            });
//                            window.setTimeout('$("#Warning").hide()', 4000);
//                            $("#Warning").fadeIn();
//                            break;
//                        case "message":
//                            $("#Warning").html("<img alt='Сообщение' class='icon' src='Styles/message.png' />" + message[0]);
//                            window.setTimeout('$("#Warning").hide()', 3000);
//                            $("#Warning").fadeIn();
//                            break;
//                        default:
//                            $("#Warning").css({ "visibility": "hidden"
//                            });
//                            break;
//                    }
//                    return;
//                });
//             }
////


