function createCookie(name, value, minutesToExpire) {
	var expires = "";
	if (minutesToExpire) {
		var date = new Date();
		date.setTime(date.getTime() + (minutesToExpire * 60 * 1000));
		expires = "; expires=" + date.toGMTString();
	} 		
	document.cookie = name + "=" + value + expires + "; path=/";
}

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 eraseCookie(name) {
	createCookie(name,"",-1);
}

// code from Vivek...
var timeout;
// to make the first popover message appear
function appear(){ 
	var the_style = getStyle("floatingflash");
	if (the_style) {
		if (the_style.display == 'none')  the_style.display = 'block';
		var current_top = parseInt(the_style.top);
		// you can play around by changing values here to change the popover transitioning affect
		var new_top = current_top + 5;  
		if (document.layers) {
			the_style.top = new_top;
		} else {
			the_style.top = new_top + "px";
		}
		if (new_top < 150) {
			the_timeout = setTimeout('appear();', 10);
		} else {
			// survey has been positioned on the screen; mark it as viewed
			markSurveyViewed()
			clearInterval(surveyInterval);
		}
	}
} 

//to make the second popover message appear
function appear1(){ 
	// show second popup
	var the_style = getStyle("floatingflash1");
	if (the_style) {
		if (the_style.display == 'none')  the_style.display = 'block';
		var current_top = parseInt(the_style.top);
		// you can play around by changing values here to change the popover transitioning affect
		var new_top = current_top + 5;
		if (document.layers) {
			the_style.top = "150px";
		} else {
			the_style.top = "150px";
		}
		if (new_top < 150) {
			the_timeout = setTimeout('appear1();',10);
		}
	}
}

//to make the first popover message disappear
function disappear() { 
	var the_style = getStyle("floatingflash");
	the_style.display = 'none';
}

//to make the second popover message disappear
function disappear1() { 
	disappear();
	var the_style = getStyle("floatingflash1");
	the_style.display = 'none';
}

function getStyle(ref) {
	if(document.getElementById && document.getElementById(ref)) {
		return document.getElementById(ref).style;
	} else if (document.all && document.all(ref)) {
		return document.all(ref).style;
	} else if (document.layers && document.layers[ref]) {
		return document.layers[ref];
	} else {
		//alert("element not found: " + ref);
		return false;
	}
}

// Close window after __ number of seconds
closetime = 3; 
// 0 = do not close, anything else = number of seconds
function Start(URL, WIDTH, HEIGHT) {
	//var the_style1 = getStyle("floatingflash");
	windowprops = "left=50,top=50,width=" + WIDTH + ",height=" + HEIGHT;
	preview = window.open(URL, "_self", windowprops);
	if (closetime) setTimeout("disappear();", closetime*1000);
	window.focus();
}

function doPopup() {
	window.open (SURVEY_URL, "mywindow"); 
	disappear1();
}
// ...code from Vivek

function showSurvey() {
	// there might be a second browser with applicable page, that already popped up the survey
	var userSawSurvey = readCookie(SURVEY_VIEWED_COOKIE_NAME);
	if ("Y" == userSawSurvey) {
		return;
	}
	// code from Vivek: show first popup
	appear();
}

function markSurveyViewed() {
	createCookie(SURVEY_VIEWED_COOKIE_NAME, "Y", 10000000);
}

function resetSurveyCookies() {
	eraseCookie(SURVEY_VIEWED_COOKIE_NAME);
	eraseCookie(SURVEY_TIME_SEC_SPENT_ON_APPLICABLE_PAGES_COOKIE_NAME);
}

function countDownSurvey() {
	d("inside of session countdown");
	var sess = readCookie(SURVEY_TIME_SEC_SPENT_ON_APPLICABLE_PAGES_COOKIE_NAME);
	var timeOnAppPagesSec;
	d("sess=" + sess);
	if (null == sess) {
		// this shouldn't be happening - we get into interval handler only when this cookie is set...
		d("timeOnAppPagesSec=null; something is wrong!!!");
		return;
	}
	timeOnAppPagesSec = Math.floor(Number(sess)) + SURVEY_COUNTDOWN_INTERVAL_SEC;
	d("after increment; timeOnAppPagesSec=" + timeOnAppPagesSec);
	// remember how much time we spent on applicable page(s) after this interval tick
	createCookie(SURVEY_TIME_SEC_SPENT_ON_APPLICABLE_PAGES_COOKIE_NAME, timeOnAppPagesSec, SESSION_EXPIRE_MIN);
	if (SHOW_POPUP_THRESHOLD_SEC < timeOnAppPagesSec) {
		// this happens if user returned to applicable page after session passed SHOW_POPUP_THRESHOLD_SEC
		d("passed the popup threshold; show the survey");
		showSurvey();
	} else {
		d("wait until next countdown tick");
	}
}

function d(s) {
	if (ENABLE_DEBUG) {
		console.log("Survey DEBUG: ", s);
	}
}

function main() {
	// turn off the survey by returning after entering main...
	return;
	if (surveyLanguage == "FR") {
		SURVEY_URL = SURVEY_URL_FR;
	}
	// check if cookies are enabled
	var cookieEnabled = (navigator.cookieEnabled) ? true : false;
	// if not IE4+ nor NS6+
	if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled){ 
		document.cookie="testcookie";
		cookieEnabled=(document.cookie.indexOf("testcookie") != -1) ? true : false;
	}
	if (!cookieEnabled) {
		return;
	}
	// did user see the servey?
	userSawSurvey = readCookie(SURVEY_VIEWED_COOKIE_NAME);
	if ("Y" == userSawSurvey) {
		d("user saw servey; bailing out");
		return;
	}
	// see if session cookie exists
	var sess = readCookie(SURVEY_TIME_SEC_SPENT_ON_APPLICABLE_PAGES_COOKIE_NAME);
	d("sess=" + sess);
	if (null == sess) {
		// session cookie doesn't exist or expired - roll a dice...
		if (1 != Math.floor(Math.random() * SURVEY_FREQUENCY)) {
			// no luck - mark user as if he saw the survey
			luckyUser = "no; setting user survey as marked; reset cookies to keep testing";
			d("luckyUser=" + luckyUser);
			markSurveyViewed();
		} else {
			luckyUser = "yes; this user is selected for the survey; start keeping tracking the session";
			d("luckyUser=" + luckyUser);
			surveyStarted = "yes";
			createCookie(SURVEY_TIME_SEC_SPENT_ON_APPLICABLE_PAGES_COOKIE_NAME, 0, SESSION_EXPIRE_MIN);
			d("starting session countdown");
			surveyInterval = setInterval("countDownSurvey()", 1000 * SURVEY_COUNTDOWN_INTERVAL_SEC);
		}
	} else {
		// countdown is active - restart from where we left it...
		d("resuming session countdown");
		surveyInterval = setInterval("countDownSurvey()", 1000 * SURVEY_COUNTDOWN_INTERVAL_SEC);
	}
}

// config vars
var ENABLE_DEBUG = false;
var SURVEY_TIME_SEC_SPENT_ON_APPLICABLE_PAGES_COOKIE_NAME = "SURVEY_SESSION_COUNTDOWN_340967456854";
var SURVEY_VIEWED_COOKIE_NAME = "SURVEY_VIEWED_12837515456";
var SURVEY_COUNTDOWN_INTERVAL_SEC = 1;
var SESSION_EXPIRE_MIN = 30;
var SHOW_POPUP_THRESHOLD_SEC = 60;
var SURVEY_FREQUENCY = 5;
var SURVEY_URL_EN = "http://cc3na2.voxco.com/survey/intweb.dll/project/webspace1/BMOCOM_EN/PIN=&urlimport=1&questlist=LANG&LANG=EN";
var SURVEY_URL_FR = "http://cc3na2.voxco.com/survey/intweb.dll/project/webspace1/BMOCOM_FR/PIN=&urlimport=1&questlist=LANG&LANG=FR";
var SURVEY_URL = SURVEY_URL_EN;

// working global vars
var surveyInterval;
var userSawSurvey;
var surveyStarted = "no";
var luckyUser = "";

