﻿UserLogin = Class.create();
UserLogin.prototype =
{
    // initialize data members
    initialize : function(cdaLogin)
    {
        this.m_cdaLogin = cdaLogin;
        this.m_username = $("username");
        this.m_password = $("password");
        this.m_login = $("login");

        this.m_username.focus();
        this.m_login.onclick = this.userLogin.bind(this);
        //this.bindEnterKey(this.m_username, this.m_login);
        //this.bindEnterKey(this.m_password, this.m_login);
    },

    // user login
    userLogin : function()
    {
       var userName=this.m_username.value.replace(/\s/g,"");
       var pwd=this.m_password.value.replace(/\s/g,"");
       if(userName=="" && pwd=="") {
           alert('Please input user name and password!');
           this.m_username.focus();
           return; }
       else if(userName=="" ) {
           alert('Please input user name!');
           this.m_username.focus();
           return ; }
       else if(pwd=="") {
           alert('Please input password!');
           this.m_password.focus();
           return; }
        if (this.m_cdaLogin == true && typeof(AdminLogin) != "undefined")
        {
            AdminLogin.UserLogin(this.m_username.value, this.m_password.value, this.userLoginCallback.bind(this));
            return;
        }
        else if (typeof(StaffLogin) != "undefined")
        {
            StaffLogin.UserLogin(this.m_username.value, this.m_password.value, this.userLoginCallback.bind(this));
            return;
        }
            
       alert("Sorry, the internet got a problem, please try again!");
       window.location=window.location;
    },
    
    // a callback for user login
    // userId : user id if login successful, otherwise Constant.INVALID_USER_ID
    userLoginCallback :function(userId)
    {
        if (userId.value > 0)
        {
            if (this.m_cdaLogin == true)
                {
                    //window.location = "CDHome.aspx?user=" + userId.value;
                     var page = AdminLogin.GotoPage();
                     window.location = page.value;
                }
            else
                window.location = "PickHat.aspx?from=login&user=" + userId.value;
        }
        else
        {
            alert("User name or password error, please try again!");
            this.m_password.focus();
        }
    },
    
    // bind enter key
    bindEnterKey : function(input, button)
    {
        if (navigator.appName == 'Netscape')
            input.onkeydown = this.inputKeydown4Firefox.bindAsEventListener(this, button);
        else
            input.onkeydown = this.inputKeydown4IE.bindAsEventListener(this, button);
    },
    
    // for firefox
    inputKeydown4Firefox : function(eventObject, button)
    {
        if (eventObject.keyCode == 13)
            button.onclick();
    },
    
    // for ie
    inputKeydown4IE : function(eventObject, button)
    {
        if (event.keyCode == 13)
            button.onclick();
    }
}
