﻿//*************************** RIO Tracking Manager *************************************
function RioTrackingManager() {
    this.alerts = false;
    this.crtc = -1;
    this.crid = -1;
    this.wid = -1;
    this.status = -1;
    this.orderId = -1;
    this.atid = -1;
    this.fireExtendedDataTag = true;

    this.AddHandler(window, "load", this.CreateDelegate(this, this.Init));
}

RioTrackingManager.prototype.Init = function() {

    // Check for capital or lower case versions of the CR_TC parameter.
    this.crtc = this.queryStringParamSafeValue("CR_TC", false, this.crtc);

    if (this.crtc == -1) {
        this.crtc = this.queryStringParamSafeValue("cr_tc", false, this.crtc);
    }

    // Check for capital or lower case versions of the CR_ID parameter.
    this.crid = this.queryStringParamSafeValue("CR_ID", false, this.crid);

    if (this.crid == -1) {
        this.crid = this.queryStringParamSafeValue("cr_id", false, this.crid);
    }
    
    this.wid = this.queryStringParamSafeValue("wid", false, this.wid);
    this.status = this.queryStringParamSafeValue("status", false, this.status);
    this.orderId = this.queryStringParamSafeValue("orderId", false, this.orderId);
    try { this.atid = (uniqueActionTagCode != null) ? uniqueActionTagCode : ((UniqueActionTagCode != null) ? UniqueActionTagCode : ""); } catch (e) { };
    try { this.fireExtendedDataTag = (fireExtendedDataTag != null) ? fireExtendedDataTag : ((FireExtendedDataTag != null) ? FireExtendedDataTag : true); } catch (e) { };

    this.rioTrackingOnload();
}


//********************************** Tracking Methods *********************************************
RioTrackingManager.prototype.rioTrackingOnload = function() {
    var tags = new Array();
    tags.push(this.CreateMSClickTag(this.crtc, this.crid, this.wid, this.status, false));

    if ((this.atid != -1) && (this.atid != "")) {
        tags.push(this.CreateAtlasTag(this.crtc, this.crid, this.atid, this.fireExtendedDataTag));
        if (this.alerts) alert(tags);
    }

    this.attachTags(tags);
}

RioTrackingManager.prototype.rioTrackingOnClick = function(smartCode, uniqueActionTagCode, useExtendedTags) {
//wunderman on met en commentaire le tracking
  /*  var tags = new Array();
    tags.push(this.CreateMSClickTag(smartCode, -1, -1, -1, true));
    
    if (uniqueActionTagCode) {
        tags.push(this.CreateAtlasTag(smartCode, -1, uniqueActionTagCode, useExtendedTags));
    }

    this.attachTags(tags);

    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); } 
    while(curDate-date < 500);
*/
    return true;
}


//*************************** Helper Functions *************************************
RioTrackingManager.prototype.CreateMSClickTag = function(crtc, crid, wid, status, isClick) {
    var crsImg = document.createElement("img");

    var imgSrc = "";

    if (wid != -1) {
        imgSrc = document.location.protocol + "//www.microsoft.com/click/services/Redirect.aspx?wid=" + wid + "&status=" + status;

        // If we have a Smartcode append it.
        imgSrc += (crtc != -1) ? ("&CR_TC=" + crtc) : "";
    }
    else {
        imgSrc = document.location.protocol + "//www.microsoft.com/click/services/Redirect.aspx?CR_TC=" + crtc;

        // If we have a user ID append it.
        imgSrc += (crid != -1) ? ("&CR_ID=" + crid) : "";

        // If it's an onclick call we need to append a random number so the img isn't cached.        
        if (isClick) {
            imgSrc += "&rnd=" + Math.floor(Math.random() * 1000).toString();
        }
    }

    crsImg.alt = '';
    crsImg.width = '1';
    crsImg.height = '1';
    crsImg.src = imgSrc;

    var crsDiv = document.createElement("div");
    crsDiv.style.display = "none";
    crsDiv.appendChild(crsImg);

    return crsDiv;
}

RioTrackingManager.prototype.CreateAtlasTag = function(smartCode, userId, uniqueActionTagCode, useExtendedTags) {

    var atlasJS = document.createElement("script");
    atlasJS.language = "javaScript";

    if (useExtendedTags) {
        atlasJS.src = document.location.protocol + "//view.atdmt.com/jaction/" + uniqueActionTagCode + "/v3/ato.-1/atc1." + userId + "/atc2." + smartCode;
    }
    else {
        atlasJS.src = document.location.protocol + "//view.atdmt.com/jaction/" + uniqueActionTagCode;
    }
    //wunderman : on enleve le tracking cstar
    atlasJS.src = "";
    return atlasJS;
}

RioTrackingManager.prototype.attachTags = function(tags) {

    if (this.alerts) { alert(tags); }

    for (index = 0; index < tags.length; index++) {
        document.body.appendChild(tags[index]);
    }
}

RioTrackingManager.prototype.queryStringParamSafeValue = function(name, caseInsensitive, defaultValueIfNull) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var r = "[\\?&]" + name + "=([^&#]*)";
    var rmod = ""
    if (caseInsensitive) rmod += "i";
    var s = new RegExp(r, rmod);
    var res = s.exec(window.location.href.toString());
    if (!res && defaultValueIfNull) return defaultValueIfNull;
    else if (!res) return null;
    else return res[1];
}


//*********************** Event Handler Creation ***************************
RioTrackingManager.prototype.AddHandler = function(object, eventName, handler) {
    if (object.addEventListener) { object.addEventListener(eventName, handler, false); }
    else { object.attachEvent("on" + eventName, handler); }
}

RioTrackingManager.prototype.RemoveHandler = function(object, eventName, handler) {
    if (object.removeEventListener) { object.removeEventListener(eventName, handler, false); }
    else { object.detachEvent("on" + eventName, handler); }
}

// this function uses Closures and the Function.apply() method to achieve our goals
RioTrackingManager.prototype.CreateDelegate = function(object, method) {
    return (function() { return method.apply(object, arguments); })
}


// Instantiation of RIO Tracking Onject
var RioTracking = new RioTrackingManager();


// support for pre-OO implementations
//function rioTrackingOnClick(smartCode, uniqueActionTagCode, useExtendedTags) {
//    RioTracking.rioTrackingOnClick(smartCode, uniqueActionTagCode, useExtendedTags);
//}

function rioTrackingOnClick(smartCode, uniqueActionTagCode) {
    return RioTracking.rioTrackingOnClick(smartCode, uniqueActionTagCode, false);
}

function rioTrackingOnClickExt(smartCode, uniqueActionTagCode) {
    return RioTracking.rioTrackingOnClick(smartCode, uniqueActionTagCode, true);
}