function calDate(s1,d){
	var bufferA= Date.parse( s1) ;
	var bufferB= Date.parse( d ) ;
	var number= bufferB-bufferA ;
	if(number>0){
		number= parseInt(number / 86400000) ;
	}
	if(isNaN(number)){
		number= 0;
	}
	return number;
}
function baseZag () {
/* Set the value of numPercentToShow to determine
	what percentage of page hits should bake the cookie.
	Example values:
	.01, means that 1% will be cookied
	*/

	var usePowerRule= 1;
	var use30dayRule= 0;
	var numPercentToCookie= 0;
	//var numPercentToCookie= 0.016666667;
	//var numPercentToCookie= .33;

	// generate a random number: if the random num is less
	// than the decimal, bake cookie.

	var rand= Math.random();
	//user can change these values --- t, s, p --- start ------
	var t= 3650;  //time frame - in days
	var s= 2;	// session number
	var p= 0;	// page view

	// ------- end ----------------
	var tempDate= new Date();	// today's date
	var d= tempDate.getMonth() + 1 + "/" + tempDate.getDate() + "/" + tempDate.getYear(); //today's date in mm/dd/yyyy
	var pc= 0; //actual pageview
	var s1= 0; //session 1
	var sc= 0; //actual session number
	var tempTimeFrame= 0; //temp variable to hold time passed between 2 dates
	var newZagCookieValue= ""; //temp variable to hold cookie information
	var oldsc= 0;	//temp variable to hold session number
	var zagCookie= usat.cookie.get("zagCookie");

	if (zagCookie == null) {
		if (rand < numPercentToCookie) {
   			usat.cookie.set("zagCookie", "2", new Date(2025, 11, 31), "/", ".usatoday.com");
			zagCookie= usat.cookie.get("zagCookie");
		}
   } //if

        if (!(parseInt(zagCookie) >= 0)) {
		if(zagCookie != null){
			sc= getCookieStringParamValue(zagCookie,"sc");
			oldsc= sc;
			pc= getCookieStringParamValue(zagCookie,"pc");
			s1= getCookieStringParamValue(zagCookie,"s1");
			var arrayS= new Array(sc);
			for (var n= 1;n<=sc;n++) {
				sxValue= getCookieStringParamValue(zagCookie,"s"+n);
				newZagCookieValue= newZagCookieValue + "&" + "s"+n + "=" + sxValue;
				arrayS[n]= sxValue;
			}
		}
			tempTimeFrame= calDate(s1,d);
		if(usat.cookie.get("zagSession") == null){
			pc= 1;
			sc++;
			usat.cookie.set("zagSession", "inSession", "", "/", ".usatoday.com");
		}
		else{
			pc++;
		}

		if (usePowerRule && -1<('|'+usat.cookie.get("TData")+'|').indexOf("AMS_00443")) {
			usat.cookie.set("zagCookie", "0", new Date(2025, 11, 31), "/", ".usatoday.com");
		} else if(use30dayRule && sc >= s && tempTimeFrame <=t && pc >= p) //zagged, expire zagCookieValues cookie
		{
			usat.cookie.set("zagCookie", "0", new Date(2025, 11, 31), "/", ".usatoday.com");
		} else if(tempTimeFrame > t){ //passed timeframe
			timeWindowShifted= false;
			newZagCookieValue= "";
			pc= 1;
			for (var i= 1;i<=oldsc;i++) {
				arrayDate= calDate(arrayS[i],d);
				if(arrayDate<=t){ //old dates within time frame
					timeWindowShifted= true;
					for(j= 1;j<=oldsc-(i-1);j++){
						newNum= i+j-1;
						newZagCookieValue= newZagCookieValue + "&s" + j + "=" + arrayS[newNum];
						sc= j;
					}
					sc++
					tempS= "sc=" + sc + "&pc=" + pc + newZagCookieValue + "&s" + sc + "=" + d;
					break;
				}
			}
			if(timeWindowShifted){	//if time window was shifted
				usat.cookie.set("zagCookie", tempS, new Date(2025, 11, 31), "/", ".usatoday.com");
			}
			else{ //start counting again
				tempS= "sc=1&pc=1&s1=" +d;
				usat.cookie.set("zagCookie", tempS, new Date(2025, 11, 31), "/", ".usatoday.com");
			}
		}
		else{	//reset cookie values
			if(oldsc == sc){ //old session
				ta= newZagCookieValue;
			}
			else{	//new session, user refreshes, or on a new page
				ta= newZagCookieValue + "&" + "s" + sc + "=" + d;
			}
			var tempS= "sc=" + sc + "&pc=" + pc + ta;
			usat.cookie.set("zagCookie", tempS, new Date(2025, 11, 31), "/", ".usatoday.com");
		}
	}
} //function baseZag ()
baseZag();

//zagStrInt= "http://asp.usatoday.com/registration/zag2/gather-b.aspx";
zagStrInt= "http://reg.usatoday.com/registration/zag3/zago.htm";
zagStrUrl= window.location.toString();
zagEnabled= 1;	//To disable zag, set it to 0. To enable zag, set it to 1

function zagFromHereTest() {
	var onlyFromFronts= 0;
	var doit= 1;
	if (onlyFromFronts) {
		doit= (document.URL.indexOf("/life/front.htm")>-1)||(document.URL.indexOf("/sports/front.htm")>-1)||(document.URL.indexOf("/news/front.htm")>-1)||(document.URL.indexOf("/money/front.htm")>-1)||(document.URL.indexOf("/travel/front.htm")>-1)||(document.URL.indexOf("/tech/front.htm")>-1)||(document.URL.indexOf("/weather/weatherfront.aspx")>-1);
	} else {
		doit= (document.URL.indexOf(".htm")>-1) && (document.URL.indexOf("usafront.htm")==-1);
	}
	doit= doit &&((usat.cookie.get("zagCookie")=="0") &&((usat.cookie.get("zagSession")+"").indexOf("error")==-1));
	return doit;
} //function zagFromHereTest()

function doLoad(){
	if (zagEnabled && zagFromHereTest()){
		//do ZAG
		for(x= 0;x<document.links.length;x++){
			if ((document.links[x].target.indexOf("popup")==-1)&&(document.links[x].href.indexOf("javascrip")==-1)&&(document.links[x].href.indexOf("/survey/")==-1)&&(document.links[x].href.indexOf("marketing/legal.htm")==-1)&&(document.links[x].href.indexOf("ads/usat/inside_usat.htm")==-1)&&(document.links[x].href.indexOf("money/jobcenter/front.htm")==-1)&&(document.links[x].href.indexOf("educate/homesplash.htm")==-1)&&(document.links[x].href.indexOf("_ads/sweepstakes")==-1)&&(document.links[x].href.indexOf("subscribe.usatoday")==-1)&&(document.links[x].href.indexOf("newspaperads.com")==-1)&&(document.links[x].href.indexOf("ad.usatoday.com")==-1)&&(document.links[x].href.indexOf("newstracker")==-1)&&(document.links[x].href.indexOf("marketing/feedback.htm")==-1)&&(document.links[x].href.indexOf("qasb.pqarchiver")==-1)&&(document.links[x].href.indexOf("passport.com")==-1)&&(document.links[x].href.indexOf("moneyreg.aspx")==-1)&&(document.links[x].href.indexOf("portfolio.usatoday")==-1)&&(document.links[x].href.indexOf("email.usatoday")==-1)&&(document.links[x].href.indexOf("registration.usatoday")==-1)&&(document.links[x].href.indexOf("careerbuilder.com")==-1)&&(document.links[x].href.indexOf("eharmony.com")==-1)&&(document.links[x].href.indexOf("marketplace/front.htm")==-1)&&(document.links[x].href.indexOf("cars.com")==-1)&&(document.links[x].href.indexOf("concordpromotions.com")==-1)&&(document.links[x].href.indexOf("shermanstravel.com")==-1)&&(document.links[x].href.indexOf("#")==-1)) {
				document.links[x].onclick= new Function("return doLink(document.links["+x+"].href)");
			}//if
		} //for
	}//if
}

function doLink(strURL){
	newURL= zagStrInt+"?destination="+strURL+"&orignation="+zagStrUrl;
	window.location= newURL;
	return(false);
}
