// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.

$(function() {
  $("#addplan").submit(function() {
    jQuery.post("/newplan/add.json", { name: $("#addplan > input[name=keyword]").val(), authenticity_token: getCSRFtoken() }, addplan_callback)
    .fail(
      function() {
      alert("post failed");
      }
    );
    return false;
  });
});

function addplan_callback(data) {
  var li = $("<li>");
  li.addClass("ui-state-default");
  li.text(data["name"]);
  $("#sortable").append(li);
  $("#addplan > input[name=keyword]").val("");
};

$(function() {
  $("#saveplan").submit(function() {
    var all_card = get_all_card();
    jQuery.post("/newplan/save.json", { "all_card": all_card, authenticity_token: getCSRFtoken() }, saveplan_callback, "text")
    .fail(
      function() {
      alert("save plan post failed");
      }
    );
    return false;
  });
});

function saveplan_callback(data) {
  alert("post ok");
};

function get_all_card() {
  var arr = new Array();
  for(var i = 0 ; i < $("#sortable > li").length ; i++){
    arr[i] = $("#sortable > li").eq(i).text();
  }
  return arr;
}

@@ -200,40 +200,23 @@
     
-jQuery.post(%22/newplan/add.json%22,
+var post_data =
  %7B n
@@ -303,98 +303,121 @@
 () %7D
-, addplan_callback)%0A    .fail(%0A      function() %7B%0A      aler
+;%0A    var post_url  = %22/newplan/add.json%22;%0A%0A    jQuery.pos
 t(
-%22
 post
- failed%22);%0A      %7D%0A    
+_url, post_data, addplan_callback).fail(fail_func
 );%0A 
@@ -431,37 +431,35 @@
 rn false;%0A  %7D);%0A
-%7D);%0A%0A
+%0A  
 function addplan
@@ -472,24 +472,26 @@
 ack(data) %7B%0A
+  
   var li = $
@@ -496,24 +496,26 @@
  $(%22%3Cli%3E%22);%0A
+  
   li.addClas
@@ -537,16 +537,18 @@
 ault%22);%0A
+  
   li.tex
@@ -562,24 +562,26 @@
 %22name%22%5D);%0A  
+  
 $(%22#sortable
@@ -595,16 +595,18 @@
 nd(li);%0A
+  
   $(%22#ad
@@ -648,26 +648,12 @@
 %22);%0A
-%7D;%0A%0A$(function() %7B
+  %7D%0A
 %0A  $
@@ -698,72 +698,21 @@
 var 
-all_card = get_all_card();%0A    jQuery.post(%22/newplan/save.json%22,
+post_data   =
  %7B %22
@@ -722,24 +722,28 @@
 _card%22: 
+get_
 all_card
 , authen
@@ -734,16 +734,18 @@
 all_card
+()
 , authen
@@ -778,117 +778,168 @@
 () %7D
-, saveplan_callback, %22text%22)%0A    .fail(%0A      function() %7B%0A      alert(%22
+;%0A    var post_url    = %22/newplan/save.json%22;%0A    var return_type = %22text%22;%0A%0A    jQuery.post(post_url, post_data, 
 save
- 
 plan
- post failed%22);%0A      %7D%0A    
+_callback, return_type).fail(fail_func
 );%0A 
@@ -964,14 +964,12 @@
  %7D);
-%0A%7D);
 %0A%0A
+  
 func
@@ -999,16 +999,18 @@
 data) %7B%0A
+  
   alert(
@@ -1025,12 +1025,77 @@
 %22);%0A
-%7D;%0A%0A
+  %7D%0A%0A  var fail_func = function() %7B%0A    alert(%22post failed%22);%0A  %7D%0A%0A  
 func
@@ -1120,17 +1120,24 @@
 ) %7B%0A
+  
   var a
-rr
+ll_card
  = n
@@ -1154,27 +1154,24 @@
 ;%0A  
-for(var i = 0 ; i %3C
+  var size     =
  $(%22
@@ -1193,17 +1193,45 @@
 ).length
- 
+;%0A    for(var i = 0; i %3C size
 ; i++)%7B%0A
@@ -1234,19 +1234,26 @@
 +)%7B%0A    
-arr
+  all_card
 %5Bi%5D = $(
@@ -1286,18 +1286,22 @@
 xt();%0A  
-%7D%0A
+  %7D%0A  
   return
@@ -1305,11 +1305,22 @@
 urn 
+all_c
 ar
-r;%0A%7D
+d;%0A  %7D%0A%7D);
 %0A

Update app/assets/javascripts/newplan.js Fix code (refactoring)