var d = document;

function nscrListen(id, prod) {
	location.href = "/trackpass/scanner/napa/drivers/" + id + ".html";

}

function nscrToggleImg( imgName, imgLocation )
{
	document[imgName].src = imgLocation ;
}

//domain fix
var cnnDocDomain = '';
if(location.hostname.indexOf('.nascar.com')>0) {cnnDocDomain='nascar.com';}
if(location.hostname.indexOf('turner.com')>0) {if(document.layers){cnnDocDomain='turner.com:'+location.port;}else{cnnDocDomain='turner.com';}}

function getCookies() {
	var hash = new Array;
	if (document.cookie != null) {
		var a = document.cookie.split('; ');
		for (var i=0; i < a.length; i++) {
			var nv = a[i].split('=');
			if (nv[1] != null) {
				hash[nv[0]] = nv[1];
			}
		}
	}
	return hash;
}

function setCookies(name) {
	var expDate = new Date();
	expDate.setDate(expDate.getDate() + 365);
	document.cookie = name + '=true;expires=' + expDate.toGMTString() + ';domain=' + cnnDocDomain + ';path=/';
}

var cs = getCookies();
var authIDCookie = cs['authid'];
var cnnIDCookie = cs['CNNid'];
var gsCookie = cs['napa_scanner'];
var MSIBCookie = cs['nscrMSIBservice'];


function checkUserAuth() {
	if (location.hostname.indexOf('.turner.com') == -1) {
		if (MSIBCookie) {
			if (!authIDCookie || !cnnIDCookie) {
				/* show login */
				document.getElementById('custom-auth-login').style.display = 'block';
			} else if (!gsCookie || gsCookie.indexOf(cnnIDCookie) == -1) {
				/* show optin */
				document.getElementById('custom-auth-optin').style.display = 'block';
			} else {
				/* show app */
				document.getElementById('app').style.display = 'block';
			}
		} else {
			// set nscrMSIBservice cookie
			setCookies('nscrMSIBservice');
			/* redirect to MSIB to track the PID */
			location.replace("https://secure.audience.nascar.com/services/nascar/flow/services?pid=nascar.trackpass.napascanner&url=http://www.nascar.com/trackpass/scanner/napa/");
		}
	} else {
		/* show app */
		document.getElementById('app').style.display = 'block';
	}
}

function cnnRedirectParent(url) {
	if (window.opener && !window.opener.closed) {
		window.opener.location = url;
	} else {
		window.open(url);
	}
}


