var UserClicked = false;
var joints = "";
var survey = "/public/exit_survey.cfm";

document.onkeydown = checkClick;
document.onmousedown = checkClick;

function checkClick() {
	UserClicked = true;
	setTimeout("UserClicked = false", 2000);
}

function prLinks() {
	for(i=0; i < document.links.length; i++) {
		document.links[i].onclick = checkLink;
	}
}

function checkLink() {
	var addr = this.toString(); 
	
	if (!addr.match(document.domain) && !addr.match("javascript") && !addr.match("cedarpoint.com")) {
		//only external links will alert
		var verify = Boolean(getc("cpExitSurvey"));
		if (!verify) {
			//check = confirm(popuptext);
			//if (check) popup();
			//else setc("cpExitSurvey", true);
			popup();
			setc("cpExitSurvey", true);
		}
	}
}

function popup() {
	var win = window.open(survey,"","width=400,height=400");
}

function getc(name) {
	var params = document.cookie.split(";");
	var key = "";
	var val = false;
	var name = name.toLowerCase();
	
	for (var i=0; i < params.length; i++) {
		key = params[i].split("=");
		var n = key[0].replace(" ","");
		if (n.toLowerCase() == name) val = key[1];
	}
	return val;
}

function setc(name, value) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + 365);
	document.cookie = name + "=" + escape(value) + ";expires=" + exdate.toGMTString();
}

function checkUnload() {
	
	if (!UserClicked) {
		var verify = Boolean(getc("cpExitSurvey"));
		if (!verify) {
			//check = confirm(popuptext);
			//if (check) popup();
			//else setc("cpExitSurvey", true);
			popup();
			setc("cpExitSurvey", true);
		}
	}
}

window.onload = prLinks;
window.onbeforeunload = checkUnload;
