$(function() {
  /* rename */
  $("#exam-list-rename-button, .exam-details-rename-button").click(function() {
    $(".exam-list-column-name", $(this).closest("tr")).hide();
    $("#exam-list-rename", $(this).closest("tr")).css('display', 'inline');
    $("#exam-list-rename").find("input").select();
  });

  /* rename ajax */
  $('.exam-list-rename-submit').click(function() {
    var row = $(this).closest("tr");
    var name = $('#exam-list-rename-name', row).val();
    var url = row.find(".exam-list-column-name a").prop("href");
    $.ajax({
      method: 'POST',
      url: url,
      data: {'new_name': name},
      headers: {"X-CSRFToken": getCookie('csrftoken')},
      success: function(data, textStatus, xhr) {

        $(".exam-list-column-name", row).html(
          $("<a/>", {
            'class': "real-link",
            href: "/dashboard/exam/" + data.exam_pk + "/",
            text: data.new_name
          })
        ).show();
        $('#exam-list-rename', row).hide();
        // addMessage(data['message'], "success");
      },
      error: function(xhr, textStatus, error) {
	 addMessage("uhoh", "danger");
      }
    });
    return false;
  });

});
