if(!Array.prototype.mapContains)
{
	Array.prototype.mapContains = function(obj)
	{
		for (a in this)
		{
			if (!Array.prototype[a])
			{
				if (a === obj)
	      			return true;
			}
		}
		return false;
	}
}

if(!Array.prototype.contains)
{
	Array.prototype.contains = function(obj)
	{
		var i = this.length;
		while (i--)
		{
			if (this[i] === obj)
			{
				return true;
	    	}
	    }
		return false;
	}
}

if(!Function.prototype.bind) {
	Function.prototype.bind = function(object) {
	  var __method = this;
	  return function() {
		return __method.apply(object, arguments);
	  };
	};
}

function NcpTracker()
{
	this._init = function()
	{
		this._ncp_use_title_as_name = 0;
		this._ncp_install_tracker = 1;
		
		this._ncp_rtu = '';
		try {
			this._ncp_rtu = top.document.referrer;
		} catch(e1) {
			if(parent){
				try{ this._ncp_rtu = parent.document.referrer; } catch(e2) { this._ncp_rtu=''; }
			}
		}
		if(this._ncp_rtu == '') {
			this._ncp_rtu = document.referrer;
		}

		this._ncp_title = '';
		if (document.title && document.title!='') this._ncp_title = this._ncp_escape(document.title);

		this._ncp_called;
		this._ncp_tracker_site;
		this._ncp_tracker_url;

		this._ncp_url = document.location.href;
		this._ncp_da = new Date();

		this._ncp_custom_vars = new Array();

		this._ncp_custom_vars['panelID'] = 0;

		this._ncp_custom_params_map = new Array();
		this._ncp_custom_params_map[11] = new Array();
		this._ncp_custom_params_map[12] = new Array();
		this._ncp_custom_params_map[13] = new Array();
		this._ncp_custom_params_map[14] = new Array();
		this._ncp_custom_params_map[11]['refID'] = 'customvid';
        this._ncp_custom_params_map[11]['kip'] = 'kip';
        this._ncp_custom_params_map[12]['afflt'] = 'customvid';

		this._ncp_src;
		this._ncp_site;
		this._ncp_action_name ='';
		this._ncpSecureServer = "https://s.ncp.imrworldwide.com";
		this._ncpServer = "http://ncp.imrworldwide.com";

		this._ncpBaseURL = (("https:" == document.location.protocol) ? this._ncpSecureServer : this._ncpServer);
		this._ncp_pkurl = this._ncpBaseURL + '/cgi-bin/ncp.do';

		this._action = 'action';
		this._url = 'url';
		this._cur_url = window.location.href;

		this._eventsArray = new Array();
		this._reservedParameters = new Array();
		this._reservedParameters.push('tfid');
		this._reservedParameters.push('url');
		this._reservedParameters.push('action_name');
		this._reservedParameters.push('title');
		this._reservedParameters.push('urlref');
		this._reservedParameters.push('cbvrand');

		this._fired = false;
	};

	this._ncp_escape = function(_ncp_str)
	{
		if(typeof(encodeURIComponent) == 'function') {
			return encodeURIComponent(_ncp_str);
		} else {
			return escape(_ncp_str);
		}
	};

	this._ncp_gup = function(param_name)
	{
		try
		{
			param_name = param_name.replace(/[\[]/,'\\\[').replace(/[\]]/,'\\\]');
			var regexS = '[\\?&]'+param_name+'=([^&#]*)';
			var regex = new RegExp( regexS );
			var results = regex.exec( window.location.href );
			if( results == null )
				return null;
			else
				return results[1];
		}catch(err)
		{
			return null;
		}
	};

	this._ncp_is_site_hostname = function(_ncp_hostname)
	{
		var alias, offset;

		for(var i in this._ncp_hosts_alias)
		{
			alias = this._ncp_hosts_alias[i];

			if( _ncp_hostname === alias )
				return true;

			if ( alias.substr(0, 2) == "*." ) {
				if ((_ncp_hostname) == alias.substr(2))
					return true;

				offset = _ncp_hostname.length - alias.length + 1;
				if ((offset > 0) && (_ncp_hostname.substr(offset) == alias.substr(1)))
					return true;
			}
		}
		return false;
	};

	this.piwik_log = function()
	{
		if(this._ncp_called && (!this._ncp_action_name || this._ncp_action_name=='')) return;
		var _ncp_src2 = this._ncp_getUrlLog();
		document.writeln('<img src="'+_ncp_src2+'" alt="" style="border:0" />');
		if(!this._ncp_action_name || this._ncp_action_name=='') this._ncp_called=1;

		this._ncp_init_tracker();
	};

	this._ncp_init_tracker = function()
	{
		if( typeof(piwik_install_tracker) != "undefined" )
			this._ncp_install_tracker = piwik_install_tracker;
		
		this._ncp_hosts_alias = new Array();
		this._ncp_hosts_alias.push(window.location.hostname);

		if( !this._ncp_install_tracker )
			return;

		this._ncp_tracker_site = this._ncp_site;
		this._ncp_tracker_url = this._ncp_pkurl;

	};

	this._ncp_interceptParameters = function()
	{
		try
		{
			if(this._ncp_site && this._ncp_site!=null && this._ncp_custom_params_map[this._ncp_site])
			{
				for (a in this._ncp_custom_params_map[this._ncp_site])
				{
					if (!Array.prototype[a])
					{
						var value = this._ncp_gup(a);
						if(value!=null && value.length>0)
						{
							var paramName = this._ncp_custom_params_map[this._ncp_site][a];
							this._ncp_setCustomVar(paramName, value);
						}
					}
				}
			}
		}catch(err){}
	};

	this._ncp_setCustomVar = function(paramName, value, checkNull)
	{
		try
		{
			if(this._ncp_custom_vars[paramName]==null || !checkNull || checkNull==null)
				this._ncp_custom_vars[paramName] = value;
		}catch(err){}
	};

	this.insertScriptTag = function(url)
	{
		try
		{
		    var scr = document.createElement('script');
		    scr.setAttribute('type','text/javascript');
		    scr.src=url;
	    	document.getElementsByTagName('body')[0].appendChild(scr);
	    }catch(err){}
	};

	this.insertImageTag = function(url)
	{
		try
		{
		    var src = document.createElement('img');
		    src.setAttribute('src',url);
		    src.setAttribute('width','1');
		    src.setAttribute('height','1');
		    document.getElementsByTagName('body')[0].appendChild(src);
		}catch(err){}
	};

	this.loadFlash = function(callbackName, id)
	{
		try
		{
		    var fc = document.createElement('div');
		    //fc.setAttribute('id','NCP');
		    fc.style.display='block';
		    fc.style.position='absolute';
		    fc.style.left='0px';
		    fc.style.top='0px';
		    document.getElementsByTagName('body')[0].appendChild(fc);
		    var svlURL = this._ncpSecureServer + '/ncp.swf?cname='+ callbackName + '&id=' + id + '&r='+Math.random();
		    var flashStr = ''+
		    '<object id="NCP" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="https://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="1" height="1">'+
		    '   <param name="allowScriptAccess" value="always" />'+
		    '   <param name="movie" value="'+svlURL+'" />'+
		    '   <param name="quality" value="high" />'+
		    '   <param name="wmode" value="transparent" />'+
		    '   <embed src="'+svlURL+'" quality="high" wmode="transparent" width="1" height="1" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="https://www.macromedia.com/go/getflashplayer" />'+
		    '</object>';

		    fc.innerHTML = flashStr;
		}catch(err){}
	};

    this.setNCPCookie = function(id)
    {
        try
        {
            // will always call ncp.do?id='+id (which calls log())
            var additionalParam = '&id=' + id;
            this._callBackVar = '_'+Math.floor(Math.random()*10000000000000);
            this.insertScriptTag(this._ncp_src+'&cbvrand='+this._callBackVar + additionalParam);
            this._waitForResponse();

            //this.insertScriptTag(this._ncp_src + additionalParam);
        }catch(err){}
    };

	this.setFlashCookieIfNotExists = function(id)
	{
		try
		{
	    	this.loadFlash('_setFlashCookieIfNotExists', id);
	    }catch(err){}
	};

	this.checkReturnOrSetFlashCookieValue = function(id)
	{
		try
		{
	    	this.loadFlash('_checkReturnOrSetFlashCookieValue', id);
	    }catch(err){}
	};

	this.checkReturnFlashCookieValue = function(id)
	{
		try
		{
	    	this.loadFlash('_checkReturnFlashCookieValue', id);
	    }catch(err){}
	};

	this.setFlashCookie = function(cookie_value)
	{
		try
		{
	    	this.getFlashObject('NCP').setFlashCookie(cookie_value);
	    }catch(err){}
	};

	this.getFlashObject = function(str)
	{
	    var flo = document.getElementById(str);
	    var em=null;
	    try{em=flo.getElementsByTagName('EMBED')[0];}catch(e){}
	    if(em) flo = em;
	    return flo;
	};

	this.getFlashCookie = function()
	{
		try
		{
	    	this.getFlashObject('NCP').getFlashCookie();
	    }catch(err){}
	};

	this.deleteFlashCookie = function(cookie_name)
	{

	};

	this.setPanelID = function(panelID)
	{
		try
		{
		    if(panelID && panelID != '')
		    {
		    	this._ncp_setCustomVar('panelID', panelID);
		    }
		}catch(err){}
	};

	this.setActionName = function(action_name)
	{
		try
		{
	    	this._ncp_action_name = action_name;
	    }catch(err){}
	};

	this.setDivisionID = function(divisionID)
	{
		try
		{
	    	this._ncp_site = divisionID + 10;
	    }catch(err){}
	};

    this.setEmail = function(email)
    {
        try
        {
            this._ncp_setCustomVar('em', (email!=null)?email.toLowerCase():email, true);
        }catch(err){}
    };

    this.setVendorToken = function(vendorToken)
	{
		try
		{
			this._ncp_setCustomVar('vt', vendorToken, true);
	    	this.setVendorID(vendorToken);
	    }catch(err){}
	};

	this.setVendorID = function(vendorID)
	{
		try
		{
			this._ncp_setCustomVar('vid', vendorID, true);
		}catch(err){}
	};

	this.setSurvey = function(survey)
	{
		try
		{
	    	this.setContext(survey);
	    }catch(err){}
	};

	this.setSurveyID = function(surveyID)
	{
		try
		{
	    	this.setSubContext(surveyID);
	    }catch(err){}
	};

	this.setRespondentID = function(respondentID)
	{
		try
		{
	    	this.setPanelID(respondentID);
	    }catch(err){}
	};

	this.setThirdPartyEmailToken = function(thirdPartyToken)
	{
		try
		{
			this._ncp_setCustomVar('tpet', thirdPartyToken);
		}catch(err){}
	};

	this.setContext = function(context)
	{
		try
		{
			this._ncp_setCustomVar('ctx', context);
		}catch(err){}
	};

	this.setSubContext = function(subContext)
	{
		try
		{
			this._ncp_setCustomVar('sctx', subContext);
		}catch(err){}
	};

	this.ncp_log = function()
	{
		try
		{
		    if (window.addEventListener)
		    {
		        window.addEventListener("load", this._ncp_log.bind(this),false);
		    }
		    else
		    {
		        window.attachEvent("onload", this._ncp_log.bind(this));
		    }
		}catch(err){}
	};

	this._waitForResponse = function()
	{
		try
		{
			if(this.isDefined(window,this._callBackVar))
			{
				if(!this._ncp_action_name || this._ncp_action_name=='') this._ncp_called=1;
				this._fired = true;
				this._logEvents();
			}
			else
			{
				setTimeout(this._waitForResponse.bind(this), 100);
			}
		}catch(err){}
	};

	this.isDefined = function(object, variable)
	{
		try
	    {
	    	return (typeof(eval(object)[variable]) != 'undefined');
	    }catch(err){return false;}
	};

	this._ncp_log = function()
	{
	    try
	    {
	    	if(!this._fired)
	    	{
		        if(this._ncp_called && (!this._ncp_action_name || this._ncp_action_name=='')) return;
		        this._ncp_interceptParameters();
		        this._ncp_src = this._ncp_getUrlLog();
				this._callBackVar = '_'+Math.floor(Math.random()*10000000000000);
		        this.insertScriptTag(this._ncp_src+'&cbvrand='+this._callBackVar);
				this._waitForResponse();
                /*this.ftpp();*/
	    	}
	    	else
	    	{
	    		this._logEvents();
	    	}

	    }
	    catch(e){}
	};

	this._logEvents = function()
	{
		try
		{
			for(var x=0;x<this._eventsArray.length;x++)
	        {
	        	try
	        	{
		            var event = this._eventsArray[x];
		            if(!event.isFired())
		            {
		            	var _ncp_eventSrc = this._ncp_getCommonUrlLog() + this._composeCustomVarsString(event._ncp_custom_vars, this._ncp_custom_vars);
		            	this.insertScriptTag(_ncp_eventSrc);
		            	event.setFired(true);
		            }
	        	}catch(e){}
	        }
		}catch(err){}
	};

	this._composeCustomVarsString = function(custom_vars_array, parent_custom_vars_array)
	{
		try
		{
			var _ncp_custom_vars_str = '';
			if(typeof custom_vars_array == 'undefined')
			{
				custom_vars_array = false;
			}
			if(typeof parent_custom_vars_array == 'undefined')
			{
				parent_custom_vars_array = false;
			}
			if(parent_custom_vars_array)
			{
				for (var i in parent_custom_vars_array)
				{
					if (!Array.prototype[i])
					{
						if(!this._reservedParameters.contains(i) && !custom_vars_array.mapContains(i))
						{
							_ncp_custom_vars_str = _ncp_custom_vars_str + '&'+ escape(i) + "=" + escape(parent_custom_vars_array[i]);
						}
					}
				}
			}
			if(custom_vars_array)
			{
				for (var i in custom_vars_array)
				{
					if (!Array.prototype[i])
					{
						if(!this._reservedParameters.contains(i))
							_ncp_custom_vars_str = _ncp_custom_vars_str + '&'+ escape(i) + "=" + escape(custom_vars_array[i]);
					}
				}
			}
			return _ncp_custom_vars_str;
		}catch(err){return ''}
	};

	this._ncp_getCommonUrlLog = function()
	{
		try
		{
			var _ncp_src2 = this._ncp_pkurl
				+'?tfid='+this._ncp_site
		        +'&url='+this._ncp_escape(document.location.href)
				+'&action_name='+this._ncp_escape(this._ncp_action_name)
                +'&title='+this._ncp_title
				+'&urlref='+this._ncp_escape(this._ncp_rtu)
			return _ncp_src2;
		}catch(err){return ''}
	};

	this._ncp_getUrlLog = function()
	{
		return this._ncp_getCommonUrlLog() + this._composeCustomVarsString(this._ncp_custom_vars);
	};

	this.newEvent = function()
	{
		try
		{
			var event = new NcpTrackerEvent();
			this._eventsArray.push(event);
			return event;
		}catch(err){return null;}
	}
};

function NcpTrackerEvent()
{
	this._ncp_custom_vars = new Array();
	this._fired = false;

	this._ncp_setCustomVar = function(paramName, value, checkNull)
	{
		try{
		if(this._ncp_custom_vars[paramName]==null || !checkNull || checkNull==null)
			this._ncp_custom_vars[paramName] = value;
		}catch(err){}
	};

	this.setSurvey = function(survey)
	{
		try{this.setContext(survey);}catch(err){}
	};

	this.setSurveyID = function(surveyID)
	{
		try{this.setSubContext(surveyID);}catch(err){}
	};

	this.setContext = function(context)
	{
		try{this._ncp_setCustomVar('ctx', context);}catch(err){}
	};

	this.setSubContext = function(subContext)
	{
		try{this._ncp_setCustomVar('sctx', subContext);}catch(err){}
	};

	this.setFired = function(fired)
	{
		try{this._fired = fired;}catch(err){}
	};

	this.isFired = function()
	{
		return this._fired;
	}
}

var _ncpTracker = new NcpTracker();
_ncpTracker._init();

function ncp_log()
{
	try	{_ncpTracker.ncp_log();}catch(err){}
}

function _ncp_log()
{
	try{_ncpTracker._ncp_log();}catch(err){}
}

function firePanelPixels(){}

function ncp_log2()
{
	try{firePanelPixels();}catch(err){}
}

function setNCPCookie(id)
{
	try{_ncpTracker.setNCPCookie(id);}catch(err){}
}

function setFlashCookieIfNotExists(id)
{
	try{_ncpTracker.setFlashCookieIfNotExists(id);}catch(err){}
}

function checkReturnOrSetFlashCookieValue(id)
{
	try{_ncpTracker.checkReturnOrSetFlashCookieValue(id);}catch(err){}
}

function checkReturnFlashCookieValue(id)
{
	try{_ncpTracker.checkReturnFlashCookieValue(id);}catch(err){}
}

function setFlashCookie(cookie_value)
{
	try{_ncpTracker.setFlashCookie(cookie_value);}catch(err){}
}

function getFlashCookie()
{
	try{_ncpTracker.getFlashCookie();}catch(err){}
}

function setPanelID(panelID)
{
	try{_ncpTracker.setPanelID(panelID);}catch(err){}
}

function setActionName(action_name)
{
	try{_ncpTracker.setActionName(action_name);}catch(err){}
}

function setDivisionID(divisionID)
{
	try{_ncpTracker.setDivisionID(divisionID);}catch(err){}
}

function setEmail(email)
{
	try{_ncpTracker.setEmail(email);}catch(err){}
}

function setVendorToken(vendorToken)
{
	try{_ncpTracker.setVendorToken(vendorToken);}catch(err){}
}

function setVendorID(vendorID)
{
	try{_ncpTracker.setVendorID(vendorID);}catch(err){}
}

function setSurvey(survey)
{
	try{_ncpTracker.setSurvey(survey);}catch(err){}
}

function setSurveyID(surveyID)
{
	try{_ncpTracker.setSurveyID(surveyID);}catch(err){}
}

function setRespondentID(respondentID)
{
	try{_ncpTracker.setRespondentID(respondentID);}catch(err){}
}

function setThirdPartyEmailToken(thirdPartyToken)
{
	try{_ncpTracker.setThirdPartyEmailToken(thirdPartyToken);}catch(err){}
}

function setContext(context)
{
	try{_ncpTracker.setContext(context);}catch(err){}
}

function setSubContext(subContext)
{
	try{_ncpTracker.setSubContext(subContext);}catch(err){}
}

function insertScriptTag(url)
{
	try{_ncpTracker.insertScriptTag(url);}catch(err){}
}

function insertImageTag(url)
{
	try{_ncpTracker.insertImageTag(url);}catch(err){}
}
