﻿var requesterID;
var queryString;
queryString = '';

function addR1Path() {
    var imgTagURL;

    requesterID = readCookie('R1RequesterID');

    // Check we have a cookie
    if (requesterID == null) {
        createCookie('R1RequesterID', createGUID(), 1825);
        requesterID = readCookie('R1RequesterID');
    }

    visitID = readCookie('VisitID');

    // check we have a session id
    if (visitID == null) {
        createCookie('VisitID', createGUID(), '');
        visitID = readCookie('VisitID');
    }

    // Now create the image tag
    imgTagURL = 'http://www.sysro.co.uk/path.gif?R1UID=' + requesterID + '&visitid=' + visitID;

    if (document.referrer != '') {
        imgTagURL = imgTagURL + '&REFUrl=' + document.referrer;
    }

    if (queryString != null && queryString != '') {
        imgTagURL = imgTagURL + '&' + queryString;
    }

    // Finally write the image tag	
    var imageControl = document.createElement('img');
    imageControl.setAttribute('alt', ' ');
    imageControl.setAttribute('src', imgTagURL);
    imageControl.setAttribute('id', 'path');

    var bodyElement = document.getElementsByTagName("body");
    bodyElement.item(0).appendChild(imageControl);
}

function addR1SecPath() {
    var imgTagURL;

    requesterID = readCookie('R1RequesterID');

    // Check we have a cookie
    if (requesterID == null) {
        createCookie('R1RequesterID', createGUID(), 1825);
        requesterID = readCookie('R1RequesterID');
    }

    visitID = readCookie('VisitID');

    // check we have a session id
    if (visitID == null) {
        createCookie('VisitID', createGUID(), '');
        visitID = readCookie('VisitID');
    }

    // Now create the image tag
    imgTagURL = 'https://www.sysro.co.uk/path.gif?R1UID=' + requesterID + '&visitid=' + visitID;

    if (document.referrer != '') {
        imgTagURL = imgTagURL + '&REFUrl=' + document.referrer;
    }

    if (queryString != null && queryString != '') {
        imgTagURL = imgTagURL + '&' + queryString;
    }

    // Finally write the image tag	
    var imageControl = document.createElement('img');
    imageControl.setAttribute('alt', ' ');
    imageControl.setAttribute('src', imgTagURL);
    imageControl.setAttribute('id', 'path');

    var bodyElement = document.getElementsByTagName("body");
    bodyElement.item(0).appendChild(imageControl);
}

function addPathOnLoad(queryStringValue) {
    queryString = queryStringValue;
    var oldonload = window.onload;

    if (typeof window.onload != 'function') {
        window.onload = addR1Path;
    }
    else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }

            addR1Path();
        }
    }
}

function addSecPathOnLoad(queryStringValue) {
    queryString = queryStringValue;
    var oldonload = window.onload;

    if (typeof window.onload != 'function') {
        window.onload = addR1SecPath;
    }
    else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }

            addR1SecPath();
        }
    }
}



function createCookie(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=/";
    return document.cookie;
}

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;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

function guidPart() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}

function createGUID() {
    return (guidPart() + guidPart() + "-" + guidPart() + "-" + guidPart() + "-" + guidPart() + "-" + guidPart() + guidPart() + guidPart());
}
