/**

@fileoverview

This class encapsulates the signup page behavior so that we
do not have to reload for every username attempt.

@author Christopher Dorman <cdorman@ironicdesign.com>

*/

/**

Declare the new class, using the style established by prototype.js

@constructor

*/

var Signup = Class.create();

/**

Do the real work of creating a new Signup instance.

<p>

This is called automagically by the standard constructor code that
prototype.js provides.

<p>

This merely attaches bits of behavior to the check username button.

*/
Signup.prototype.initialize = function () {
    console.log ("Entering initialize()");

    console.log ("Handle the check username form");
    $$('input#username').each (this.attach.bind(this, 'blur', 'check'));
    $$('select#domain').each (this.attach.bind(this, 'change', 'check'));

    if (document.signup.username.value) {
        console.log ("Munging form");
        var parameters = Form.serialize (document.signup);
        
        console.log ("Making request");
        new Ajax.Request ("available",
                          {method: 'post',
                           evalScripts : true,
                           onFailure : function (transport) {
                               if (transport.status == 400) {
                                   alert ('That is not a valid username.');
                               } else {
                                   alert ('Could not confirm availability.');
                               }
                           },
                           onSuccess : function (form, transport) {
                               if (transport.responseText == "TRUE") {
                                   $('username_message').update ("That username is available.");
                               } else if (transport.responseText == "FALSE") {
                                   $('username_message').update ("That username is taken.  Please try again.");
                               }
                           }.bind (this, document.signup),
                           parameters : parameters});
    }

    this.lastUsername = document.signup.username.value;
    this.lastDomain = document.signup.domain.value;

    console.log ("Leaving initialize()");
};

/**

Attach a method to an element's event.

<p>

This is just a small wrapper around bindAsEventListener that to avoid
a certain amount of excess typing.  It helps us make sure that 'this'
is bound to the instantiated object instead of some other item.

@param {String} event The name of the event to observe
@param {String} handler The name of the handler routine to run
@param {Element,String} el The element to which it should be bound

*/
Signup.prototype.attach = function (event, handler, el) {
    $(el).observe (event, this[handler].bindAsEventListener (this));
};

/**

Handle requests to check usernames

<p>

Gets the username to check and makes an ajax request
to confirm the availability of the username.

*/
Signup.prototype.check = function (e) {
    console.log ("Entering check()");

    if (this.lastUsername == document.signup.username.value && this.lastDomain == document.signup.domain.value) {

        console.log ("Username and Domain have not changed, not executing");
    } else if (document.signup.username.value == "") {

        console.log ("Username is blank, not executing");
        $('username_message').update ("");
    } else {

        console.log ("Getting element");
        var el = Event.element(e);
  
        console.log ("Element is %s", el.tagName);
  
        var form = el.tagName.toLowerCase() == "form" ? el : el.form;
 
        console.log ("Setting last value to " + form.username.value);
        this.lastUsername = form.username.value;
        this.lastDomain = form.domain.value;

        console.log ("Munging form");
        var parameters = Form.serialize (form);
        
        console.log ("Making request");
        new Ajax.Request ("available",
                          {method: 'post',
                           evalScripts : true,
                           onFailure : function (transport) {
                               if (transport.status == 400) {
                                   alert ('That is not a valid username.');
                               } else {
                                   alert ('Could not confirm availability.');
                               }
                           },
                           onSuccess : function (form, transport) {
                               if (transport.responseText == "TRUE") {
                                   $('username_message').update ("That username is available.");
                               } else if (transport.responseText == "FALSE") {
                                   $('username_message').update ("That username is taken.  Please try again.");
                               }
                           }.bind (this, form),
                           parameters : parameters});
    }

    console.log ("Leaving check()");
};

