/*
Script: InPageLogin.js
        Creates an in-page login component that will show content behind authentication without reloading the page.

Dependancies:
        mootools - <Moo.js>, <Array.js>, <Element.js>, <Function.js>, <String.js>, <Ajax.js>, <Dom.js>, <Json.js>
        CNET libraries - <ajax.cnet.js>, <element.cnet.js>, <cnet.functions.js>

Author:
        Bill Krueger, <bill [dot] krueger [at] cnet [dot] com>

Class: InPageLogin
Given the required parameters, will construct an in-page login component.  This component allows users to authenticate
without leaving the page they are currently on.  If authentication is successful, the protected content is shown.
Otherwise, an error is surfaced to the user.

Arguments:
        options - an object containing options necessary for initializing the InPageLogin object

Options:
        authenticationUrl - URL to be used to authenticate via the RPS Ajax interface (1325 pagetype)
        signupUrl - URL for the URS Registration page
        forgotPasswordUrl - URL for the forgot password page
        loginAreaSelector - CSS selector that returns the area of content containing the login form
        contentAreaSelector - CSS selector that returns the area of content that is behind authentication
        loginFormSelector - CSS selector that returns the actual login form
        errorsAreaSelector - CSS selector that returns the area where authentication errors should be presented
        submitSelector - CSS selector that returns the form submission element (i.e. an anchor element or input button)
        signupSelector - CSS selector that returns the sign up element (i.e. an anchor element or input button)
        forgotSelector - CSS selector that returns the forgot password element (i.e. an anchor element or input button)
        uloginInId - ID of the Universal Login element shown when a user *is not* logged in, defaults to 'uloginIn'
        uloginOutId - ID of the Universal Login element shown when a user *is* logged in, defaults to 'uloginOut'
        onComplete - function executed when the XMLHttpRequest for authentication completes successfully
        onFailure - function executed when the XMLHttpRequest for authentication fails
*/
var InPageLogin = new Class({

    setOptions: function(options) {
        this.options = {
            uloginInId: 'uloginIn',
            uloginOutId: 'uloginOut',
            onComplete: Class.empty,
            onFailure: Class.empty
        };
        Object.extend(this.options, options || {});
    },

    initialize: function(options) {

        this.EXPLICIT_SUCCESS_CODE = 1;
        this.setOptions(options);
        this.form = $E(this.options.loginFormSelector);
        this.form.setProperty('action', this.options.authenticationUrl);
        this.addFormEvents();
        this.setInitialDisplay();
    },

    addFormEvents: function() {
        $E(this.options.submitSelector, this.options.loginFormSelector).addEvent('click', this.submitForm.bind(this));
        $E(this.options.signupSelector, this.options.loginFormSelector).addEvent('click', this.goToSignup.bind(this));
        $E(this.options.forgotSelector, this.options.loginFormSelector).addEvent('click', this.goToForgot.bind(this));
        //$(this.form.PASSWORD).addEvent('keypress', this.handleEnterKey.bindAsEventListener(this));
    },

    setInitialDisplay: function() {
        if (UserVars.loggedIn || UserVars.rememberMe) {
            dbug.log("logged in or rememebered");
            $E(this.options.contentAreaSelector).show();
            $E(this.options.loginAreaSelector).hide();
        } else {
            dbug.log("not logged in or rememebered");
            $E(this.options.errorsAreaSelector).setHTML('');
            $E(this.options.loginAreaSelector).show('block');
            $E(this.options.contentAreaSelector).hide();
        }
    },

    submitForm: function() {
        var authUrl = this.options.authenticationUrl;
        if(this.form.EMAILADDR.value != '') {
        
            authUrl = authUrl + '?EMAILADDR=' + this.form.EMAILADDR.value;
            if(this.form.PASSWORD.value != '')
                authUrl = authUrl + '&PASSWORD=' + this.form.PASSWORD.value;
        }
        this.form.setProperty('action', authUrl);    
    
        this.authReq = this.form.send({
            fireNow: false,
            onComplete: function() {
                
                this.response = Json.evaluate(this.authReq.transport.responseText);
                if (this.response.returnCode == this.EXPLICIT_SUCCESS_CODE) {
                    $E(this.options.contentAreaSelector).show('block');
                    $E(this.options.loginAreaSelector).hide();
                    UserVars.loggedIn = this.EXPLICIT_SUCCESS_CODE;
                    UserVars.userName = this.response.userName;
                    UserVars.userNameDisplay = unescape(this.response.userName.replaceAll('\\\+', ' '));
                    $(this.options.uloginOutId).hide();
                    new LoginStatus();
                } else {
                    if (null != this.response.errors && null != this.response.errors.length  && this.response.errors.length > 0) {
                        $E(this.options.errorsAreaSelector).setHTML(this.response.errors[0].text);
                    }
                }
                this.options.onComplete.bind(this)();
            }.bind(this),
            onFailure: function() {
                this.options.onFailure.bind(this)();
            }.bind(this)

        });
    },

    goToSignup: function() {
        window.location = this.options.signupUrl+'?path='+escape(location.href);
    },

    goToForgot: function() {
        var url = this.options.forgotPasswordUrl;
        var emailAddress = this.form.EMAILADDR.value.trim();
        if (null != emailAddress && emailAddress.length > 0) {
            window.location = url+'?path='+escape(location.href)+'&EMAILADDR='+escape(emailAddress);
        } else {
            window.location = url+'?path='+escape(location.href);
        }
    },

    handleEnterKey: function(event) {
        if (event && event.keyCode == 13) {
            this.submitForm();
        } else if (event && event.which == 13) {
            this.submitForm();
        }
    }
    

});
/* Do not edit below this line */
/* Section: Change Log

$Source: /cvs/webapps/www-rb-api/resources/js/InPageLogin.js,v $
$Log: not supported by cvs2svn $
Revision 1.5  2006/12/13 00:51:26  kruegerw
Need both userName and userNameDisplay to be set

Revision 1.4  2006/12/13 00:35:28  kruegerw
restoring commit log that I blew away

Revision 1.3  2006/12/13 00:34:02  kruegerw
setting userNameDisplay instead of userName
 
Revision 1.2  2006/12/11 21:20:54  kruegerw      Revision 1.3  2006/12/13 00:34:02  kruegerw
Fixing one of the UserVars   setting userNameDisplay instead of userName
     
Revision 1.1  2006/12/08 23:11:53  kruegerw      
initial commit

*/
