	var total = document.getElementById("total").value; sessionStorage.setItem("total", total);

function save_form(){
	var name = document.getElementById("name").value; sessionStorage.setItem("name", name);
	var email = document.getElementById("email").value; sessionStorage.setItem("email", email);
	var phone = document.getElementById("phone").value; sessionStorage.setItem("phone", phone);
	var state = document.getElementById("state").value; sessionStorage.setItem("state", state);
	var city = document.getElementById("city").value; sessionStorage.setItem("city", city);
	var postal_code = document.getElementById("code").value; sessionStorage.setItem("postal_code", postal_code);
	var address = document.getElementById("address").value; sessionStorage.setItem("address", address);
	var shipping_cost = document.getElementById("shipping_cost").value; sessionStorage.setItem("shipping_cost", shipping_cost);
	var total = document.getElementById("total").value; sessionStorage.setItem("total", total);
	var ip_address = document.getElementById("ip_address").value; sessionStorage.setItem("ip_address", ip_address);
	
}
// This is your test publishable API key.
const stripe = Stripe("pk_test_51KQHspEpSvSQJjk4z5Hans3nrV1AqDOiW0Wb3WMskMR5i9nUmJsFVTKgS4SOUP0rveYdmp5nl9ubLbtJzDRlpPIC00nexw05HL");

// The items the customer wants to buy
const items = [{ id: "Candy-Cleanse", amount: total*100 }];

let elements;

initialize();

document
  .querySelector("#payment-form")
  .addEventListener("submit", handleSubmit);

// Fetches a payment intent and captures the client secret
async function initialize() {
  const { clientSecret } = await fetch("ss/public/create.php", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ items }),
  }).then((r) => r.json());

  elements = stripe.elements({ clientSecret });

  const paymentElementOptions = {
    layout: "tabs",
  };

  const paymentElement = elements.create("payment", paymentElementOptions);
  paymentElement.mount("#payment-element");
}

async function handleSubmit(e) {
  e.preventDefault();
  setLoading(true);

  const { error } = await stripe.confirmPayment({
    elements,
    confirmParams: {
      // Make sure to change this to your payment completion page
      return_url: "https://webriders.co/demos/candy-weightloss/ss/public/complete.php",
    },
  });

  // This point will only be reached if there is an immediate error when
  // confirming the payment. Otherwise, your customer will be redirected to
  // your `return_url`. For some payment methods like iDEAL, your customer will
  // be redirected to an intermediate site first to authorize the payment, then
  // redirected to the `return_url`.
  if (error.type === "card_error" || error.type === "validation_error") {
    showMessage(error.message);
  } else {
    showMessage("An unexpected error occurred.");
  }

  setLoading(false);
}

// ------- UI helpers -------

function showMessage(messageText) {
  const messageContainer = document.querySelector("#payment-message");

  messageContainer.classList.remove("hidden");
  messageContainer.textContent = messageText;

  setTimeout(function () {
    messageContainer.classList.add("hidden");
    messageContainer.textContent = "";
  }, 4000);
}

// Show a spinner on payment submission
function setLoading(isLoading) {
  if (isLoading) {
    // Disable the button and show a spinner
    document.querySelector("#submit").disabled = true;
    document.querySelector("#spinner").classList.remove("hidden");
    document.querySelector("#button-text").classList.add("hidden");
  } else {
    document.querySelector("#submit").disabled = false;
    document.querySelector("#spinner").classList.add("hidden");
    document.querySelector("#button-text").classList.remove("hidden");
  }
}