//***********************************
//This .js is file is used by the tracker window and survey window.  It has onLoad and onUnload functions
//for those two pages. the most important is the pollSurveyStatus function, which is launched when the tracker
//window loads. The tracker window will continue polling, called this function repeatedly at the pollingInterval.
//This function determines if it's time to launch the survey window. If so, it does so by switching out its
//own content
//*****************************************************


// The URL of the content for the exit survey
var SurveyWindowPath;

//Specify any pages where the survey tracker window should be killed
var RestrictedPages = [];

//The polling Interval in milliseconds. This is how often the function PollSurveyStatus will be called
var PollingInterval = 700;

//Global variable for the timer used to manage polling
var timer;

// A counter used to control the number of submit actions
var submitCounter = 0;

//loads Exit survey into the tracker window, stops polling
function loadExitSurvey() {
	if (isMainWindowIntuit()) { return; }
	location.href=SurveyWindowPath;
	clearInterval(timer);
}

//The tracker window's polling function. This works based on a hack related to the window.opener
//property. Window.opener.location is no longer a valid property once the main window changes
//to another domain. Trying to access it will result in an exception, which tells us that the 
//main window has changed to another site, so then we know it's time to launch the exit survey
function pollSurveyStatus() {
	if (isSurveyKilled()) { close(); }
	try { 
	 	//If the main window is closed then launch the survey
	 	if (window.opener.closed){
			unLoadTrackerNow();
			loadExitSurvey();
	 	} 
	 	else {
	 		var openerLocation = window.opener.location.href;
	 		if (isRestricted(openerLocation)) { close(); }
	 	}
	 }
	 //If there's any exception thrown when trying to access the property window.opener,
	 //then we know that the main window has changed domains, so it's also time to launch the
	 //survey, UNLESS the tracker cookie is set, which is how QB-friendly sites like the search
	 //page can also keep the survey in the background
	 catch (e) {
	 	loadExitSurvey();
	 } 
	 return; 
}

//Tracker window onLoad function. This kicks off polling for the survey
function trackerOnLoad(pExitPath) {
	SurveyWindowPath = pExitPath;
	timer = setInterval(pollSurveyStatus, PollingInterval);
}

//Tracker window onUnload function. This currently doesn't do anything, but it might be useful
//in the future if we make enhancements to the exit survey functionality.
function trackerOnUnLoad() {

}

//onLoad function for the exit survey. Do one more check to see if the main window is on an Intuit
//site, if not then load the survey
function exitSurveyOnLoad() {
	if (!isMainWindowIntuit()) { focus(); }
	else { location.href=SurveyPreviewPath ; }
}

//See if the URL of the current main window page is in the list of restricted URLs that should not show
//the survey
function isRestricted(currentPage) {
	if(isSubStringInArray(RestrictedPages,currentPage)) return true;
	else return false;
}

//Utility function. Tests if the string paramenter exists as a substring of any of the elements
//in the array parameter
function isSubStringInArray(array,string) {
	var numElements = array.length;
	for(var j = 0; j < numElements; j++) {
		if (stringContains(string,array[j])) { return true; }
	}
	return false;
}

//Utility function to check if the largestring contains the smallstring as a substring
function stringContains(largeString,smallString) {
	if(largeString.length > smallString.length) {
		if(largeString.indexOf(smallString) != -1) { 
			return true; 
		}
	}
	return false;
}

var counter = 0;
function monitor() {
	counter++;
	if(counter > 1) {return false;}
	return true;
}