Event.observe(window, 'load', function() {
   
    $$('div.emoi_options p.alert-stock a').each(function(el,i){
        el.observe('click',function(event){
           Event.stop(event);
           Lightview.show({ href: "#registerLight", rel: 'inline', options: { width: 640, height: 425 }});
           $('form-validate-light').observe('submit',function(event){
                
                Event.stop(event);
                new Ajax.Request($('form-validate-light').action, {
                    method: 'post',
                    parameters: $('form-validate-light').serialize(false),
                    requestHeaders: {Accept: 'application/json'},
                    onSuccess: function(response) {
                        $('divLoading').remove();
                        var json = response.responseText.evalJSON();
                        if(json.length > 0)
                        {
                            var ulError = document.createElement('ul');
                            ulError.id="ulError";
                            json.each(function(el,i){
                               var liError = document.createElement('li').update(el);
                               
                               ulError.insert(liError);
                            });
                            $('form-validate-light').insert(ulError,'top');
                        }
                        else
                        {
                            var successMsg = document.createElement('h4').update("Votre inscription a bien été prise en compte. Merci de patienter pendant la création de votre alerte.");
                            $('form-validate-light').insert(successMsg);
                            window.location.href = $('alertStock').href;
                        }
                            
                    },
                    onLoading: function () {
                        if($('ulError'))
                                $('ulError').remove();
                        var divLoading = document.createElement('div');
                        divLoading.id = "divLoading";
                        $('form-validate-light').insert(divLoading);
                    }
             });

           });
        });
    });
});


