﻿function FDMTracking()
{
    try {    
        this.SubmitTrackingUrl = parent.location.protocol + "//www.fairinvestment.co.uk/fdm/tracking/SubmitTracking.aspx";        
        //this.SubmitTrackingUrl = "SubmitTracking.aspx";
        this.SourceAffId = this.getQS('affid');
        this.DestinationAffId = '';     // User defined
        this.IsThankYou = 0;            // User defined
        this.ProductName = '';          // User defined
        this.IPAddress = '' ;           // Get from server
        this.TrackingSessionId = this.getTrackingSessionId();
        this.PageName = '';             // User defined
        this.PageUrl = window.location;
        this.PageReferrerUrl = document.referrer;
        this.PageUserAgent = '';        // Get from server
        this.TrackingError = '';        // Errors?
        this.DebugMode = false;             // 1 for debug mode
        this.CacheBuster = Math.floor(Math.random()*99999999) ;
   } catch (ex) {
        this.TrackingError = ex.name + ' ' + ex.description;
        if (this.DebugMode) alert(this.TrackingError);
   }   
}

FDMTracking.prototype.Submit = function()    // Define Method
{   

    myImage = new Image()    
    if (this.DebugMode) {
        myImage.onerror = function(evt) { alert('Failed to load'); }
        myImage.onload = function(evt) { alert('Succesfully loaded'); }        
    }    
    myImage.src = this.SubmitTrackingUrl + '?' + this.getPostData();
    if (this.DebugMode) {
        alert('Image created: ' + myImage.src);
    }
}


FDMTracking.prototype.getTrackingSessionId = function () {
    var CookieId = this.readCookie('FDMTrackingSessionId');    
    return (CookieId);
}

/* Helper functions */

FDMTracking.prototype.getPostData = function() {
    var result = '';
    for (key in this) { 
        if (typeof this[key]!='function') {
            if (result!='') result += '&';
            result += escape(key) + '=' + escape(this[key]);
        }
    }
    return (result);
} 

FDMTracking.prototype.createCookie = function (name,value,days) {
    if (days) {
	    var date = new Date();
	    date.setTime(date.getTime()+(days*24*60*60*1000));
	    var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

FDMTracking.prototype.readCookie = function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
	    var c = ca[i];
	    while (c.charAt(0)==' ') c = c.substring(1,c.length);
	    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

FDMTracking.prototype.getQS =  function (_key) {
    var qsParm = new Array();
    var query = window.location.search.substring(1);
    var parms = query.split('&');
    for (var i=0; i<parms.length; i++) {
        var pos = parms[i].indexOf('=');
        if (pos > 0) {
            var key = parms[i].substring(0,pos);
            var val = parms[i].substring(pos+1);
            if (key==_key) return (val);
        }
    }
    return ('');
}
