﻿/*
* http://www.arofex.info
* @version 3.19
*/





/*********** GENERAL ***********/
/*
* Gets html element for different browsers
*/
function GetElement(elementID)
{
    if (document.all && !document.getElementById) { if(document.all[elementID]) return document.all[elementID]; } /* "IE" */
    if (document.getElementById) { if(document.getElementById(elementID))  return document.getElementById(elementID); } /* "NN6" */
    if (document.layers) { if(document.layers[elementID]) return document.layers[elementID]; } /* "NN4" */
}
function GetValueFromCookie(variable) 
{
    var search = variable + "=";
    var returnvalue = "";
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search);
        if (offset != -1) { 
            offset += search.length;
            end = document.cookie.indexOf(";", offset);
            if (end == -1) end = document.cookie.length;
            returnvalue= unescape(document.cookie.substring(offset, end));
        }
    }
    return returnvalue;
}
function MakeDoubleDelegate(function1, function2) {
    return function() {
        if (function1) function1();
        if (function2) function2();
    };
}
/*********** END GENERAL ***********/





/*********** TRACER ***********/
function NoTrace() {}
/*
* Tracer constructor
* @constructor
* @param {String} _tracerID ID of the tracer in html
* @param {String} _externalActionPage Page to which the data gets sent
* @param {String} _traceLinks Enables tracing for links
* @param {String} _traceControls Enables tracing for controls
*/
function Tracer(tracerID, externalActionPage, traceLinks, traceControls)
{
    this._tracerID = tracerID;
    this._traceLinks = traceLinks;
    this._traceControls = traceControls;
    this._initAllControls = false;
    this._externalActionPage = externalActionPage;
    
    this._language = GetValueFromCookie("lng");
    
    this.GeneratePageID();
    this.GenerateSessionID();
    this.GenerateComputerUserID();
    
    this.InitTracer();
}
Tracer.prototype.InitTracer = function()
{
    this.SendPageAction("Open");

    if (this._traceLinks) this.InitLinks();
    if (this._traceControls) this.InitControls();
};
/*
*
*/
Tracer.prototype.CloseTracer = function()
{	
    this.SendPageAction("Close");
};
/*
* Inits the tracing for controls
*/
Tracer.prototype.InitControls = function()
{
    /* get the forms from the html page */
    var theformAr = document.getElementsByTagName("form");
    /* if (theformAr.length == 0) return; */
	
    /* search the different forms */
    for (var j = 0; j < theformAr.length; j++)
    {
        /* search the current form for controls */
        var curForm = theformAr[j];
        for (var i = 0; i < curForm.length; i++)
        {
            var element = curForm[i];
            if (element.type != "password")
            {
                if (this.initAllControls) this.SendAction("Control", element.type + ":" + this.GetObjName(element.form) + "." + this.GetObjName(element) + ":Init:" + element.value);
    			
                /* set the tracer attribute to this page */
                element.arofexTracer = this;
                /* alert (String(element.onclick)); */
                if (String(element.onclick).toLowerCase().indexOf("notrace") == -1)
                {
                    /* copy the onclick event to a variable */
                    element.onclickf = element.onclick;
                    element.onclick = function()
                    {
                        var plr = this.arofexTracer;
                        /* sends the action information to the tracer */
                        var bool = plr.SendAction("Control", this.type + ":" + plr.GetObjName(this.form) + "." + plr.GetObjName(this) + ":Clicked:" + this.value);
                        /* resumes the onclick event (saved in variable) */
                        if (this.onclickf) this.onclickf();
                        return bool;
                    };
                }
            }
        }
    }
};
/*
* Inits the tracing for links
*/
Tracer.prototype.InitLinks = function()
{	
    /* get the links from the html page */
    var links = new Array();
    /* regular links */
    var hrefs = document.getElementsByTagName("a");
    /* image area links */
    var areas = document.getElementsByTagName("area");
    for (i = 0; i < hrefs.length; i++) { links.push(hrefs[i]); }
    for (k = 0; k < links.length; k++)
    {
        var element = links[k];
        if (String(element.onclick).toLowerCase().indexOf("notrace") == -1)
        {
            element.arofexTracer = this;
            /* copy the onclick event to a variable */
            element.onclickf = element.onclick;
            element.onclick = function()
            {
                var plr = this.arofexTracer;
                /* sends the action information */
                var ref = this.href;
                if (ref == "") ref = plr.GetObjName(this);
                var bool = plr.SendAction("Control", "Link" + ":" + escape(ref) + ":Clicked:" + escape(this.innerHTML));
                /* resumes the onclick event (saved in variable) */
                if (this.onclickf) this.onclickf();
                return bool;
            };
        }
    }
};
/*
*
*/
Tracer.prototype.GeneratePageID = function()
{
    var date = new Date();
    var random = Math.round(Math.random() * 9999);
    var pageID = String(date.getFullYear()) + String(date.getMonth()) + String(date.getDay()) + String(Math.abs(date.getTimezoneOffset()));
    pageID = pageID + String(date.getHours()) + String(date.getMinutes()) + String(date.getSeconds()) + String(date.getMilliseconds()) + random;
    this._pageID = pageID;
};
/*
*
*/
Tracer.prototype.GenerateSessionID = function()
{
    var sessionID = GetValueFromCookie("SessionID");
    if(!sessionID)
    {
        var date = new Date();
        var random = Math.round(Math.random() * 9999);
        sessionID = String(date.getFullYear()) + String(date.getMonth()) + String(date.getDay()) + String(Math.abs(date.getTimezoneOffset()));
        sessionID = sessionID + String(date.getHours()) + String(date.getMinutes()) + String(date.getSeconds()) + String(date.getMilliseconds()) + random;
        document.cookie = "SessionID=" + sessionID + "; path=/;"; 
    }
    this._sessionID = sessionID;
};
/*
*
*/
Tracer.prototype.GenerateComputerUserID = function()
{
    var computerUserID = GetValueFromCookie("ComputerUserID");
    if(!computerUserID)
    {
        var date = new Date();
        var random = Math.round(Math.random() * 9999);
        computerUserID = String(date.getFullYear()) + String(date.getMonth()) + String(date.getDay()) + String(Math.abs(date.getTimezoneOffset()));
        computerUserID = computerUserID + String(date.getHours()) + String(date.getMinutes()) + String(date.getSeconds()) + String(date.getMilliseconds()) + random;
        date.setMonth(date.getMonth() + 1);
        document.cookie = "ComputerUserID=" + computerUserID + "; expires=" + date.toGMTString() + "; path=/;";
        this._computerUserID = computerUserID;
        this.SendAction("User", "Data:" + this.GetNavigatorData());
    }
    else 
    {
        var date = new Date();
        date.setMonth(date.getMonth() + 1);
        document.cookie = "ComputerUserID=" + computerUserID + "; expires=" + date.toGMTString() + "; path=/;";
    }
    this._computerUserID = computerUserID;
};
/*
* Returns the name of an object (checks for name, id and href)
*/
Tracer.prototype.GetObjName = function(obj)
{
    if (!obj) return "";
    if (obj.name) return obj.name;
    if (obj.id) return obj.id;
    if (obj.href) return obj.href;
    return "";
};
/*
* 
*/
Tracer.prototype.GetNavigatorData = function()
{
    t = navigator.cookieEnabled + ":";
    t += screen.width + "x" + screen.height;
    return t;
};
/*********** END TRACER ***********/





/*********** SEND ACTION ***********/
/*
*
*/
Tracer.prototype.SendClickAction = function(sender)
{
    return this.SendAction("Control", sender.type + ":" + this.GetObjName(sender) + ":Clicked:" + sender.value);
};
/*
*
*/
Tracer.prototype.SendChangeAction = function(sender)
{
    return this.SendAction("Control", sender.type + ":" + this.GetObjName(sender) + ":Changed:" + sender.value);
};
/*
*
*/
Tracer.prototype.SendHrefAction = function(sender, href, comment)
{
    return this.SendAction("Control", "Link:" + href + ":Clicked:" + comment);
};
/*
*
*/
Tracer.prototype.SendPageAction = function(actionName)
{
    return this.SendAction("Page", actionName + ":" + document.title);
};
/*
* Sends an action to the action page
*/	
Tracer.prototype.SendAction = function(entity, parameters, flashuser, flashsession, player)
{
    if(this._externalActionPage)
    {
        var traceUrl = document.location.protocol + "//" + this._externalActionPage + "?";
        if (this._pageID) traceUrl += "pid=" + this._pageID;
        if (this._sessionID) traceUrl += "&sid=" + escape(this._sessionID);
        if (this._computerUserID) traceUrl += "&uid=" + escape(this._computerUserID);
        if (this._language) traceUrl += "&lng=" + escape(this._language);
        if (entity) traceUrl += "&ent=" + escape(entity);
        if (parameters) traceUrl += "&par=" + escape(parameters);
        if (player) traceUrl += "&fpl=" + escape(player);
        traceUrl += "&pg=" + escape(document.location.pathname); 
        if (String(entity).toLowerCase() == "page") if(document.referrer) traceUrl += "&ref=" + escape(document.referrer);
        traceUrl+= "&cache=" + Math.random(100000);
        	
        if(document.images)
        {
            (new Image(1,1)).src = traceUrl;
        }
        else document.write("<img src="+traceUrl+" width='1' height='1'>");
    }
    return true;
};
/*********** END SEND ACTION ***********/





/*********** DEBUGGING ***********/
/*
* Initialises the debugger. Writes div to the page
*/
Tracer.prototype.InitDebugger = function()
{
    document.write("<div style='position:absolute;top:0px;left:0px;height:150px;width:150px;overflow:auto;' id='arofexDebugger'></div>");
};
/*
* Output debug text
*/
Tracer.prototype.Debug = function(text)
{
    GetElement("arofexDebugger").innerHTML += text + "<br>";
};
/*********** END DEBUGGING ***********/





/*********** INITIALISE ***********/
var tracer;
window.onbeforeunload = MakeDoubleDelegate(window.onbeforeunload, function () { 
    if(tracer) tracer.CloseTracer(); 
});
/*********** END INITIALISE ***********/