﻿// Process the login panel's login logic.
// Pass into the panel instance, triger selector, submit link selector, username selector, password selector, cookie value and the URL that accept value to process login.
function loginViaAjax(inst, trigerID, submitLinkID, usernameID, passwordID, cookieID, loginViaAjaxURL) {
    var submitLink = $(submitLinkID, inst);
    var username = $(usernameID, inst);
    var password = $(passwordID, inst);
    var loading = $("#loginLoad", inst);
    var loginErrorBox = $("#loginError", inst);
    var triger = $(trigerID, inst);
    var cookie = $(cookieID, inst);
    loading.hide();
    submitLink.addClass("disabled").show();
    // Add key up and change events for username, password input control.
    // It will set the button's status.
    $.each([username, password], function(i, n) {
        var checkSubmitStatus = function() {
            if (username.val() != "" && password.val() != "") {
                submitLink.removeClass("disabled");
            } else {
                submitLink.addClass("disabled");
            }
        };
        n.keyup(function(event) {
            switch (event.keyCode) {
                case 13:
                    submitLink.click();
                    break;
                default:
                    checkSubmitStatus();
                    break;
            }
        }).change(function() {
            checkSubmitStatus();
        });
    });

   
    
    // Sets the submit link's login events.
    submitLink.click(function() {
        //identify page for GA
        if (typeof (roomIndex) != 'undefined') {
            pageTracker._trackEvent('Room_RCO', 'Signup');
        } else {
            pageTracker._trackEvent('Dining_RCO', 'Signup');
        }
        loading.ajaxStart(function() {
            submitLink.hide();
            $(this).show();
        }).ajaxComplete(function() {
            $(this).hide();
            submitLink.show();
        });
        username.val(username.val().trim());
        if (username.val() == "" || password.val() == "") {
            return false;
        }

        $.ajaxSetup({ cache: false });
        $.postJSON(loginViaAjaxURL, { "username": username.val(), "password": password.val(), "persistent": cookie.attr("checked") }, function(json) {
            if (json.isRedirect) {
                location.href = json.redirectURL;
                return false;
            }


            if (json.isSuccess) {
                $.trigger("loggedIn");
                inst.hide();
                triger.box("destroy");
                if (json.noSecQuest) {
                    $(this).dialog({
                        title: 'Set Security Question',
                        modalClass: "sitewindow",
                        cancelClick: true,
                        cancelClose: true,
                        ShowCancelX: false,
                        buttonText: "Set Security Question and Answer",
                        buttonEvent: function() {
                        var SecurityAnswerjs = $("#SecurityAnswer").val();
                            if (SecurityAnswerjs != "") {
                                $(".marginchange").text("");
                                $("#accountInfoForm").submit();
                            }
                            else {
                                $(".marginchange").text(RoomResources.SecurityAnsReq);
                                return false;
                            }
                        },
                        jqmOptions: {
                            ajax: BASEURL + "/Profile/SetSecurityQuestion",
                            onLoad: function() {
                            }
                        }
                    }).showDialog();
                }
                //identify page for GA
                if (typeof (roomIndex) != 'undefined') {
                    pageTracker._trackEvent('Room_RCO', 'SigninSuccess');
                } else {
                    pageTracker._trackEvent('Dining_RCO', 'SigninSuccess');
                }
            } else {
                // Display the error message.
                submitLink.addClass("disabled");
                password.val("");
                var message = json.errorMessage;
                loginErrorBox.html($("<span />").addClass("field-validation-error").text(message));
                //identify page for GA
                if (typeof (roomIndex) != 'undefined') {
                    pageTracker._trackEvent('Room_RCO', 'SigninFail');
                } else {
                    pageTracker._trackEvent('Dining_RCO', 'SigninFail');
                }
            }
        });
        $.ajaxSetup({ cache: true });
        return false;
    });
}

