$(document).ready(function() {
  $(document).on('evo:changed.article.price', function(event, data) {
    if (typeof $sd_bonuspunkte_rate != 'undefined') {
      var newbonus = Math.round(parseFloat(data.price * $sd_bonuspunkte_rate)) / 100;
      $('.sd-bonus-price').html(newbonus);
    }
  });

  $(document).on('evo:loaded.io.request', function(event, data) {
    if (typeof $sd_bonuspunkte_rate != 'undefined') {
      var newbonus = Math.round(parseFloat($('.price').text().replace('.', '').replace(',', '.')) * $sd_bonuspunkte_rate) / 100;
      $('.sd-bonus-price').html(newbonus);
    }
  });

  $('#sd-apply-bonusguthaben').on('click', function(event) {
    if ($('#basket-credit-form').length === 1) {
      return;
    }
    event.preventDefault();
    var action = $(this).data('action');
    var form = $('<form action="' + action + '" method="POST"></form>')
      .append('<input type="hidden" name="applyDigitalMoney" value="1"/>')
      .append('<input type="hidden" name="bonus-count" value="' + $('input[name="bonus-count"]').val() + '"/>')
      .append('<input type="hidden" name="jtl_token" value="' + $('input[name="jtl_token"]').val() + '"/>');
    $('body').append(form);
    form.submit();
  });
  $(".bonuses-info").insertBefore('table');
});
