dojo.addOnLoad(function(){
    //find ajax forms
    dojo.query('form.ajax').forEach(function(e){

        //change the onsubmit
        dojo.connect(e, 'onsubmit', e, function(e){
            dojo.stopEvent(e);

            //add a working message and disable the buttons
            var message = dojo.create('span');
            message.innerHTML = 'Working...';
            dojo.place(message, this, 'last');
            var buttons = dojo.query('button', this);
            buttons.attr('disabled', 'disabled');

            //determing the error mode
            var errors = dojo.attr(this, 'ew_error_dialog');
            var errorMode = 'div';
            if(errors){
                errorMode = 'dialog';
            }else{
                //find the errors placeholder or create one
                dojo.query('div.ajax_errors', this).forEach(function(e){
                    errors = e;
                });
                if(!errors){
                    errors = dojo.create('div');
                    dojo.addClass(errors, 'ajax_errors');
                    dojo.place(errors, this, 'first');
                    errors.style.display = 'none';
                }
            }

            //build all the form values into an object
            var content = {};
            dojo.query('input, textarea, select', this).forEach(function(e){
                switch(e.type){
                    case 'checkbox':
                    case 'radio':
                        if(!content[e.name]){
                            content[e.name] = [];
                        }
                        if(e.checked){
                            content[e.name].push(e.value);
                        }
                        break;
                    default:
                        content[e.name] = e.value;
                        break;
                }
            }, this);

            redir = dojo.attr(this, 'ew_redir');

            //do the post
            dojo.xhrPost({
                url: this.action,
                content: content,
                handleAs: 'json',
                load: function(response){
                    if(response.status == 'success'){
                        if(response.data.redir){
                            window.location = response.data.redir;
                        }
                        else{
                            window.location = redir;
                        }
                    }
                    else{
                        dojo.destroy(message);

                        if(errorMode == 'div'){
                            errors.innerHTML = response.data.message;
                            errors.style.display = 'block';
                            buttons.removeAttr('disabled');
                            errors.scrollIntoView();
                        }
                        else{
                            dojo.byId(errors + '_content').innerHTML = response.data.message;
                            dojo.byId(errors + '_content').innerHTML += '<button onclick="ew.modal.close(\'' + errors + '\');">OK</button>';
                            ew.modal.show(errors);
                            buttons.removeAttr('disabled');
                        }
                    }
                },
                error: function(e){
                    alert('An error occurred.\n' + e);
                }
            });
        });

        //find cancel buttons in the form and change the onclick
        dojo.query('button.cancel', e).forEach(function(e){
            dojo.connect(e, 'onclick', this, function(e){
                dojo.stopEvent(e);
                window.location = dojo.attr(this, 'ew_redir');
            })
        }, e);
    });

    //find non ajax forms
    dojo.query('form.form:not(form.ajax) button.cancel').forEach(function(e){
        dojo.connect(e, 'onclick', e, function(e){
            dojo.stopEvent(e);
            window.location = dojo.attr(this, 'ew_redir');
        });
    });
    
});


