//*******************
//This .js file has onLoad and onUnload functions that should be included by the body tag of
//every page on QB, and other Intuit sites, that support the
//exit survey. The functions here are related to launching the exit survey from the main window, 
//keeping it in the background, and killing it.
//*******************

//Name of cookie that stores the mini-survey response from customers. This cookie is only set
//if the customer submits the mini-survey on the home page, so it can also be used to indicate
//if the the mini-survey has been filled out. Timeout set to 180 days
var ExitSurveyCookie = "QB_Exit_Survey_Message";
var ExitSurveyTimeout = 60*60*24*54*2000;

//Name of cookie used to indicate the customer is on on Intuit-owned site where the survey tracker
//window should stay in the background. Whenever this cookie is set, then the tracker window knows
//that it should stay in the background. Timeout set to 5 seconds.
var SurveyTrackerCookie = "Survey_Tracker";
var SurveyTrackerTimeout = 20000;
var SurveyTrackerValue = "TRUE";

//Constant value used to indicate if survey has been killed
var SurveyKilledMessage = "Kill_Survey";

//Name of survey tracker window that is popped up
var SurveyWindowName = "_survey";

//Constants used to place all survey related cookies at the root level of the Intuit domain
var COOKIE_DOMAIN_LOCAL = "intuit.com";
var ROOT_LEVEL_LOCAL = "/";

//Global variables used by the survey form
var submitCounter = 0;

//Open a tracker window, which will become the survey window when the user leaves Intuit
function OpenSurveyTracker(pReason,pPreviewPath,pExitPath,pSurveyWindowAttributes) {
	if (isSurveyKilled()) { return; }
	var soon = new Date();
	soon.setTime(soon.getTime() + ExitSurveyTimeout);
	sbweb.util.cookies.setCookie(ExitSurveyCookie,pReason,soon,ROOT_LEVEL_LOCAL,COOKIE_DOMAIN_LOCAL);
	surveyExitPath = pPreviewPath + "?exitPath=" + pExitPath;
	var surveyWindow = window.open(surveyExitPath,SurveyWindowName,pSurveyWindowAttributes);
	focus();
}

//See if the survey tracker window has been opened based on the ExitSurveyCookie
function isSurveyPending() {
	var cookieVal = sbweb.util.cookies.getCookie(ExitSurveyCookie);
	if (cookieVal == null) { return false; }
	if (cookieVal == "") { return false; }
	if (cookieVal == SurveyKilledMessage) { return false; }
	return true;
}

//This function can be used whenever the survey needs to be killed. It can be called from
//page events or from within the javascript.
function KillSurvey() {
	var soon = new Date();
	soon.setTime(soon.getTime() + ExitSurveyTimeout);
	sbweb.util.cookies.setCookie(ExitSurveyCookie,SurveyKilledMessage,soon,ROOT_LEVEL_LOCAL,COOKIE_DOMAIN_LOCAL);
}

function isSurveyKilled() {
	var cookieVal = sbweb.util.cookies.getCookie(ExitSurveyCookie);
	if (cookieVal == null) { return false; }
	if (cookieVal == "") { return false; }
	if (cookieVal == SurveyKilledMessage) { return true; }
	else { return false; }
}

//Function to test if the Tracker cookie is set. if it is, then it means the main
//window is still on an Intuit Site and the exit survey should not be launched.
function isMainWindowIntuit() {
	var cookieVal = sbweb.util.cookies.getCookie(SurveyTrackerCookie);
	if (cookieVal == null) { return false; } 
	if (cookieVal == "") { return false; }
	else { return true; }
}

//Default onLoad function for all QB pages including: product,staticpage template,comp chart, but NOT category
//Must be added as an onLoad event to the body tag of any page where the exit survey should stay in the background
//This function resets the 
function loadTracker() {
	sbweb.util.cookies.deleteCookie(SurveyTrackerCookie,ROOT_LEVEL_LOCAL,COOKIE_DOMAIN_LOCAL);
	sbweb.util.cookies.setCookie(SurveyTrackerCookie,SurveyTrackerValue,null,ROOT_LEVEL_LOCAL,COOKIE_DOMAIN_LOCAL);
}

//Default onUnloadFunction for all QB and Intuit pages that support the exit survey.
//This resets to the tracker cookie to one that will expire within a short time. The time
//interval should be enough to allow the next page to load and set the Tracker cookie again,
//so that the survey won't go off while someone is still browsing an Intuit site.
function unLoadTracker() {
	sbweb.util.cookies.deleteCookie(SurveyTrackerCookie,ROOT_LEVEL_LOCAL,COOKIE_DOMAIN_LOCAL);
	var soon = new Date();
	soon.setTime(soon.getTime() + SurveyTrackerTimeout);
	sbweb.util.cookies.setCookie(SurveyTrackerCookie,SurveyTrackerValue,soon,ROOT_LEVEL_LOCAL,COOKIE_DOMAIN_LOCAL);
}

function unLoadTrackerNow() {
	sbweb.util.cookies.deleteCookie(SurveyTrackerCookie,ROOT_LEVEL_LOCAL,COOKIE_DOMAIN_LOCAL);
	var now = new Date();
	sbweb.util.cookies.setCookie(SurveyTrackerCookie,SurveyTrackerValue,now,ROOT_LEVEL_LOCAL,COOKIE_DOMAIN_LOCAL);
}