var mxmROIURI      = "maxemailodd.emailcenteruk.com/ROI/";
var mxmROICookie   = "mxm_roi_data";
var mxmQueryStrKey = "mxmroi";

function mxmSetCookie(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=/";
}

function mxmDelCookie(name)
{
    mxmSetCookie(name, '', -1);
}

function mxmSetNewCookie(cookieData)
{
    var cookieData = cookieData['roi_string'] + '-' + cookieData['test'] + '-' + cookieData['response_id'];
    mxmSetCookie(mxmROICookie, cookieData, 30);
}

function mxmGetCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);

    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }

    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }

    return unescape(dc.substring(begin + prefix.length, end));
}

function mxmGetQuery(variable)
{
    var query = window.location.search.substring(1);
    var vars = query.split("&");

    // for each of the variables in the query string
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            // return variable value
            return pair[1];
        }
    }

    return null;
}

function mxmFormatQueryValues()
{
    // extract values
    var retValue     = new Array();
    var mxmROIString = mxmGetQuery(mxmQueryStrKey);
    var mxmRegEx     = new RegExp("(.*)/(.*)/(.*)");
    var mxmArray     = mxmRegEx.exec(unescape(mxmROIString));

    // set values
    retValue['roi_string']  = mxmGetUniqueID();
    retValue['client_id']   = mxmArray[1];
    retValue['response_id'] = mxmArray[2];
    retValue['test']        = mxmArray[3];

    return retValue;
}

function mxmFormatCookieValues()
{
    var retValue    = new Array();
    var cookieValue = mxmGetCookie(mxmROICookie);

    cookieValue = cookieValue.split('-');

    retValue['roi_string']  = cookieValue[0];
    retValue['test']        = cookieValue[1];
    retValue['response_id'] = cookieValue[2];

    return retValue;
}

function mxmGetReturnValues()
{
    var retValue  = new Array();
    var retCookie = '';
    var retQuery  = '';

    retValue['values_set'] = false;

    if (mxmGetCookie(mxmROICookie) != null && mxmGetQuery(mxmQueryStrKey) != null) {
        // cookie and query set
        retCookie = mxmFormatCookieValues();
        retQuery  = mxmFormatQueryValues();

        if (retCookie['response_id'] == retQuery['response_id'] &&
            retCookie['test'] == retQuery['test']) {
            retValue['roi_string'] = retCookie['roi_string'];
            retValue['test'] = retCookie['test'];
        } else {
            retValue = retQuery;
            mxmSetNewCookie(retValue);
        }

        retValue['values_set'] = true;
    } else if (mxmGetCookie(mxmROICookie) != null) {
        // cookie set
        retCookie = mxmFormatCookieValues();
        retValue['roi_string'] = retCookie['roi_string'];
        retValue['test'] = retCookie['test'];

        retValue['values_set'] = true;
    } else if (mxmGetQuery(mxmQueryStrKey) != null) {
        // query set
        retValue = mxmFormatQueryValues();
        mxmSetNewCookie(retValue);

        retValue['values_set'] = true;
    }

    return retValue;
}

function mxmConstructParams()
{
    var params = mxmGetReturnValues();
    var param  = false;
    var first  = true;

    if (params['values_set']) {
        if (params['test'] == 'false') {
            param = '';
            for (var key in params) {
                if (!(typeof params[key] == 'function' || (typeof params[key] == 'object' && !(params[key] instanceof String)))) {
                    if (first) {
                        param = param + '?';
                        first = false;
                    } else {
                        param = param + '&';
                    }
                    param = param + key + '=' + params[key];
                }
            }
        }
    }

    return param;

}

function mxmGetUniqueID()
{
    var uniqueString = '';
    for (var i = 0; i < 10; i++) {
        uniqueString = uniqueString + String.fromCharCode(65 + Math.round(Math.random() * 25));
    }

    var timeNow = new Date();
    return uniqueString + timeNow.getTime()
}

function mxmGetProtocol()
{
    if (document.location.protocol == 'https:') {
        return 'https://';
    } else {
        return 'http://';
    }
}

/* Public Functions */

function mxm_roi_tracking()
{
    var param = mxmConstructParams();
    var protocol = mxmGetProtocol();

    if (param != false) {
        document.writeln('<img src="' + protocol + mxmROIURI + param + '&mode=track" height="0" width="0" visible="false" border="0" />');
    }
}

function mxm_roi_conversion(value)
{
    var param = mxmConstructParams();
    var protocol = mxmGetProtocol();

    if (param != false) {
        document.writeln('<img src="' + protocol + mxmROIURI + param + '&value=' + value + '&mode=conversion" height="0" width="0" visible="false" border="0" />');
    }
}

function mxm_roi_confirm()
{
    var param = mxmConstructParams();
    var protocol = mxmGetProtocol();

    if (param != false) {
        document.writeln('<img src="' + protocol + mxmROIURI + param + '&mode=confirm" height="0" width="0" visible="false" border="0" />');
        //mxmDelCookie(mxmROICookie);
    }
}