(function(){


    /**
    * Display validateRegistration effect.
    */
    aura.provide('s100g.effect.validateRegistration');

    var effect = aura.namespace('s100g.effect.validateRegistration');

    effect.process = function(node)
    {

        var o = {
            form: node,
            validateUri: aura.dom.getAttribute(node, 'validateUri'),
            validated: false
        };

        aura.event.on(node, 'submit', function(e)
        {
            if ( ! o.validated )
            {
                aura.event.stop(e);
                var now = new Date();
                var uri = o.validateUri + '.json?haloRenderer=json&method=registration&username=' + o.form['username'].value + '&password=' + o.form['password'].value + '&bump=' + now.getTime();
                var request = YAHOO.util.Connect.asyncRequest('GET', uri, {
                    success: function(response) {
                        var json = aura.jsonDecode(response.responseText);
                        if ( json.response.status == 'fail' )
                        {
                            alert('Could not register. ' + json.response.reason);
                        }
                        else
                        {
                            o.validated = true;
                            o.form.submit();
                        }
                    },
                    failure: function(o) { alert("Failed!"); }
                });
            }
        });

    };

    /**
    * Register us with the effects manager.
    */
    aura.effect.registerEffect(
        's100g.effect.validateRegistration',
        effect.process
    );


})();
