// launch cb survey
// - requires /common/shared/js/p3_global.js

var old_survey_cookie	= "CB_cogix_homepage_stud_par";
var new_survey_cookie	= "CB_SURVEY";
var surveys           = new Array();
var sawSurveyToday    = false;

function writeCbSurveyCookie(){
	arg             = writeCbSurveyCookie.arguments;
	document.cookie = arg[0] + '; path=/; expires='+cookieTime(arg[1]);
}

function chance(size) {		// set (size) to "0" for 1/1 chance, "1" for 1/2 chance, etc.
	var chance = Math.round((Math.random()*size)); 	//	chance is a number between 0 and [size]
	return (chance == size) ? true : false;			//	is it a 1 / [size]?
}

function pad(n){ return n<10 ? ('0'+n) : n; }

function surveyDateCheck(date){
	var rc = false;
	for(var i=0;i<surveyIds.length;i++){
		if(surveys[surveyIds[i]] == date){
			rc = true;
		}
	}
	return rc;
}

function launch_survey(url, sample_size, cookie_name) {

	var now	= new Date();
	var dt  = now.getFullYear()+''+pad((now.getMonth()+1))+''+pad(now.getDate());

	// if surveyed before, no survey
	// wipe out old cookie and create new if necessary
	//
	if(document.cookie.indexOf(old_survey_cookie) != -1) {

		// delete the old cookie
		writeCbSurveyCookie(old_survey_cookie+'=viewed',-10);

		var start = document.cookie.indexOf(new_survey_cookie);
		if(start == -1) {
			// new cookie doesn't exist. just write a new one for the student homepage
			writeCbSurveyCookie(new_survey_cookie+'=000'+dt+'-',3000);
		}
		else {
			//new cookie exists. append student homepage value pair to existing cookie value
			var cookieValue = document.cookie.substr(start).split('-;')[0];
			writeCbSurveyCookie(new_survey_cookie+'=000'+dt+'-'+cookieValue.substr((new_survey_cookie.length+1),11)+'-',3000);
		}
		return false;
	}
	else {
		var start = document.cookie.indexOf(new_survey_cookie);
		if(start != -1) {

			// get the cookie string
			var cookieValue = document.cookie.substr(start);

			// split into individual values based on id. each value pair is 11 digits long.
			// a 3 digit id, and an 8 digit date - i.e. 00120050101
			// values are delimited by a dash '-'
			// the id-date pair tells the date the survey(id) was presented

			// store the id-date value pairs in an array

			var ar          = cookieValue.split(';')[0].split('-');

			for(var i=0;i<ar.length;i++){
				var surveyDate = ar[i].substr((new_survey_cookie.length+4),8);
				surveys[ar[i].substr((new_survey_cookie.length+1),3)] = surveyDate;
				if(surveyDate == dt){
					sawSurveyToday = true;
				}
			}

			// was the survey given?
			if(surveys[cookie_name] == undefined){

				// was any survey already given today?
				if(!sawSurveyToday){

					// should the sample be given based on some random visititation formula?
					if(chance(sample_size)) {

						// met all criteria show a survey
						// new cookie exists. append value pair for this survey to existing cookie value

						var cookieValue = document.cookie.substr(start).split('-;')[0];
						writeCbSurveyCookie(new_survey_cookie+'='+cookie_name+dt+'-'+cookieValue.substr((new_survey_cookie.length+1),11)+'-',3000);
						return popup(url,'survey',500,600);
					}
				}
			}
			else {
				return false;
			}
		}
		else {
			// if user is lucky, show survey
			if(chance(sample_size)) {
				writeCbSurveyCookie(new_survey_cookie+'='+cookie_name+dt+'-',3000);
				return popup(url,'survey',500,600);
			}
		}
	}
}
