<script>
	var formName = "{{$form_id}}";
	$('#{{$form_id}}').submit(function (e) {

		e.preventDefault();
		if ($('#{{$form_id}}').valid()) {
			$(".user-validator").text('');
			$(this).find('input.btn').prop('disabled', true);

			var count = $('#count_add').val();
			var formData = $(this).serialize();

			$.ajax({
				type: "POST",
				url: '{{route($route_name)}}',
				data: formData,
				success: function (value) {

					if (formName == 'create-user-form') {
						$("#create-user-form")[0].reset();
						$("#create-user-form select").val('').trigger('change');
					}

					$('tbody').append(value);

					$('#{{$form_id}}').find('input.btn').prop('disabled', false);

					if ($.isEmptyObject(value.error)) {
						count++;
						$('#count_add').val(count);
						$('#asset_count').html(count);
					} else {
						var data = "<div class='alert alert-danger'><ul><li>" + value.error + "</li></ul></div>";
						$('#error_div').html(data);
					}
				},
				error: function (jqXHR, textStatus, errorThrown) {
					if (jqXHR.status == 422) {
						$('#{{$form_id}}').find('input.btn').prop('disabled', false);

						$.each(jqXHR.responseJSON.errors, function (index, ertext) {
							$("[name=" + index + "]").next('.user-validator').text(ertext);
						});

					}

				}
			});

		}

	});
</script>