﻿// JScript File
function tabs()
{   
    var self = this;
    
    // when page first loads, hide the login box
    $(document).ready(function()
    {
        self.HideLoginBox();
    
        // keep login box open when user is clicking within the login box
        $('.loginForm').click(function(e) {
            e.stopPropagation();
        });
        // hide login box when user clicks anywhere else on the page
        $(document).click(function() {
            self.HideLoginBox();
        });

    
    });
 
    // display login box
    this.ShowLoginBox = function()
    {
        $(".loginForm").show();
    }
    
    // hide login box
    this.HideLoginBox = function()
    {
        $(".loginForm").hide();
    }


    this.LogoutUser = function(srvName) {
        // call logout function in TabsUserLogin.aspx
        var d = new Date();
        /* 
        //logout from BotoxCosmetic.com
        $.getJSON('http://' + srvName + '.botoxcosmetic.com/Ajax/TabsUserLogin.aspx', { action: "logout", callback: d },
        function(data) {
        if (data.Error) {
        // if there is an error logging out...
        }
               
        });
        */
        $.getJSON('../Ajax/TabsUserLogin.aspx', { action: "logout", callback: d },
            function(data) {
                if (data.Error) {
                    // if there is an error logging out...
                }
                else {
                    // if log out was successful, clear "welcome username" message
                    // hide "welcome username" div
                    // display login/register div
                    $(".welcomeMessage").html("");
                    $(".pnlLogIn").show();
                    $(".pnlLoggedInAs").hide();
                    //location.reload();
                    location = "../users/BDDefault.aspx";
                }
            });
    }
    
    this.DisplayUser = function(name)
    {
        // hide login/register div
        // show "welcome message" div
        // set welcome message with name of user.  The words "Welcome" and "! | Logout" are hardcoded in usercontrol
        $(".welcomeMessage").html(name);
        $(".pnlLogIn").hide();
        $(".pnlLoggedInAs").show();
    }

    this.LoginUser = function(LandingPageUrl) {

        // retrieve the username and password values entered by user
        var username = $(".tabUsername").val();
        var password = $(".tabPassword").val();
        var d = new Date();
        
        // if there are values in both username and password fields, move on to login
        if (username.length > 0 && password.length > 0) {
            // call login function in TabsUserLogin.aspx, pass through username and password
            //HH (Jan, 18), use post to avoid risky on public pc cache
            $.post('../Ajax/TabsUserLogin.aspx', { action: "login", username: username, password: password, callback:d},
            
            function(data) {
               

                if (data.Error) {
                    // if there is error during login, set login failed message
                    $(".loginFailed").html("Username and/or password is invalid. Please try again, or utilize the password retrieval function below");

                }
                else {
                    // hide login box
                    self.HideLoginBox();

                    // redirect to logged in landing page
                    window.location = LandingPageUrl;

                    // display username and clear input fields
                    //                    $(".welcomeMessage").html(data.String);
                    //                    $(".pnlLogIn").hide();
                    //                    $(".pnlLoggedInAs").show();
                    //                    $(".tabUsername").val("");
                    //                    $(".tabPassword").val("");
                    //                    $(".loginFailed").html("");

                }
            }, "json");
        }
        else {
            // if either username or password is missing, set error message
            $(".loginFailed").html("Username and/or password is invalid. Please try again, or utilize the password retrieval function below");
        }
    }
    
    this.SearchKeyPress = function(e, LandingPageUrl)
    {
        var code;
	    if (!e) var e = window.event;
	    if (e.keyCode) code = e.keyCode;
	    else if (e.which) code = e.which;
	    if(code == 13)
	    {
	        this.LoginUser(LandingPageUrl);
	        return false;
	    }
	    return true;
    }

}

var tabs = new tabs();
                                                                                    
