/*********
* Config *
*********/
var zagEnabled= 1; // use zag form?
var ZAGEnabled= 1; // do anything at all?
var zagPrefix= 'http://reg.usatoday.com/registration/zag4';
// for development only: zagPrefix='/ZagWebTier';
var gciUsatURL= zagPrefix+'/gciUSATv1.js';
var throttle=5;


/************
* utilities *
************/
function getCookie(nm) {
// when we find multiple cookies with the same name, try to
// return the value containing the most information
	var v= (' '+document.cookie).match(new RegExp(' '+nm+'=[^;]*', 'g')) || [];
	var l= 0;    		// length of match
	var r= null;		// result
	for (var j= 0; j < v.length; j++) {
		if (v[j].length > l) {
			l= v[j].length;
			r= unescape(v[j].substring(2+nm.length));
		}
	}
	return r;
}

function setSessionCookie(nm, val) { /* only for session cookies */
	document.cookie= nm+'='+escape(val)+'; path=/; domain=.usatoday.com';
}

function nowDtNum(y,m,d) { /* pack today's date */
	var now= new Date();
	var yr= now.getFullYear();
	yr+= yr < 200 ?1900 :0; /* for broken browser implementations */
	yr+= yr < 1970 ?100 :0; /* for broken browser implementations */
	var mn= now.getMonth();
	var dt= now.getDate()-1;
	return ((yr-2000)*12+mn)*31+dt;
}

/*****************
* gcion wrappers *
*****************/
// HACK: try to emulate callback behavior
function gciSetCallback(fn) {
	var interval= null;
	var old= GCION.Cookies.Value ?GCION.Cookies.Value :null;
	function watchGCIONID() {
		if (!window.GCION) { /* page is unloading */
			clearInterval(interval);
		} else {
			if (GCION.Cookies.Value && old != GCION.Cookies.Value) {
				clearInterval(interval);
				fn(GCION.Cookies.Value);
			}
		}
	}
	interval= setInterval(watchGCIONID, 500);
}

function gcionSet(fnName) {
	/* note: this has to be my editted version */
	GCION.Sites.USAT.ConvertToGCION();
	gciSetCallback(fnName);
}

function gcionGet(fnName) {
	GCION.Utils.Include.Once(GCION.Utils.Data.GetGcionUrl("q=3&NoCookie=1"));
	gciSetCallback(fnName);
}

function gcionWill(fn, isSet) {
	var interval= null;
	function waitForIE() {
		if (!window.ZAGEnabled) { /* system turned off or page is unloading */
			clearInterval(interval);
		} else {
			if (window.GCION) { // have GCION, assume it's populated
				clearInterval(interval);
				if (isSet) {
					gcionSet(fn);
				} else {
					gcionGet(fn);
				}
			}
		}
	}
	if (window.GCION) {
		if (isSet) {
			gcionSet(fn);
		} else {
			gcionGet(fn);
		}
	} else {
		interval= setInterval(waitForIE, 50);
	}
}

function gciCallback(fn, doSet) {
/* This is the start of, potentially, a two level chain of callbacks
 * First, we have to guarantee that the gciUsatURL has been loaded;
 * this should be a simple document.Write() of a couple script tags
 * but IE chokes unless it's given some time to calm down
 * -- zagExecuteWithGciUsat() handles this
 * Second, once that is loaded, we need to query gci's server
 * (doSet indicates which query we need)
 * -- gcionWill() handles this
 * Finally, when we've gotten the response from gci, we can run
 * the function named by fnName
 */
	zagExecuteWithGciUsat(function() {gcionWill(fn, doSet)});
}

/********************************
* execute after loading gciUsatURL *
* (work around an IE problem)   *
********************************/
var zagWillHaveGciUsat= 0;
var zagHaveGciUsat= 0;
var zagExecuteWithGciUsatList= [];
function gciUsatLoadedCallback() {
	zagHaveGciUsat= 1;
	for (var j in zagExecuteWithGciUsatList) {
		try {
			zagExecuteWithGciUsatList[j]();
		} catch(er) {}
	}
}
function zagExecuteWithGciUsat(fn) {
	if (!zagHaveGciUsat) {
		zagExecuteWithGciUsatList.push(fn);
		if (!zagWillHaveGciUsat) {
			var script= document.createElement('script');
			script.setAttribute('language', 'javascript');
			script.setAttribute('src', gciUsatURL);
			document.getElementsByTagName('head')[0].appendChild(script);
			zagWillHaveGciUsat= script;
		}
	} else {
		fn();
	}
}


/**************
* zag support *
**************/
var zago= '-1';
function gci2Zag(gcio) {
	if (gcio) {
		gcio.adr= null;
		var zago= GCION.Sites.USAT.ParseZago(getCookie('zagCookie'));
		if (gcio.gcionid) zago.gci= gcio.gcionid;
 		if (!GCION.Utils.Data.IsNullOrEmpty(gcio.zip)) {
 			var status='GCI0';
 			zago.version= 3;
			for (var p in gcio)
				if (GCION.Utils.Data.IsNullOrEmpty(zago[p]) && 3 == p.length && (status='GCI' /*not a test*/))
					if ('gen' == p)
						zago['fem']= 2-gcio[p];
					else
						zago[p]= gcio[p];
			zago.gdt= zago.gdt= nowDtNum();
			zago.sav= 0;
			GCION.Sites.USAT.SetZago(zago);
			postzago(zago, 'Saved');
			setSessionCookie('zagSession', status);
		} else {
			GCION.Sites.USAT.SetZago(zago);
			if ('PreSync' == getCookie('zagSession')) {
				gciCallback(zag2Gci, 1);
				setSessionCookie('zagSession', 'PreSynced');
			} else if (-1 < (""+getCookie('TData')).indexOf('AMS_00443')) {
				setSessionCookie('zagSession', '0');
				doLoadZag();
			} else {
				setSessionCookie('zagSession', 'Ready');
			}
		}
	} else {
		setSessionCookie('zagSession', 'GCI Down');
	}
}

function zag2Gci(gcio) {
	if (gcio) {
		/* in principle, we have updated GCI with our zag data */
		var zago= GCION.Sites.USAT.ParseZago(getCookie('zagCookie'));
		zago.gci= gcio.gcionid;
		zago.gdt= nowDtNum();
		zago.sav= 0;
		GCION.Sites.USAT.SetZago(zago);
		setSessionCookie('zagSession', 'Synced');
		if (!zago.sav) { // sav: no
			postzago(zago, 'Saved It');
		}
	} else {
		setSessionCookie('zagSession', 'GCI down');
	}
}

function postzago(data, finalState) {
	/* pack up GCION.Cookies.value into a query string,
	/* and tell postzago about it */
	var url= zagPrefix+'/postzago.ashx?log=1&keyCd=zag002'
	for (p in data)
		if (3 == p.length)
			url+='&'+escape(p)+'='+escape(data[p]);
	var img= document.createElement('img');
	img.onload= function() {
		var zs= ''+getCookie("zagSession"); /* double save to avoid browsers that are 'too smart' */
		setSessionCookie("zagSession", finalState);
		if (-1 < zs.indexOf("error"))
			setSessionCookie("zagSession", zs);
	};
	img.setAttribute('src', url);
}

function doLoadZag() {
	var url= document.URL;
	if (!zagEnabled) return;
	if (-1==url.indexOf(".htm")) return;
	if (-1<url.indexOf("usafront.htm")) return;
	var exclude= ['javascrip', '/survey/', 'marketing/legal.htm',
		'ads/usat/inside_usat.htm', 'money/jobcenter/front.htm',
		'educate/homesplash.htm', '_ads/sweepstakes',
		'subscribe.usatoday', 'newspaperads.com', 'ad.usatoday.com',
		'newstracker', 'marketing/feedback.htm', 'qasb.pqarchiver',
		'passport.com', 'moneyreg.aspx', 'portfolio.usatoday',
		'email.usatoday', 'registration.usatoday', 'careerbuilder.com',
		'eharmony.com', 'marketplace/front.htm', 'cars.com',
		'concordpromotions.com', 'shermanstravel.com', '.4info.net/nfl', '#'];
	var links= document.links;
	for (x= 0; x<links.length; x++) {
		var link=links[x]
		var href=link.href;
		var check= function(str) {return -1==href.indexOf(str)}
		var ok= -1==link.target.indexOf('popup');
		if (ok) for (var y= 0; y<exclude.length; y++) {
			if (!(ok= check(exclude[y]))) break;
		}
		if (ok) link.onclick= getZagLink(href);
	}
}

function getZagLink(href) {
	return function() {
		window.location= zagPrefix+'/zago.htm?destination='+escape(href)+'&origination='+escape(window.location.toString());
		return false;
	}
}

function pickup() {
	// system dropped cookies on floor, pick them up
	setSessionCookie('zagSession', 'Saving');
	zagExecuteWithGciUsat(pickupContinued);
}

function pickupContinued() {
	postzago(GCION.Sites.USAT.ParseZago(getCookie("zagCookie")), "Saved Session");
}


/*************************
* fundamental zag logic  *
* invoked elsewhere, so  *
* interstitial logic can *
* disable                *
*************************/
function doLoad() {
	if (throttle <= parseInt((""+getCookie('RMID')).substring(0,7), 16)%100)
		setSessionCookie('zagSession', 'Throttled');

	var sess= getCookie('zagSession');
	if ("0" == sess) {
		doLoadZag();
	} else if (null == sess || 'Initialized' == sess || 'Session Saved' == sess) {
		zago= ""+getCookie('zagCookie');
		var zver= parseInt(zago);
		if (-1 < zago.indexOf('n676474v')) { // have gdt means we synced with gci
			if (-1 < zago.indexOf('n736176v0')) { // sav: no (not yet)
				pickup();
			} else {
				setSessionCookie('zagSession', 'Done');
			}
		} else if (0 < zver && 2 != zver) { // zver 1 or 3+: user zagged
			if (-1 < zago.indexOf('n676369v')) { // gci means we have gcionid
				setSessionCookie('zagSession', 'Sync');
				gciCallback(zag2Gci, 1);
			} else if (-1 < zago.indexOf('n736176v0')) { // sav: no (not yet)
				pickup();
			} else { // no gcionid, not saved, get gcionid (and maybe zag)
				setSessionCookie('zagSession', 'PreSync');
				gciCallback(gci2Zag, 0);
			}
		} else { // user not zagged, here, maybe elsewhere?
			setSessionCookie('zagSession', 'Checking');
			gciCallback(gci2Zag, 0)
		}
	}
}


