if (window.history.replaceState) {
    window.history.replaceState(null, null, window.location.href);
};

$(function(){
    $('.promo-code').on("cut copy paste",function(e) {
        e.preventDefault();
    });
});

$(function(){
    $('.promo-code-button').on('click', function(e) {
        e.preventDefault();
        e.stopPropagation();
        $("#promo-code-response").empty();

        var button = $(this);
        var buttonoriginal = button.html();
        var bValue = button.data('id');
        var data = {promocode: bValue};
        data = $("#uw-promo-code-form").serialize() + '&' + $.param(data);

        changeButton(button, 'Canjeando código');

        $.ajax({
            type: 'POST',
            url: '',
            data: data,
            dataType: 'json',
            success: function(data) {
                $("#promo-code-response").append(data.message).hide().slideDown();

                if (data.success === true) {
                    button.html("Código aceptado");
                    button.css("color","#d79602");
                    setTimeout(function() {
                	$("#uw-promo-code-form")[0].reset();
                	restoreButton(button, buttonoriginal);
                    }, 5000);
                }
                else {
                    setTimeout(function() {
                        $("#promo-code-response").slideUp( function() {
                        $("#promo-code-response").empty();
                        restoreButton(button, buttonoriginal);
                        grecaptcha.reset();
                        });
                    }, 5000);
                }
            },
            error: function() {
                setTimeout(function() {
                    alert("Algo ha salido mal. Por favor intente más tarde");
                    window.location.reload();
                }, 2000);
            }
        });
    });
});
