
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');
        }
}

/*
 * TNT Thursday
 */
function doTNT() {
        var args = getArgs();
        var isFromCNN = (args.cnn == 'yes');
        var today = new Date();
        var isWednesdayOrThursday = ((today.getDay() == 3) || (today.getDay() == 4));
        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("xGOLFsurvey") == '')) {
			window.open('/survey/2002/profiler/nascar/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(scope) {

	return false;

	var randomNum = MD_random(1, scope);
	if (randomNum == scope) {
		if (WM_browserAcceptsCookies() && (WM_readCookie("xFantasySurvey") == '')) {
			window.open('/survey/2002/fantasy/surveyInvite.html', 'surveyInvite', 'scrollbars=yes,resizable=yes,width=410,height=300');
		}
	}
} 

function checkCookie(cookieName) {
 	if(WM_readCookie(cookieName) == "") {
		return true;
	}
	else {
		return false;
		
	}
}
 
