/**
* Strip the mail ID out of the URL query string and return the value.  The id for the 
* recipient must be in the query string as "mailId" and the query string must use standard 
* name value pair structures.
*
* @return	string	ID from amilId variable if found otherwise -1
*/
function getMailId(){
	var mailId = -1;
	var queryStringFull = window.location.search;
	var queryStringClean = queryStringFull.slice(1, queryStringFull.length);
	var queryStringArray = queryStringClean.split("&");         

	// Loop through all query string parameters and find the mail Id
	for(index=0; index<queryStringArray.length; index++)
	{
		tmpVarArray = queryStringArray[index].split("=");

		if(tmpVarArray[0]=="mailId")
		{
			mailId = tmpVarArray[1];
		}
	} 

	return mailId;
}

/**
* Create a cookie to hold the tracking data for future use within this browser session.  The cookie
* will be called "emailTrackingCookie".
*/
function createTrackingCookie()
{
	var mailId = -1;

	// Load the value of the cookie from the current page
	mailId = getMailId();

	// Put the cookie into the current domain (the site must use the cookie from the same domain as it is created)
	if(mailId!=-1)
	{
		var today = new Date();
		today.setTime(today.getTime());
		var expiryDate=new Date();
		expiryDate.setDate(expiryDate.getDate()+7);
		document.cookie = "emailTrackingCookie=" + escape(mailId)+";expires="+expiryDate.toUTCString();
	}
}

/**
* Get the value of the cookie from the browser
*
* @return	string	mail id from cookie or -1
*/
function getTrackingCookie()
{
	var cookieName = "emailTrackingCookie";
	var start = document.cookie.indexOf(cookieName + "=");
	var cookieVal = -1;

	if(start!=-1)
	{
		// Get the value of the cookie (mailId)
		var len = start + cookieName.length + 1;
		var end = document.cookie.indexOf(";", len);
	
		if(end==-1)
		{	
			end = document.cookie.length;
		}
		cookieVal = unescape(document.cookie.substring(len, end));
	}

	return cookieVal;
}

/**
* Function to initiate a post click tracking event when running on a secure masked domain.  Note that
* the email mail Id cookie must have been created with the name "emailTrackingCookie".
*/
function trackPage(trackingType, trackingDesc)
{
	var cookieVal = getTrackingCookie();

	if(cookieVal!=-1)
	{
		// Get the URL to track this event
		img1 = new Image();
		img1.src = "http://response.pure360.com/_act/tracking.php?id=" + cookieVal + "&type=" + trackingType + "&desc=" + trackingDesc;
//		img1.src = "http://response.darkrock.pur3.net/_act/tracking.php?id=" + cookieVal + "&type=" + trackingType + "&desc=" + trackingDesc;
	}
}

function trackPageSecure(trackingType, trackingDesc)
{
	var cookieVal = getTrackingCookie();

	if(cookieVal!=-1)
	{
		// Get the URL to track this event
		img1 = new Image();
		img1.src = "https://response.pure360.com/_act/tracking.php?id=" + cookieVal + "&type=" + trackingType + "&desc=" + trackingDesc;
//		img1.src = "http://response.darkrock.pur3.net/_act/tracking.php?id=" + cookieVal + "&type=" + trackingType + "&desc=" + trackingDesc;
	}
}


