var surveyname = "BCO_Survey_200911";      	// set name of survey here
var redirectpath = "http://www.breastcancer.org/utils/survey_request.html";	// set path for redirect here
var threshold = 3;						 	// set number of site session page views before redirect

var indexcookie = surveyname + "Index";
var sessioncookie = surveyname + "Session";
var refercookie = surveyname + "Refer";
var hitCount = 1;

function trackHit() {
	if ( readCookie(indexcookie) != null ) {
		var value = parseInt(readCookie(indexcookie));
	
		if ( value == -30 )								// -30 = they opted to maybe take this survey later
			{
			if ( readCookie(sessioncookie) == null )	// only reset their hitCount if they've started a new session (i.e. next visit)
				hitCount = 1;
			else
				hitCount = -30;
			}
		else if ( value >= 0 )							// they are accumulating hits
			{
			if ( readCookie(sessioncookie) == null )	// if this is a new session then reset hit counter
				hitCount = 1;
			else
				hitCount = value + 1;
			}
		else
			hitCount = value;
	}
	
	if ( hitCount > threshold ) {
			createCookie(refercookie, window.location.href);
			window.location.href = redirectpath;
	}
	
	createCookie(indexcookie, hitCount, 365);
	createCookie(sessioncookie, true);
}

function noSurvey() {
	createCookie(indexcookie, -60, 365); // mark indexcookie to not bother user anymore
	redirectback();
}

function surveyLater() {
	createCookie(indexcookie, -30, 365); // mark indexcookie to not bother user until next session
	redirectback();
}

function tookSurvey() {
	createCookie(indexcookie, -90, 365); // mark indexcookie to not bother user anymore
}

function redirectback() {
	referpage = readCookie(refercookie);
	if (referpage == null) { referpage = "/"; } 
	window.location.href = referpage; //redirect bach to referrer page
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/; domain=.breastcancer.org";
}

