
function dblClicTracker(progid)    {
    this.progid    = progid;
    this.affid    = null;
    this.siteid    = null;
    this.banid    = null;
    this.linkid    = null;
    this.campid = null;     
    this.checkReferrer = function()    {
        
        
        return (document.referrer.indexOf('afilio.com.br') != -1);
    }
    
    this.checkCookie = function()    {
        
        
        return (this.getCookie("afilio_dblclic_"+this.progid) == "active");
    }
    
    this.getDomain = function() {
        var mai = document.domain;
        var dot = mai.lastIndexOf(".");
        var dname = mai.substring(0,dot);

        var dot2 = dname.indexOf(".");
        var dname2 = mai.substring(dot2+1,dot);
        var host = mai.substring(dot,mai.length);
        return dname2+host;
    }
    
    this.setCookie = function()    {
        var host = this.getDomain();
        document.cookie = "afilio_dblclic_"+this.progid+"=active&progid="+this.progid+"&affid="+this.affid+"&siteid="+this.siteid+"&banid="+this.banid+"&linkid="+this.linkid+";domain="+host+";path=/";
        
    }
    
    this.destroyCookie = function()    {
        var host = this.getDomain();        
        document.cookie = "afilio_dblclic_"+this.progid+"=inactive;domain="+host+";path=/";
    }
    
    this.getCookie = function(name)    {
        
        
        
        
        ///afilio_dblclic_24
        
        var cookie    = document.cookie+"";
        var i        = cookie.indexOf(name);
        
        
        if (i == -1) return null;

        cookie=cookie.substring(i+name.length+1);
        var j=cookie.indexOf("&");
        
        if (j != -1) cookie=cookie.substring(0, j);
        cookie=cookie.split(';');
        
        cookie=cookie[0];
        return cookie;
    }
    
    this.extractReferrer = function()    {
        
        var referrer = document.referrer;
        var i = referrer.indexOf('?');
        var params;
        if (i == -1)    return false;
        
        params = referrer.substring(i+1);
        params = params.split('&');
        for(j=0;j<params.length;j++)    {
            name  = (params[j].substring(0,params[j].indexOf('=')));
            value = (params[j].substring(params[j].indexOf('=')+1));
            this[name] = value;
        }
        campid = this.campid.split(';');
        this.affid  = campid[0];
        this.progid = campid[1]; 
    }
    
    this.extractCookie = function()    {
        
        this.affid     = this.getCookie('affid');
        this.siteid = this.getCookie('siteid');
        this.banid     = this.getCookie('banid');
        this.linkid = this.getCookie('siteid');
    }
    
    this.track = function()    {
        
        document.write('<img src="http://v2.afilio.com.br/doubleclic.php?progid='+this.progid+'&affid='+this.affid+'&siteid='+this.siteid+'&banid='+this.banid+'&linkid='+this.linkid+'" border="0" width="1" height="1"></img>');
    }

       if (this.checkReferrer())    {
        
        if (!this.checkCookie()){
            
               this.extractReferrer();       
            this.setCookie();
        }
    }   else    {
        
       if (this.checkCookie())   {
           
           
            this.extractCookie();
            this.track();
            this.destroyCookie();
        }
    }
}
