﻿//Stores the campaign and contact ids
var CampaignId = 0;
var ContactId = 0;
//Stores whether the current mode is campaign mode
var CommuniGator_IsCampaignMode = false;
//Stores the users transaction id
var CommuniGator_TrackingTransactionID = "";
//Stores the contacts email address for anonoymous mode
var CommuniGator_ContactEmailAddress = "";
//Stores the xml to post to the server
var TrackerSendXML = '';


//function attempts to load the CampaignId and ContactId (if found) and sets default values
function CommuniGator_LoadROI()
 {
    try {
        //attempt to retrieve the query string args
        //if they exist store the details in a cookie
        var page = new PageQuery(window.location.search);
        CampaignId = page.getValue("commCamId");
        ContactId = page.getValue("commConId");

        if (CampaignId != false && ContactId != false) {
            //store the details in a cookie
            setCookie("CommuniGator_CampaignId", CampaignId.toString(), 14);
            setCookie("CommuniGator_ContactId", ContactId.toString(), 14);
        } else {
            //attempt to retrieve the details from a cookie
            CampaignId = getCookie("CommuniGator_CampaignId");
            ContactId = getCookie("CommuniGator_ContactId");

            if (CampaignId == "") {
                CampaignId = 0;
            }
            if (ContactId == "") {
                ContactId = 0;
            }
        }

        //If the ContactId and CampaignId is not 0 then we are in campaign mode
        if (CampaignId != 0 && ContactId != 0) {
            CommuniGator_IsCampaignMode = true;
        }
    } catch (exception) {
    }
}

//function stores an ROI Tracker to send to the server
function CommuniGator_AddTracker(Name, Value, Unique, Cumulative) {
    //attempt to send the details and silently catch any errors and continue.
    try {

        if (Name == null) {
            Name = '';
        }
        if (Value == null) {
            Value = '';
        }
        if (CommuniGator_ContactEmailAddress == null) {
            CommuniGator_ContactEmailAddress = '';
        }
        if (CommuniGator_TrackingTransactionID == null) {
            CommuniGator_TrackingTransactionID = '';
        }
        if (Unique == null) {
            Unique = false;
        }
        if (Cumulative == null) {
            Cumulative = false;
        }
        if (CommuniGator_ContactEmailAddress == '' && ContactId == 0) {
            return;
        }
        if (Name == '') {
            return;
        }

        //replace any invalid characters
        Name = ReplaceAll(Name, "<", "gtr_lb");
        Name = ReplaceAll(Name, ">", "gtr_rb");
        Name = ReplaceAll(Name, "%", "gtr_perc");
        Value = ReplaceAll(Value, "<", "gtr_lb");
        Value = ReplaceAll(Value, ">", "gtr_rb");
        Value = ReplaceAll(Value, "%", "gtr_perc");        

        TrackerSendXML += "<Tracker Name='" + Name + "' Value='" + Value + "' Unique='" + Unique + "' Cumulative='" + Cumulative + "' />";
    } catch (exception) {
    }
}

//function sends the trackers to the server
function CommuniGator_SendTrackers(TrackerUrl) {
    if (TrackerSendXML != '') {
        try {
            var contentToSend = "<Trackers CampaignId='" + CampaignId + "' ContactId='" + ContactId + "' EmailAddress='" + CommuniGator_ContactEmailAddress + "' TransactionId='" + CommuniGator_TrackingTransactionID + "'>" + TrackerSendXML + "</Trackers>";
            //replace any spaces, ampersands or question marks
            contentToSend = ReplaceAll(contentToSend, "&", "gtr_amp");
            contentToSend = ReplaceAll(contentToSend, "?", "gtr_qst");

            // create a script element
            var script_object = document.createElement('SCRIPT');
            script_object.src = TrackerUrl + '/ROI_Tracking.aspx?Trackers=' + contentToSend;
            script_object.type = 'text/javascript';

            // do the insert
            var head = document.getElementsByTagName('HEAD')[0];
            head.appendChild(script_object);
        } catch (exception) {
        }
    }
}

//load ROI
CommuniGator_LoadROI();

//#######################################################//Resources//#########################################################################//
function PageQuery(q) {
    if (q.length > 1) this.q = q.substring(1, q.length);
    else this.q = null;
    this.keyValuePairs = new Array();
    if (q) {
        for (var i = 0; i < this.q.split("&").length; i++) {
            this.keyValuePairs[i] = this.q.split("&")[i];
        }
    }
    this.getKeyValuePairs = function() { return this.keyValuePairs; }
    this.getValue = function(s) {
        for (var j = 0; j < this.keyValuePairs.length; j++) {
            if (this.keyValuePairs[j].split("=")[0] == s)
                return this.keyValuePairs[j].split("=")[1];
        }
        return false;
    }
    this.getParameters = function() {
        var a = new Array(this.getLength());
        for (var j = 0; j < this.keyValuePairs.length; j++) {
            a[j] = this.keyValuePairs[j].split("=")[0];
        }
        return a;
    }
    this.getLength = function() { return this.keyValuePairs.length; }
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
            ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString() + ";path=/");
}

function CreateGUID() {
    return (RandNumber() + RandNumber() + "-" + RandNumber() + "-" + RandNumber() + "-" + RandNumber() + "-" + RandNumber() + RandNumber() + RandNumber()).toUpperCase()
}
function RandNumber() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
function ReplaceAll(string, find, replace) {
    var matchIndex = string.indexOf(find);

    while (matchIndex != -1) {
        // Relace out the current instance.
        string = string.replace(find, replace)
        // Get the index of any next matching substring.
        matchIndex = string.indexOf(find);
    }

    return string;
}