/*
 * Form functions for the YAHOO.applegate.form namespace.
 * Assumes that the namespace is already created (see global-layout.js)
 */

(function() {
  YAHOO.namespace("applegate.form");
  var Dom = YAHOO.util.Dom,
    $ = Dom.get,
    Event = YAHOO.util.Event,
    Selector = YAHOO.util.Selector,
    lang = YAHOO.lang,
    applegate = YAHOO.applegate,
    global = YAHOO.applegate.global,
    form = YAHOO.applegate.form,
    validation = applegate.validation;

  /**
   * Makes the supplied form submit via Ajax to it's declared action attribute.
   *
   * @submitButton the submit button for the form so we can attach to Events like "onclick"
   * @form the form name | Object to make into an Ajax form
   * @errorMessage Object containing body and title (in that order) of the message box to be displayed on error
   * @successDialog *optional Object (as above) of the message box to be displayed on error
   * @functionToCallOnClick *optional an anonymous Function to call on click
   * @functionToCallOnSuccess *optional Function to call on success, if not supplied it will forward to the "view"
   * attribute passed back in the JSON response
   */
  form.newAjaxForm = function(submitButton, form, errorMessage, successDialog, functionToCallOnClick, functionToCallOnSuccess) {
    submitButton.on("click", handleOperation);

    function handleOperation(p_oEvent) {
      submitButton.set('disabled', true);

      if (functionToCallOnClick && functionToCallOnClick instanceof Function) {
        functionToCallOnClick.call(self);
      }
      // short hand
      this.form = $(form);
      validation.clearMessages(this.form);
      this.YUC = YAHOO.util.Connect;
      this.YUC.setForm(this.form);

      this.YUC.asyncRequest('POST', this.form.action, {
        success: handleSuccess,
        failure: handleFailure,
        scope: this
      });
    }

    var handleSuccess = function (o) {
      var obj = lang.JSON.parse(o.responseText);
      if (obj.hasErrors) {
        validation.applyBindingResultToForm(obj, this.form);
        submitButton.set('disabled', false);
        // If there is a Captcha then refresh it on error
        if (Selector.query("img#challenge").length) {
          YAHOO.applegate.captcha.handleNewCaptcha();
        }
      } else {
        validation.clearMessages(this.form);
        if (successDialog) {
          applegate.dialog.showSuccess(successDialog.body, successDialog.title, successDialog.callback || function () {
            if (functionToCallOnSuccess && functionToCallOnSuccess instanceof Function) {
              functionToCallOnSuccess.call(self);
            } else {
              location.href = global.contextPath() + '/' + obj.view;
            }
          });
        } else {
          if (functionToCallOnSuccess && functionToCallOnSuccess instanceof Function) {
            functionToCallOnSuccess.call(self);
          } else {
            // Forward to the view passed back in the JSON request
            location.href = global.contextPath() + '/' + obj.view;
          }
        }
      }
    };

    var handleFailure = function (o) {
      applegate.dialog.showWarning(errorMessage.body, errorMessage.title);
      submitButton.set('disabled', false);
      // If there is a Captcha then refresh it on error
      if (Selector.query("img#challenge").length) {
        YAHOO.applegate.captcha.handleNewCaptcha();
      }
    };
  }
})();