var querystring = location.search.substring(1).toString();

if (querystring.indexOf("plvid=") == -1)
	{
		// querystring does not contain a view ID; do nothing
		// ex: plvid=176619&plpid=1003&plsid=309
	} else {
		//querystring view ID found

		var cookieName = "querystring=";
		var allcookies = document.cookie;
		var cookiePos = allcookies.indexOf(cookieName);
		
		if (cookiePos == -1)
			{
				// landing page cookie does not already exist; create it
				
				addCookie(cookieName, querystring);
			} else {
				// landing page cookie already exists
				// check for window.overwriteCookie value
				
				var cookieOverwrite = "never";
				if (window.overwriteCookie) cookieOverwrite = window.overwriteCookie;

				switch (cookieOverwrite)
					{
						case "never":
							// do nothing
							break;
							
						case "always":
							addCookie(cookieName, querystring);
							break;
		
						case "new test":
							// determine if the set id parameter in the querystring is not empty, and is different from the one in the cookie
							// if yes, add the cookie
							
							var param = "plsid";
							
							var queryID = getItem(param, querystring);
							var cookieID = getItem(param, unescape(allcookies));
							
							
							if (queryID != "" && queryID != cookieID)
								{
									addCookie(cookieName, querystring);
								}
							break;
		
						case "new page":
							// determine if the page id parameter in the querystring is not empty, and is different from the one in the cookie
							// if yes, add the cookie
							
							var param = "plpid";
							
							var queryID = getItem(param, querystring);
							var cookieID = getItem(param, unescape(allcookies));
										
							if (queryID != "" && queryID != cookieID)
								{
									addCookie(cookieName, querystring);
								}
							break;
							break;
		
						default:
							// do nothing
							break;
					}
			}
	}
	
function addCookie (cookieName, querystring)
	{
		
		var expires = new Date();
		expires.setFullYear(expires.getFullYear() + 1);
		
		var domain = document.location.hostname;
		
		var temp = new Array(); 
		temp = domain.split(".");
		
		var start = temp.length-2;
		var end = temp.length-1;
		
		var tld = ".";
		
		for (i = start; i <= end; i++)
			{
				tld = tld + temp[i];
				if (i != end) tld = tld + ".";
			}
		
		var cookiestring = cookieName + escape(querystring) + "; expires=" + expires + "; path=/; domain=" + tld;
		
		document.cookie = cookiestring;
		if (querystring != "")
			{
				var vid = getItem("plvid", querystring);
		
				if (vid == "test")
					{
						document.write ("TRACKING_TEST_SUCCEEDED<p>" + cookiestring);
					}
			}
	}
	
function getItem(key, text)
	{
		var pos = text.indexOf(key + "=");
		
		if (pos != -1)
			{
				var start = pos + key.length + 1;
				var end = text.indexOf("&", start);
				if (end == -1) end = text.length;

				return text.substring(start, end);
			} else {
				return "";
			}
	}
