﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />

/// <reference path="jquery-1.2.6-vsdoc.js"/>

// See: http://asp.net/ajax/documentation/live/ClientReference/Sys.Services/AuthenticationServiceClass/default.aspx

SCOTT.CSC.Login = function(clientId, successMessageText, failureMessageText, registerMessageText) {
    this.ClientID = clientId;
    this.Selector = '#' + clientId;
    this.SuccessMessageText = successMessageText;
    this.FailureMessageText = failureMessageText;
    this.RegisterMessageText = registerMessageText;

    this.IsValid = true;

    this.Initialize();
};

SCOTT.CSC.Login.prototype =
{
    Name: 'SCOTT.CSC.Login',
    __typeName: 'SCOTT.CSC.Login',
    __class: true,
    AttachEvents: function() {
        var __app = this;
        var context = $(this.Selector);

        $('.Watermark', context).titleWatermark();

        this.AuthenticationService.set_defaultUserContext(this);

        $('a.SignInButton', context).click(function(e) {
            if ('A' === this.nodeName) {
                $(this).blur();
            }
            __app.Login();
            e.preventDefault();
        });

        // listen for the enter key and do login when entered
        $('.LoginForm', context).keydown(function(e) {
            if (e.keyCode === 13) {
                e.preventDefault();
                __app.Login();
                return false;
            }
        });
    },
    ShowErrors: function(errors) {
        var __app = this;
        var context = $(this.Selector);

        $('.Message span', context).empty().hide();
        $('.ErrorMessage span', context).html(errors.join('<br />')).show();
    },
    ShowMessage: function(message) {
        var __app = this;
        var context = $(this.Selector);

        $('.ErrorMessage span', context).empty().hide();
        $('.Message span', context).html(message).show();
    },
    Validate: function(username, password) {
        var __app = this;
        var context = $(this.Selector);

        var usernameError = $('.Login input.UserName').nextAll('span.Error:first');
        var passwordError = $('.Login input.Password').nextAll('span.Error:first');

        var errors = [];
        if (username.length === 0) {
            $(usernameError).show();
            errors[errors.length] = 'Email address is required';
        }
        else {
            $(usernameError).hide();
        }
        if (password.length === 0) {
            $(passwordError).show();
            errors[errors.length] = 'Password is required';
        }
        else {
            $(passwordError).hide();
        }

        __app.ShowErrors(errors);

        __app.IsValid = (errors.length === 0);
    },
    ShowWelcomeMessage: function() {
        var __app = this;
        var context = $(this.Selector);
		__app.UsersService.GetAdmin(function(admin) {
            
            if (admin == "Admin") {
            //('.MemberDisplay1', context).style.Margin-Left('195px');
            // $('.MemberDisplay1', context).style.margin-left('195px');
                     $('.LoginForm', context).slideUp(250, function() {
                $('.MemberDisplay1', context).slideDown();
            });
                }
           
        });

        __app.UsersService.GetScreenName(function(screenName) {
            $('.MemberDisplay span.Greeting', context).text('Welcome ' + screenName + '!');
            $('.LoginForm', context).slideUp(250, function() {
                $('.MemberDisplay', context).slideDown();
            });
        });
    },
    Login: function() {
        var __app = this;
        var context = $(this.Selector);
        var username = $('input.UserName', context).val();
        var password = $('input.Password', context).val();
        var isPersistent = $('#LoginRememberMe', context).is(':checked');

        var onLoginCompleted = function(validCredentials, userContext, methodName) {
            if (validCredentials) {
                __app.ShowMessage(__app.SuccessMessageText);

                setTimeout(function() {
                    __app.ShowWelcomeMessage();
                    SCOTT.CSC.GetApplication().ChangeLoginStatus(true);
                }, 2000);
            }
            else {
                __app.ShowMessage(__app.FailureMessageText);
            }
        };

        __app.Validate(username, password);
        if (__app.IsValid) {
            var handleUserStatus = function(status) {
                if (status !== "CSC-User") {
                    __app.ShowMessage(__app.RegisterMessageText);
                }
                else {
                    __app.AuthenticationService.login(
                    username, password, isPersistent, '',
                    null, onLoginCompleted, null, null);
                }
            };

            this.UsersService.GetUserStatus(username, handleUserStatus);
        }
    },
    ChangeLoginStatus: function(isAuthenticated) {
        var __app = this;
        __app.ShowWelcomeMessage();
    }
};

SCOTT.CSC.Extend(SCOTT.CSC.Login, SCOTT.CSC.UserControl);
