<!DOCTYPE html><html><head><meta charset="utf-8"><title>Request License</title><style>body{font-family:Segoe UI,system-ui,sans-serif;font-size:14px;color:#242424;margin:0;padding:20px 24px}h1{font-size:18px;font-weight:600;margin:0 0 6px}p{margin:0 0 14px;color:#424242}table{border-collapse:collapse;margin:6px 0 14px}td{padding:4px 14px 4px 0;vertical-align:top}td:first-child{color:#616161;white-space:nowrap}td code{font-family:Consolas,monospace;user-select:all;word-break:break-all}label{display:block;color:#616161;margin:8px 0 3px}input[type=text],input[type=email]{font:inherit;width:100%;box-sizing:border-box;padding:5px 8px;border:1px solid #8a8886;border-radius:4px}input:focus{outline:2px solid #0f6cbd;outline-offset:-1px;border-color:#0f6cbd}.msg{margin:12px 0;padding:8px 10px;border-radius:4px;display:none}.msg.err{display:block;background:#fde7e9;color:#a4262c}.msg.ok{display:block;background:#dff6dd;color:#107c10}.buttons{margin-top:18px;display:flex;gap:8px}button{font:inherit;padding:6px 18px;border-radius:4px;border:1px solid #8a8886;background:#fff;cursor:pointer}button.primary{background:#0f6cbd;border-color:#0f6cbd;color:#fff}button:disabled{opacity:.5;cursor:default}</style></head><body><h1>Request a license</h1><p id="intro">Buy a license for this organization online, or start a free trial. Either way the license file is emailed to you.</p><table><tr><td>Application</td><td id="app"></td></tr><tr><td>Organization ID</td><td><code id="org">…</code></td></tr><tr><td>Organization URL</td><td><code id="url"></code></td></tr></table><label for="customer">Customer (your company)</label><input type="text" id="customer" maxlength="200" autocomplete="organization"><label for="mail">Email address</label><input type="email" id="mail" maxlength="200" autocomplete="email"><div class="msg" id="msg"></div><div class="buttons"><button class="primary" id="buy" disabled hidden>Buy online</button><button id="trial" disabled hidden>Request Trial</button><button id="copy" disabled>Copy</button><button id="close">Close</button></div><script>
(function () {
  // Where Request Trial sends requests, by Application Name. An app without an entry has no Request Trial button.
  var TRIAL_URLS = { "bpf studio": "https://bpfstudio-licensestore.azurewebsites.net/api/trial-request" };
  // Where Buy online goes, by Application Name. An app without an entry has no Buy online button.
  var STORE_URLS = { "bpf studio": "https://bpfstudio.app/" };
  var $ = function (id) { return document.getElementById(id); };
  var parentXrm = window.parent && window.parent.Xrm;
  var base = (function () {
    try { if (parentXrm && parentXrm.Utility) return parentXrm.Utility.getGlobalContext().getClientUrl().replace(/\/$/, ""); } catch (e) { }
    return location.origin;
  })();
  var app = (function () {  // navigateTo passes its data string as ?data=...
    var data = new URLSearchParams(location.search).get("data") || "";
    var m = /(?:^|&)app=([^&]*)/.exec(data);
    return (m && decodeURIComponent(m[1]).trim()) || "BPF Studio";
  })();
  var org = "";
  var store = STORE_URLS[app.toLowerCase()], trialUrl = TRIAL_URLS[app.toLowerCase()];
  if (store) $("buy").hidden = false;
  if (trialUrl) $("trial").hidden = false;
  if (!store && !trialUrl) $("intro").textContent = "Send these details to the publisher to get a license for this organization.";
  else if (!trialUrl) $("intro").textContent = "Buy a license for this organization online. The license file is emailed to you.";
  else if (!store) { $("intro").textContent = "Start a free trial for this organization. The license file is emailed to you."; $("trial").className = "primary"; }
  $("app").textContent = app;
  $("url").textContent = base;
  function show(text, ok) { var m = $("msg"); m.textContent = text; m.className = "msg " + (ok ? "ok" : "err"); }

  fetch(base + "/api/data/v9.2/WhoAmI", { credentials: "same-origin", headers: { "Accept": "application/json" } })
    .then(function (r) { if (!r.ok) throw new Error("HTTP " + r.status); return r.json(); })
    .then(function (w) {
      org = w.OrganizationId; $("org").textContent = org;
      $("copy").disabled = $("buy").disabled = false;
      updateTrial();
      // prefill the user's email (editable)
      get("systemusers(" + w.UserId + ")?$select=internalemailaddress").then(function (u) {
        if (u && u.internalemailaddress && !$("mail").value) { $("mail").value = u.internalemailaddress; updateTrial(); }
      });
    }, function () { $("org").textContent = "(couldn't read it)"; show("Couldn't read this organization's ID. It's also under Settings > Developer resources."); });

  function get(path) {
    return fetch(base + "/api/data/v9.2/" + path, { credentials: "same-origin", headers: { "Accept": "application/json" } })
      .then(function (r) { return r.ok ? r.json() : null; }, function () { return null; });
  }
  function email() { return $("mail").value.trim(); }
  function customer() { return $("customer").value.trim(); }
  var requesting = false;
  // Request Trial is available only once the organization ID is known and Customer and Email are filled in.
  function updateTrial() { $("trial").disabled = requesting || !org || !customer() || !email(); }
  $("customer").addEventListener("input", updateTrial);
  $("mail").addEventListener("input", updateTrial);

  function details() {
    return (customer() ? "Customer: " + customer() + "\n" : "") + (email() ? "Email: " + email() + "\n" : "")
      + "Application: " + app + "\nOrganization ID: " + org + "\nOrganization URL: " + base;
  }

  $("copy").addEventListener("click", function () {
    var text = details();
    var done = function () { show("Copied.", true); };
    if (navigator.clipboard && navigator.clipboard.writeText) {
      navigator.clipboard.writeText(text).then(done, function () { fallbackCopy(text) ? done() : show("Couldn't copy. Select the details and copy them by hand."); });
    } else if (fallbackCopy(text)) done();
    else show("Couldn't copy. Select the details and copy them by hand.");
  });
  function fallbackCopy(text) {
    var t = document.createElement("textarea");
    t.value = text; t.style.position = "fixed"; t.style.opacity = "0";
    document.body.appendChild(t); t.select();
    var ok = false;
    try { ok = document.execCommand("copy"); } catch (e) { }
    document.body.removeChild(t);
    return ok;
  }

  $("buy").addEventListener("click", function () {
    var url = store + "?org=" + encodeURIComponent(org) + (email() ? "&email=" + encodeURIComponent(email()) : "") + "#pricing";
    try {
      if (parentXrm && parentXrm.Navigation) parentXrm.Navigation.openUrl(url, { width: 1100, height: 800 });
      else window.open(url, "_blank", "noopener");
      show("The store opened in a new window. Your license will be emailed to you after payment; add it here with Add License.", true);
    } catch (e) { show("Couldn't open the store. Go to " + store + " and use this organization's ID."); }
  });

  $("trial").addEventListener("click", function () {
    if (!customer()) { show("Enter your company name."); $("customer").focus(); return; }
    if (!$("mail").checkValidity() || !/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email())) {
      show("Enter a valid email address. The trial license is sent there."); $("mail").focus(); return;
    }
    requesting = true; updateTrial();
    show("Requesting your trial…", true);
    fetch(trialUrl, {
      method: "POST", headers: { "Content-Type": "text/plain" },   // a "simple" request: no CORS preflight
      body: JSON.stringify({ application: app, organizationId: org, organizationUrl: base, customer: customer(), email: email() })
    }).then(function (r) {
      return r.json().then(function (j) { return j; }, function () { return { ok: false, message: "The license service answered " + r.status + "." }; });
    }).then(function (j) {
      show(j.message || (j.ok ? "Your trial license is on its way." : "The trial couldn't be issued."), !!j.ok);
      if (!j.ok) { requesting = false; updateTrial(); }
    }, function () {
      show("Couldn't reach the license service. Check your connection and try again.");
      requesting = false; updateTrial();
    });
  });

  $("close").addEventListener("click", function () {
    var p = window.parent && window.parent.document;
    var x = p && (p.querySelector('[data-id="dialogCloseIconButton"]') || p.querySelector('button[aria-label="Close"]'));
    if (x) x.click(); else window.close();
  });
})();
</script></body></html>