﻿// 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: "/Housing/Templates/Pages/Utilities/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);
                if (typeof (mypages) != 'undefined') {
                    location.href = mypages;
                }
            }
            else if (obj.success == -3) {
                document.getElementById('passwordMod').value = '';                
                $('#verifyEmail').text(document.getElementById('emailMod').value);
                modalPopup.switchModal('loginmodal', 'loginverifymodal');
            } else {
                document.getElementById('passwordMod').value = '';
                modalPopup.switchModal('loginmodal', 'loginfailedmodal');
            }
            link.style.cursor = '';
        },
        error: function(obj, err) {
            alert("error: " + err);
        }

    });
    
    
//    $.post("/Housing/Templates/Pages/Utilities/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("/Housing/Templates/Pages/Utilities/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("/Housing/Templates/Pages/Utilities/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='';
        }
    );
    }

function verify(link) {

    link.style.cursor = 'wait';
    $.ajax({
        type: "POST",
        url: "/Housing/Templates/Pages/Utilities/LoginAjax.aspx",
        dataType: "json",
        data: "action=verify&email=" + document.getElementById('emailMod').value + "&password=" + document.getElementById('passwordMod').value,
        success: function (obj) {
            alert(obj.success);
            if (obj.success == 0)
                modalPopup.switchModal('loginverifymodal', 'loginverifysentmodal');
            else if (obj.success == -1)
                alert('Det har redan skickats ett valideringsmail till denna adress.');
            else
                alert('Ett fel har inträffat. Valideringsmailet kunde inte skickas.');
            link.style.cursor = '';
        },
        error: function (obj, err) {
            alert("error: " + err);
        }
    });
}



