window.onerror = _emEorrHandler;
function _emEorrHandler() {
}

 
function _em_clientWidth() {
	var myWidth = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myWidth = window.innerWidth;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth;
	  }
	  return myWidth;

}
function _em_clientHeight() {
	var  myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    myHeight = document.body.clientHeight;
	  }
	  return myHeight;

}
 
 

function _em_get_host() {
	var schema = location.protocol.indexOf('https') > -1 ? 'https:' : 'http:';
	return schema + '//www.effectivemeasure.net';
}

function _em_get_cdn_host() {
	var schema = location.protocol.indexOf('https') > -1 ? 'https:' : 'http:';
	return schema + '//www-cdn.effectivemeasure.net';
}

function _em_get_sv_host() {
	var schema = location.protocol.indexOf('https') > -1 ? 'https:' : 'http:';
	return schema + '//survey.effectivemeasure.net';
}

function _em_encode64(inp) {
	var key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	var chr1, chr2, chr3, enc3, enc4, i = 0, out = "";
	while (i < inp.length) {
		chr1 = inp.charCodeAt(i++);
		if (chr1 > 127) {
			chr1 = 88;
		}
		chr2 = inp.charCodeAt(i++);
		if (chr2 > 127) {
			chr2 = 88;
		}
		chr3 = inp.charCodeAt(i++);
		if (chr3 > 127) {
			chr3 = 88;
		}
		if (isNaN(chr3)) {
			enc4 = 64;
			chr3 = 0;
		} else {
			enc4 = chr3 & 63;
		}
		if (isNaN(chr2)) {
			enc3 = 64;
			chr2 = 0;
		} else {
			enc3 = ((chr2 << 2) | (chr3 >> 6)) & 63;
		}
		out += key.charAt((chr1 >> 2) & 63)
				+ key.charAt(((chr1 << 4) | (chr2 >> 4)) & 63)
				+ key.charAt(enc3) + key.charAt(enc4);
	}
	return encodeURIComponent(out);
} 

function _em_isie() {
	return (navigator.appVersion.indexOf("MSIE") != -1);
}
function _em_iswin() {
	return (navigator.appVersion.toLowerCase().indexOf("win") != -1);
}
function _em_isopera() {
	return (navigator.userAgent.indexOf("Opera") != -1);
}

function _em_addext(src, ext) {
	if (src.indexOf('?') != -1)
		return src.replace(/\?/, ext + '?');
	else
		return src + ext;
}
 

function _em_genobj(objAttrs, params, embedAttrs) {
	var str = '';

	if (_em_isie() && _em_iswin() && !_em_isopera()) {
		str += '<object ';
		for ( var i in objAttrs)
			str += i + '="' + objAttrs[i] + '" ';
		str += '>';
		for ( var i in params)
			str += '<param name="' + i + '" value="' + params[i] + '" /> ';
		str += '</object>';
	} else {

		str += '<embed ';
		for ( var i in embedAttrs)
			str += i + '="' + embedAttrs[i] + '" ';
		str += '> </embed>';
	}

	document.write(str);
}

function _em_writecont() {
	var ret = _em_getargs(arguments, ".swf", "movie",
			"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000",
			"application/x-shockwave-flash");
	_em_genobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function _em_getargs(args, ext, srcParamName, classid, mimeType) {
	var ret = new Object();
	ret.embedAttrs = new Object();
	ret.params = new Object();
	ret.objAttrs = new Object();
	for ( var i = 0; i < args.length; i = i + 2) {
		var currArg = args[i].toLowerCase();
		switch (currArg) {
		case "classid":
			break;
		case "pluginspage":
			ret.embedAttrs[args[i]] = args[i + 1];
			break;
		case "src":
		case "movie":
			args[i + 1] = _em_addext(args[i + 1], ext);
			ret.embedAttrs["src"] = args[i + 1];
			ret.params[srcParamName] = args[i + 1];
			break;
		case "type":
		case "codebase":
			ret.objAttrs[args[i]] = args[i + 1];
			break;
		case "id":
		case "width":
		case "height":
		case "align":
		case "vspace":
		case "hspace":
		case "class":
		case "title":
		case "name":
			ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i + 1];
			break;
		default:
			ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i + 1];
		}
	}
	ret.objAttrs["classid"] = classid;
	if (mimeType)
		ret.embedAttrs["type"] = mimeType;
	return ret;
}

function _em_has_swf() {
	if (navigator.plugins["Shockwave Flash"]) {
		var desc = navigator.plugins["Shockwave Flash"].description;
		var desc_split = desc.split(" ");
		var version = parseInt(desc_split[2]);
		return version >= 6;
	} else {
		var hasObject = 0;
		try {
			var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
			hasObject = 1;
		} catch (e) {
		}
		if (hasObject == 0) {
			try {
				var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
				axo.AllowScriptAccess = "always";
				hasObject = 1;
			} catch (e) {
			}
		}
		return hasObject == 1;
	}
}

function _em_swf(urlvar) {
	var emsrc = _em_get_cdn_host() + "/emn";
	_em_writecont("src", emsrc, "width", "1", "height", "1", "align", "middle",
			"id", "_em_flash", "quality", "low", "name", "_em_flash",
			"allowScriptAccess", "always", "type",
			"application/x-shockwave-flash", "style",
			"position: absolute; left: -1px;", "flashVars", urlvar);
}

function _em_survey_invite(id, cid1, cid2, networkId, langId, isTest, adslotId){	
	var w = typeof(window._em_width) != 'undefined' ? window._em_width: _em_clientWidth();//this should not happen in iframe
	var h = typeof(window._em_height) != 'undefined' ? window._em_height: _em_clientHeight();
	var invUrl = _em_get_host()+'/emnsv_invite.php?id='+id+'&cid1='+cid1+'&cid2='+cid2+'&isTest='+isTest+
	'&langId='+langId+'&networkId='+networkId+'&w='+w+'&h='+h+'&adslotId='+adslotId;
	if(window!=window.top){
		location.href = invUrl
	}else{
		var ef = document.getElementById('_em_inv_frame');
		if(ef){
			ef.style.width = w+'px';
			ef.style.height = h+'px';
			ef.src = invUrl;
		}
		
	}
}
//empty function for backward support
function _em_go(){} 

function _em_exe(){
		
		var _em_page = window==window.top ? document.location.href :document.referrer;
		var appending = '';
		if(typeof(window._em_allow_invite) != 'undefined' && window._em_allow_invite == 1 ){
			appending += '_em_allow_invite=1';
		}
		//Test ONLY
		if( typeof(window.NetworkID) != 'undefined' ){
			appending += appending==''? '' : '&';
			appending += '_em_network_id='+parseInt(window.NetworkID);
			//appending += '_em_network_id=1';
		}
		//TEST ONLY
		if(typeof(window.AdSpotID) != 'undefined' ){
			appending += appending==''? '' : '&';
			appending += '_em_adslot_id='+_em_encode64(window.AdSpotID);
			//appending += '_em_adslot_id='+_em_encode64('373658');
		}
		
		if(appending != ''){
			_em_page += _em_page.indexOf('?') < 0 ? '?':'&';
			_em_page += appending;
		}	
	  
		var _em_xx=_em_page.split('/');
		var _em_page_host = _em_xx[2];
		if(window==window.top){
			document.write('<iframe id="_em_inv_frame" frameborder="0" style=" border: 0; margin: 0; padding: 0; height:0px; width:0px" ></iframe>')
		}
 
		_em_swf('em_host='+_em_get_host()+'/emn.php&pageURL='+_em_encode64(_em_page)+'&pageHost='+_em_page_host+'&sv_host='+_em_get_sv_host());
}

//automatically call
_em_exe();
