$(document).ready(function() {
////Validation
$("#newexercise").validate({
      rules: {
            name: {
                  required: true,
                  remote: {
                            cache:false,
                            async:false,
                            dataType: 'json',
                            url: "/admin/exercises",
                            type: "post",
                            data: {
                                    name: function() {
                                    return $("#name").val();
                                   }
                            },
                            dataFilter: function(data) {
                                if($("#name").attr("value") === $("#name").attr("oldvalue") ) {
                                   console.log("Old = New " + JSON.parse(data).name);
                                   return JSON.stringify(true);
                                   //return $.toJSON(true);
                                } else {
                                    console.log("Old != New");
                                    return (JSON.parse(data).name);
                                }

                            }
                  }
            },
            difficulty: {
                required: true,
                digits: true,
                range: [1, 10]
            },
            description: {
                required: true,
                maxlength: 24
            },
            keywords: {
                required: true
            }
        },
        messages:  {
                 name: {
                    required: "Please enter an exercise name",
                    remote: "Name already taken"
                 }
        }
});

$(".musclearray").each(function(){
     $(this).rules("add", {
       required: true,
       digits: true,
       range: [0, 10],
       messages: {
         required: "Muscle Array values must be betwen 0 and 10"
       }
     });
   });

});