jQuery(function() {
    pwlparaHandleFormSubmission('#pwlpara-welcome-form');

    jQuery('#pwlpara-welcome-next').on('click', function(e) {
        var activeStep = parseInt(jQuery('.pwlpara-welcome-step:visible').attr('data-step'));
        var nextStep = parseInt(activeStep) + 1;
        var lastStep = parseInt(jQuery('.pwlpara-welcome-step:last').attr('data-step'));

        jQuery('.pwlpara-welcome-step').hide();
        jQuery('.pwlpara-stepper-item[data-step="' + activeStep + '"]').removeClass('active').addClass('completed');
        jQuery('.pwlpara-stepper-item[data-step="' + nextStep + '"]').removeClass('completed').addClass('active');

        var nextStepElement = jQuery('.pwlpara-welcome-step[data-step="' + nextStep + '"]');
        if (nextStepElement) {
            nextStepElement.show();
            jQuery('#pwlpara-welcome-prev').show();
        }

        if (nextStep == lastStep) {
            jQuery('#pwlpara-welcome-next').hide();
            jQuery('#pwlpara-welcome-done').show();
        }

        e.preventDefault();
        return false;
    });

    jQuery('#pwlpara-welcome-prev').on('click', function(e) {
        var activeStep = parseInt(jQuery('.pwlpara-welcome-step:visible').attr('data-step'));
        var prevStep = parseInt(activeStep) - 1;

        jQuery('.pwlpara-welcome-step').hide();
        jQuery('.pwlpara-stepper-item[data-step="' + activeStep + '"]').removeClass('active');
        jQuery('.pwlpara-stepper-item[data-step="' + prevStep + '"]').removeClass('completed').addClass('active');

        var prevStepElement = jQuery('.pwlpara-welcome-step[data-step="' + prevStep + '"]');
        if (prevStepElement) {
            prevStepElement.show();
            jQuery('#pwlpara-welcome-done').hide();
            jQuery('#pwlpara-welcome-next').show();
        }

        if (prevStep == 1) {
            jQuery('#pwlpara-welcome-prev').hide();
        }

        e.preventDefault();
        return false;
    });

    jQuery('#pwlpara-welcome-reward-points').on('propertychange change keyup input paste', function() {
        var points = 0;
        var pointsString = jQuery(this).val();
        if (pointsString) {
            points = parseInt(pointsString);
        }

        jQuery('.pwlpara-welcome-offer').each(function() {
            var button = jQuery(this);
            var label = button.find('.pwlpara-offer-button-points');

            if (points > 0) {
                var pointMultiplier = button.attr('data-point-multiplier');
                label.text((points * pointMultiplier).toLocaleString());
            } else {
                label.text(pwlpara.i18n.free);
            }
        });
    });

    jQuery('#pwlpara-welcome-skip-button').on('click', function(e) {
        if (!confirm(pwlpara.i18n.skipWelcome)) {
            e.preventDefault();
            return false;
        } else {
            window.onbeforeunload = '';
        }
    });
});
