// The "module" exported by this script is called "ngbundle":
// Applies drag and drop behaviour to templates/form

var ngform = function() {

function Form(ajax_form_list) {
  
  if(!(this instanceof Form)) {
    return new Form(ajax_form_list);
  }
  
  this._ajax_form_list = ajax_form_list;

  clog("Form setup");
  clog(this._ajax_form_list);
}

Form.prototype.setup = function() {
  this._ajax_form_list.each(function() {
    var form = $(this);
    form.ajaxForm({
      dataType: "json",

      success: function(response, status) {
        if (response.error) {
          $("<div>Please fill out the form completely.</div>").dialog({
            buttons: { "Ok": function() { $(this).dialog("close"); } }
          });
        } else {
          $("<div>Thank you.</div>").dialog({
            buttons: { "Ok": function() { $(this).dialog("close"); } }
          });
          form.resetForm();
        }
      },

      beforeSubmit: function(formData, form, options) {
        // form.find("div.row.last span.spinner").show();
        // formData is an array; here we use $.param to convert it to a string to display it 
        // but the form plugin does this for you automatically when it submits the data 
        // var queryString = $.param(formData); 
    
        // jqForm is a jQuery object encapsulating the form element.  To access the 
        // DOM element for the form do this: 
        // var formElement = jqForm[0]; 
    
        // alert('About to submit: \n\n' + queryString); 
    
        // here we could return false to prevent the form from being submitted; 
        // returning anything other than false will allow the form submit to continue 
        return true;
      }
    });
  });
};

return {Form: Form};

}();