var expirationDates = "15";
var cookieName = "silverpop";
var cookieSeparator = "|";

function createSilverpopCookie() {
	var url = document.location.href;
	var domain = url.substring(url.indexOf(".") + 1, url.length);
	if(domain.indexOf("/") > 0) {
		domain = domain.substring(0, domain.indexOf("/"));
	}
	
	var regexMailingId = new RegExp('spMailingID=[a-zA-Z0-9]+');
	var mailingId = regexMailingId.exec(url);
	
	var regexUserId = new RegExp('spUserID=[a-zA-Z0-9]+');
	var userId = regexUserId.exec(url);
	
	var regexJobId = new RegExp('spJobID=[a-zA-Z0-9]+');
	var jobId = regexJobId.exec(url);

	if (mailingId != null && userId != null && jobId != null) {
		setCookie(cookieName, mailingId + cookieSeparator + userId + cookieSeparator + jobId , expirationDates, "/", domain, "");
	}
}

function createSilverpopCookieForListing(mailingID, userID, jobID) {
	var domain = url.substring(url.indexOf(".") + 1, url.length);
	setCookie(cookieName, mailingId + cookieSeparator + userId + cookieSeparator + jobId , expirationDates, "/", domain, "");
	
}

function getDefaultSilverpopCookie() {
	return getSilverpopCookie("", "", "");
}

function getSilverpopCookie(processName, detail, amount ) {
	var cookie = getCookie(cookieName);
	
	if(cookie != null)
	{
		var source = "";
		
		if(processName == "") {
			source = getDefaultPixelSource(cookie);
		}
		else {
			source = getPixelByProcessSource(cookie, processName, detail, amount);
		}
		
		appendImage(source);
	}
}

function getDefaultPixelSource(cookie) {
	var servlet_loc = "http://recp.mkt41.net/";
	var servlet = "cst";
	var name = document.title;
	if(name.length > 1000) {
		name = name.substring(0, 1000);
	}
	
	var siteURL = document.location.href;
	if(siteURL.length > 1000) {
		siteURL = sireURL.substring(0, 1000);
	}

	var values = cookie.split(cookieSeparator);
	
	if(values.length >= 2)
	{
		var mailingId = values[0];
		var userId = values[1];
		var jobId = values[2];
					
		mailingId =  mailingId.substring(mailingId.indexOf("=") + 1, mailingId.length);
		userId = userId.substring(userId.indexOf("=") + 1, userId.length);
		jobId = jobId.substring(jobId.indexOf("=") + 1, jobId.length);
		
		return servlet_loc + servlet + "?m=" + mailingId + "&r=" + userId + "&j=" + jobId + "&name=" + name + "&s=" + siteURL; 
	}
	
	return "";
}

function getPixelByProcessSource(cookie, processName, detail, amount) {
	var servlet_loc = "http://recp.mkt41.net/";
	var servlet = "cot";
	
	var values = cookie.split(cookieSeparator);
	
	if(detail.length > 128) {
		detail = detail.substring(0, 128);
	}
	
	if(values.length >= 2)
	{
		var mailingId = values[0];
		var userId = values[1];
		var jobId = values[2];
					
		mailingId =  mailingId.substring(mailingId.indexOf("=") + 1, mailingId.length);
		userId = userId.substring(userId.indexOf("=") + 1, userId.length);
		jobId = jobId.substring(jobId.indexOf("=") + 1, jobId.length);

		return servlet_loc + servlet + "?m=" + mailingId + "&r=" + userId + "&j=" + jobId + "&a=" + processName + "&d=" + detail + "&amt=" + amount;;
	}
	
	return "";
}

function appendImage(source) {
		var img=document.createElement("img");
		img.setAttribute('src', source);
		img.setAttribute('height', '1');
		img.setAttribute('width', '1');
		document.getElementById('imagenSilverpop').appendChild(img);
}
