document.addEventListener("DOMContentLoaded", function() {
    var locationGet = window.location.search.substr(1);
    var r_done = locationGet.indexOf('r_done=1') !== -1 ? true : false;

	var body = document.getElementsByTagName('body')[0],
	iframeSrc    = 'https://letsgetrusty.kartra.com/page_embed/GWh4' + (r_done ? '?r_done=1' : ''),
	appDomainSrc = 'kartra.com';

	var iframe = document.createElement('iframe');
	iframe.setAttribute('src', iframeSrc);
	iframe.setAttribute('style', 'width:100%;height:100%;position:absolute; top:0px; left:0px; border:none;');
	iframe.setAttribute('scrolling', 'yes');
	iframe.setAttribute('allowfullscreen', 'yes');

	var head = document.getElementsByTagName('head')[0];

	var viewport = document.createElement('meta');
	viewport.setAttribute('name', 'viewport');
	viewport.setAttribute('content', 'width=device-width, initial-scale=1.0');
	head.appendChild(viewport);

	window.addEventListener('message', function(event) {
	    if (event.data.error) {
            switch (event.data.error) {
                case 'no_visitor_cookie':

                	if (!r_done) {
                        window.location.replace('https://app.' + appDomainSrc +
                            '/front/domain_validation?step=1&domain=' + appDomainSrc +
                            '&url=' + encodeURIComponent(window.location.href));
                        return;
					}
            }
        }

		if (event.data.title) {
			var title = document.getElementsByTagName('title');

			if (title.length > 0) {
				title[0].innerHTML = event.data.title;
			} else {
				title = document.createElement('title');
				title.innerHTML = event.data.title;
				head.appendChild(title);
			}
		}

		if (event.data.description) {
			var description = document.getElementsByName('description');

			if (description.length > 0) {
				description[0].content = event.data.description;
			} else {
				description = document.createElement('meta');
				description.setAttribute('name', 'description');
				description.setAttribute('content', event.data.description);
				head.appendChild(description);
			}
		}

		if (event.data.keywords) {
			var keywords = document.getElementsByName('keywords');

			if (keywords.length > 0) {
				keywords[0].content = event.data.keywords;
			} else {
				keywords = document.createElement('meta');
				keywords.setAttribute('name', 'keywords');
				keywords.setAttribute('content', event.data.keywords);
				head.appendChild(keywords);
			}
		}

		if (event.data.robots) {
			var robots = document.getElementsByName('robots');

			if (robots.length > 0) {
				robots[0].content = event.data.robots;
			} else {
				robots = document.createElement('meta');
				robots.setAttribute('name', 'robots');
				robots.setAttribute('content', event.data.robots);
				head.appendChild(robots);
			}
		}

		if (event.data.domain) {
			var favicon = document.createElement('link');
			favicon.setAttribute('rel', 'shortcut icon');
			favicon.setAttribute('href', 'https://kartrausers.s3.amazonaws.com/letsgetrusty/28523189_62998e8e10c09_LGR_YouTube_Logo.png');
			head.appendChild(favicon);
		}

		if (event.data.redirectUrl) {
			if (event.data.newTab && 'true' === event.data.newTab) {
				window.open(event.data.redirectUrl)
			} else {
				window.location = event.data.redirectUrl;
			}
		}
	}, false);

	body.appendChild(iframe);
});