function build_iframe() {
	var data = 'action=get';
	var path = 'http://' + location.hostname + '/systems/campaign/session.php';

	// Get utm_codes from the php session
	$.ajax({
		url: path,
		data: data,
		cache: false,
		async: false,
		success: function(json) {
			var js_params = get_js_params();
			var utm_codes = eval('(' + json + ')');
			var utm_string = '';
			
			if(utm_codes['utm_campaign']) {
				utm_string = '&utm_campaign=' + utm_codes['utm_campaign'];
				
				if(utm_codes['utm_medium']) utm_string += '&utm_medium=' + utm_codes['utm_medium'];
				if(utm_codes['utm_source']) utm_string += '&utm_source=' + utm_codes['utm_source'];
				if(utm_codes['utm_content']) utm_string += '&utm_content=' + utm_codes['utm_content'];
				if(utm_codes['utm_term']) utm_string += '&utm_term=' + utm_codes['utm_term'];
				
				utm_string += '&website=' + window.location.hostname.replace('www.', '');
				utm_string += '&pagina=' + document.location.pathname;
			}
			
			var iframe = '<iframe src="http://messagent.dminterface.nl/optiext/optiextension.dll?ID={hash}' + utm_string + '" id="nieuwsbrief" name="nieuwsbrief" width="{width}" height="{height}" scrolling="no" frameborder="0" marginheight="0" marginwidth="0" ></iframe>';
				iframe = iframe.replace('{width}', ((js_params['width']) ? js_params['width'] : ''));
				iframe = iframe.replace('{height}', ((js_params['height']) ? js_params['height'] : ''));
				iframe = iframe.replace('{hash}', js_params['hash']);
			document.write(iframe);
		}
	});
}

function get_js_params() {
	var query = Array();

	$('script').each(function() {
		if(/email.js/.test($(this).attr('src'))) {
			var params = $(this).attr('src').split('?')[1];
			
			if(!params) return false;
			
			params = params.split('&');

			for(var i in params) {
				params[i] = params[i].split('=');
				query[params[i][0]] = params[i][1];
			}
			
			return false; // aka break for a jQuery each loop
		}
	})

	return (query) ? query : false;
}

build_iframe();