function getCookie(cname) {
let name = cname + “=”;
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(‘;’);
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function setHiddenFormData(name, value) {
let selector = "input[name='" + name + "']";
let field = document.querySelector(selector);
if (!field) return;
if (!field.value) {
field.value = value;
}
}
function setUTMFormData(name) {
setHiddenFormData(name, getCookie(name));
}
// Default redirect URL and mapping for `numemployees` values
let redirectUrl = 'https://nilohealth.com/thank-you-page/';
const redirectUrls = {
'': 'https://nilohealth.com/thank-you-page/',
'0-50': 'https://nilohealth.com/thank-you-with-calendar-50/',
'50-100': 'https://nilohealth.com/thank-you-with-calendar-50/', // 51-200
'100-250': 'https://nilohealth.com/thank-you-with-calendar/', // 201-500
'250-1000': 'https://nilohealth.com/thank-you-with-calendar/', // 501-1000
'1000-5000': 'https://nilohealth.com/thank-you-with-calendar/', // 1001-5000
'5000-10000': 'https://nilohealth.com/thank-you-with-calendar-2/', // 5001-10000
'10000+': 'https://nilohealth.com/thank-you-with-calendar-2/' // 10000+
};
// Function to determine the correct redirect URL
function testNumemployees(numemployees) {
console.log("numemployees value:", numemployees);
redirectUrl = redirectUrls[numemployees] || 'https://nilohealth.com/thank-you-page/';
}
hbspt.forms.create({
region: "na1",
portalId: "8346198",
formId: "96bffe69-db53-4f75-84ed-4bfc0e36183b",
inlineMessage: 'Thank you!',
onFormReady: function($form) {
const formId = '96bffe69-db53-4f75-84ed-4bfc0e36183b';
let numemployeesFieldElement = document.querySelector(`#numemployees-${formId}`);
if (!numemployeesFieldElement) {
console.error("numemployees field not found!");
return;
}
// Set initial redirect URL based on field value
testNumemployees(numemployeesFieldElement.value);
// Update redirect URL when the field value changes
numemployeesFieldElement.addEventListener("change", function() {
testNumemployees(numemployeesFieldElement.value);
});
// Set UTM and custom data from cookies
setUTMFormData("utm_source");
setUTMFormData("utm_medium");
setUTMFormData("utm_campaign");
setUTMFormData("utm_content");
setUTMFormData("utm_term");
setHiddenFormData("acid", getCookie("_ga"));
let submitButton = document.querySelector(`#hsForm_${formId} input[type="submit"], #hsForm_${formId} button`);
if (submitButton) {
submitButton.id = "cta-contact"; // Unique ID for this contact page button
console.log("Submit button ID set:", submitButton.id);
} else {
console.error("Submit button not found!");
}
},
onFormSubmitted: function() {
console.log("Form submitted! Redirecting to:", redirectUrl);
window.location.href = redirectUrl;
}
});