﻿$(document).ready(function () {

    var jRewardsCardForm = $("#RewardsCardForm");
    if (jRewardsCardForm.length) {
        var acceptanceCheckUrl = $("#AcceptanceCheckUrl").val();

        jRewardsCardForm.validate({
            rules: {
                CardNumber: {
                    required: true,
                    minlength: 14,
                    maxlength: 14,
                    digits: true
                }
            },
            submitHandler: function (form) {
                var cardNum = $("#CardNumber").val();
                // also check if user has already accepted terms
                $.post(acceptanceCheckUrl,
                    { cardNumber: cardNum },
                    function (response) {
                        if (typeof response == "boolean" && response) {
                            // let the submit go through
                            form.submit();
                        } else {
                            // if they haven't, block the current submit and give them a prompt to accept terms
                            // this fancybox trigger is hacky, but the direct call version doesn't seem to work in our version and upgrading or replacing will take too long -- MSC
                            var jAcceptancePromptLink = $("#AcceptancePromptLink");
                            jAcceptancePromptLink.attr('href', jAcceptancePromptLink.attr('href') + "?CardNumber=" + cardNum);
                            jAcceptancePromptLink.fancybox({
                                'hideOnContentClick': false,
                                'frameWidth': 760,
                                'frameHeight': 580,
                                'overlayShow': true,
                                'overlayOpacity': 0.75,
                                'centerOnScroll': false,
                                'padding': 0
                            });
                            jAcceptancePromptLink.trigger('click');
                        }
                    }
                );
            }
        });
    }

    // Rewards show more/less
    $('#more').hide();
    $('a#readmore').click(function (e) {
        e.preventDefault();
        $('#more').slideToggle('fast', function () {
            $('#more').is(":visible") ? $('a#readmore').text('Read Less').addClass('arrow-up') : $('a#readmore').text('Read More').removeClass('arrow-up');
        });
    });

    // Rewards image button hover (default button)
    $('#RewardsCardForm input.bttn').hover(function () {
        $(this).css('background-position', '0 -24px');
    }, function () {
        $(this).css('background-position', '0 0');
    }
    );

    // scrollpane style (passport terms)
    if ($(".scroll-pane").length) {
        $.getScript('/assets/js/jquery.jscrollpane.min.js', function () {
            $('.scroll-pane').jScrollPane({ showArrows: true, verticalDragMinHeight: '9' });
        });
    }

    // passport agreement validation
    $("#PassportCardForm").validate({
        rules: {
            PassportAgrees: "required"
        },
        messages: {
            PassportAgrees: "You must agree to the terms and conditions"
        },
        errorElement: "div",
        wrapper: "div",  // a wrapper around the error message
        errorPlacement: function (error, element) {
            offset = element.offset();
            error.insertAfter('#error');
            error.addClass('message');  // add a class to the wrapper
        },
        submitHandler: function(form) {
            $.post($("#AcceptanceSubmitUrl").val(), function () {
                window.location.href = $(form).attr('action');
            });
        }
    });


});
