var qtype="";

$("#closebutton").click(function(e) {
  $("#myModal").hide()
});

$("#savebutton").click(function(e) {
  $("#myModal").hide()
  if (qtype=="heading")
  {
    $("#id_specification").val($("#id_specification").val()+"="+$("#modalval").val()+"=\n");
  }
  if (qtype=="checklistitem")
  {
    $("#id_specification").val($("#id_specification").val()+"[]"+$("#modalval").val()+"\n");
  }
  if (qtype=="checklistgroupitem")
  {
    $("#id_specification").val($("#id_specification").val()+"()"+$("#modalval").val()+"\n");
  }

  if (qtype=="textbox")
  {
    $("#id_specification").val($("#id_specification").val()+"[...]"+$("#modalval").val()+"\n");
  }

  $("#modalval").val("")

});

$("#backbutton").click(function(e) {
  history.go(-1);
});
$("#id_specification").focus();
$("#clear").click(function(e) {
  $("#id_specification").val("");
  $("#id_specification").focus();
});

$("#addheading").click(function(e) {
  qtype="heading";
  $("#modaltitle").val("Add Heading");
  $("#myModal").show();
});

$("#addchecklistitem").click(function(e) {
  qtype="checklistitem";
  $("#modaltitle").textContent="Add Checklist Item";
  $("#myModal").show()
});

$("#addchecklistgroupitem").click(function(e) {
  qtype="checklistgroupitem";
  $("#modaltitle").val("Add Checklist group item");
  $("#myModal").show();
});

$("#addtextbox").click(function(e) {
  qtype="textbox";
  $("#modaltitle").val("Add textbox");
  $("#myModal").show();
});

$("#preview").click(function(e) {
  $("#checklistform").submit();
});


@@ -1,19 +1,4 @@
-var qtype=%22%22;%0A%0A
 $(%22#
@@ -63,160 +63,50 @@
 );%0A%0A
-$(%22#savebutton%22).click(function(e) %7B%0A  $(%22#myModal%22).hide()%0A  if (qtype==%22heading%22)%0A  %7B%0A    $(%22#id_specification%22).val($(%22#id_specification%22).val()+%22=%22+
+function modal(type, text, pre, post) %7B%0A  
 $(%22#
@@ -114,327 +114,145 @@
 odal
-val
+title
 %22).val(
-)+%22=%5Cn%22);%0A  %7D%0A  if (qtype==%22checklistitem%22)%0A  %7B%0A    $(%22#id_specification%22).val($(%22#id_specification%22).val()+%22%5B%5D%22+
+text);%0A  $(%22#myModal%22).show();%0A  
 $(%22#m
+yM
 odal
-val%22).val()+%22%5Cn%22);%0A  %7D%0A  if (qtype==%22checklistgroupitem%22)%0A  %7B%0A    $(%22#id_specification%22).val($(%22#id_specification%22).val()+%22()%22+
+ input%22).focus();%0A  $(%22#savebutton%22).click(function(e) %7B%0A    
 $(%22#m
+yM
 odal
-val%22).val()+%22%5Cn%22);%0A  %7D%0A%0A  if (qtype==%22textbox%22)%0A  %7B
+%22).hide()
 %0A   
@@ -312,15 +312,11 @@
 l()+
-%22%5B...%5D%22
+pre
 +$(%22
@@ -333,16 +333,21 @@
 ).val()+
+post+
 %22%5Cn%22);%0A 
@@ -351,11 +351,8 @@
 ;%0A  
-%7D%0A%0A
   $(
@@ -375,13 +375,35 @@
 (%22%22)
-%0A%0A
+;%0A    return false;%0A  
 %7D);
+%0A%7D
 %0A%0A$(
@@ -635,22 +635,22 @@
 (e) %7B%0A  
-qtype=
+modal(
 %22heading
@@ -654,70 +654,33 @@
 ing%22
-;%0A  $(%22#modaltitle%22).val(%22Add Heading%22);%0A  $(%22#myModal%22).show(
+, %22Add Heading%22, %22=%22, %22=%22
 );%0A%7D
@@ -720,38 +720,38 @@
 function(e) %7B%0A  
-qtype=
+modal(
 %22checklistitem%22;
@@ -753,85 +753,42 @@
 tem%22
-;%0A  $(%22#modaltitle%22).textContent=%22Add Checklist Item%22;%0A  $(%22#myModal%22).show()
+, %22Add Checklist Item%22, %22%5B%5D%22, %22%22);
 %0A%7D);
@@ -839,22 +839,22 @@
 (e) %7B%0A  
-qtype=
+modal(
 %22checkli
@@ -869,33 +869,10 @@
 tem%22
-;%0A  $(%22#modaltitle%22).val(
+, 
 %22Add
@@ -897,32 +897,18 @@
 tem%22
-);%0A  $(%22#myModal%22).show(
+, %22()%22, %22%22
 );%0A%7D
@@ -954,14 +954,14 @@
 %7B%0A  
-qtype=
+modal(
 %22tex
@@ -969,70 +969,36 @@
 box%22
-;%0A  $(%22#modaltitle%22).val(%22Add textbox%22);%0A  $(%22#myModal%22).show(
+, %22Add textbox%22, %22%5B...%5D%22, %22%22
 );%0A%7D

Refactor form button script