$ = $telerik.$;
//PAGE LOAD EVENT HANDLING
$(document).ready(function () {
    if (username !== undefined) {
        $("#divUsername").html(username);
        $("#loginA").css("display", "none");
        $("#loginDisplay").css("display", "");

    } else {
        $("#loginDisplay").css("display", "none");
        $("#loginA").css("display", "");
    }

    $("#logouthv").click(function () {
        Sys.Services.AuthenticationService.logout(null,
                   function () {
                       //reload for loggin only when loggout worked
                       //$("#loginA").css("display", "");
                       $("#loginDisplay").css("display", "none");
                   },
                   function () { radalert("<strong>Logout mislykket!</strong>", 400, 100, "Result"); }, null);
    });
    //some css repairs
    $(".rsAllDayRow").css("height", "73px");
    $(".rsMainHeader th").css("height", "73px");
    $(".rsMainHeader th").css("width", "100px");
    $("#Scheduler1_Scheduler1_ConfigurationPanel1Panel").css("position", "absolute");

    lp = $find("Scheduler1_RadAjaxLoadingPanel1");
});
var lp; //loading panel
function confirmCallBackFn(arg) //the answer yes/no true/false
{
    //console.log(arg);
    if (arg) {
        if (selectedOption === "login") {

            WebServices.GetHashedPassword(password, function (hashedPassword) {

                Sys.Services.AuthenticationService.login(username, hashedPassword, false, null, null, onLoginCallCompleted, onLoginCallFailed, username);
                lp = $find("Scheduler1_RadAjaxLoadingPanel1");
                if (lp) { lp.show("Scheduler1_Panel1"); }
            });
        }
        else if (selectedOption === "emailReset") {

            if (tbResetEmailText.indexOf("@") === -1 && tbResetEmailText.indexOf(".") === -1) {
                return;
            } else {
                WebServices.ResetPassword(tbResetEmailText, function (answer) {
                    if (answer) {
                        alert("Password er sendt til din email!");
                    } else {
                        alert("Email findes ikke i systemet!");
                    }
                    if (lp) {//on scheduler page!
                        lp.hide("Scheduler1_Panel1");
                    }
                });
            }
        }
        else if (selectedOption === "register")
        { }
    }
    //WebServices.SendEmail
    else {
        selectedOption = "login"; //setback the initial popup
        username = undefined; //put it back at unlogged value!
    }
    //
}
function onLoginCallCompleted(result, context, methodName) {
    username = context;
    if (result == true) {
        $("#loginA").css("display", "none");

        $("#divUsername").html(username); //put the user name!

        if (username !== undefined && username !== '') {
            WebServices.IsUserInRole(username, "admin", function (answer) {
                if (answer) {
                    $("#divUsername").html($("#divUsername").html() + " [ admin ] ");
                } else {
                    $("#divUsername").html($("#divUsername").html() + "");
                }

            });
        }
        $("#loginDisplay").css("display", "");
        //refresh scheduler, move in scheduler script! with a global event!
        lp = $find("Scheduler1_RadAjaxLoadingPanel1");
        if (lp) {//on scheduler page!
            lp.hide("Scheduler1_Panel1");
            //button will make another one!
            __doPostBack('Scheduler1$MyButton', '');
        }
        $("#Scheduler1_RadScheduler1_Form_AdvancedInsertForm1_tbKontaktNavn_text").html(username);
    } else {
        radalert("Brugeren findes ikke i systemet. Prøv igen eller kontakt Mobil lab's administrator.", 400, 100, "Result");
        if (lp) {//on scheduler page!                
            lp.hide("Scheduler1_Panel1");
        }
    }
}
function onLoginCallFailed(result, context, methodName) {
    //console.log(result);
    radalert("<strong>Login</strong> mislykket! Prøv igen eller kontakt Mobil lab's administrator.", 400, 100, "Result");
    if (lp) { lp.hide("Scheduler1_Panel1"); }
}
function resetEmail() {
    $("#divLogin").css("display", "none");
    $("#divEmailReset").css("display", "");
    selectedOption = "emailReset";

}

function showLogin() {
    var givenEmail = '';
    //        if (this === "isFromPopup") {
    //            givenEmail = $("#Scheduler1_RadScheduler1_Form_AdvancedInsertForm1_tbUserNameValue_text").val();
    //        }
    //console.log(this);
    selectedOption = "login";
    radconfirm('<div id="divAuth">'
            + '<div id="divLogin">&nbsp;'
            + '<br/>Email:<input type="text" id="tbUserName" onblur="username=this.value;" style="margin-left:50px;width:150px" value="' + givenEmail + '"/><br/>' + ''
            + 'Adgangskode:&nbsp;&nbsp;<input style="margin-left:0px;width:150px" type="password" id="tbPassword" onblur="password=this.value;"/><br/>'
    // + 'Register <a href="#">here</a><br/>Forgot password <a href="#" onclick="resetEmail();">here</a> </div>'
            + '<br/>Glemt adgangskode? <a href="#" style="text-decoration:underline" onclick="resetEmail();">Klik her</a> </div>'
            + '<div id="divEmailReset" style="display:none"> Indtast&nbsp;din&nbsp;email&nbsp;for&nbsp;at&nbsp;få&nbsp;tilsendt&nbsp;en&nbsp;ny&nbsp;adgangskode.<br/>'
            + 'E-mail :<input type="text" id="tbEmail" onblur="tbResetEmailText=this.value;" style="margin-left:0px;width:150px" /> </div>'
            + '</div>'
            , confirmCallBackFn, 400, 120, null, 'Mobil lab - Log ind');
}
