﻿/// <summary>
///
/// </summary>
var loginOverlay;
$(document).ready(function () {
    var redirectUrl = SvrVar.redirectUrl;

    // Data object to be sent as input to the web method.
    var Login = function (username, password, is_checked) {
        this.username = username;
        this.password = password;
        this.is_checked = is_checked;
    }

    loginOverlay = $(SvrVar.overlayPanelId).overlay({
        top: 'center',
        left: 'center',
        mask: {
            color: '#ebecff',
            loadSpeed: 200,
            opacity: 0.9
        },
        onBeforeLoad: function () { //Fix Z-Index Bug - IE
            this.getOverlay().appendTo('body');
        },
        load: false
    });

    function DoDataBinding(usernameLabel, passwordLabel, remembermeLabel, failLabel, buttonLabel) {
        usernameLabel = '#' + usernameLabel;
        passwordLabel = '#' + passwordLabel;
        remembermeLabel = '#' + remembermeLabel;
        failLabel = '#' + failLabel;
        buttonLabel = '#' + buttonLabel;

        $(usernameLabel + ',' + passwordLabel).bind("keypress", function (event) {
            if (event.keyCode == 13) {
                $(buttonLabel).click();
                return false;
            }
        });

        $(buttonLabel).click(function () {
            var username = $(usernameLabel).val();
            var pwd = $(passwordLabel).val();
            var check = $(remembermeLabel)[0].checked;

            $(failLabel).text("Working...");

            var jsLogin = new Login(username, pwd, check);
            var request = Ektron.JSON.stringify({ data: jsLogin });

            // Timeout so that the Ajax call actually appears to 
            // do something, otherwise it is too fast.
            setTimeout(function () {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: SvrVar.webServiceUrl + "/LoginWithPersistance",
                    data: request,
                    dataType: "json",
                    success: function (msg) {
                        if (msg.d) {
                            $(failLabel).text("Success");
                            //Refresh Page 
                            if (redirectUrl == '') {
                                var currentHref = document.location.href;
                                var index = currentHref.indexOf("#");
                                if (index > -1)
                                    currentHref = currentHref.substring(0, index);
                                document.location.href = currentHref;
                            }
                            else {
                                document.location.href = redirectUrl;
                            }
                        }
                        else {
                            $(failLabel).text("Your login information is invalid please select 'trouble logging in' for assistance.");
                        }
                    },
                    error: function (e, r, t) {
                        if (Ektron.JSON.parse(e.responseText) != null) {
                            var data = Ektron.JSON.parse(e.responseText);
                            for (var key in data) {
                                if (key == "Message") {
                                    $(failLabel).text(data[key]);
                                }
                            }
                        }
                        else {
                            $(failLabel).text("Could not contact server to log in.");
                        }
                    }
                });
            }, 250);

            return false;
        });
    }

    DoDataBinding(SvrVar.overlayUserNameId, SvrVar.overlayPasswordId, SvrVar.overlayRememberMeId, SvrVar.overlayMessageId, SvrVar.overlayButtonId);
    DoDataBinding(SvrVar.headerUserNameId, SvrVar.headerPasswordId, SvrVar.headerRememberMeId, SvrVar.headerMessageId, SvrVar.headerButtonId);
});

function ShowLogonWindow() {
    loginOverlay.overlay().load();
}

function getToken() {
    var url = SvrVar.sharedServiceUrl + "/RequestToken?jsonp=validateToken";
    url += "&timestamp=" + new Date().getTime().toString(); // Prevents caching.

    var script = document.createElement("script");
    script.setAttribute("src", url);
    script.setAttribute("type", "text/javascript");
    document.body.appendChild(script);
}

function logoffCurrentSite() {
    var url = SvrVar.webServiceUrl + "/Logoff?jsonp=logoffSharedSite";
    url += "&timestamp=" + new Date().getTime().toString(); // Prevents caching.

    var script = document.createElement("script");
    script.setAttribute("src", url);
    script.setAttribute("type", "text/javascript");
    document.body.appendChild(script);
}

function logoffSharedSite() {
    var url = SvrVar.sharedServiceUrl + "/Logoff?jsonp=redirectSite";
    url += "&timestamp=" + new Date().getTime().toString(); // Prevents caching.

    var script = document.createElement("script");
    script.setAttribute("src", url);
    script.setAttribute("type", "text/javascript");
    document.body.appendChild(script);
}

function redirectSite(redirect) {
    if (redirect.d) {
        window.location = redirect.d;
    }
}

function validateToken(token) {
    if (token.d) {
        var jsonText = JSON.stringify({ data: token.d });
        jQuery.ajax({
            type: "POST",
            url: SvrVar.webServiceUrl + "/ValidateToken",
            data: jsonText,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                if (msg.d == true)
                    window.location.reload();
            },
            error: function (msg) {
                alert(msg.statusText);
            }
        });
    }
}

