﻿var CTTCAuth = function() {

    return {
        
        IsAuthenticated: false,

        // called from the lytebox after successful login (see login_complete.html)
        Login_Complete: function() {
            myLytebox.end();
            this.SetAuthenticationLink();
        },

        Logout: function() {
            CTTC_AuthenticationLogout('iMIS', 'CTTCAuth.Logout_Complete');            
        },
        
        // callback fuction for logout
        Logout_Complete: function() {
            this.SetAuthenticationLink();            
        },
        
        AuthenticationCheck: function() {
            CTTC_AuthenticationCheck('iMIS', 'CTTCAuth.AuthenticationCheck_Complete')
        },
        
        // callback function for authenication check
        AuthenticationCheck_Complete: function(jsonData) {
            var result = eval("(" + jsonData + ")");
            this.IsAuthenticated = result.User.IsAuthenticated;
        },
        
        // display the appropriate links
        SetAuthenticationLink: function() {
            CTTC_AuthenticationCheck('iMIS', 'CTTCAuth.SetAuthenticationLink_Complete')
        },
        
        // callback function for authenication check
        SetAuthenticationLink_Complete: function(jsonData) {
            var result = eval("(" + jsonData + ")");
            this.IsAuthenticated = result.User.IsAuthenticated;
            if (!this.IsAuthenticated) {
                $("#cttcLoginLink").show();
                $("#cttcLogoutLink").hide();
                $(".editButton").hide()
             } else {
                $("#cttcLoginLink").hide();
                $("#cttcLogoutLink").show();
                $(".editButton").each(function(){
                    if ($(this).prev('input').val() != '') {
                        $(this).show();
                    }
                });
             }
        },
        
        init: function() {
            CTTC_Init("LIVE");  // values: DEV, STAGING or LIVE
            this.SetAuthenticationLink();
        }
    };

}();
