
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open('GET', 'https://www.norma.uz/scripts/sso?w=al', true);
xhr.onload = function() {
    var data = JSON.parse(this.responseText);

    function getCookie (name) {
        let value = '; '+document.cookie;
        let parts = value.split('; '+name+'=');
        if (parts.length === 2) return parts.pop().split(';').shift();
    }
    if (data.result == 'ok') {
        if (data.url) {
            location.href = data.url+'&returnUrl='+encodeURIComponent(location.href);
        } else if (data.name) {
            showPopover = getCookie('sso_al_close') != 1;
            if (showPopover) {
            var popover = document.createElement('div');
            popover.id = 'sso_al_popover';
            popover.className = 'popover';
            popover.innerHTML = '<h3 class="popover-title"><a href="#" class="sso-popover-closebtn" ' +
            'onclick = "document.getElementById(\'sso_al_popover\').style.display=\'none\';document.cookie = \'sso_al_close=1; path=/\';">×</a> Войти как ...</h3>' +
            '<div class="popover-content"> <div  class="sso-popover-user"><img src="/files/sso/user.png"><span><b>'+
                data.name +'</b><br>'+data.email+
                '</span></div>'+
                '<p style="text-align: center;margin-top: 10px;"><button id="ssoLogin" class="btn btn-primary">Войти</button></p><p class="sso-popover-footer"><a href="https://www.norma.uz/scripts/sso?logout='+
                encodeURIComponent(location.href) 
                +'">'+
            '    Перестать узнавать меня' +
            '</a></p> </div>';
            var style = document.createElement('style');
            style.innerHTML = '#sso_al_popover { '+
                'display: block; '+
                'right: 10px; '+
                'top: 10px; '+
                'left: auto; '+
                'z-index: 99999; '+
                'width: 250px; } ' +
                '.sso-popover-footer { '+
                    'text-align: center; '+
                    'padding: 10px 0 0 0; '+
                    ' } '+
                    '.sso-popover-closebtn { ' +
                        ' float: right;' +
                        ' font-size: 20px; '+
                        ' line-height: 16px; ' +
                        ' color: #000;' +
                        ' text-decoration: none !important; '+
                        '} ' +
                        '.sso-popover-user { ' +
                            '    display: flex;'+
                            'align-items: center; '+
                            'width: 100%; '+
                           //' border-radius: 3px; '+
                           ' padding: 2px 5px; '+
                            //' border: 1px solid #ccc; '+
                       ' } ' +
                            '.sso-popover-user img { '+
                              '  width: 32px; '+
                               ' height: 32px; '+
                              '  margin-right: 10px; } '+
                              '.sso-popover-user span { '+
                                ' flex-grow: 1; '+
                                ' text-align: left; '+
                                ' } '
                            ;

                    document.body.appendChild(style);
                    document.body.appendChild(popover);
                    document.getElementById('ssoLogin').addEventListener('click', function (event) {

                        location.href=data.authUrl+'&returnUrl='+encodeURIComponent(location.href);
                        event.preventDefault();

                        }, false);
            }
        }
    }
}

/*xhr.onerror = function() {
  alert( 'Ошибка ' + this.status );
}*/

xhr.send();

function ssoShowPopup(w, h) {
  if (!w) w = 400;
  if (!h) h = 500;
  var left = (screen.width / 2) - (w / 2);
  var top = (screen.height / 2) - (h / 2); //https://www.norma.uz/auth/login
  var w = window.open('', 'normauz_sso', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
  w.focus();
}

window.addEventListener("message", function(event) {
    if (event.data.m == 'sso_reload' && event.data.url) {
        if (event.origin != 'https://www.norma.uz') return;
        location.href = event.data.url;
    }
}, false);