
var wrgsv = {
    url_onlinereg: `https://expodat.com/onlinereg/buybrand-expo-2025`,
    url_style: `https://expodat.com/expodat/appRegistration/assets/css/widget.css`,
    url_style_dop: ``,
    init: function() {
        this.addStyle();
        if(this.url_style_dop) this.addStyle(1);
        document.body.insertAdjacentHTML('beforeEnd', `
<div id="modal_expo_widg">
    <div class="modal-expo-widg-content">
        <div class="modal-expo-widg-close" onclick="wrgsv.closeModal()">
            <svg width="30" viewBox="0 0 32 32" xmlns="http://www.w3.org/2000/svg">
                <defs>
                    <style>
                        .cls-1{fill:none;stroke:#000;stroke-linecap:round;stroke-linejoin:round;stroke-width:2px;}
                    </style>
                </defs>
                <g id="cross">
                    <line class="cls-1" x1="7" x2="25" y1="7" y2="25"/>
                    <line class="cls-1" x1="7" x2="25" y1="25" y2="7"/>
                </g>
            </svg>
        </div>
        <div class="modal-expo-widg__body">
            <div id="expodat__frame_js" style="display: none;"></div>
        </div>
    </div>
</div>`);
        
var url_frame_exd  = new URL(`https://expodat.com/onlinereg/buybrand-expo-2025`),
    url_site       = new URL(window.location),
    params_list    = ['email', 'cdr', 'payed_promocode', 'visitor_status_id', 'no_confirm', 'utm_labels'],
    sendstart = 0, sendend = 0;

params_list.forEach(param => {
    if(url_site.searchParams.has(param)) {
        url_frame_exd.searchParams.set(param, url_site.searchParams.get(param));
    }
})
document.getElementById('expodat__frame_js').insertAdjacentHTML('beforebegin', `
    <iframe id="ExdFrm" src='${url_frame_exd.href}' name="ExdFrm" width="100%" height="237" frameborder="0" scrolling="no"></iframe>
`);


window.addEventListener('message', function (e) {
    var h_menu = 90, /** Size menu */
    h = e.data,
	iframe = document.getElementById("ExdFrm");
		
	if (!isNaN(h) && (e.origin !== 'https://mod.expodat.com')) {		
		
		if(h.hasOwnProperty("scroll")){
			iframe.scrollIntoView(true);
			window.scrollBy(0, (h.scroll - h_menu));
		} else if (!isNaN(h)){
			if ((h > 0)) {
				iframe.height = e.data;
				if ((h > 700) && (sendstart==0)){
					sendstart = 1; 
									} 
			} else if ((h == -33) && (sendend == 0)){
				iframe.scrollIntoView(true);
				window.scrollBy(0, -h_menu); 
				 
				sendend = 1; 
			}
		}
	}	
});

function YaClientID() {    
    var match = document.cookie.match('(?:^|;)\\s*_ym_uid=([^;]*)');
    return (match) ? decodeURIComponent(match[1]) : false;
}

function getUtmFromCookies() {
	const regex = /utm_exdt=([^;]+)/;
	const match = document.cookie.match(regex);
	return match?.length > 1 ? decodeURIComponent(match[1]) : "";
}

function isEmptyOrSpaces(str){
    return str === null || str.match(/^ *$/) !== null;
}
	
function GetUTM() {   
	utm = getUtmFromCookies();

	if(isEmptyOrSpaces(utm)){
		utm = window.location.search.replace('?', '');
	}	
	
	let domainName = window.location.hostname;
	const regex = /biot/i;
	if(regex.test(domainName)) {
		utm = utm +'&'+domainName;
	}	
	return (utm) ? utm : "";
}


//var name_roistat_cookie = 'roistat_visit',
//    roistat_visit = document.cookie.match(new RegExp("(?:^|; )" + name_roistat_cookie.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"));

setInterval(() => document.getElementById("ExdFrm").contentWindow.postMessage({
//    roistat_visit: roistat_visit ? decodeURIComponent(roistat_visit[1]) : 'nocookie',
    utm_labels   : GetUTM(),
	yandex_client_id: YaClientID()
}, '*'), 2000);
        
        document.getElementById('modal_expo_widg').addEventListener('click', (event) => {
            if(event.target.id == 'modal_expo_widg') this.closeModal();
        });
    },
    addStyle: function(dop = 0) { 
        style = document.createElement('link'); 
        style.rel = 'stylesheet'; 
        style.type = 'text/css'; 
        style.href = dop ? this.url_style_dop : this.url_style; 
        document.head.appendChild(style); 
    },
    closeModal: function() {
        var src = document.getElementById('ExdFrm').src,
            srcDef = src.split('?')[0];

        document.getElementById('ExdFrm').src = srcDef;
        document.body.classList.remove('modal_expo_widg__active');
    },
    regModal: function(status, dual_reg = null) {
        var reg = (dual_reg !== null) ? this.url_onlinereg.replace(`buybrand-expo-2025`, dual_reg) : this.url_onlinereg;
	
        document.getElementById('ExdFrm').src = `${reg}?lang=ru&visitor_status_id=${status}`;
        document.body.classList.add('modal_expo_widg__active');
    }
};
wrgsv.init();

function YaClientID() {    
    var match = document.cookie.match('(?:^|;)\\s*_ym_uid=([^;]*)');
    return (match) ? decodeURIComponent(match[1]) : false;
}

function getUtmFromCookies() {
	const regex = /utm_exdt=([^;]+)/;
	const match = document.cookie.match(regex);
	return match?.length > 1 ? decodeURIComponent(match[1]) : "";
}

function isEmptyOrSpaces(str){
    return str === null || str.match(/^ *$/) !== null;
}
	
function GetUTM() {   
	utm = getUtmFromCookies();

	if(isEmptyOrSpaces(utm)){
		utm = window.location.search.replace('?', '');
	}	
	
	let domainName = window.location.hostname;
	const regex = /biot/i;
	if(regex.test(domainName)) {
		utm = utm +'&'+domainName;
	}	
	return (utm) ? utm : "";
}


//var name_roistat_cookie = 'roistat_visit',
//    roistat_visit = document.cookie.match(new RegExp("(?:^|; )" + name_roistat_cookie.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"));

setInterval(() => document.getElementById("ExdFrm").contentWindow.postMessage({
//    roistat_visit: roistat_visit ? decodeURIComponent(roistat_visit[1]) : 'nocookie',
    utm_labels   : GetUTM(),
	yandex_client_id: YaClientID()
}, '*'), 2000);
