//sp variables
var m = ""; // Mailing ID - matches from "spMailingID"
var r = ""; // Recipient ID - matches from "spUserID"
var j = ""; // Mailing Job ID - matches from "spJobID"
var rj = ""; // Report Job ID - matches from "spReportId"
var spURL = "";
var spTag = "";
//clickstream variables
var purl = ""; // URL of the current page
var pname = ""; // title of the current page
//conversion variables and settings
var convnum = 50; // Maximum number of conversion items that can be processed - increasing may slow client load times down.
var a = "";
var d = "";
var amt = "";

function URLInit() {
    var val = null;
    var key = null;
    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 = unescape(parms[i].substring(0,pos));
          var val = unescape(parms[i].substring(pos+1));
          val = val.replace(/\+/g, ' ');
          if (val == "") { }
          else if (val == " ") { }
          else {
            if (key == "spMailingID") { m = val; document.cookie = "m=" + val + ";path=/";}
            else if (key == "spUserID") { r = val; document.cookie = "r=" + val + ";path=/";}
            else if (key == "spJobID") { j = val; document.cookie = "j=" + val + ";path=/";}
            else if (key == "spReportId") { rj = val; document.cookie = "rj=" + val + ";path=/";}
            else { }
              }
        }
    }
    if (m == "") {
        m = getCookie("m");
        r = getCookie("r");
        j = getCookie("j");
        rj = getCookie("rj");
    }
    else { }
}
URLInit();
function spCST(var1, var2, var3) { //var1 = POD # or URL, var2 = webpage name, var3 = webpage URL (variables removed, edit var3 below to include them)
    if (var1 == "1") {
        spURL = "http://sdm3.rm04.net/cst";
    }
    else if (var1 == "2") {
        spURL = "http://recp.rm05.net/cst";
    }
    else if (var1 == "3") {
        spURL = "http://recp.mkt31.net/cst";
    }
    else if (var1 == "4") {
        spURL = "http://recp.mkt41.net/cst";
    }
    else { spURL = var1; }
    if (var2 == null) {
        pname = document.title;
    }
    else { pname = var2; }
    if (var3 == null) {
        var fullurl = String(document.location);
        var index = fullurl.indexOf("?");
        if (index == "-1") { purl = document.location; }
        else {
        purl = fullurl.substr(0,index);
        //purl = document.location;
        }
    }
    else { purl = var3; }
    if (m == "" | m == " " | spURL == "" | m == null) { }
    else {
        spTag = "";
        spTag = '<img border="0" src="'+spURL+'?m='+m+'&r='+r+'&j='+j+'&rj='+rj+'&name='+pname+'&s='+purl+'" width="1" height="1">';
        //alert(spTag);
        document.write(spTag);
    }
}
function spCOT(arg1, arg2, arg3, arg4) { //arg1 = POD # or URL, arg2 = conversion name, arg3 = conversion details, arg4 = conversion amount
    if (arg1 == "1") {
        spURL = "http://sdm3.rm04.net/cot";
    }
    else if (arg1 == "2") {
        spURL = "http://recp.rm05.net/cot";
    }
    else if (arg1 == "3") {
        spURL = "http://recp.mkt31.net/cot";
    }
    else if (arg1 == "4") {
        spURL = "http://recp.mkt41.net/cot";
    }
    else { spURL = var1; }
    if (arg2 == null) {
        if (m == "" | m == " " | spURL == "" | m == null) { }
        else {
            spTag = "";
            for (i=1;i<convnum;i++) {
                a = "";
                d = "";
                amt = "";
                var curNum = "item"+i;
                var curName = curNum+"name";
                a = getVal(curName);
                var curDetails = curNum+"details";
                d = getVal(curDetails);
                var curAmount = curNum+"amount";
                amt = getVal(curAmount);
                if (a == "" | d == "" | amt == "") { }
                else {
                    spTag = spTag+'<img border="0" src="'+spURL+'?m='+m+'&r='+r+'&j='+j+'&rj='+rj+'&a='+a+'&d='+d+'&amt='+amt+'" width="1" height="1">';
                }
            }
            //alert(spTag);
            document.write(spTag);
        }
    }
    else {
        spTag = "";
        if (m == "" | m == " " | spURL == "" | arg2 == null | arg3 == null | arg4 == null) { }
        else {
            spTag = spTag+'<img border="0" src="'+spURL+'?m='+m+'&r='+r+'&j='+j+'&rj='+rj+'&a='+arg2+'&d='+arg3+'&amt='+arg4+'" width="1" height="1">';
            //alert(spTag);
            document.write(spTag);
        }
    }
}
function getCookie( check_name ) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split( ';' );
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f
    
    for ( i = 0; i < a_all_cookies.length; i++ )
    {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split( '=' );
        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
        
        // if the extracted name matches passed check_name
        if ( cookie_name == check_name )
        {
            b_cookie_found = true;
            cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if ( !b_cookie_found )
    {
        return null;
    }
}
function getVal(varg1) {
    curVal = "";
    var spElement = document.getElementById(varg1);
    if (spElement) {
        if (spElement.nodeName == "INPUT") {
            var curVal = spElement.value;
            return curVal;
        }
        else {
            var curVal = spElement.innerHTML;
            return curVal;
        }
    }
    else {return "";}
}