$(document).ready(function() {
  init();
  $.ajaxSetup({
    crossDomain: false, // obviates need for sameOrigin test
    beforeSend: function(xhr, settings) {
      if (!csrfSafeMethod(settings.type)) {
        xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
      }
    }
  });
});

function init() {
  $('#compare').click(function() {
    $.ajax({
      url: '/compare/',
      type: 'post',
      data: {
        doc1: $('#doc1').val(),
        doc2: $('#doc2').val(),
      },
      success: function(data){
        $('#result').html(data);
      },
    });
  });
}

function getCookie(name) {
  var cookieValue = null;
  if (document.cookie && document.cookie != '') {
    var cookies = document.cookie.split(';');
    for (var i = 0; i < cookies.length; i++) {
      var cookie = jQuery.trim(cookies[i]);
      // Does this cookie string begin with the name we want?
      if (cookie.substring(0, name.length + 1) == (name + '=')) {
        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
        break;
      }
    }
  }
  return cookieValue;
}

function csrfSafeMethod(method) {
  // these HTTP methods do not require CSRF protection
  return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}

@@ -38,260 +38,8 @@
 ();%0A
-  $.ajaxSetup(%7B%0A    crossDomain: false, // obviates need for sameOrigin test%0A    beforeSend: function(xhr, settings) %7B%0A      if (!csrfSafeMethod(settings.type)) %7B%0A        xhr.setRequestHeader(%22X-CSRFToken%22, getCookie('csrftoken'));%0A      %7D%0A    %7D%0A  %7D);%0A
 %7D);%0A
@@ -322,24 +322,415 @@
 );%0A  %7D);%0A%7D%0A%0A
+// csrf%0A$.ajaxSetup(%7B%0A  crossDomain: false, // obviates need for sameOrigin test%0A  beforeSend: function(xhr, settings) %7B%0A    if (!csrfSafeMethod(settings.type)) %7B%0A      xhr.setRequestHeader(%22X-CSRFToken%22, getCookie('csrftoken'));%0A    %7D%0A  %7D%0A%7D);%0A%0Afunction csrfSafeMethod(method) %7B%0A  // these HTTP methods do not require CSRF protection%0A  return (/%5E(GET%7CHEAD%7COPTIONS%7CTRACE)$/.test(method));%0A%7D%0A%0A
 function get
@@ -966,17 +966,17 @@
      // 
-D
+d
 oes this
@@ -1195,16 +1195,16 @@
   %7D%0A  %7D%0A
+
   return
@@ -1223,150 +1223,4 @@
 ;%0A%7D%0A
-%0Afunction csrfSafeMethod(method) %7B%0A  // these HTTP methods do not require CSRF protection%0A  return (/%5E(GET%7CHEAD%7COPTIONS%7CTRACE)$/.test(method));%0A%7D%0A

Clean up csrf helper functions