/*
   SiteComponents version:
   6.6.4.1, tag SC_6_6_4_1, created Tue Oct 27 21:28:23 +0100 2009
   
   Disclaimer
   
   While we make every effort to ensure that this code is fit for its intended
   purpose, we make no guarantees as to its functionality. CoreTrek AS will
   accept no responsibility for the loss of data or any other damage or
   financial loss caused by use of this code.
   
   Copyright
   
   This programming code is copyright of CoreTrek AS. Permission to run this
   code is given to approved users of CoreTrek's publishing system CorePublish.
   
   This source code may not be copied, modified or otherwise repurposed for use
   by a third party without the written permission of CoreTrek AS.
   
   Contact webmaster@coretrek.com for information.
  
*/

/*

    ============================================================================
    IMPORTANT! This javascript is dependent on Prototype and Scriptaculous.
    ============================================================================
    
    CtForm Ajax rewrite for Lightbox
    
    Rewrites forms from CtWebForm module and CtForms using the
    AppbaseFormViewDefault to submit using Ajax when displayed inside a
    lightbox.
    
    Class for enhancing CtForms inside lightbox, initialization is done below.
    Init is triggered from event lightbox:contentUpdated
*/

var CtFormLightbox = Class.create({
    
    initialize: function(element) {
        this.element = element;
        
        // The element is in this case a form
        form = this.element;
        
        // Lets submit these form using Ajax
        form.observe('submit', function(event) {
            // Stop the original submit
            event.stop();
            
            var form = event.element();
            
            // Removes any ctwebform-element-validationerrors from the DOM
            // below this element
            form.select('.ctwebform-element-validationerror').each(function(element) {
                element.up().removeClassName('ctform-element-container-validationerror');
                element.remove();
            });
            
            // Do an Ajax post to the forms action using the defined method
            new Ajax.Request(form.readAttribute('action'), {
                method: form.readAttribute('method'),
                parameters: form.serialize(),
                onSuccess: function(res) {
                    // If form was submitted, we output the form saved message
                    // from the form handler, and hide the lightbox
                    alert(res.responseText.evalJSON());
                    lightbox.hide();
                },
                onFailure: function(res) {
                    var status = res.responseText.evalJSON();
                    for(i in status) {
                        // i contains the element name that has an error, lets
                        // get the actual element from DOM
                        var el = $('ctwebform-element-' + i);
                        
                        if(el == null) {
                            el = $('appbase-form-view-default-' + i);
                        }
                        
                        // Here we add the error handling for this element
                        
                        // Add validation error span if the element is tagged
                        // with a validation error
                        var err = el.select('.ctwebform-element-validationerror');
                        if(err.size() == 0) {
                            el.insert({ bottom: '<span class="ctwebform-element-validationerror">' + status[i] + '</span>' });
                            el.addClassName('ctform-element-container-validationerror');
                        }
                        
                        // Lets pulase the element that did not validate, just
                        // to draw user attention
                        new Effect.Pulsate(el, { pulses: 1, duration: 0.5 });
                    }
                }
            });
        });
    }
    
});

// When lightbox content is updated
document.observe('lightbox:contentUpdated', function(event) {
    
    // Find all forms in content with class ctform. This is all forms created
    // by the CtForm framework or the CtWebForm module
    lightbox.content.select('form.ctform').each(function(form) {
        new CtFormLightbox(form);
    });
});
