function trial_modal(){
	var el = document.createElement('div');
	el.id='modal_div';
	el.style.cssText='background:rgba(0, 0, 0, 0.3); position:fixed; width:100%; height:100%; top:0px; left:0px; z-index:100;';
	el.innerHTML = '<iframe style="width:90%;max-width:500px;height:590px;background:#fff;display:block;border:0;margin:auto;margin-top:100px;box-shadow:0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);" scrolling="no" src="/app/trial/?start=trial&c='+encodeURIComponent(cookies_obj()['_ga'])+'&p='+encodeURIComponent(location.href)+'"></iframe>';
	document.body.appendChild(el);
	return false;
}

function modal_close(){
	document.body.style.overflow='unset';
    var modal = document.getElementById('modal_div');
    if (modal) { modal.parentNode.removeChild(modal); }
}

function callback_modal(){
	document.body.style.overflow='hidden';
	var el = document.createElement('div');
	el.id='modal_div';
	el.style.cssText = 'background:rgba(0, 0, 0, 0.3); position:fixed; width:100%; height:100%; top:0px; left:0px; z-index:100; opacity:0; transition: opacity 0.3s ease-in-out;';
	el.innerHTML = '<iframe style="width:90%;max-width:500px;height:590px;background:#fff;display:block;border:0;margin:auto;margin-top:100px;box-shadow:0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);" scrolling="no" src="/help/callback/?c='+encodeURIComponent(cookies_obj()['_ga'])+'&p='+encodeURIComponent(location.href)+'"></iframe>';
	document.body.appendChild(el);
	void el.offsetWidth;
    el.style.opacity = '1';
	return false;
}
function cookies_obj() {
	var cookies = {};
	document.cookie.split(';').forEach(function(el) {
		var splitCookie = el.split('=');
		var key = splitCookie[0].trim();
		var value = splitCookie[1];
		cookies[key] = value;
  	});
	return cookies;
}
function intToString(number) {
	const absNumber = Math.abs(number);
	const abbreviations = {
	    T: 1000000000000,
	    B: 1000000000,
	    M: 1000000,
	    K: 1000,
	  };

  	for (const key in abbreviations) {
			    if (absNumber >= abbreviations[key]) {
			      const roundedValue = number / abbreviations[key];
			      const decimalPlaces = roundedValue < 10 ? 1 : 0;
			      return roundedValue.toFixed(decimalPlaces) + key;
			    }
	}
  return number.toString();
}

window.addEventListener('DOMContentLoaded', function() {
	if (document.cookie.split('; ').some(cookie => cookie.startsWith('ea='))) {
		var script = document.createElement('script');
		script.src = "https://app.endole.co.uk/backend/redirect_open_to_app.php";
		document.head.appendChild(script);
	}
});


function get_first_visit(){
	var google_cookie = cookies_obj()['_ga'].split('.')[3];
	if(google_cookie){return google_cookie;}

	var first_visit = cookies_obj()['first_visit'];
	if(first_visit){ return first_visit; }

	var timestamp = Math.floor(Date.now() / 1000);
	var expiryDate = new Date();
	expiryDate.setFullYear(expiryDate.getFullYear() + 20);

	document.cookie = 'first_visit=' + timestamp + '; path=/; domain=.endole.co.uk; expires=' + expiryDate.toUTCString();
	return timestamp;
}

function get_session_time() {
    var session_time = localStorage.getItem('session_time');
    if (!session_time) { session_time = time(); localStorage.setItem('session_time', session_time); }
    return Number(session_time);
}

function time() {
    return Math.floor(Date.now() / 1000);
}