﻿
var login = {
    initialize: function (formWrapper, isPopup, showPwdRecovery, redirectUrl, isFromKA) {


        $(formWrapper + " .txtPassword").hide();
        $(formWrapper + " .txtPasswordPlaceholder").focus(function () {
            $(this).hide();
            $(formWrapper + " .txtPassword").show().focus();
        });
        $(formWrapper + " .txtPassword").blur(function () {
            if ($(this).val().length == 0) {
                $(this).hide();
                $(formWrapper + " .txtPasswordPlaceholder").show();
            }
        });

        // Shows forgot password screen if user clicks forgot password link
        $(formWrapper + " .hlForgotPassword").click(function () {
            $(formWrapper + " .divLogin").hide();
            $(formWrapper + " .divForgotPassword").show();
        });

        // Reshow login form if user cancels out of Forgot password screen
        $(formWrapper + " .btnCancelForgot").click(function () {
            $(formWrapper + " .divLogin").show();
            $(formWrapper + " .divForgotPassword").hide();
            return false;
        });

        // If control is set to be a popup a cancel button is shown on the login screen so users can close login if they want to back out.
        if (isPopup == "False") $(formWrapper + " .btnCancelLogin").hide();
        $(formWrapper + " .btnCancelLogin").click(function () {
            $(formWrapper).hide();
            return false;
        });

        if (showPwdRecovery) {
            $(formWrapper + " .divLogin").hide();
            $(formWrapper + " .divForgotPassword").show();
        }

        //submits login form when enter key is pressed
        $(formWrapper + " .divLogin input").keypress(function (e) {
            if (e.keyCode == 13) {
                SubmitLoginForm();
            }
        });        

        //Submits login form
        $(formWrapper + " .hlSubmitLogin").click(function () {
            SubmitLoginForm();
        });

        function SubmitLoginForm() {
            var errorSpan = $(formWrapper + " .divLogin .error");
            //checks if username is entered
            var username = $(formWrapper + " #txtUsername").val();
            if (username == "" || username == "Enter your user name") {
                errorSpan.html("Username required");
            }
            else {
                //checks if password is entered
                var password = $(formWrapper + " #txtPassword").val();
                if (password == "" || password == "Enter your password") {
                    errorSpan.html("Password required");
                }
                else {
                    //calls login service
                    $.ajax({
                        type: "POST",
                        url: "/Services/Auth.aspx/Login",
                        data: '{"username":"' + username + '", "password":"' + password + '"}',                        
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (msg) {
                            var response = $.parseJSON(msg.d);
                            if (response.status == "INVALID") {
                                errorSpan.html("Invalid username or password");
                            } else {                                
                                if (isFromKA) {

                                    redirectUrl = redirectUrl.replace("[KASESSIONID]", response.kaSessionId).replace("[KATRANSACTIONID]", response.kaTranId);
                                }
                                                         
                                if (redirectUrl == "")
                                    location.reload(true);
                                else
                                    window.location = redirectUrl;
                            }  
                        }
                    });
                }
            }
        }

        //submits password reset form when enter key is pressed
        $(formWrapper + " .divForgotPassword input").keypress(function (e) {
            if (e.keyCode == 13) {
                SubmitResetPasswordForm();
            }
        });

        //Submits forgotpassword
        $(formWrapper + " .btnSubmitPasswordReset").click(function () {
            SubmitResetPasswordForm();
        });

        function SubmitResetPasswordForm() {
            var errorSpan = $(formWrapper + " .divForgotPassword .error");
            //checks if email is entered
            var email = $(formWrapper + " #txtEmailAddress").val();
            if (email == "" || email == "Enter your e-mail address") {
                errorSpan.html("Email address required");
            }
            else {
                //calls password reset method
                $.ajax({
                    type: "POST",
                    url: "/Services/Auth.aspx/PasswordReset",
                    data: '{"email":"' + email + '"}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        if (msg.d == "OK") {
                            errorSpan.html("Instructions on how to change your password have been sent to your email address.");
                            $(formWrapper + " #txtEmailAddress").val("");
                        }
                        if (msg.d == "INVALID") {
                            errorSpan.html("Invalid email");
                        }
                    }
                });
            }
        }
    }
}


//function EncodeText(textToEncode) {
//    //replace double quotes with _
//    textToEncode = textToEncode.replace(/"/g, "");
//    //replace single quotes with _
//    textToEncode = textToEncode.replace(/'/g, "");
//    //replace backslash with _
//    textToEncode = textToEncode.replace("/", "");
//    //replace forwardslash with _
//    textToEncode = textToEncode.replace("\\", "");
//    //replace { with _
//    textToEncode = textToEncode.replace(/{/g, "");
//    //replace } with _
//    textToEncode = textToEncode.replace(/}/g, "");
//    //encode string
//    textToEncode = $('<div/>').text(textToEncode).html();
//    return textToEncode;

//}
