

function getCookie (name) {
	var dc = document.cookie;
	var cname = name + "=";
	var clen = dc.length;
	var cbegin = 0;

	while (cbegin < clen) {
		var vbegin = cbegin + cname.length;
		if (dc.substring(cbegin, vbegin) == cname) {
			var vend = dc.indexOf (";", vbegin);
			if (vend == -1) vend = clen;
			return unescape(dc.substring(vbegin, vend));
		}
		cbegin = dc.indexOf(" ", cbegin) + 1;
		if (cbegin== 0) break;
	}
	return null;
}

function setCookie(name,value,expireDate,expires,path,domain,secure) {

	var myLoc = "" + document.location;
	var myDev = "dev";
	var myTest = "test";
	var myStage = "stage";
	
	
	if (myLoc.indexOf(myDev) != -1){
	
		// Added - sk1247 - circa 11/15/06 (during migration of CoreDisp and SiteUtils to att.com) - Using a localization function to write the CJ Cookie on the sbc.com domain 
    		document.write("<img src=\"http://dev.localization.sbc.com/loc/cookie/setsbcsessioncookies.jsp?cname=" + name +"&cvalue=" + value +"&cexpire=" + expires + "\" width=\"1\" height=\"1\" border=\"0\" alt=\"\">");
    
		// Added - JA1761 - 04/01/09 - Using a localization function to write the CJ Cookie on the bellsouth.com domain
		document.write("<img src=\"http://dev.localization.ourbellsouth.com/loc/cookie/setsbcsessioncookies.jsp?cname=" + name +"&cvalue=" + value +"&cexpire=" + expires + "\" width=\"1\" height=\"1\" border=\"0\" alt=\"\">");

		// Added - JA1761 - 04/01/09 - Using a localization function to write the CJ Cookie on the att.com domain
		document.write("<img src=\"http://dev.localization.att.com/loc/cookie/setsbcsessioncookies.jsp?cname=" + name +"&cvalue=" + value +"&cexpire=" + expires + "\" width=\"1\" height=\"1\" border=\"0\" alt=\"\">");
	}
	else if (myLoc.indexOf(myTest) != -1){

		// Added - sk1247 - circa 11/15/06 (during migration of CoreDisp and SiteUtils to att.com) - Using a localization function to write the CJ Cookie on the sbc.com domain 
    		document.write("<img src=\"http://test.localization.sbc.com/loc/cookie/setsbcsessioncookies.jsp?cname=" + name +"&cvalue=" + value +"&cexpire=" + expires + "\" width=\"1\" height=\"1\" border=\"0\" alt=\"\">");
    
		// Added - JA1761 - 04/01/09 - Using a localization function to write the CJ Cookie on the bellsouth.com domain
		document.write("<img src=\"http://test.localization.ourbellsouth.com/loc/cookie/setsbcsessioncookies.jsp?cname=" + name +"&cvalue=" + value +"&cexpire=" + expires + "\" width=\"1\" height=\"1\" border=\"0\" alt=\"\">");

		// Added - JA1761 - 04/01/09 - Using a localization function to write the CJ Cookie on the att.com domain
		document.write("<img src=\"http://test.localization.att.com/loc/cookie/setsbcsessioncookies.jsp?cname=" + name +"&cvalue=" + value +"&cexpire=" + expires + "\" width=\"1\" height=\"1\" border=\"0\" alt=\"\">");
	}
	else if (myLoc.indexOf(myStage) != -1){

		// Added - sk1247 - circa 11/15/06 (during migration of CoreDisp and SiteUtils to att.com) - Using a localization function to write the CJ Cookie on the sbc.com domain 
    		document.write("<img src=\"http://stage.localization.sbc.com/loc/cookie/setsbcsessioncookies.jsp?cname=" + name +"&cvalue=" + value +"&cexpire=" + expires + "\" width=\"1\" height=\"1\" border=\"0\" alt=\"\">");
    
		// Added - JA1761 - 04/01/09 - Using a localization function to write the CJ Cookie on the bellsouth.com domain
		document.write("<img src=\"http://stage.localization.bellsouth.com/loc/cookie/setsbcsessioncookies.jsp?cname=" + name +"&cvalue=" + value +"&cexpire=" + expires + "\" width=\"1\" height=\"1\" border=\"0\" alt=\"\">");
	
		// Added - JA1761 - 04/01/09 - Using a localization function to write the CJ Cookie on the att.com domain
		document.write("<img src=\"http://stage.localization.att.com/loc/cookie/setsbcsessioncookies.jsp?cname=" + name +"&cvalue=" + value +"&cexpire=" + expires + "\" width=\"1\" height=\"1\" border=\"0\" alt=\"\">");
	}
	else{ // default to production
	
		// Added - sk1247 - circa 11/15/06 (during migration of CoreDisp and SiteUtils to att.com) - Using a localization function to write the CJ Cookie on the sbc.com domain 
    		document.write("<img src=\"http://localization.sbc.com/loc/cookie/setsbcsessioncookies.jsp?cname=" + name +"&cvalue=" + value +"&cexpire=" + expires + "\" width=\"1\" height=\"1\" border=\"0\" alt=\"\">");
    
		// Added - JA1761 - 04/01/09 - Using a localization function to write the CJ Cookie on the bellsouth.com domain
		document.write("<img src=\"http://localization.bellsouth.com/loc/cookie/setsbcsessioncookies.jsp?cname=" + name +"&cvalue=" + value +"&cexpire=" + expires + "\" width=\"1\" height=\"1\" border=\"0\" alt=\"\">");

		// This was the original code and writes the CJ cookie on the domain specified in the parameter list on the call to the setCookie function   
		document.cookie = name + "=" +value +
        	( (expires) ? ";expires=" + expireDate.toGMTString() : "") +
   	        ( (path) ? ";path=" + path : "") + 
   	        ( (domain) ? ";domain=" + domain : "") +
   	        ( (secure) ? ";secure" : "");
	}
}


function QueryString(key)
{
	var value = null;
	for (var i=0;i<QueryString.keys.length;i++)
	{
		//case sensitive lookup
		if (QueryString.keys[i]==key)
		{
			value = QueryString.values[i];
			break;
		}
	}
	return value;
}

QueryString.keys = new Array();
QueryString.values = new Array();


function queryStringParse(searchString)
{
	var query = searchString;
	var pairs = query.split("&");
	
	for (var i=0;i<pairs.length;i++)
	{
		var pos = pairs[i].indexOf('=');
		if (pos >= 0)
		{
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			QueryString.keys[QueryString.keys.length] = argname;
			QueryString.values[QueryString.values.length] = value;		
		}
	}

}


function getTimeZoneCode()
{
	var timeZoneCode = "";

	//get the current date value
	var strDate = new Date();
	
	// Convert this Date Object to a String
	strDate = strDate.toString();

	// Split the Date/Time String into an Array
	strDate = strDate.split(" ");

	// find the IE version location of the timezone
	timeZoneCode = strDate[4].substring(0,1);
	if (timeZoneCode != "E" && timeZoneCode != "C" &&  timeZoneCode != "M" &&  timeZoneCode != "P") {
		//this must be a netscape browser, try to find it in that format
		timeZoneCode = strDate[6].substring(1,2);	
	}
	
	return timeZoneCode;
	
}

function padWithZero(theInteger) {
	return theInteger=(theInteger<10)?"0"+theInteger:theInteger
}


function getEventId()
{
	//set the visit id to the current date / time in the following format "YYYYMMDDHHMMSSTIMEZONE
	var now = new Date();												   
	var eIVal = now.getFullYear()+ "" +padWithZero(parseInt(now.getMonth())+1)+ "" +padWithZero(now.getDate())+ "" +padWithZero(now.getHours())+ "" +padWithZero(now.getMinutes())+ "" +padWithZero(now.getSeconds());
	
	// add timezone to the visitId
	eIVal = eIVal + getTimeZoneCode();
	
	return eIVal;
	
}

function getVisitorLocation()
{

	var stateCode = "";
	var stateName = "";

	//determine what state this visitor has localized to
	if (getCookie("local_state")) {
		stateName = getCookie("local_state");
		
		//convert the verbose state name to the state code
		switch (stateName)
		{
			case "Arkansas": stateCode = "AR"; break;
			case "California": stateCode = "CA"; break;
			case "Connecticut": stateCode = "CT"; break;
			case "Illinois": stateCode = "IL"; break;
			case "Indiana": stateCode = "IN"; break;
			case "Kansas": stateCode = "KS"; break;
			case "Michigan": stateCode = "MI"; break;
			case "Missouri": stateCode = "MO"; break;
			case "Nevada": stateCode = "NV"; break;
			case "Ohio": stateCode = "OH"; break;
			case "Oklahoma": stateCode = "OK"; break;
			case "Texas": stateCode = "TX"; break;
			case "Wisconsin": stateCode = "WI"; break;
			default: stateCode = ""; break;
		}
	
	}
	
	//convert the state string to the state abreviation
		
	return stateCode;
}


//-------------------------------------------------------------
function ct() 
{

	var pixelPage = 0;
	var pixelImgQueryString = "?";
	var pixelImgUrl = "https://sw51.sbc.com/ctrk/p.gif";  //https is used to comply with web shopping cart and DSL IOT formatting.  When the image pixel is posted https will be converted to http within core display.
	var pixelImgUrl = " ";
	var eventId = getEventId();
	var visitorLocation = getVisitorLocation();
	
	//test if the query string contains a ci value, do this test before checking the cookie, 
	//incase the user has an existing CT cookie, and there is a new ci in this url's query string that should overwrite the cookie CI info
	if (location.search.indexOf("CI=") != "-1") {
		pixelPage = 1;	// set the pixel page flag
			
		queryStringParse(window.location.search.substring(1));	//parse the current querystring
		
		pixelImgQueryString = pixelImgQueryString+ "E=L"	//append the event ("E") value to L for a landing event
				
		pixelImgQueryString = pixelImgQueryString+ "&CI=" +QueryString("CI"); 	//append the CI value

		// add any additional variables if present
		if (QueryString("UI") != null) {
			pixelImgQueryString = pixelImgQueryString+ "&UI=" +QueryString("UI"); 	//append the UI value
		}
		if (QueryString("RI") != null) {
			pixelImgQueryString = pixelImgQueryString+ "&RI=" +QueryString("RI"); 	//append the RI value
		}
		if (QueryString("RD") != null) {
			pixelImgQueryString = pixelImgQueryString+ "&RD=" +QueryString("RD"); 	//append the UI value
		}
	
		//if provided set the pixelImgUrl to the one specified in the url.
		if (QueryString("TI") != null) {
			pixelImgUrl = unescape(QueryString("TI"));	//unescape the TI value incase it was url escaped
		}
			
		//set the "CT" cookie ---------------------------------
		var cookieDaysInSec = 60 * 24 * 60 * 60; //60 days * 24 hours * 60 min * 60 sec = 5184000
		var cookieExpireDate = new Date();
		cookieExpireDate.setDate(cookieExpireDate.getDate()+60);
		var cookieValue = "";

		//set the cookie value
		cookieValue = cookieValue + "E=L&EI=" +eventId+ "&CI=" +QueryString("CI")+ "&EL=" +visitorLocation+ "&TI=" +pixelImgUrl;
		//This pixel is removed for QC Content-Tools #1626 - Site Utilities: Remove Legacy SBC Clickstream & Campaign Tracking Tags to be released on June 18, 2008
		//cookieValue = cookieValue + "E=L&EI=" +eventId+ "&CI=" +QueryString("CI")+ "&EL=" +visitorLocation+ "&TI=" + " ";
		
		// add any additional variables to the cookie if present
		if (QueryString("UI") != null) {
			cookieValue = cookieValue + "&UI=" +QueryString("UI");
		}
		if (QueryString("RI") != null) {
			cookieValue = cookieValue + "&RI=" +QueryString("RI");
		}
		if (QueryString("RD") != null) {
			cookieValue = cookieValue + "&RD=" +QueryString("RD");
		}

		setCookie("CT",unescape(cookieValue),cookieExpireDate,cookieDaysInSec,"/",".att.com");  //set a domain cookie named CT, also make sure that the value is unescaped, setting it on att.com now

		
	} else if (getCookie("CT")) {	//test if the "CT" cookie exists
		pixelPage = 1;
	
		queryStringParse(getCookie("CT"));	//parse the query string value stored in the cookie

		//set the "E" or event value to L because this user has again come to a landing page, this time without a CI in the url, but an exisitng CI in the cookie
		pixelImgQueryString = pixelImgQueryString+ "E=L";	

		pixelImgQueryString = pixelImgQueryString+ "&CI=" +QueryString("CI");	//append the CI value 

		//determine if a tracking site url was provided	
		if (QueryString("TI") != null) {
			pixelImgUrl = unescape(QueryString("TI"));	//unescape the TI value incase it was url escaped
		}
		
		//if the user does not have an existing localization cookie then try to obtain the EL value from the CT cookie
		if (visitorLocation == "") {
			visitorLocation = QueryString("EL");
		}

		//if the cookie contains a EI value then use that one
		if (QueryString("EI") != null) {
			eventId = QueryString("EI");	
		}

		
	}
	
	if (pixelPage == 1) {
						
		//append this events information to the pixelImgQueryString 
		pixelImgQueryString = pixelImgQueryString+ "&EI=" +eventId+ "&EL=" +visitorLocation;
	
		//remove the HTTPS from the tracking URL
		pixelImgUrl = pixelImgUrl.replace("https:", "http:");
				
		//build the query string that will be posted to the pixel url
		var pixelImgSCRVal = pixelImgUrl + pixelImgQueryString;
	
		//pixel the page   This pixel is removed for QC Content-Tools #1626 - Site Utilities: Remove Legacy SBC Clickstream & Campaign Tracking Tags to be released on June 18, 2008
		//document.write("<img src=\"" +pixelImgSCRVal+ "\" width=\"1\" height=\"1\" border=\"0\" alt=\"\">");
		
		//display the pixeled image on the page. FOR TESTING ONLY
		//document.write("<p>&nbsp;</p><hr>Campaign tracking pixel value='<font size=3 color=red>" +pixelImgSCRVal+ "</font>'");
		
	}		

}

//-------------------------------------------------------------
function nimbus(trackingId) 
{
	var pixelImgQueryString = "?";
	var pixelImgUrl = "http://sw51.sbc.com/ctrk/k.gif";  
	var pixelImgUrl = " ";  
	var visitorLocation = getVisitorLocation();

	if (trackingId != null) {

		pixelImgQueryString = pixelImgQueryString+ "E=V"	//set the "E" or event value to V for a 'view' event
		pixelImgQueryString = pixelImgQueryString+ "&CI=" +trackingId	//add the trackingId to the query string
		pixelImgQueryString = pixelImgQueryString+ "&EL=" +visitorLocation	//add the trackingId to the query string

		var pixelImgSCRVal = pixelImgUrl + pixelImgQueryString	//build the query string that will be posted to the pixel url

		//pixel the page.     This pixel is removed for QC Content-Tools #1626 - Site Utilities: Remove Legacy SBC Clickstream & Campaign Tracking Tags to be released on June 18, 2008
		//document.write("<img src=\"" +pixelImgSCRVal+ "\" width=\"1\" height=\"1\" border=\"0\" alt=\"\">");

		//display the pixeled image on the page. FOR TESTING ONLY
		//document.write("<p>&nbsp;</p><hr>Nimbus tracking pixel value='<font size=3 color=red>" +pixelImgSCRVal+ "</font>'");
	
	}

}
