jQuery(function($){function update_cart(elt){if(elt.data('reload')=='on'){$('<form>',{'method':'POST'}).append(elt.clone()).append($('<input>',{'name':'nonce','value':elt.data('nonce'),'type':'hidden'})).append($('<input>',{'name':'system','value':elt.closest('.lws_wr_pointsoncart_bloc[data-pool]').data('pool'),'type':'hidden'})).appendTo($('body')).submit()}else{$.ajax({url:elt.data('url'),data:{action:'lws_woorewards_pointsoncart_reserve_amount',amount:elt.find('.lws_wr_pointsoncart_amount_value').val(),system:elt.closest('.lws_wr_pointsoncart_bloc[data-pool]').data('pool'),nonce:elt.data('nonce'),},success:function(data){if(data.success!=undefined){btn=$('.woocommerce-cart-form button[name="update_cart"], .woocommerce-cart-form input[name="update_cart"]');if(btn.length){btn.prop('disabled',!1);btn.trigger('click')}else{location.reload()}}else alert(data.error!=undefined?data.error:'An error occured, refresh the page and try again.')}}).fail(function(jqXHR,textStatus){console.log(textStatus)})}}
$('body').on('click','.lws_wr_pointsoncart_amount_apply',function(e){e.preventDefault();update_cart($(e.target).closest('.lws_wr_pointsoncart_contribution'));return!1});$('body').on('click','.lws_wr_pointsoncart_use_max_amount',function(e){e.preventDefault();var root=$(e.target).closest('.lws_wr_pointsoncart_contribution');var input=root.find('.lws_wr_pointsoncart_amount_value');if(input.length){input.val(input.data('usemax'));update_cart(root)}
return!1});$('body').on('change, keyup','.lws_wr_pointsoncart_amount_value',function(e){var root=$(e.target).closest('.lws_wr_pointsoncart_contribution');root.find('.lws_wr_pointsoncart_amount_apply').prop('disabled',!1);root.find('.lws_wr_pointsoncart_use_max_amount').prop('disabled',!1)});$('body').on('keydown','.lws_wr_pointsoncart_amount_value',function(e){if(e.which==10||e.which==13){e.preventDefault();update_cart($(e.target).closest('.lws_wr_pointsoncart_contribution'));return!1}});$('body').on('updated_wc_div update_checkout wc_fragments_loaded',function(e){$('.lws_wr_pointsoncart_bloc[data-url]').each(function(){var bloc=$(this);if(undefined==bloc.data('opacity'))
bloc.data('opacity',bloc.css('opacity'));bloc.css('opacity','0.5');$.ajax({url:bloc.data('url'),data:{'action':'lws_woorewards_pointsoncart_bloc_refresh','origin':bloc.data('origin'),'system':bloc.data('pool')},dataType:'html',success:function(data){bloc.replaceWith(data)},error:function(resultat,status,error){bloc.css('opacity',bloc.data('opacity'));console.log("Cannot refresh Points on Cart bloc "+status);console.log(error)},})})})})