/* checks login form for empty / incorrect fields -- borrowed from member_auth.jsp */
function lookover() {
	if (document.auth1.user_email.value == ""){
		alert("Please enter your Email Address");
		document.auth1.user_email.focus();
		return false;
   	} else {
      	var emailReg = "^[\\w-_\.\'\+]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
      	var regex = new RegExp(emailReg);
      	if (!regex.test(document.auth1.user_email.value)) {
        	alert("Please enter a valid E-mail address");
         	document.auth1.user_email.focus();
         	return false;
      	}
   	}
   	if (document.auth1.password.value == ""){
      	alert("Please enter your password");
      	document.auth1.password.focus();
      	return false;
   	}
   	return true;
}

/* finds and passes along the requested content url */
function getRequested() {
	var parameter = location.search;				// Grabs "?" + "string" (url of content user is trying to access)
	var newParam = unescape(parameter);				// unencodes the string
	var url = newParam.indexOf('http');
	for (var i = url; i <= newParam.length; i++) {
		if (url != -1) {
			document.auth1.requestedpage.value = newParam.substring(url,i);		// removes "?" from the passed variable and sets the content url as the requested page
		} else {
			document.auth1.requestedpage.value = 'http://www.nascar.com/trackpass/';
		}
	}
}

/* Custom denial redirects for limited-access partners */
function getReferrer() {
	var parameter = location.search;				// Grabs "?" + "string" (url of content user is trying to access)
	var newParam = unescape(parameter);		
	var comcastDenial = '/multimedia/trackpass/landing/interim/error/comcast/index.html';
	if (newParam.indexOf('referrer=comcast') > -1) {		
		location.replace(comcastDenial+parameter);
	}
}

function getErrorPage() {
     var dest = getDestinationURL(window.location);
     document.auth1.error_page.value += ("?url=" + unescape(dest));
     //alert("errorPage: " + document.auth1.error_page.value);
}

/* Gets the destination URL (the location of the requested content */
function getDestinationURL(location) {
     var regex = /url=([^&]+)/
     if (regex.test(location)) {
         var matches = regex.exec(location);
         return matches[1];
     } else {
         return null;
     }
}


/* self explanatory */
function findpassword() {
   	if (document.auth1.user_email.value != "") {
	open('https://secure.audience.nascar.com/services/nascar/pitpass/passHint.do?source=nascar&pid=nascar.default&user_email='+document.auth1.user_email.value+'&url=http%3A%2F%2Fwww.nascar.com%2Ftrackpass%2F');
   	} else {
      	alert("Please enter your Email Address");
      	document.auth1.user_email.focus();
   	}
}

/* opens conversion landing page */
function openInfo() {
	window.open('/multimedia/trackpass/landing/conversion/');
	setTimeout('self.close()','2000');
}

/* opens conversion landing page */
function free() {
	window.open('https://secure.audience.nascar.com/services/nascar/pitpass/member_auth.jsp?source=nascar&pid=nascar.richmond&url=https%3A%2F%2Fsecure.audience.nascar.com%2Fservices%2Fnascar%2Frichmondfreeweekend%2Frichmondauth.jsp%3Furl%3Dhttp%3A%2F%2Fwww.nascar.com%2Fpromos%2F2005%2Frichmond%2Ffree%2Findex.html');
	setTimeout('self.close()','2000');
}

/* detects error code and displays appropriate message */
function getError() {
	var parameter = location.search;							// Grabs "?" + "string" (error code from user's incorrect entry)
	if (parameter.indexOf('error=Bad%20email') > -1) {					// Unrecognized email address
		document.getElementById('badEmail').style.display='block';
	} else if (parameter.indexOf('error=Bad%20password') > -1) {		// Bad password
		document.getElementById('badPass').style.display='block';
	} else if (parameter.indexOf('error=2') > -1) {											// Not a subscriber
		document.getElementById('notSub').style.display='block';
	} else if (parameter.indexOf('error=3') > -1) {											// Expired subscription
		document.getElementById('expiredSub').style.display='block';
	} else if (parameter.indexOf('error=4') > -1) {											// Wrong subscription
		document.getElementById('wrongSub').style.display='block';
	} else if (parameter.indexOf('error=5') > -1) {											// Backend (LDAP) error
		document.getElementById('intError').style.display='block';
	} else if (parameter.indexOf('errorMsg=User%20not%20found') > -1) {						// Unrecognized email address (Member Services)
		document.getElementById('badEmail').style.display='block';
	} else if (parameter.indexOf('errorMsg=Password%20did%20not%20match') > -1) {			// Bad password (Member Services)
		document.getElementById('badPass').style.display='block';
	}
}


function getCookies() {
	cc = checkCookies();
	var name = "msemail";
	with (document) {
		if (cc[name]) {
			document.auth1.user_email.value = unescape(cc[name]);
		}
	}
}


function checkCookies() {
	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;
}


/* opens TP or Scanner landing (buy) pages */
var agt=navigator.userAgent.toLowerCase();
var is_aol=(agt.indexOf("aol") != -1);

function signupTP(mc) {
    var landingUrl="";
    if (is_aol) {
        landingUrl='http://premium.nascar.com/multimedia/trackpass/landing/aol.html?mc=';		// IF AOL > aol landing page
    } else { 
        landingUrl='http://premium.nascar.com/trackpass/landing/index.html?mc=';		// ELSE > main landing page
    }
    window.open(landingUrl+mc);
	setTimeout('self.close()','2000');
}

function signupScanner(mc) {
    var landingUrl="";
    /*if ( is_aol ) {
        landingUrl=('http://premium.nascar.com/multimedia/trackpass/landing/scanner_aol.html?mc=');		// IF AOL > aol landing page
    } else {*/
        landingUrl=('http://premium.nascar.com/trackpass/landing/index.html?mc=');    		// ELSE > main landing page
    //}
    window.open(landingUrl+mc);
	setTimeout('self.close()','2000');
}