﻿Default = Class.create();
Default.prototype =
{
    // initialize data members
    initialize : function()
    {
        // for login user
        this.m_username = $("username");
        this.m_password = $("password");
        this.m_login = $("login");
        
        if (this.m_login != null  )
        {
            this.m_username.focus();
            this.bindEnterKey(this.m_username, this.m_login);
            this.bindEnterKey(this.m_password, this.m_login);         
            this.m_login.onclick = this.userLoginClick.bind(this);
            
        }    
    },

    // user login click
    userLoginClick : function()
    {
        if(this.m_username.value=="" || this.m_password.value=="")
        {
            if(this.m_username.value=="" && this.m_password.value==""){
                alert("Please input username and password!");
                this.m_username.focus();
            }
            else if(this.m_username.value==""){
                alert("Please input username!");
                this.m_username.focus();
            }
            else if(this.m_password.value==""){
                alert("Please input password!");
                this.m_password.focus();
            }
            return;
        }
        Subscriber_SubscriberLogin.UserLogin(this.m_username.value, this.m_password.value, this.userLoginClickCallback.bind(this));
    },    
    
    // a callback for user login
    // userId : user id if login successful, otherwise Constant.INVALID_USER_ID
    userLoginClickCallback :function(userId)
    {
        if (userId.value > 0)
            window.location = "SubscriberHome.aspx";
        else
        {
            alert("Username or password error, please try again!");
            this.m_username.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();
    }
}

