function initForm(config) {
	var route = config.route;
	var formId = config.formId;

	var activeBlock = document.getElementById(formId);
	initSignupBlock(activeBlock);

	document.addEventListener("DOMContentLoaded", function() {
		sendHeightPostMessage();
	});

	window.addEventListener("resize", function() {
		sendHeightPostMessage();
	});

	var sendHeightPostMessage = function() {
		var body = document.body;
		var html = document.documentElement;
		var height = Math.max(body.offsetHeight, html.scrollHeight, html.offsetHeight);
		var heightMessage = JSON.stringify({
			action: "iframeHeight",
			height: height
		});

		window.parent.postMessage(heightMessage, "*");
	};

	function getParameterByName(name, url) {
		if (!url) {
			url = window.location.href;
		}

		name = name.replace(/[\[\]]/g, "\\$&");
		var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)");
		var results = regex.exec(url);

		if (!results) {
			return null;
		}

		if (!results[2]) {
			return "";
		}

		return decodeURIComponent(results[2].replace(/\+/g, " "));
	}



	function initSignupBlock(signupBlock) {

		//var route  = "/wp-content/themes/edm-blog-v2/subscriber.php";
		var email  = signupBlock.getElementsByClassName("newsletterEmail")[0];
		var resBox = signupBlock.getElementsByClassName("subscribe-box-result")[0];

		signupBlock.getElementsByClassName("subscribe-submit")[0].addEventListener("click", validate);

		function signup() {
			var mail = email.value;
			var url = (window.location != window.parent.location) ? document.referrer: document.location.href;

			var trackLocation = {
				parentWindowUrl: url,
				page: getParameterByName("page"),
				location: getParameterByName("location")
			};

			var identifyMessage = JSON.stringify({
				action: "identify",
				extUserId: mail,
				extFlag: trackLocation.parentWindowUrl
			});

			parent.postMessage(identifyMessage, "*");

			// tracker.identify(mail, trackLocation.parentWindowUrl);

			var xhr = new XMLHttpRequest();
			xhr.open("POST", route, true);
			xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

			var list = "beta";
			if (getParameterByName("page") === "subscribe-newsletter") {
				list = "newsletter";
			}

			resBox.innerHTML = "Subscribing...";
			xhr.send("email=" + encodeURIComponent(mail) + "&list=" + list);

			var eventMessage = JSON.stringify({
				action: "event",
				eventName: list + "-" + "signup-started",
				properties: trackLocation
			});

			parent.postMessage(eventMessage, "*");
			// tracker.track((list + "-" + "signup-started"), trackLocation);

			xhr.onload = function() {
				if (xhr.status === 200) {
					signupBlock.classList.remove("error");
					signupBlock.classList.add("success");
					resBox.innerHTML = "Thank you for being interested. Please check your inbox and confirm your subscription.";

					var eventMessage = JSON.stringify({
						action: "event",
						eventName: list + "-" + "signup-completed",
						properties: {
							gaProperties: {
								location: trackLocation.location,
								eventCategory: trackLocation.location
							}
						}
					});

					parent.postMessage(eventMessage, "*");
					// tracker.track((list + "-" + "signup-completed"));
				} else {
					signupBlock.classList.add("error");
					resBox.innerHTML = "Subscription failed on server side";

					var eventMessage = JSON.stringify({
						action: "event",
						eventName: list + "-" + "signup-errored"
					});

					parent.postMessage(eventMessage, "*");

					// tracker.track((list + "-" + "signup-errored"));
				}
			};

		}

		function validate() {
			var re = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i;
			var mail = email.value;

			if (mail && re.test(mail)) {
				signup();
			} else {
				signupBlock.classList.add("error");
				resBox.innerHTML = "Please check your email again!";

				var eventMessage = JSON.stringify({
					action: "event",
					eventName: "signup-incorrect-mail"
				});

				parent.postMessage(eventMessage, "*");
				// tracker.track("signup-incorrect-mail");
			}
		}
	}
}