function WM_readCookie(name) {
        if(document.cookie == '') {
            return "";
        } else {
            //alert(document.cookie);
            var firstChar, lastChar;
                var theBigCookie = document.cookie;
                firstChar = theBigCookie.indexOf(name);
                var NN2Hack = firstChar + name.length;
                if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) {
                        firstChar += name.length + 1;
                        lastChar = theBigCookie.indexOf(';', firstChar);
                        if(lastChar == -1) lastChar = theBigCookie.length;
                        return unescape(theBigCookie.substring(firstChar, lastChar));
                } else {
                    //alert('no cookie found of that name');
                        return "";
                }
        }
} // WM_readCookie

function WM_browserAcceptsCookies() {
        var WM_acceptsCookies = false;
        if ( document.cookie == '' ) {
                document.cookie = 'WM_acceptsCookies=yes';
                if ( document.cookie.indexOf( 'WM_acceptsCookies=yes' ) != -1 ) {
                        WM_acceptsCookies = true;
                }
        } else {
                WM_acceptsCookies = true;
        }

        return ( WM_acceptsCookies );
}

function getArgs() {
	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split(",");
	for (var i = 0; i < pairs.length; i++) {
		var pos = pairs[i].indexOf('=');
		if (pos == -1) continue;
		var argname = pairs[i].substring(0,pos);
		var value = pairs[i].substring(pos+1);
		args[argname] = unescape(value);
	}
	return args;
}

function MD_random(r1, r2) {
  if (r2 > r1) return (Math.round(Math.random()*(r2-r1))+r1);
  else return (Math.round(Math.random()*(r1-r2))+r2);
}

function doRand() {
  	
	//set random number range here
	totalran = 300;
	
	//set frequency here
	freq = 1;
	outof = 300;
	//set frequency here
	
	myseconds = MD_random(1, totalran);
	convfreq = (freq * totalran) / outof;
	mymultiple = totalran / convfreq;

	for(i=0; i<=convfreq; i++) {
		tempsec = i * mymultiple;
		if(myseconds == tempsec) {
			myshow = checkCookie("xsurvey");
			if(myshow && WM_browserAcceptsCookies()) {
		window.open('/survey/2002/multisite/survey.exclude.html','survey','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=250,height=420');
	 			
			//WM_setCookie("cnnSurveyWebCookie", "surVeyed", 120, "/");
	 		}
			break;
		}
	}
}

/* 
 * Scope determines frequency. so, for 1 in 300, scope = 300.
 */
function altDoRand(scope) {
	var randomNum = MD_random(1, scope);
	if (randomNum == scope) {
		if (WM_browserAcceptsCookies() && (WM_readCookie( "xsurvey" ) == '')) {
			window.open('/survey/2002/multisite/survey.exclude.html', 'survey', 'scrollbars=no,resizable=no,width=250,height=420');
			// WM_setCookie( "cnnSurveyWebCookie", "surVeyed", 120, "/", ".cnn.com" );
		}
	}
}

/*
 * helper for doGTC()
 */
function isGoodTime() {
        var Today = new Date();

        // no < Monday noon est < yes < Wednesday 7am est < no < Wednesday 11am est < Wednesday noon est < no
        // utc = est +5 
        if ((Today.getUTCDay() == 1) && (Today.getUTCHours() >= 17 )) {
                return true;
        } else if (Today.getUTCDay() == 2) {
                return true;
        } else if ((Today.getUTCDay() == 3) && (Today.getUTCHours() < 17)) {
                if ((Today.getUTCHours() >= 12) && (Today.getUTCHours() <= 15)) {
                        return false;
                }
                return true;
        }
        return false;
}

/*
 * Guess the Cover
 */
function doGTC() {
        var args = getArgs();
        var isFromCNN = (args.cnn == 'yes');
        //var isMondayOrTuesday = ((today.getDay() == 1) || (today.getDay() == 2));
        if (isGoodTime() && !isFromCNN && WM_browserAcceptsCookies() && (WM_readCookie("xGTC") == '')) {
                WM_setCookie("xGTC", "set", 24); // expire in 24 hours
                window.open('http://guessthecover.si.cnn.com/mp/gtc-entry','gtc','scrollbars=yes,resizable=yes,width=495,height=535');
        }
}

/*
 * Trvia Promo
 */
function triviaPromo() {
        var args = getArgs();
        var isFromCNN = (args.cnn == 'yes');
        if (!isFromCNN && WM_browserAcceptsCookies() && (WM_readCookie("xTrivia") == '')) {
                WM_setCookie("xTrivia", "set", 24); // expire in 24 hours
                window.open('/advertisers/si/trivia/flashHolder.html','300x250','scrollbars=no,resizable=yes,width=300,height=250');
        }
}

/*
 * TNT Thursday
 */
function doTNT() {

	//return false;

        var args = getArgs();
        var isFromCNN = (args.cnn == 'yes');
        var today = new Date();
        //var isWednesdayOrThursday = ((today.getDay() == 3) || (today.getDay() == 4));
	var isWednesdayOrThursday = false;
        if (isWednesdayOrThursday && !isFromCNN && WM_browserAcceptsCookies() && (WM_readCookie("xTNT") == '')) {
                WM_setCookie("xTNT", "set", 24); // expire in 24 hours
                window.open('/advertisers/2002/si/tnt_nba2.html','gtc','scrollbars=no,resizable=no,width=250,height=250');
        }
}
 
/*
 * Swimsuit Survey
 */
function doSwim(scope) {
	var randomNum = MD_random(1, scope);
	if (randomNum == scope) {
		if (WM_browserAcceptsCookies() && (WM_readCookie("xSwimsuit2003Survey") == '')) {
			window.open('/survey/2003/swimsuit/surveyInvite.html', 'surveyInvite', 'scrollbars=no,resizable=yes,width=410,height=300');
		}
	}
}

/*
 * Profiler Survey 
 */
function doProfileSurvey(scope) {
	var randomNum = MD_random(1, scope);
	if (randomNum == scope) {
		if (WM_browserAcceptsCookies() && (WM_readCookie("xTENNISsurvey") == '')) {
			window.open('/survey/2002/profiler/tennis/surveyInvite.html', 'surveyInvite', 'scrollbars=no,resizable=no,width=250,height=250');
		}
	}
}


/*
 * Insight Survey 
 */
function doInsightSurvey(scope) {
	var randomNum = MD_random(1, scope);
	if (randomNum == scope) {
		if (WM_browserAcceptsCookies() && (WM_readCookie("x_insight_survey") == '')) {
			window.open('/survey/2003/insight/surveyInvite.html', 'surveyInvite', 'scrollbars=yes,resizable=no,width=700,height=500');
		}
	}
}

/*
 * Fantasy Survey
 */
function siFantasySurvey() {

	return false;

	var scope = 2;
	var randomNum = MD_random(1, scope);
	if (randomNum == scope) {
		if (WM_browserAcceptsCookies() && (WM_readCookie("xFantasySurvey2003") == '')) {
			window.open('http://sportsillustrated.cnn.com/survey/2003/fantasy/surveyInvite.html', 'surveyInvite', 'scrollbars=yes,resizable=yes,width=410,height=300');
		}
	}
} 

function checkCookie(cookieName) {
 	if(WM_readCookie(cookieName) == "") {
		return true;
	}
	else {
		return false;
		
	}
}
 

function gallerySubPop (url) 
{
	popupWin = window.open(url, 'newWindow', 'menubar=no,scrollbars=no,resizable=no,width=500,height=520');
	popupWin.focus();
}
