function LoginUI()
{
	this.Init = function(id)
	{
	    this._clientId = id;
	    
	    // Add handlers so we can call functions associated with this object.
        $addHandlers($get("LoginButton"), {click:this.Login}, this);
        $addHandlers($get("PasswordRecoveryLink"), {click:this.SendPassword}, this);

        // Handlers for key press to map "enter" to the send button.
        $addHandlers($get("divLogin"), {keypress:this.CheckKey}, this);
        
        this._delShowInfo = Function.createDelegate(this, this.ShowInfo);
	
        // Setup a method to cleanup
        var delDispose = Function.createDelegate(this, this.Dispose);
        Sys.Application.add_unload(delDispose);
	}
	
    // Clean up after ourself
	this.Dispose = function()
	{
        $clearHandlers($get("LoginButton"));
        $clearHandlers($get("PasswordRecoveryLink"));
        
        this._delShowInfo = null;
	}
	
	this.Show = function()
	{
        removeCssClass($get('loginpanelonly'), "hide");

        // Set focus in the user field.
        setFocus($get("UserName"));

        this.ShowInfo("");
	}
	
	this.Hide = function()
	{
        addCssClass($get('loginpanelonly'), "hide");
	}
	
	this.Login = function()
	{
	    if(this.Validate())
	    {
	        Reynolds.CL.Web.WebServices.LoginFunctions.Login(
		        $get("UserName").value,
		        $get("Password").value,
		        $get("RememberMe") && $get("RememberMe").checked,
		        _onLoginComplete,
		        _onLoginFailed,
		        this
	        );
		}
		
		return false;	
	}
	
	function _onLoginComplete(result, context) 
	{
		if (result) 
		{	
		    // The master page needs to refresh the login/logged in area.
		    Master_RefreshLoginArea();
	    }
		else 
		{
		    context.ShowInfo("Login Failed. Please try again.");
		}  
	}
	
	function _onLoginFailed(err, context) 
	{
		context.ShowInfo(err.get_message());
	}

	// Validate inputs on the control.
	this.Validate = function()
	{
	    valid = true;
	    var elm;
	    var elmFocus = null;
	    
	    // Check the First Name value.
	    elm = $get("UserName");
	    ClearErrBkg(elm);
	    if(0 == elm.value.length)
	    {
	        valid = false;
       	    removeCssClass($get("UserNameRequired"), "hide");
	        SetErrBkg(elm);
            if(!elmFocus){elmFocus = elm;}
	    }
	    
	    // Check the First Name value.
	    elm = $get("Password");
	    ClearErrBkg(elm);
	    if(0 == elm.value.length)
	    {
	        valid = false;
       	    removeCssClass($get("PasswordRequired"), "hide");
	        SetErrBkg(elm);
            if(!elmFocus){elmFocus = elm;}
	    }
	    
	    if(!valid)
	    {
            setFocus(elmFocus);
	    }
	    	    
	    return valid;

    }
    
    // Send the users password to their email address.
    this.SendPassword = function()
    {
        this.ShowInfo('');
        
        elmUser = $get("UserName");
        						
        if (elmUser.value == null || elmUser.value == '')
        {
            this.ShowInfo('Please provide your email address.');
        }
        else
        {
            Reynolds.CL.Web.WebServices.LoginFunctions.SendEmail(elmUser.value, 
                                                        _sendEmailSucceed, 
                                                        _sendEmailFailed, 
                                                        this);
        }
    }
    
   	function _sendEmailSucceed(result, context) 
   	{ 
	   context.ShowInfo(result);
	   setTimeout(context._delShowInfo,5000);
	}
	
	function _sendEmailFailed(err, context) 
	{
	   context.ShowInfo(err.get_message());
	}

    // Show an info message above the login fields.
    this.ShowInfo = function(mess)
    {
        var el;
        el = $get("messagediv");
        if (el && null != el)
        {
            if('undefined' == typeof(mess)){mess = "";}

            el.innerHTML = mess;
            if (mess && (mess != ""))
            {
                removeCssClass(el, "hide");
            }
            else
            {
                addCssClass(el, "hide");
            }
        }
    }
    
    this.CheckKey = function(evt)
    {
        // evt is a Sys.UI.DomEvent object
     
        if (mapKeyToAction(evt))
        { 
            this.Login();
            setFixValidation();
            return false;
        }
    }

}


