(function() {
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  window.Form = (function() {
    function Form(form_element, options) {
      this.form_element = form_element;
      this.options = options != null ? options : {};
      this.next_form = null;
      this.prev_form = null;
      this._bind_events();
      this._refresh_additional_elements();
      this.hide();
    }
    Form.prototype.is_first = function() {
      return !Boolean(this.prev_form);
    };
    Form.prototype.is_last = function() {
      return !Boolean(this.next_form);
    };
    Form.prototype.is_not_first = function() {
      return !(typeof this.is_first === "function" ? this.is_first() : void 0);
    };
    Form.prototype.is_not_last = function() {
      return !(typeof this.is_last === "function" ? this.is_last() : void 0);
    };
    Form.prototype.ancestors = function() {
      var ancestors, form;
      ancestors = [];
      form = this;
      while (true) {
        if (form.prev_form) {
          ancestors.push(form.prev_form);
          form = form.prev_form;
        } else {
          break;
        }
      }
      return ancestors;
    };
    Form.prototype.get_all_forms_to_submit = function() {
      var forms;
      forms = this.ancestors();
      forms.push(this);
      return forms;
    };
    Form.prototype.serialize_forms = function(forms) {
      var form, serialized_array, _i, _len;
      serialized_array = [];
      for (_i = 0, _len = forms.length; _i < _len; _i++) {
        form = forms[_i];
        serialized_array = $.merge(serialized_array, form.form_element.serializeArray());
      }
      return serialized_array;
    };
    Form.prototype.submit_all_forms = function() {
      var data, forms_to_submit, url;
      forms_to_submit = this.get_all_forms_to_submit();
      url = "./";
      data = this.serialize_forms(forms_to_submit);
      data.push({
        name: "form_sended_for_insert",
        value: true
      });
      return $.ajax({
        "async": false,
        "url": url,
        "data": data,
        "dataType": "json",
        "type": "post",
        "success": __bind(function(json) {
          var show_el, status;
          this.hide();
          status = json["status"];
          if ($("#" + status).length) {
            show_el = $("#" + status);
          } else {
            show_el = $("#fullError");
          }
          return show_el.fadeIn();
        }, this),
        "error": function(response) {
          if (typeof console !== "undefined" && console !== null) {
            return console.log(response.responseText);
          } else {
            return alert(response.responseText);
          }
        }
      });
    };
    Form.prototype.set_next_form = function(next_form) {
      this.next_form = next_form;
      this.next_form.prev_form = this;
      return this._refresh_additional_elements();
    };
    Form.prototype.unset_next_form = function() {
      var temp_form;
      temp_form = null;
      if (this.next_form) {
        temp_form = this.next_form;
        this.next_form.prev_form = null;
        this.next_form = null;
      }
      return this._refresh_additional_elements(temp_form);
    };
    Form.prototype._refresh_additional_elements = function(form) {
      if (form == null) {
        form = null;
      }
      this._refresh_action_buttons();
      if (this.next_form) {
        this.next_form._refresh_action_buttons();
      }
      if (this.prev_form) {
        this.prev_form._refresh_action_buttons();
      }
      if (form) {
        return form._refresh_additional_elements();
      }
    };
    Form.prototype._refresh_action_buttons = function() {
      var html, prev_button_value, submit_value;
      html = "";
      if (typeof this.is_not_first === "function" ? this.is_not_first() : void 0) {
        prev_button_value = "Назад";
        html += "<input type=\"button\" class=\"back_button\" value=\"" + prev_button_value + "\" />&nbsp;";
      }
      if (typeof this.is_last === "function" ? this.is_last() : void 0) {
        submit_value = "Подать заявку";
      } else {
        submit_value = "Далее";
      }
      html += "<input type=\"submit\" value=\"" + submit_value + "\" />";
      this.form_element.find(".submit-container").html(html);
      return this._bind_action_buttons_events();
    };
    Form.prototype._bind_events = function() {};
    Form.prototype._bind_action_buttons_events = function() {
      this._bind_submit();
      return this._bind_back_button();
    };
    Form.prototype._bind_submit = function() {
      this.form_element.unbind("submit");
      return this.form_element.bind("submit", __bind(function(e) {
        e.preventDefault();
        if (typeof this.is_valid === "function" ? this.is_valid() : void 0) {
          this.next_step();
        }
        return false;
      }, this));
    };
    Form.prototype._bind_back_button = function() {
      var back_button_element;
      back_button_element = this.form_element.find(".back_button");
      back_button_element.unbind("click");
      return back_button_element.click(__bind(function() {
        return this.prev_step();
      }, this));
    };
    Form.prototype.is_valid = function() {
      var is_valid, tr_array, url;
      url = "./";
      tr_array = this.form_element.find("tr");
      tr_array.removeClass("error-row");
      tr_array.find(".errors").hide();
      this.form_element.find("tr.all_errors_container").hide();
      is_valid = false;
      $.ajax({
        "async": false,
        "url": url,
        "data": this.form_element.serialize(),
        "dataType": "json",
        "type": "post",
        "success": __bind(function(json) {
          var error_container, error_text, item, key, tr_to_mark;
          if (json["is_valid"]) {
            return is_valid = true;
          } else {
            for (key in json) {
              item = json[key];
              error_text = "";
              try {
                error_text = item.join(", ");
              } catch (e) {

              }
              error_container = this.form_element.find("." + key + ".errors");
              error_container.html(error_text);
              error_container.show();
              tr_to_mark = error_container.parents("tr");
              tr_to_mark.addClass("error-row");
              tr_to_mark.show();
            }
            return is_valid = false;
          }
        }, this),
        "error": function(response) {
          if (typeof console !== "undefined" && console !== null) {
            console.log(response.responseText);
          } else {
            alert(response.responseText);
          }
          return is_valid = false;
        }
      });
      return is_valid;
    };
    Form.prototype.next_step = function() {
      if (typeof this.is_not_last === "function" ? this.is_not_last() : void 0) {
        return this.show_next_form();
      } else {
        return this.submit_all_forms();
      }
    };
    Form.prototype.prev_step = function() {
      if (typeof this.is_not_first === "function" ? this.is_not_first() : void 0) {
        return this.show_prev_form();
      }
    };
    Form.prototype.show_prev_form = function() {
      this.hide();
      return this.prev_form.show();
    };
    Form.prototype.show_next_form = function() {
      this.hide();
      return this.next_form.show();
    };
    Form.prototype.hide = function() {
      return this.form_element.hide();
    };
    Form.prototype.show = function() {
      this.form_element.fadeIn("slow");
      return this._refresh_additional_elements();
    };
    return Form;
  })();
}).call(this);

