/**
 * ajaxWM - the free web based window manager for remote terminals
 * Copyright (C) 2007 Dennis Felsing, Andreas Waidler
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * $Id: AjaxWM_Login_Screen.js 55 2007-11-30 13:40:43Z pwnd $
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * AjaxWM_Login_Screen
 *
 * The login screen object displays the screen where the user has to enter name
 * and password to proceed. It will implement some additional options like
 * specifying the host on which the user wants to be logged in, debug verbosity
 * and theme selection later.
 *
 * Locks the screen, calls the window manager which will send a request to the
 * server and handle the response. After that, the method setResult() of this
 * object is called to display if the login was successful or not and removing
 * the login screen or its lock.
 */
//-----------------------------------------------------------------------------

ajaxWM_LoginScreen = new function AjaxWM_Login_Screen()
{
//{{{properties

    // self-referencing properties
    var _this  = this;
    var _class = "AjaxWM_Login_Screen";

    // flag that indicates if the screen is locked or not
    var _isLocked = false;
    // {{{ HTML nodes

    var _background;
    var _div;
    var _form;
    var _input_name;
    var _input_pass;
    var _input_submit;

    //}}}
//}}}
//{{{this.create = function()

    /**
     * Creates the login screen.
     *
     * Creates the html nodes and appends them to the body.
     * Sets the login screen class for the body.
     */

    this.create = function()
    {
        //{{{create HTML nodes

        _background = document.getElementsByTagName('body')[0];
        _background.className = 'login';

        _div = document.createElement('div');
        _div.className = 'login';

        _form = document.createElement('form');
        _form.className = 'login'
        _form.onsubmit = _submit;

        _input_name = document.createElement('input')
        _input_name.name = 'name';
        _input_name.className = 'name';

        _input_pass = document.createElement('input')
        _input_pass.name = 'pass';
        _input_pass.className = 'pass';
        _input_pass.type = 'password';

        _input_submit = document.createElement('input');
        _input_submit.name = 'submit';
        _input_submit.className = 'submit';
        _input_submit.type = 'image';
        _input_submit.src = 'themes/' + ajaxWM.getTheme() + '/img/login.png';
//        _input_submit.value = 'login';
//        _input_submit.onclick = _submit;
//        _input_submit.onsubmit = _submit;

        //}}}
        //{{{append HTML nodes

        _form.appendChild(_input_name);
        _form.appendChild(_input_pass);
        _form.appendChild(_input_submit);
        _div.appendChild(_form);
        _background.appendChild(_div);

        //}}}
    }

//}}}
//{{{this.setResult = function(result)

    /**
     * Sets the login result.
     *
     * Displays if the login was successful. If so, the login screen
     * will be closed by _close(). If not the screen will be unlocked.
     * Will be called by AjaxWM_Manager on handling the response of the
     * login event.
     */

    this.setResult = function(result)
    {
        ajaxWM.debugMessage(_class, "setResult", "result=" + result);

        switch (result) {
        case true:
            _close();
            break;

        case false:
            _clear('password');
            _setLockState(false);
            break;

        default:
            ajaxWM.debugMessage(_class, "setResult",
                                "result has a bad value");
        }
    }

//}}}
//{{{function _submit(window_event)

    /**
     * Submits the login form if it not empty.
     *
     * Calls the window managers login method which will perform
     * the request. After the request is finished the function
     * setResult() will be called which handles screen unlocking
     * and closing.
     *
     * @return false Prevents form from being submitted normally
     */

    function _submit()
    {
        try {
            if (_isLocked)
                throw 'locked';

            var name = _input_name.value;
            var pass = _input_pass.value;

            if ("" == name || "" == pass)
                throw 'empty';

            _setLockState(true);
            ajaxWM.login(name, pass);
        } catch (e) {
            var error;
            switch (e) {
            case 'locked':
                error = 'Screen is locked, not submitting.';
                break;
            case 'empty':
                error = 'At least one field is empty, not submitting.';
                break;
            default:
                throw e;
            }
            ajaxWM.debugMessage(_class, '_submit', error);
        } finally {
            return false;
        }
    }

//}}}
//{{{ function _setLockState(state)

    /**
     * Locks or unlocks the screen if it is not in the state that was
     * passed as parameter.
     *
     * Sets a flag that indicates if the screen is locked or not. If
     * that flag is already set to the one passed as parameter this
     * method immediately returns.
     */

    function _setLockState(state)
    {
    if (state == _isLocked) {
            ajaxWM.debugMessage(_class, "_setLockState", "State " +
                                state + " already enabled");
            return;
        }

        switch (state) {
        case false:
        case true:
            _input_name.disabled = state;
            _input_pass.disabled = state;
            _input_submit.disabled = state;

            if (state == true) {
                _background.className = "login_locked";
                _input_submit.className = "submit_locked";
            } else {
                _background.className = "login";
                _input_submit.className = "submit";
            }

            _isLocked = state;
            
            break;

        default:
            ajaxWM.debugMessage(_class, "_setLockState",
                                "Wrong state: " + state);
        }
    }

//}}}
//{{{function _clear()

    /**
     * Clears input of the login form.
     *
     * If no parameter is passed or if the parameter is "both" both name
     * and password are cleard. If it is "name" or "password" the corresponding
     * input will be cleared.
     *
     * @param string input [optional] Input to be cleared.
     *                     Valid values are "name", "password" and "both".
     * @return void
     */

    function _clear(input)
    {
        if (null == input)
            input = 'both';

        switch (input) {
        case 'both':
            _input_pass.value = "";
            // no break here to clear name in "case 'name'"
        case 'name':
            _input_name.value = "";
            break;
        case 'password':
            _input_pass.value = "";
            break;
        default:
            throw 'Invalid argument: ' + input;
        }
    }

//}}}
//{{{function _close()

    /**
     * Closes the login screen.
     *
     * Removes the nodes from the display and deletes their contents
     * to free resources.
     */

    function _close()
    {
        _background.removeChild(_div);
        delete(_input_name);
        delete(_input_pass);
        delete(_input_submit);
        delete(_form);
    }

//}}}
}

//-----------------------------------------------------------------------------
// Ensure that the code does not exceed 79 characters per line using an indent
// of 4 spaces/level. Use NO TABS at all. Leave the mode line as it is. For
// further information see file docs/CodingConvention.
// vim: et ts=4 sw=4 fdm=marker
