﻿// 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() {
        // call logout function in TabsUserLogin.aspx
        var d = new Date();
        $.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();
                }
            });
    }
    
    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.CleanDisplayUser = function() {
        // 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("");
        $(".pnlLogIn").show();
        $(".pnlLoggedInAs").hide();
    }

    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.");
                }
                else {

                    // hide login box
                    self.HideLoginBox();
                    var rawStr=data.String;
                    var aryData = data.String.split("-XXX-");
                    // clear input fields
                    $(".welcomeMessage").html(aryData[0]);  //data.String
                    $(".tabUsername").val("");
                    $(".tabPassword").val("");
                    $(".loginFailed").html("");

                    // redirect to logged in landing page
                    window.location = LandingPageUrl + aryData[1];


                }
            }, "json");
        }
        else {
            // if either username or password is missing, set error message
            $(".loginFailed").html("Username and/or password is invalid.  Please try again.");
        }
    }
    
    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();
                                                                                    