setTimeout( function() { initChatbot(); }, 0 ); var cbConfig = { "desktop": { "maxHeight": "330px", "minHeight": "50px", "maxWidth": "550px", "minWidth": "300px", "right": "10px" }, "mobile": { "maxHeight": "100%", "minHeight": "50px", "maxWidth": "100%", "minWidth": "300px", "right": "0px" }, "toggleButtonColor": "#474a50" }; function initChatbot() { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { if (!this.responseText) { return false; } var response = JSON.parse(this.responseText); factfinderCbToken = response.cbtoken; cbDisplay = response.cbdisplay; adviserName = response.adviser_name; adviserFacepath = response.adviser_facepath; adviserJobTitle = response.adviser_jobtitle; companyName = response.company_name; content = response.content; if (factfinderCbToken) { if (cbDisplay == 1) { var url = adviserAiUrl + '/factfinder/cb/' + factfinderCbToken; setDimensions(); document.body.insertAdjacentHTML( 'beforeend', '
'); var cbStyle = getChatBotExit().style; cbStyle.position = 'fixed'; cbStyle.display = 'none'; cbStyle.bottom = '0vh'; cbStyle.top = '0vh'; cbStyle.right = '0vw'; cbStyle.width = '100vw'; cbStyle.height = '100vh'; if (!isMobile()) { setTimeout(() => { function addEvent(obj, evt, fn) { if (obj.addEventListener) { obj.addEventListener(evt, fn, false); } else if (obj.attachEvent) { obj.attachEvent("on" + evt, fn); } } addEvent(document, 'mouseout', function(evt) { if (evt.toElement == null && evt.relatedTarget == null ) { openExitModal(); }; }); }, 2000); } } else if (cbDisplay == 2) { cbConfig = { "desktop": { "maxHeight": "650px", "minHeight": "100px", "maxWidth": "350px", "minWidth": "300px", "right": "10px" }, "mobile": { "maxHeight": "550px", "minHeight": "100px", "maxWidth": "100%", "minWidth": "300px", "right": "0px" }, "toggleButtonColor": "#7ec3b4" }; var url = adviserAiUrl + '/factfinder/cb/' + factfinderCbToken; setDimensions(); if (!isMobile()) { document.body.insertAdjacentHTML( 'beforeend', '
'); } if (isMobile()) { document.body.insertAdjacentHTML( 'beforeend', '
'); } var cbStyle = getChatBot().style; cbStyle.position = 'fixed'; cbStyle.bottom = '0px'; cbStyle.right = cbConfig.right; cbStyle.width = cbConfig.maxWidth; cbStyle.height = cbConfig.maxHeight; } else { var url = adviserAiUrl + '/factfinder/cb/' + factfinderCbToken; setDimensions(); if (!isMobile()) { document.body.insertAdjacentHTML( 'beforeend', '
'); } if (isMobile()) { document.body.insertAdjacentHTML( 'beforeend', '
'); } var cbStyle = getChatBot().style; cbStyle.position = 'fixed'; cbStyle.bottom = '0px'; cbStyle.right = cbConfig.right; cbStyle.width = cbConfig.maxWidth; cbStyle.height = cbConfig.maxHeight; } } }; } if (typeof chatbotCbtoken == 'undefined') { chatbotCbtoken = ''; } if (typeof chatbotReference == 'undefined') { chatbotReference = ''; } if (typeof chatbotForename == 'undefined') { chatbotForename = ''; } if (typeof chatbotSurname == 'undefined') { chatbotSurname = ''; } if (typeof chatbotEmail == 'undefined') { chatbotEmail = ''; } if (typeof chatbotSms == 'undefined') { chatbotSms = ''; } if (typeof chatbotPostcode == 'undefined') { chatbotPostcode = ''; } if (typeof chatbotUserIdentifier == 'undefined') { chatbotUserIdentifier = ''; } if (typeof chatbotPostcode == 'undefined') { chatbotPostcode = ''; } if (typeof chatbotMediaCampaignID == 'undefined') { chatbotMediaCampaignID = ''; } if (typeof chatbotChannelId == 'undefined') { chatbotChannelId = ''; } if (typeof chatbotAddress1 == 'undefined') { chatbotAddress1 = ''; } if (typeof chatbotAddress2 == 'undefined') { chatbotAddress2 = ''; } if (typeof chatbotIpAddress == 'undefined') { chatbotIpAddress = ''; } if (typeof chatbotLeadTypeId == 'undefined') { chatbotLeadTypeId = ''; } if (typeof chatbotMarketingPermission == 'undefined') { chatbotMarketingPermission = ''; } if (typeof chatbotSupplierId == 'undefined') { chatbotSupplierId = ''; } if (typeof chatbotSupplierKey == 'undefined') { chatbotSupplierKey = ''; } if (typeof chatbotTown == 'undefined') { chatbotTown = ''; } if (typeof chatbotSmoker == 'undefined') { chatbotSmoker = ''; } if (typeof chatbotCoveramount == 'undefined') { chatbotCoveramount = ''; } if (typeof chatbotCoverterm == 'undefined') { chatbotCoverterm = ''; } if (typeof chatbotCoverType == 'undefined') { chatbotCoverType = ''; } if (typeof chatbotPolicyType == 'undefined') { chatbotPolicyType = ''; } if (typeof chatbotGender == 'undefined') { chatbotGender = ''; } if (typeof chatbotDateOfBirth == 'undefined') { chatbotDateOfBirth = ''; } if (typeof chatbotWebsiteId == 'undefined') { chatbotWebsiteId = ''; } xmlhttp.open('GET', adviserAiUrl + '/chatbot/' + chatbotCreativeIdentifier + '?cbtoken=' + chatbotCbtoken + '&reference=' + chatbotReference + '&forename=' + chatbotForename + '&surname=' + chatbotSurname + '&email=' + chatbotEmail + '&sms=' + chatbotSms + '&postcode=' + chatbotPostcode + '&ar='+chatbotUserIdentifier+'&MediaCampaignID='+chatbotMediaCampaignID+'&IpAddress='+chatbotIpAddress+'&ChannelId='+chatbotChannelId+'&DateOfBirth='+chatbotDateOfBirth+'&WebsiteId='+chatbotWebsiteId+'&LeadTypeId='+chatbotLeadTypeId+'&MarketingPermission='+chatbotMarketingPermission+'&SupplierId='+chatbotSupplierId+'&SupplierKey='+chatbotSupplierKey+'&town='+chatbotTown+'&Smoker='+ chatbotSmoker +'&coveramount='+ chatbotCoveramount +'&coverterm='+ chatbotCoverterm +'&coverType='+ chatbotCoverType +'&policyType='+ chatbotPolicyType +'&Gender='+ chatbotGender); xmlhttp.send(); } function getChatBot() { return document.getElementById('adviserchatbot'); } function getChatBotExit() { return document.getElementById('adviserchatbotexit'); } function openExitModal() { var chatbot = getChatBotExit(); var cbData = chatbot.dataset; var toggle = getExitToggleState(); var viewed = getExitViewedState(); var body = document.body; if (viewed == 'no') { switch (toggle) { case 'close': cbData.toggle = 'open'; chatbot.style.display = "block"; body.style.overflow = "hidden"; break; } } } function closeExitModal() { var chatbot = getChatBotExit(); var cbData = chatbot.dataset; var toggle = getExitToggleState(); var viewed = getExitViewedState(); var body = document.body; switch (toggle) { case 'open': cbData.toggle = 'close'; cbData.viewed = 'yes'; chatbot.style.display = "none"; body.style.overflow = "auto"; break; } } function toggleChatBot() { var chatbot = getChatBot(); var cbStyle = chatbot.style; var cbData = chatbot.dataset; var toggle = getToggleState(); var toggleBtnStyle = document.getElementById('toggle_chat').style; switch (toggle) { case 'close': cbStyle.height = cbConfig.maxHeight; cbStyle.width = cbConfig.maxWidth; toggleBtnStyle.width = "50px"; toggleBtnStyle.color = cbConfig.toggleButtonColor; cbStyle.right = cbConfig.right; cbData.toggle = 'open'; break; case 'open': cbStyle.height = cbConfig.minHeight; cbStyle.width = cbConfig.minWidth; toggleBtnStyle.width = "100%"; toggleBtnStyle.color = "transparent"; if(isMobile()) { cbStyle.right = 'calc(50% - 150px)'; } else { cbStyle.right = cbConfig.right; } cbData.toggle = 'close'; break; } } function getToggleState() { return getChatBot().dataset.toggle; } function getExitToggleState() { return getChatBotExit().dataset.toggle; } function getExitViewedState() { return getChatBotExit().dataset.viewed; } function isMobile() { var width = Math.max(document.documentElement.clientWidth, window.innerWidth || 0); if(width < 768) { return true; } return false; } function setDimensions() { if(isMobile()) { maxHeight = cbConfig.mobile.maxHeight; minHeight = cbConfig.mobile.minHeight; maxWidth = cbConfig.mobile.maxWidth; minWidth = cbConfig.mobile.minWidth; right = cbConfig.mobile.right; } else { maxHeight = cbConfig.desktop.maxHeight; minHeight = cbConfig.desktop.minHeight; maxWidth = cbConfig.desktop.maxWidth; minWidth = cbConfig.desktop.minWidth; right = cbConfig.desktop.right; } cbConfig.maxHeight = maxHeight; cbConfig.minHeight = minHeight; cbConfig.maxWidth = maxWidth; cbConfig.minWidth = minWidth; cbConfig.right = right; } window.addEventListener("resize", function(){ setDimensions(); var chatbot = getChatBot(); if (!chatbot) { return false; } var cbStyle = chatbot.style; switch(getToggleState()) { case 'close': chatbotHeight = cbConfig.minHeight; chatbotWidth = cbConfig.minWidth; if(isMobile()) { cbStyle.right = 'calc(50% - 150px)'; } else { cbStyle.right = cbConfig.right; } break; case 'open': chatbotHeight = cbConfig.maxHeight; chatbotWidth = cbConfig.maxWidth; cbStyle.right = cbConfig.right; break; } cbStyle.height = chatbotHeight; cbStyle.width = chatbotWidth; });