tenants.prototype.profile = function () {
  var lease_id = this.lease_id;
  // NOTE: Always unbind the last events that bubbled up the DOM tree.
  $(document).off('change enter', '.updateTenant');
  // Now bind the newly created event listeners.
  $(document).on("change enter", ".updateTenant", function () {
    // Get data attributes from the input that was changed.
    var fieldname = $(this).attr('data-changed');
    var newvalue = $(this).val();
    cloneObject.make_profile_changes(fieldname, newvalue);
  }); // <-- END: onChane event (.updateTenant)

  $(document).off('change enter', '.updateTenantSelect');
  $(document).on("change enter", ".updateTenantSelect", function () {
    // Get data attributes from the input that was changed.
    var fieldname = $(this).find(':selected').attr('data-changed');
    var newvalue = $(this).find(':selected').val();
    cloneObject.make_profile_changes(fieldname, newvalue);
  }); // <-- END: onChane event (.updateTenantSelected)               

  // Validation for #editTenant
  $('#editTenant')
          .form({
            inline: true,
            on: 'submit',
            keyboardShortcuts: false,
            onSuccess: function () {
              debug_print('#editTenant form has passed validation');
            },
            fields: {
              status: {
                identifier: 'status',
                rules: [
                  {
                    type: 'empty',
                    prompt: 'Select Tenant Status'
                  }
                ]
              },
              first_name: {
                identifier: 'first_name',
                rules: [
                  {
                    type: 'empty',
                    prompt: 'Please enter the tenants first name'
                  }
                ]
              },
              last_name: {
                identifier: 'last_name',
                rules: [
                  {
                    type: 'empty',
                    prompt: 'Please enter the tenants last name'
                  }
                ]
              },
              mainPhone: {
                identifier: 'mainPhone',
                rules: [
                  {
                    type: 'empty',
                    prompt: 'Please enter the primary phone number'
                  }
                ]
              }

            } // <-- END: fields	
          }); // <-- END: .form	

};

tenants.prototype.make_profile_changes = function (fieldname, newvalue) {
  var filter = this.filter; /* This filter is used by HASH router navigate */
  var tenant_id = this.id; /* This tenant ID is used for AJAX save */
  var user_id = this.user_id; /* This defines the User for AJAX call */
  // validate the form before we proccess it.
  var validate = $('#editTenant').form('validate form');
  // Get the result of the validation.
  var validateResult = $('#editTenant').form('is valid');
  // If the form passes validation the begin processing.
  if (validateResult === true) {
    // Get data attributes from the input that was changed.
    debug_print("Data Passed to AJAX: tenant_id=" + tenant_id + "&fieldname=" + fieldname + "&newvalue=" + newvalue);
    $.ajax({
      beforeSend: saving_toast,
      url: 'app/tenants/edit-tenant.php',
      type: 'GET',
      async: true,
      data: {id: tenant_id, field: fieldname, val: newvalue, user_id: user_id},
      success: function (response, status, xhr) {
        if (!has_error(response, xhr)) {
          has_saved(response, xhr, fieldname);

          if (fieldname === "first_name" || fieldname === "last_name") {
            window.setTimeout(function () {
              document.location.reload();
            }, 3000);
            return false;
          }

          if (fieldname === "status") {
            router.navigate('Tenants/' + newvalue + '/' + tenant_id + '/Profile/' + lease_id);
            return false;
          }

          cloneObject.load_left('start_up');

          // Re load the right side without the animation for sticky message timstamp update
          if (fieldname === "status_msg") {
            var rside = cloneObject.load_right('start_up');
            $.when(rside).done(function () {
              cloneObject.profile(); /* RE-bind self */
            });
          }

          done_loading();
        } // END if !ERROR
      }// <-- END: on success

    }); // <-- END: .ajax() 

  } // <-- END: validate Results
}; // End make_profile_changes() function
