 (function () {
  tp = window["tp"] || [];

  /* Checkout related */
  /**
     * Event properties
     *
     * chargeAmount - amount of purchase
     * chargeCurrency
     * uid
     * email
     * expires
     * rid
     * startedAt
     * termConversionId
     * termId
     * promotionId
     * token_list
     * cookie_domain
     * user_token
     *
     */
  function onCheckoutComplete(data) {
  }

  function onCheckoutExternalEvent() {
  }

  function onCheckoutClose(event) {
    /* Default behavior is to refresh the page on successful checkout */
    if (event && event.state == "checkoutCompleted") {
      location.reload();
    }
  }

  function onCheckoutCancel() {
  }

  function onCheckoutError() {
  }

  function onCheckoutSubmitPayment() {
  }

  /* Meter callback */
  function onMeterExpired() {

  }

  /* Meter callback */
  function onMeterActive() {

  }

   /* Callback executed when a user must login */
function onLoginRequired(params) {
	//Check if user is logged in, skip if true
  if(tp.pianoId.isUserValid())
    return;

  var customCookies = tp.util.getCustomCookies();

  //Check if cookie is set
  if(!customCookies.hasOwnProperty("_pc_usUser")) {
    console.error("Custom cookie not set");
    //Some fallback should be set here, maybe call tp.pianoId.show({screen : screen}) ?
    return;
  }

  var isUserUS = (customCookies["_pc_usUser"] === "true");
  console.log("Data type of isUserUS", typeof(isUserUS));
  var usRegistration = "national";  
  var xRegistration = "international"; 
  tp.template.close(); // Closing any defulat login template to make sure the custom flow works
  tp.pianoId.show({
    screen: "register",
    stage: isUserUS ? usRegistration : xRegistration,
	// When the user succesfully logs in, the loggedIn callback will run
   loggedIn: function(data){
    tp.template.close();
	tp.offer.startCheckout(params);
  }
  }); 
  
  /*function onLoginSuccess() {
    tp.template.close();
     tp.offer.startCheckout(params);
  }*/

}

  /* Callback executed after a tinypassAccounts login */
  function onLoginSuccess() {
    console.log("Login success called!");
  }

  /* Callback executed after an experience executed successfully */
  function onExperienceExecute(event) {
  }

  /* Callback executed if experience execution has been failed */
  function onExperienceExecutionFailed(event) {
  }

  /* Callback executed if external checkout has been completed successfully */
  function onExternalCheckoutComplete(event) {
    /* Default behavior is to refresh the page on successful checkout */
    location.reload();
  }

  tp.push(["setAid", 'zUkajvWJ04']);
  tp.push(["setCxenseSiteId", "1141973672851684435"]);
  tp.push(["setEndpoint", 'https://vx.golfdigest.com']);
  tp.push(['setComposerHost', 'https://c2.golfdigest.com']); 
  tp.push(["setUseTinypassAccounts", false ]);
  tp.push(["setUsePianoIdUserProvider", true ]);
  tp.push(['setPianoIdUrl', 'https://id.golfdigest.com']);
  

  /* checkout related events */
  tp.push(["addHandler", "checkoutComplete", onCheckoutComplete]);
  tp.push(["addHandler", "checkoutClose", onCheckoutClose]);
  tp.push(["addHandler", "checkoutCustomEvent", onCheckoutExternalEvent]);
  tp.push(["addHandler", "checkoutCancel", onCheckoutCancel]);
  tp.push(["addHandler", "checkoutError", onCheckoutError]);
  tp.push(["addHandler", "checkoutSubmitPayment", onCheckoutSubmitPayment]);

  /* user login events */
  tp.push(["addHandler", "loginRequired", onLoginRequired]);
  tp.push(["addHandler", "loginSuccess", onLoginSuccess]);

  /* meter related */
  tp.push(["addHandler", "meterExpired", onMeterExpired]);
  tp.push(["addHandler", "meterActive", onMeterActive]);

  tp.push(["addHandler", "experienceExecute", onExperienceExecute]);
  tp.push(["addHandler", "experienceExecutionFailed", onExperienceExecutionFailed]);

  /* external checkout related events */
  tp.push(["addHandler", "externalCheckoutComplete", onExternalCheckoutComplete]);

  tp.push(["init", function () {
    tp.pianoId.init();
    tp.experience.init();
  }]);
  
//Cookie JS
  window.showPianoId = function( screen ) {
  //Check if piano is initialized
  if(!tp || !tp.pianoId || !tp.pianoId.isConfigured()) {
    console.error("Piano ID not initialized")
    return;
  }

  //Check if user is logged in, skip if true
  if(tp.pianoId.isUserValid())
    return;

  var customCookies = tp.util.getCustomCookies();

  //Check if cookie is set
  if(!customCookies.hasOwnProperty("_pc_usUser")) {
    console.error("Custom cookie not set");
    //Some fallback should be set here, maybe call tp.pianoId.show({screen : screen}) ?
    return;
  }

  var isUserUS = customCookies["_pc_usUser"];
  var usTemplate = "OTP3G952TFDX";   //Replace with US registration form ID
  var xTemplate = "OTTOVY8BUPZB";   //Replace with international form ID

  tp.pianoId.show({
    screen: "register",
    templateId: isUserUS ? usTemplate : xTemplate
  }); 
}
 
  

})();




  
  // do not change this section
  // |BEGIN INCLUDE TINYPASS JS|
  (function(src){var a=document.createElement("script");a.type="text/javascript";a.async=true;a.src=src;var b=document.getElementsByTagName("script")[0];b.parentNode.insertBefore(a,b)})("https://cdn.tinypass.com/api/tinypass.min.js");
// |END   INCLUDE TINYPASS JS|

