/* 
 * Copyright (C) 2008 Church of Scientology International.
 * All Rights Reserved.  Confidential and proprietary.
 */

var LoginUi = function() {
    
    var myThis = this;
    
    this.loginPanel = new YAHOO.widget.Panel("loginPanel", {
        width:"320px",
        visible:false,
        constraintoviewport:true,
        close:false,
        draggable:false
    } );
    this.loginPanel.render();
    
    this.loginButton = new YAHOO.widget.Button("loginButton"); 
    
    // listen for login button click
    this.loginButton.on("click", function(aEvent) { myThis.onLoginButtonClick(aEvent); });
    
    this.loginPanel.show();
    
};

/**
 * Intialization stuff
 */
LoginUi.prototype.init = function() {
    
};

/**
 * Called when the login button is clicked
 */
LoginUi.prototype.onLoginButtonClick = function(aEvent) {
    this.tryLogin(
            document.getElementById('userName').value,
            document.getElementById('password').value
    );
};

/**
 * Attempt a login
 */
LoginUi.prototype.tryLogin = function(aUserName, aPassword) {
    var myThis = this;
    document.getElementById('tryingMessage').style.display = '';
    document.getElementById('errorMessage').style.display = 'none';
    // FIXME: this is all in plain text!!
    CoursePublicApi.tryLogin(
        aUserName, aPassword,
        function(aResult) { myThis.onTryLoginResult(aResult); }
    );
};

/**
 * Called when login request returns
 */
LoginUi.prototype.onTryLoginResult = function(aResult) {
    
    if (aResult) {
        // do this in order to let the browser do it's field caching thing
        // FIXME: this doesn't work well either, since the stuff is plain text
        document.getElementById('mainform').submit();
        //document.location = "selectCourse.html";
    }
    else {
        // show error message
        document.getElementById('tryingMessage').style.display = 'none';
        document.getElementById('errorMessage').style.display = '';
    }
    
};
