﻿
function initInstantLogin() {
    with (Sys.Services.AuthenticationService) {
        set_defaultLoginCompletedCallback(onLoginCompleted);
    }
    loginLoadingIMG = document.getElementById("loginLoadingIMG");
    if (loginLoadingIMG != null)
        loginLoadingIMG.src = loginLoadingIMGURL;

//    $("#txtUserName,#txtPassword").blur(function() {
//        if (this.value == "") {
//            $(this).effect("highlight", { color: "red" });
//        }
//    });
}


function loginUser() {

    var username = $get('txtUserName');
    var password = $get('txtPassword');

    if (username.value == "") {
        $(username).effect("pulsate", { times: 2 });
    } else if (password.value == "") {
        $(password).effect("pulsate", { times: 2 });
    }

    if (username.value == "" || password.value == "") {
        return false;
    } 
    $("#loginBox").hide();
    $("#loginLoading").show();
//    $("#loginBox").fadeOut("Fast", function() { $("#loginLoading").show(); });

    Sys.Services.AuthenticationService.login(username.value,
                                    password.value,
                                    false,
                                    null,
                                    null,
                                    null,
                                    onLoginFailed,
                                    "User Context");

}

var loginReturnURL = null;
function onLoginCompleted(validCredentials, userContext, methodName) {
    
    if (validCredentials == false) {
        $get("loginStatus").innerHTML = loginFailedMsg;
        $("#loginLoading").hide();
        $("#loginStatus").show();
        setTimeout("hideStatusShowLoginBox();", 2000);
    } else {
       if (loginReturnURL != null)
           document.location = loginReturnURL;
        else
           document.location.reload();
    }
    

}
function logout() {
    Sys.Services.AuthenticationService.logout(null, null, onLoginFailed, "User Context");
}
function onLoginFailed(error, userContext, methodName) {
    loginLoadingIMG.style.visibility = "hidden";
    alert(error.get_message());
}

function showLoginBox() {
    $("#loginText").fadeOut("Fast", function() { $("#loginBox").fadeIn(); });
}
function hideStatusShowLoginBox(){
   $("#loginStatus").fadeOut("Fast", function() { $("#loginBox").fadeIn(); });
}
function showLoadingBox() {
    $("#loginBox").fadeOut("Fast", function() { $("#loginLoading").fadeIn(); });
}
