﻿// Used by Popups/Login.ascx
function loginReloadPage(loginSuccess, loggedinas)
{
    if(loginSuccess)
    {
        if(window.overrideLoginReloadPage) // ful-lösning för labbet
            window.overrideLoginReloadPage(loginSuccess, loggedinas);
        else
            window.location.reload(true);
    }
    else
    {
        if(window.overrideLoginReloadPage)
        {
            window.overrideLoginReloadPage(false, null);
            
            if(modalPopup.flashIsDimmed)
                modalPopup.openFlashModal();
        }
    }
}

function showLoginModal()
{
    modalPopup.openModal('loginmodal', window, 'loginReloadPage');
    $('#emailMod').focus();
}

function login(link)
{
    link.style.cursor='wait';
    
     $.ajax({
           type: "POST",
           url: "/BoendeFb/Templates/Units/Popups/LoginAjax.aspx",
           dataType: "json",
           data: "action=login&email=" + document.getElementById('emailMod').value + "&password=" + document.getElementById('passwordMod').value,
           success: function(obj) {
             if(obj.success > 0)
                modalPopup.closeModal('loginmodal', true, obj.loggedinas);
             else
             {
                document.getElementById('passwordMod').value = '';
                modalPopup.switchModal('loginmodal', 'loginfailedmodal');
             }
             link.style.cursor='';
           }
 });
    
    
//    $.post("/BoendeFb/Templates/Units/Popups/LoginAjax.aspx",
//        {action: "login", email: document.getElementById('emailMod').value, password: document.getElementById('passwordMod').value}, 
//        function(obj) {   
//            alert(obj.success);                 
//            if(obj.success>0)
//            {
//                modalPopup.closeModal('loginmodal', true, obj.loggedinas);
//            }
//            else
//            {
//                document.getElementById('passwordMod').value = '';
//                modalPopup.switchModal('loginmodal', 'loginfailedmodal');
//            }                                           
//            link.style.cursor='';
//        }
//    );
    
    /*$.getJSON("/BoendeFb/Templates/Units/Popups/LoginAjax.aspx",
        "a=login&email=" + encodeURIComponent(document.getElementById('emailMod').value) + "&password=" + encodeURIComponent(document.getElementById('passwordMod').value), 
        function(obj) {                    
            if(obj.success>0)
            {
                modalPopup.closeModal('loginmodal', true, obj.loggedinas);
            }
            else
            {
                document.getElementById('passwordMod').value = '';
                modalPopup.switchModal('loginmodal', 'loginfailedmodal');
            }                                           
            link.style.cursor='';
        }
    );*/       
}

function forgotPassword(link)
{
    link.style.cursor='wait';
    $.getJSON("/BoendeFb/Templates/Units/Popups/LoginAjax.aspx",
        "action=forgotpassword&email=" + encodeURIComponent(document.getElementById('emailRequest').value), 
        function(obj) {                    
            if(obj.success>0)
                modalPopup.switchModal('forgotpassmodal', 'forgotpasssentmodal');
            else if(obj.success==0)                    
                alert('Hittades ej');                                           
            else
                alert('Error');                                           
            link.style.cursor='';
        }
    );            
}


