var Renalware = typeof Renalware === 'undefined' ? {} : Renalware;

Renalware.Contacts = {
  Modal: function(el, callback) {
    this.el = el,
    this.form = this.el.find("form"),
    this.errorsContainer = this.form.find(".errors-container"),
    this.errorsList = this.errorsContainer.find("ul.error-messages"),
    this.callback = callback,

    this.init = function() {
      var self = this;
      this.form.on("submit", function(event) { self._onSubmit(event) });
    },

    this.open = function() {
      this.el.foundation("reveal", "open");
      this._clearForm();
      this._clearErrors();
    },

    this._onSubmit = function(event) {
      event.preventDefault();

      var valuesToSubmit = this.form.serialize();
      var self = this;

      self._clearErrors();
      $.ajax({
        type: "POST",
        url: self.form.attr("action"), //submits it to the given url of the form
        data: valuesToSubmit,
        dataType: "JSON", // you want a difference between normal and ajax-calls, and json is standard
        statusCode: {
          201: function(contact) {
            self._onContactAdded(contact);
          },
          400: function(jqXHR) {
            var errors = jqXHR.responseJSON;
            self._onErrors(errors);
          }
        }
      });
    },

    this._onContactAdded = function(contact) {
      this.el.foundation('reveal', 'close');
      this.callback(contact);
    },

    this._clearForm = function() {
      this.form.trigger("reset");
      this.form.find(".hidden").val("");
    },

    this._clearErrors = function() {
      this.errorsList.html("");
      this.errorsContainer.hide();
    },

    this._onErrors = function(errors) {
      var list = this.errorsList;

      this._clearErrors();
      this.errorsContainer.show();
      $.each(errors, function(i) {
        list.append("<li>" + errors[i] + "</li>");
      });
    }
  }
};

$(document).ready(function() {
  var modal = new Renalware.Contacts.Modal($("#add-patient-contact-modal"), function(contact) {
    $.getScript(document.URL);
  });
  modal.init();

  $("a[data-behaviour='add-new-contact']").on("click", function(event) {
     event.preventDefault();
     modal.open();
  })
});
@@ -1932,16 +1932,105 @@
 ion() %7B%0A
+  var trigger = $(%22a%5Bdata-behaviour='add-new-contact'%5D%22);%0A%0A  if (trigger.length %3E 0) %7B%0A  
   var mo
@@ -2121,16 +2121,18 @@
 ) %7B%0A    
+  
 $.getScr
@@ -2150,22 +2150,26 @@
 t.URL);%0A
+  
   %7D);%0A
+  
   modal.
@@ -2183,48 +2183,17 @@
 %0A%0A  
-$(%22a%5Bdata-behaviour='add-new-contact'%5D%22)
+  trigger
 .on(
@@ -2220,24 +2220,26 @@
 ent) %7B%0A     
+ 
+ 
 event.preven
@@ -2251,24 +2251,26 @@
 ult();%0A     
+  
 modal.open()
@@ -2273,14 +2273,20 @@
 en();%0A  
+  %7D)%0A  
 %7D
-)
 %0A%7D);

Initialize modal only if link with behavior present