
function isDefined(property) {
  return (typeof property != 'undefined');
}

var flashVersion = 0;
function getFlashVersion() {
	var latestFlashVersion = 8;
   var agent = navigator.userAgent.toLowerCase(); 
	
   // NS3 needs flashVersion to be a local variable
   if (agent.indexOf("mozilla/3") != -1 && agent.indexOf("msie") == -1) {
      flashVersion = 0;
   }
   
	// NS3+, Opera3+, IE5+ Mac (support plugin array):  check for Flash plugin in plugin array
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		var flashPlugin = navigator.plugins['Shockwave Flash'];
		if (typeof flashPlugin == 'object') { 
			for (var i = latestFlashVersion; i >= 3; i--) {
            if (flashPlugin.description.indexOf(i + '.') != -1) {
               flashVersion = i;
               break;
            }
         }
		}
	}

	// IE4+ Win32:  attempt to create an ActiveX object using VBScript
	else if (agent.indexOf("msie") != -1 && parseInt(navigator.appVersion) >= 4 && agent.indexOf("win")!=-1 && agent.indexOf("16bit")==-1) {
	   var doc = '<scr' + 'ipt language="VBScript"\> \n';
      doc += 'On Error Resume Next \n';
      doc += 'Dim obFlash \n';
      doc += 'For i = ' + latestFlashVersion + ' To 3 Step -1 \n';
      doc += '   Set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash." & i) \n';
      doc += '   If IsObject(obFlash) Then \n';
      doc += '      flashVersion = i \n';
      doc += '      Exit For \n';
      doc += '   End If \n';
      doc += 'Next \n';
      doc += '</scr' + 'ipt\> \n';
//      document.write(doc);
   }
		
	// WebTV 2.5 supports flash 3
	else if (agent.indexOf("webtv/2.5") != -1) flashVersion = 3;

	// older WebTV supports flash 2
	else if (agent.indexOf("webtv") != -1) flashVersion = 2;

	// Can't detect in all other cases
	else {
		flashVersion = flashVersion_DONTKNOW;
	}

	return flashVersion;
}

flashVersion_DONTKNOW = -1;

function otracker(typ)
{
	var o=this;
	o.typ = typ;
	
	var http="http://web-controlling.org/.track/logger/index.php";
	var https="https://web-controlling.org/.track/logger/index.php";
	
	var v=100;
	
	o.data = new Object();	
	
	function getLogParameter(o)
	{
		if (o.typ == 0)
			return "log";
		else if (o.typ == 1)
			return "event";
		else
			return "nothing";
	}
	
	function send()
	{
		var o=this;
		var imglocation='';
		var img = new Image();
		var dstr='';
		var fstr='';

		// set Flags
		setFlags(o);
		
		// set Data
		if (o.typ == 0) {
			setData(o);
		}

		// Data
		for (var key in o.data) {
			if (dstr == '')
				dstr=rmdelim(key)+','+rmdelim(o.data[key]);
			else
				dstr+=';'+rmdelim(key)+','+rmdelim(o.data[key]);
		}
		
		//document.writeln("raw data: "+dstr+"<br>");
		//document.writeln("encoded data: "+encode(o,dstr)+"<br>");

		imglocation=getlink(o)+'?dbid=4&t='+getLogParameter(o);
    		imglocation+='&d='+encode(o,dstr);
    


		//document.writeln(imglocation);
		
		try {
			img.src = imglocation;
		} catch (e) {}
		
		if (img.src.length == 0) {
		} else {
			//while (img.complete == false) {

                        //}
		}
	}
	
	function add(key,val)
	{
		o.data[key] = val;
	}
	
	function setFlags(o)
	{
				// encodeURI on?
		    try
        {
            encodeURIComponent("bla");
            o.data['fenc'] = "1";
        }
        catch(exception)
        {
            o.data['fenc'] = "0";
        }
        
        // Cookies enabled?
        o.data['cookie'] = navigator.cookieEnabled?'1':'0';
        
        // Java enabled?
        o.data['java'] = navigator.javaEnabled()?'1':'0';
        
        // Navigator
        o.data['browser'] = navigator.userAgent;
        
        // Screen
        o.data['scrw'] = screen.width+"";
        o.data['scrh'] = screen.height+"";
        o.data['scrd'] = screen.colorDepth+"";
        
        // ViewPort
		if (self.innerHeight) // all except Explorer
		{
			o.data['vpw'] = self.innerWidth+"";
			o.data['vph'] = self.innerHeight+"";
		}
		else if (document.documentElement && document.documentElement.clientHeight) // Explorer 6 Strict Mode
		{
			o.data['vpw'] = document.documentElement.clientWidth+"";
			o.data['vph'] = document.documentElement.clientHeight+"";
		}
		else if (document.body) // other Explorers
		{
			o.data['vpw'] = document.body.clientWidth+"";
			o.data['vph'] = document.body.clientHeight+"";
		}
        
        // TimeZone
        d = new Date();
        o.data['tz'] = d.getTimezoneOffset()+"";
        
        // Flash Version
        var flashVersion = getFlashVersion(); 
        if (flashVersion > 0) {
        	o.data['fl'] = flashVersion+"";
        } else if (flashVersion == 0) {
        	o.data['fl'] = "0";
        } else if (flashVersion == flashVersion_DONTKNOW || flashVersion == null) {
        	//o.data['fl'] = "-1";
        }
        
        // Referrer
        //if (top.document.referrer != null && top.document.referrer.length > 0) { o.data['ref']=top.document.referrer; }
        //else { if (document.referrer != null && document.referrer.length > 0)  o.data['ref']=document.referrer; }

	var elmr=document.getElementsByName("abtrack_referer");
	if (elmr.length==1) {
		o.data['ref']=elmr[0].rel;
	} else {
		try {
			if (top.document.referrer != null && top.document.referrer.length > 0) { o.data['ref']=top.document.referrer; }
  			else { if (document.referrer != null && document.referrer.length > 0)  o.data['ref']=document.referrer; }
		} catch(e) {}
	}

        // Location
        try {
        	if (top.location.search != null && top.location.search.length > 0) { o.data['loc']=top.location.search; }
        	else { if (document.location.search != null && document.location.search.length > 0)  o.data['loc']=document.location.search; else o.data['loc']=""; }
        } catch(e) { o.data['loc']=""; }
        
        // Plugins - not for IE
        //for (var i = 0; i < navigator.plugins.length; i++) {
        //	o.data['plug_'+(i+1)+'_name'] = navigator.plugins[i].name;
       	//	o.data['plug_'+(i+1)+'_file'] = navigator.plugins[i].filename;
				//}
        
        // Identify-Mode    
        if (o.data['cookie'] == "1") {
        	cook = getCookie("abtrack_sid");
        	if (cook == "") {
        		id = makeID();
        		setCookie("abtrack_sid",id);
        		o.data['id'] = id;
        	} else {
        		o.data['id'] = cook;
        	}
        } else {
        	o.data['id'] = '0';
        }
	}
	
	function getPostData(post)
    {
        var ret=new Object();
        post=post.replace(/^.*\?(.+)$/,'$1');
        while((pair=post.match(/([^=]+)=\'?([^\&\']*)\'?\&?/))&&pair[0].length)
        {
            post=post.substring(pair[0].length);
            if(/^\-?\d+$/.test(pair[2]))pair[2]=parseInt(pair[2]);
            ret[pair[1]]=URLDecode(pair[2]);
            
        }
        return ret;
    }
	
function URLDecode(val)
{
   var HEXCHARS = "0123456789ABCDEFabcdef";
   var encoded = val;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
           if (ch == "+") {
               plaintext += " ";
                   i++;
           } else if (ch == "%") {
                        if (i < (encoded.length-2)
                                        && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
                                        && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
                                plaintext += unescape( encoded.substr(i,3) );
                                i += 3;
                        } else {
                                plaintext += "%[ERROR]";
                                i++;
                        }
                } else {
                   plaintext += ch;
                   i++;
                }
        } // while
   return plaintext;
}	
	
	function setData(o)
	{
		var pdat = getPostData(o.data['loc']);
		
		if(pdat.newsletter)
		{
			o.data["newsletter"] = pdat.newsletter;
		}
		if(pdat.campaign)
		{
			o.data["campaign"] = pdat.campaign;
		}
		if(pdat.adword)
		{
			o.data["adword"] = pdat.adword;
		}
		//if(pdat.cpc)
		//{
		//	o.data["cpc"] = pdat.cpc;
		//}
		
		
		
		var elm=document.getElementsByName("abtrack_name");
		var lastt=null;
        for(var at=0;at<elm.length;at++)
        {
            var t=elm[at].title,
            		ah=elm[at].rel,
            		aq=elm[at].rev,an;

            if(t.length>0 && t!=lastt)
            {
                if(ah.length>0)
                {
                    if(aq.length>0)
                    {
                         o.data['elem_'+(at+1)+'_plus'] = aq;
                    }

                    o.data['elem_'+(at+1)+'_content'] = ah;
                }
                
                o.data['elem_'+(at+1)+'_title'] = t;
                
                lastt=t;
            }
        }
        
        
    try
		{
			if(abtrackEventArr != undefined)
			{
				// Mapping
				map = new Object();
				map['event'] = 'ev_cmd';
				map['id'] = 'ev_artnr';
				map['name'] = 'ev_name';
				map['preis'] = 'ev_price';
				map['group'] = 'ev_cat';
				map['anzahl'] = 'ev_amount';
				map['var1'] = 'ev_var1';
				map['var2'] = 'ev_var2';
				map['var3'] = 'ev_var3';
				
				// Tmp Array
				tmp = new Object();
				
				err = 0;
				len = 0;
				for (var key in abtrackEventArr) {
						if (inAArray(key,map) == "1") {
							tmp[map[key]] = abtrackEventArr[key];
							len++;
						} else {
							err = 1;
							break;
						}
				}

				if (err == 0 && len==9) {
					for (var key in tmp) {
						o.data[key] = tmp[key];
					}
				}
				
			}
		}
		catch(exception)
		{
			//alert("exception");
		}
	}
	
	function inAArray(key,arr)
	{
		ret = 0
		
		for (var mkey in arr) {
			if (key == mkey)
			{
				ret = 1;
				break;
			}
		}
		
		return ret;
	}
	
        function getCookie (key) {
          var ret = "";

          if (document.cookie) {
                if (document.cookie.indexOf(key+"=") != -1) {
                    var sPos = document.cookie.indexOf(key+"=") + key.length + 1;
                    var tmps = document.cookie.substring(sPos);
                    var ePos = tmps.indexOf(";");
                    if (ePos == -1)
                      ePos = tmps.length;
                    ret = document.cookie.substr(sPos,ePos);
                  }
          }
          return ret;
        }

	function setCookie (key,val) {
		 document.cookie = key + "=" + val + " ; Path=/";
	}
	
	function encode(o,str)
	{
		if (o.data['fenc'] == "1") {
			return encodeURIComponent(str);
		} else {
      return escape(str);
    }
	}
	
	function makeID()
	{
		a=Math.floor(Math.random()*1000000000);
    b=""+a+new Date().getTime()+v;
    return b;
	}
	
	function rmdelim(str)
	{
		str=str.replace(/,/g,"#ABK#");
        str=str.replace(/;/g,"#ABS#");
        
        //str=str.replace(/\s/g,"");
        
        return str;
	}
	
	function getlink(o)
	{
        	if(location.protocol == "https:") {
                        return https;
                } else {
                        return http;
                }
	}
	
	o.add = add;
	o.send = send;
}

function sendMetaData()
{
	track=new otracker(0);
	with (track) {
		send();
	}
}

function sendArtikelEvent(eventtyp,artnr,name,price,category,amount,var1,var2,var3)
{
		tr=new otracker(1);
		with (tr) {
			tr.data['ev_cmd'] = eventtyp;
			tr.data['ev_artnr'] = artnr;
			tr.data['ev_name'] = name;
			tr.data['ev_price'] = price;
			tr.data['ev_cat'] = category;
			tr.data['ev_amount'] = amount;
			tr.data['ev_var1'] = var1;
			tr.data['ev_var2'] = var2;
			tr.data['ev_var3'] = var3;
			send();
		}
    return true;
}

function sendBidmanagementResult(sum)
{
        //sendArtikelEvent('add','1','Fake',sum,'shop','1','NULL','NULL','NULL');
	//sendArtikelEvent('order','','','','','','','','');
	window.setTimeout("sendBidmanagementResult_step1('"+sum+"')", 500);
}

function sendBidmanagementResult_step1(sum)
{
	sendArtikelEvent('add','1','Fake',sum,'shop','1','NULL','NULL','NULL');
        window.setTimeout("sendBidmanagementResult_step2()", 500);
}


function sendBidmanagementResult_step2()
{
        sendArtikelEvent('order','','','','','','','','');
}

function sendNewsletterEvent(email,typ,ok)
{
	if ((typ == 1 || typ == 2) && (ok == 1 || ok == 0)) { // 1= Eintragen; 2= Austragen
		tr=new otracker(1);
		
		if (typ == 1) {
			tr.data['ev_cmd'] = "nlsubscribe";
		} else if (typ == 2) {
			tr.data['ev_cmd'] = "nlunsubscribe";
		}
		
		with (tr) {
			data['ev_email'] = email;
			data['ev_success'] = ok+'';
			send();
		}
		return true;
	} else {
		return false;
	} 
}

function sendDownloadEvent(fname,fsize)
{
	if (fsize >= 0) {
		tr=new otracker(1);
		with (tr) {
			data['ev_cmd'] = "download";
			data['ev_file'] = fname;
			data['ev_size'] = fsize+'';
			send();
		}
		return true;
	} else {
		return false;
	} 
}

function sendSearchEvent(query,numresults)
{
	if ((query.length > 0 ) && (numresults >= 0)) {
		tr=new otracker(1);
		with (tr) {
			data['ev_cmd'] = "search";
			data['ev_query'] = query;
			data['ev_results'] = numresults+'';
			send();
		}
		return true;
	} else {
		return false;
	} 
}

function sendRegisterEvent()
{
        tr=new otracker(1);
        with (tr) {
        	data['ev_cmd'] = "register";
        	send();
        }
        return true;
}

function sendLoginEvent()
{
        tr=new otracker(1);
        with (tr) {
        	data['ev_cmd'] = "login";
        	send();
        }
        return true;
}

function sendContactEvent(email,issue)
{
        tr=new otracker(1);
        with (tr) {
        	data['ev_cmd'] = "contact";
        	data['ev_email'] = email;
        	data['ev_issue'] = issue;
        	send();
        }
        return true;
}
