     if $(this).attr("value") < $(this).data("max_value") and name_of_maximum == $(this).attr('name')
       name_of_maximum = null
     
-    sum = 0
-    $("#attributes input").each ->
-      unless this.value == ""
-        sum += (parseInt(this.value) - $(this).data("min_value"))
-    sum*= 10
-    
-    sum+= 15 if name_of_maximum?
-    
-    add_line_to_costs("attributes", "Attribute", sum)
+    add_line_to_costs("attributes", "Attribute", build_points_for_attributes())
   
   $('#metatype').change()
    
  $("#attributes input").change ->
    current_value = $(this).attr("value")
    max_value = $(this).data("max_value")
    if current_value >= max_value
      if !name_of_maximum?

    sum+= parseInt(this.textContent)
  result = parseInt($("#costs thead td").text()) - sum
  $("#costs tfoot td").text(result);

build_points_for_attributes = ->
  sum = 0
  $("#attributes input").each ->
    unless this.value == ""
      sum += (parseInt(this.value) - $(this).data("min_value"))
  sum*= 10
  
  sum+= 15 if name_of_maximum?
  
  return sum

$ -> 
  $('#metatype').change (e) ->
