/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */


$("#add_child_row").click(function () {
    $.get("parts/template_child_entry.php", {}, function (resp) {
        $("#child_list").append(resp);
    });
});

// Create a Stripe client.
var stripe = Stripe(stripe_pubkey);

// Create an instance of Elements.
var elements = stripe.elements();

// Create an instance of the card Element.
var card = elements.create('card');

// Add an instance of the card Element into the `card-element` <div>.
card.mount('#card-element');

card.addEventListener('change', function (event) {
    if (event.error) {
        $("#card-errors").removeClass("d-none");
        $("#card-errors").text(event.error.message);
    } else {
        $("#card-errors").addClass("d-none");
        $("#card-errors").text("");
    }
});

$("#membershipform").on("submit", function (event) {
    event.preventDefault();

    stripe.createToken(card).then(function (result) {
        if (result.error) {
            // Inform the customer that there was an error.
            $("#card-errors").removeClass("d-none");
            $("#card-errors").text(event.error.message);
        } else {
            $("#stripe-token").val(result.token.id);
            console.log(result.token);
            document.getElementById('membershipform').submit();
        }
    });
});