/*survey.js*/
/*for launching an exit survey on leaving the site*/
/*dependencies - cookieUtil.sj, blastWindow.js*/
//check all links for onclick

var launchSurvey = true;

//pages that stop the exit survey forever
//receipt pages and pages users see post-login
var stopSurveyPages = {
	"Receipt":"Receipt",
	"Receipt_Short":"Receipt_Short",
	"EIN_Receipt":"EIN_Receipt",
	"Business_License_Receipt":"Business_License_Receipt",
	"My_Account":"My_Account",
	"My_Invoice":"My_Invoice",
	"Pay_Invoice_Complete_Check":"Pay_Invoice_Complete_Check",
	"Pay_Invoice_Complete_Credit":"Pay_Invoice_Complete_Credit",
	"my_receipt":"my_receipt"
};

//append an onclick script to all links that don't launch a popup window (has javascript in the href).
for(z = 0; z < document.links.length; z++) {
	if(document.links[z].onclick) {
		if(document.links[z].href.indexOf('javascript') < 0) {
			document.links[z].onclick = (function(old) {
				return function() {
					dumpSurvey();
					if (old) old.call(this, arguments);
				}
			})(document.links[z].onclick);	
		}
	} else {
		if(document.links[z].href.indexOf('javascript') < 0) {
			document.links[z].onclick = dumpSurvey;
		}
	}
}

//append an onsubmit event to all forms
for(zz = 0; zz < document.forms.length; zz++) {
	if(document.forms[zz].onsubmit) {
		document.forms[zz].onsubmit = (function(old) {
			return function() {
				dumpSurvey();
				if (old) {
				   return old.call(this, arguments);
				}
			}
		})(document.forms[zz].onsubmit);
	} else {
		document.forms[zz].onsubmit = dumpSurvey;
	}
}

//function for not showing the survey
function dumpSurvey() {
	launchSurvey = false;
}

//determine if this is the first page viewed on our site (disallow survey on first page viewed)
if(document.referrer.indexOf(document.location.host) < 0) {
	launchSurvey = false;
}


//add event to body unload
window.onunload = doExitSurvey;

//stop the survey forever if you've seen a receipt page
if(globalPageName in stopSurveyPages) {
	if(!readCookie("stopSurvey")) {
		createCookie("stopSurvey","deny","365");
	}
}

//stop the survey if you have a stopSurvey cookie
if(readCookie("stopSurvey")) {
	dumpSurvey();
}


//method for initiating survey (allowExitSurvey is set in SR via common_include)
function doExitSurvey() {
	if(launchSurvey && allowExitSurvey == "true") {
		blastWindow('/scripts/survey.check','surveycheck','height=100,width=100,left=' + screen.width+100 + ',top=' + screen.height+100 + ',location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no');
	}
}