document.onmousedown = getClickedElement;
document.onkeydown = getKeyStroke;
lastClickedOn = "";
buttonValue = "";

function getClickedElement() {
	lastClickedOn = window.event.srcElement.tagName;
	inputType = window.event.srcElement.type;
	//  alert(lastClickedOn + "  " + inputType);
	return true;
}

function getKeyStroke() {
	if (window.event.keyCode == 116)  lastClickedOn = "A";  // alert('F5 pressed');
	if (window.event.keyCode == 13 && lastClickedOn == "INPUT")  inputType = "submit";  // alert('Enter Key pressed while in  a formfield');
	//  alert(window.event.keyCode + ' pressed');
	return true;
}

function checkForLeavingSite() {
	re = /(submit|button|image)/gi;
	if (lastClickedOn == "A" || lastClickedOn == "IMG" || (lastClickedOn == "INPUT" && inputType.search(re) != -1))
		return true;
	else
		exitSurvey();
		// window.open("exit_survey/site_survey.cfm","surveypopup","");
}

function exitSurvey() {
	mainURL = location.href.toLowerCase();		// get the url of the page we're on
	query_string = mainURL.split("?");			// get the url variables
	query_string = query_string[query_string.length-1];
	re = /(shoppingcart|checkout)/gi;
	usedCart = query_string.search(re);

	if ((usedCart != -1 && document.cookie.indexOf("SKIPCHECKOUTSURVEY=1") != -1) || (usedCart == -1 && document.cookie.indexOf("SKIPSITESURVEY=1") != -1))
		return true;

	if (usedCart != -1){	// User is in Shopping Cart or Checkout Process
		surveyPage = "checkout_survey.cfm?exiting=1&";
	}
	else{					// User is in other part of site
		surveyPage = "site_survey.cfm?exiting=1&";
	}

	surveypopup = window.open("exit_survey/" + surveyPage + query_string, "surveypopup", 'height=500,width=390');	//,status=1
	surveypopup.blur();
	window.focus();
}