/*
 * WebCollector Base Script
 * (c) 2005, 2006, Contentmetrics GmbH
 * $$Header: /home/cvs/wcadmin/Ressourcen/wcol_src.js,v 1.12 2006/07/11 15:46:17 axel Exp $$
 * $$Revision: 1.12 $$
 * $$Log: wcol_src.js,v $
 * $Revision 1.12  2006/07/11 15:46:17  axel
 * $wcDebug eingebaut
 * $
 * $Revision 1.11  2006/04/14 23:14:57  axel
 * $*** empty log message ***
 * $
 * $Revision 1.10  2006/04/14 23:14:33  axel
 * $Header CVS werden nicht ersetzt (ich krieg die Krise)
 * $$
 */

// Debugger Hook
function WCol_Debugger()
{
	this.registeredObjects = new Array();
	
	this.register = new Function('obj', 'this.registeredObjects.push(obj);');
	
	this.getObjects = new Function('return (this.registeredObjects);');
	
	this.temp = ''
		+ 'var w = 600;'
		+ 'var h = 700;'
		+ 'var y = (screen.height / 2) - (h/2);'
		+ 'var x = (screen.width / 2) - (w/2);'
		+ "var bb = window.open('','','width=' + w + ',height=' + h + ',location=0,menubar=1,status=1,toolbar=0,resizable=1,scrollbars=1');"
		+ 'bb.moveTo(x,y);'
		+ "var turl = 'http://webcollector.de/debugger/wcdebug.jsp';"
		+ 'bb.document.open();'
		+ 'bb.document.write(\'<scr\' + \'ipt type="text/javascript" language="JavaScript" src="http://webcollector.de/debugger/getCookies.jsp"></scr\'+\'ipt>\');'
		+ 'bb.document.write(\'<scr\' + \'ipt type="text/javascript" language="JavaScript" src="\' + turl + \'"></scr\'+\'ipt>\');'
		+ 'bb.focus();'
		+ '';
	
	this.wcDebug = new Function ( this.temp );
	
}
var _wcol_debugger_hook = new WCol_Debugger();

// constructor for a  WebCollector object
function WebCollector(xAccountIds) {
	
	// Version for internal CVS
	this.myVersion = "$$Revision: 1.12 $$";

	// register this object
	_wcol_debugger_hook.register(this);
	
	// placeholder for the image
	this.pic = null;
	
	// all names refer to the standard metrics and dims in a datadictionary !! 
	this.server 	= "_s";
	this.referrer 	= "_r";
	this.url 		= "_u";
	this.accdim 	= "_a";
	this.currency 	= "_c";
	this.userAgent   = "_ua";
	this.uaShortName = "_us";
	this.screenSize  = "_sz";
	this.colorDepth  = "_cd";
	this.plugins	= "_pi";
	this.lang		= "_lg";
	this.Cookies	= "_ce";
	this.platform	= "_os";
	this.java		= "_jv";
	this.version	= "_tv"; // Template Version Code Generation.
	 
	// General Settings
	// leave some query parameters on the url
	this.leaveQueryString = false;
	// the requested parameters to leave, '*' as a wildcard
	this.queryParameters = "";
	 
	// subsequent multiple calls in the same page
	this.ccount = 0;
	this.debug = false;
	 
	this.config = new Array();
	this.dimensions = new Array();
	this.metrics = new Array(); 
	
	this.genericSetter   = new Function("arr", "nam", "val", 'arr[nam] = val;');
	this.setDimension    = new Function("nam", "val", 'this.genericSetter(this.dimensions,  nam, val);');
	this.setMetric 	  = new Function("nam", "val", 'this.genericSetter(this.metrics, 	 nam, val);');
	this.setConfig 	  = new Function("nam", "val", 'this.genericSetter(this.config, 	 nam, val);');
	
	this.temp = 'this.temp = ai.replace(/\\s+/,\'\').split(",");'
				  + 'this.setConfig(this.accdim, ""+this.temp);'
				  + 'this.accountids = this.temp;';
	this.setAccountIds = new Function("ai", this.temp );

	   
	// basic configuration settings
	// Account ID for tracking
	this.accountids = "";
	if ( xAccountIds )
		this.setAccountIds(xAccountIds);
	
	this.temp = 'this.url = "http://webcollector.de/wcol";'
	 		 + 'var ts = (new Date()).getTime();'
	 		 + 'this.url += ("/ts" + ts + "." + this.ccount++ + "/" + this.config[this.accdim] + "?[A]");'
	
	 		 + 'for ( conf in this.config )'
	 		 + '{'
	 		 + '	var val = this.config[conf];'
	 		 + '	if ( val != "" )'
	 		 + '	this.url += ("&c[" + encodeURIComponent(conf) + "]=" + encodeURIComponent(""+val) );'
	 		 + '}'
	 		 
	 		  		 
	 		 + 'for ( dimension in this.dimensions )'
	 		 + '{ '
	 		 + '	var val = this.dimensions[dimension];'
	 		 + '	if ( this.debug) window.alert("(" + typeof(val) + ")" + dimension + "=" + val);'
	 		 + '    if ( val ) {'
	 		 + '		var t = val[0];'
	 		 + '		if ( typeof(val)=="object" && t ) {'
	 		 + '			var dimValues = "";'
	 		 + '			for (i=0;i<val.length;i++) {'
	 		 + '				dimValues += encodeURIComponent(""+val[i]);'
	 		 + '				if ( i < val.length-1 )'
	 		 + '					dimValues += ",";'
	 		 + '			}'
	 		 + '			this.url += ("&d[" + encodeURIComponent(dimension) + "]=" + dimValues );'
	 		 + '		} else {'
	 		 + '			if ( val != "" )'
	 		 + '				this.url += ("&d[" + encodeURIComponent(dimension) + "]=" + encodeURIComponent(""+val) );'
	 		 + '		}'
	 		 + '	}'
	 		 + '}'
	
	 		 + 'for ( metric in this.metrics ) {'
	 		 + '	var val = this.metrics[metric];'
	 		 + '	var t = val[0];'
	 		 + '	if ( typeof(val)=="object" && t ) {'
	 		 + '		var dimValues = "";'
	 		 + '		for (i=0;i<val.length;i++) {'
	 		 + '			dimValues += encodeURIComponent(""+val[i]);'
	 		 + '			if ( i < val.length-1 )'
	 		 + '				dimValues += ",";'
	 		 + '		}'
	 		 + '		this.url += ("&m[" + encodeURIComponent(metric) + "]=" + dimValues );'
	 		 + '	} else {'
	 		 + '		this.url += ("&m[" + encodeURIComponent(metric) + "]=" + encodeURIComponent(""+val) );'
	 		 + '	}'
	 		 + '}'
	 		  		 
	 		 + 'this.url += "&[E]";'
			 + 'return(this.url);';
	 this.cUrl = new Function(this.temp);
	
	 this.temp = 'return("<img src=\\"" + this.cUrl(wcObject) + "\\" border=\\"0\\" style=\\" visibility: hidden;\\"");';
	 this.cImgTag = new Function(this.temp);

	 // set the standard metrics and dimensions
	 this.temp = ''
		       + 'this.setDimension(this.server, document.location.hostname);'
		       + 'this.setDimension(this.referrer, document.referrer);'
		       + 'this.setDimension(this.screenSize, "" + screen.width + "*" + screen.height);'
		       + 'this.setDimension(this.colorDepth, ( screen.pixelDepth ) ? screen.pixelDepth:screen.colorDepth);'
		       + 'var pi = new Array ();'
		       + 'var temp2 = "";'
		       + 'if ( navigator.plugins ) {'
		       + '	for (var i = 0; i < navigator.plugins.length; i++) {'
		       + '		pi[navigator.plugins[i].name] = new String(navigator.plugins[i].name);'
		       + '	}'
		       + '	for (plug in pi) {'
		       + '		temp2 = ("" + plug + ";" + temp2);'
		       + '	}'
		       + '	this.setDimension(this.plugins, temp2);'
		       + '}'
		       + 'this.setDimension(this.userAgent,		navigator.userAgent?navigator.userAgent:"");'
		       + 'this.setDimension(this.uaShortName, 	navigator.appCodeName?navigator.appCodeName:"");'
		       + 'this.setDimension(this.Cookies,		navigator.cookieEnabled?navigator.cookieEnabled:"");'
		       + 'this.setDimension(this.lang,			navigator.language?navigator.language:"");'
		       + 'this.setDimension(this.platform,		navigator.platform?navigator.platform:"");'
		       + 'this.setDimension(this.java,			navigator.javaEnabled());'
		       + 'temp2 = document.location.protocol;'
		       + 'if ( temp2 = "file://" ) {'
		       + '	 this.setDimension(this.url, document.location);'
		       + '} else {' 
		       + '	this.setDimension(this.url, (document.location.pathname + document.location.hash) );'
		       + '	temp2 = "";'
		       + '	if ( this.leaveQueryString ) {' 
		       + '		if ( this.queryParameters.indexOf("*") >= 0 ) {'
		       + '			temp2 += document.location.search;'
		       + '		} else {'
		       + '			for ( param in this.queryParameters.split(",") ) '
		       + '			{'
		       + '				temp2 += "."'
		       + '			}'
		       + '		}'
		       + '		this.setDimension(this.url, temp2);'
		       + '	}'
		       + '}';
	 this.init = new Function(this.temp);
	 
	 // submit request
	 this.temp = ''
	       + 'this.pic = new Image();'
	       + 'this.pic.src = this.cUrl();';
	 this.submitRequest = new Function(this.temp);	
	 
	 // Assign a query Parameter to a Dimension / Metric
	 this.temp = ''
	       	+ "var value = ''";
	 		+ "if( key && document.location.search)"
	 		+ "{"
	 		+ "	var query = document.location.search"
	 		+ "	var qstart = query.indexOf('?');"
	 		+ "	query = qstart < 0 ? query : query.substring(qq+1);"
	 		+ "	value = s_pt(query, '&' , this.s_cgif, key.toLowerCase())"
	 		+ "}"
	 		+ "return v;"
	 		;
	 this.getQueryParam = new Function("key", this.temp);
	 
	 this.temp = ''
	       	+ "var val = this.getQueryParam(key);" 
	       	+ "this.setDimension(dim, val);"
	       	;
	 this.getQueryDimension = new Function("dim", "key", this.temp);
	 
	 this.temp = ''
	       	+ "var val = this.getQueryParam(key);" 
	       	+ "this.setMetric(met, val);"
	       	;
	 this.getQueryMetric = new Function("met", "key", this.temp);
	 
	 this.temp = ''
	       	+ "var t=s,x=0,y,r;"
	       	+ "	while(t) "
	       	+ "	{"
	       	+ "		y=t.indexOf(d);"
	       	+ "		y=y<0?t.length:y;"
	       	+ "		t=t.substring(0,y);"
	       	+ "		r=f(t,a);"
	       	+ "		if(r)"
	       	+ "			return r;"
	       	+ "		x+=y+d.length;"
	       	+ "		t=s.substring(x,s.length);"
	       	+ "		t=x<s.length?t:''"
	       	+ "	}"
	       	+ "	return '';"
	       	;
	 this.s_pt = new Function("s","d","f","a", this.temp);
			 
			 
	 this.temp = ''
	       	+ "if(t)"
	       	+ "	{"
	       	+ "		var te=t.indexOf('=');"
	       	+ "		var sk=te<0?t:t.substring(0,te);"
	       	+ "		var sv=te<0?'True':t.substring(te+1);"
	       	+ "		if ( sk==k )"
	       	+ "			return this.s_epa(sv)"
	       	+ "	}"
	       	+ "	return '';"
	       	;
	 this.s_cgif = new Function("t","k", this.temp);      
	        
	 
	 this.temp = ''
	       	+ "return s?unescape(this.s_rep(''+s,'+',' ')):s;"
	        ;
	 this.s_epa = new Function("s", this.temp);         
	        
	 this.temp = ''
	       	+ "var i=s.indexOf(o);"
	       	+ "var l=n.length>0?n.length:1;"
	       	+ "	while(s&&i>=0)"
	       	+ "	{"
	       	+ "		s=s.substring(0,i)+n+s.substring(i+o.length);"
	       	+ "		i=s.indexOf(o,i+l);"
	       	+ "	}"
	       	+ "	return s;"
	        ;
	 this.s_rep = new Function("s","o","n", this.temp);       
}
