﻿$j(document).ready(function () {
    $j(".wnLoginForm input").bind('keypress', function (e) {
        if (e.keyCode == 13) {
            doLogin(e);
        }
    });


    $j("#btnLogin").click(doLogin);
});

function doLogin(e) {

    if (Page_ClientValidate(validationGroup) == true) {
        var usr = $j('.txtLoginBox').val();
        var pwd = $j('.txtPasswordBox').val();

        PageMethods.Login(usr, pwd, '', login_done, login_error);
        $j('#loginFailed').hide();
        $j('#loginLoading').show();
        $j('#loginSucceeded').hide();
        $j('#loginpopup_generate').nyroModalManual({
            minWidth: 400, // Minimum width
            minHeight: 300, // Minimum height
            width: 400,
            height: 300,
            modal: true
        });
    }
    e.preventDefault();
}

function login_done(result) {
    if (result.Succeeded) {

        if (result.Paid) {
            checkOtherPayment(result);
        }
        else {
            checkPayment(result.Paid);
        }
    }
    else {
        $j('#loginLoading').hide();
        if (result.Messages.length > 0) {

            $j('#loginSpecificError').html(result.Messages);
            $j('#loginSpecificError').show();
            $j('#loginGenericError').hide();
        }
        else {
            $j('#loginGenericError').show();
            $j('#loginSpecificError').hide();
        }

        $j('#loginFailed').show();
        $j('#loginSucceeded').hide();
        $j(".closeLoginModal").click(function (e) {
            $j.nyroModalRemove();
            e.preventDefault();
        });

    }

}

function login_error(result) {
    $j('#loginFailed').show();
    $j('#loginSpecificError').hide();
    $j('#loginGenericError').show();


    $j('#loginLoading').hide();
    $j('#loginSucceeded').hide();

    //alert(result.get_message());
    //alert(result.get_exceptionType());
    //alert(result.get_stackTrace());


}

function checkProfileComplete() {
    PageMethods.CheckProfileCompleted(checkProfileComplete_done, checkProfileComplete_done);
}

function checkProfileComplete_done(result) {
    if (result.Succeeded) {
        hideAllPopups();
        // done, no more checks needed 
        $j('#loginSucceeded').show();
        redirectToProfileWithDelay();
    }
    else {
        // display popup
        hideAllPopups();
        var divId = 'loginProfileInComplete';
        $j('#' + divId).show();
        var url = $j('#' + divId + ' a').attr('href');
        //redirectWithDelay(url); 
    }

    var url = '';
}


function checkOtherPayment(result) {
    if (!result.ExtendPayment && !result.Reregistration) {
        checkReactions();
    }
    else {
        hideAllPopups();

        if (result.ExtendPayment)
            $j("#verlengenPopup").nyroModalManual({
                minWidth: 780,
                minHeight: 600,
                width: 780,
                height: 600,
                resizable: true,
                modal: true
            });

            else if (result.Reregistration)
            $j("#herinschrijvenPopup").nyroModalManual({
                minWidth: 525, // Minimum width
                minHeight: 600, // Minimum height
                width: 525,
                height: 600,
                modal: true
            });
        
        $j('#loginLoading').hide();
    }
}

function checkPayment(paid) {
    if (paid) {
        checkReactions();
    }
    else {
        hideAllPopups();
        
        // add personid to url, because logout has been called when payment is not completed
        url = $j("#loginPaymentPopup").attr("href");
        var usr = $j('.txtLoginBox').val();
        newurl = url + '?reg=' + usr;
        $j("#loginPaymentPopup").attr("href", newurl);
        $j("#loginPaymentPopup").nyroModalManual({
            minWidth: 400,
            minHeight: 300,
            autoSizable: true,
            resizable: true,
            title: '',
            url: newurl,
            closeButton: '<a href="#" style="right:25px" class="nyroModalClose" id="closeBut" title="' + closeWindowText + '">' + closeWindowText + '</a>'
        });
        $j('#loginLoading').hide();
    }
}

function checkReactions() {
    PageMethods.CheckReactions(checkReactions_done, checkReactions_done);
}

function checkReactions_done(result) {
    if (result.Succeeded) {
        checkProfileComplete();
    }
    else {
        hideAllPopups();

        var divId = 'loginReactions';
        $j('#' + divId).show();
        var url = $j('#' + divId + ' a').attr('href');
        redirectWithDelay(url);

    }
}

function redirectToProfileWithDelay() {
    setTimeout('redirectToProfile()', 4000);
}

function redirectToProfile() {
    document.location = profileLocation;
}

function redirectWithDelay(url) {
    setTimeout("redirect('" + url + "')", 4000);
}

function redirect(url) {
    document.location = url;
}


function hideAllPopups() {
    $j('#loginProfileInComplete').hide();
    $j('#loginFailed').hide();
    $j('#loginSpecificError').hide();
    $j('#loginGenericError').hide();
    $j('#loginReactions').hide();
    $j('#loginNoPayment').hide();

    $j('#loginLoading').hide();
    $j('#loginSucceeded').hide();
}
        
