
var url_frame_exd  = new URL(`https://expodat.com/onlinereg/tekhnosmena?lang=ru`),
    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; 
					ym(103742589,'reachGoal','start_reg');				} 
			} else if ((h == -33) && (sendend == 0)){
				iframe.scrollIntoView(true);
				window.scrollBy(0, -h_menu); 
				ym(103742589,'reachGoal','registration'); 
				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);
