//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(arg1, arg2, arg3) { //arg1 = POD # or URL, arg2 = webpage name, arg3 = webpage URL (variables removed, edit arg3 below to include them)
       if (typeof(arg1) == 'number') {
              spURL = "https://engage" + arg1 + ".silverpop.com/cst";
       }
       else { spURL = arg1; }
       if (arg2 == null) {
              pname = document.title;
       }
       else { pname = arg2; }
       if (arg3 == 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 = arg3; }
       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 (typeof(arg1) == 'number') {
              spURL = "https://engage" + arg1 + ".silverpop.com/cot";
       }
       else { spURL = arg1; }
       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 "";}
}      

