var UserClicked=false;
var page="http://www.seniorsforliving.com/popup.php";
var windowprops="width=400,height=470,location=no,toolbar=no,menubar=no,scrollbars=no,resizable=no,status=no";
var windowprops2="width=950,height=650,location=no,toolbar=no,menubar=no,scrollbars=yes,resizable=no,status=no";
var alwaysPopup;
var sameDomain;
alwaysPopup = false;
sameDomain = false;
//if loaded on blog site, add exitcheck to all links
if ( parent.document.location.substring(0,37) == 'http://www.seniorsforliving.com/blog/' )
{
	for (i=0;i<parent.document.links.length;i++)
	{
		parent.document.links[i].onclick = function() {
			exitcheck(this.href);
		}
	}
}

//do not overwrite if already set
if ( typeof ff == 'undefined' )
{
	var ff;
	ff = "/fastfind/";
}
if ( typeof thirdparty_popup == 'undefined' )
{
	var thirdparty_popup;
	thirdparty_popup = false;
}
function exitcheck(location)
{
	if (location.substring(0,31) == 'http://www.seniorsforliving.com')
	{
		sameDomain = true;
	}
}

document.onkeydown=spyclick;
document.onmousedown=spyclick;
function spyclick()
{
	UserClicked=true;
	setTimeout("UserClicked=false",2000);
}

window.onbeforeunload=popupBOU;

function request_info_popunder()
{
	if( request_sent )
	{
		popup = window.open(ff+thank_you_url+"&withrequestpopup=true","thank_you",windowprops2);
	}
	else
	{
		popup = window.open(ff+thank_you_url+"&norequestpopup=true","thank_you",windowprops2);
	}
	var gui = setInterval( function()
	{
		if( popup.document == undefined )
		{
			window.clearInterval( gui );
		}
		else if( popup.document.body != undefined && popup.document.body.innerHTML != '' )
		{ 
			window.focus();
			window.clearInterval( gui );
		}
	}, 500);
}
function popupBOU(e)
{
	if(!UserClicked && getc("AFF_ID") != "763054")
	{
		if((typeof request_sent != "undefined") && thirdparty_popup )
		{
			request_info_popunder();
		}
		else if(getc("POPUP") != "N" && getc("adwas")!="true" && !sameDomain)
		{
			if(self.location.pathname.substr(0, 9) == '/content/')
			{
				popup(5);
			}
			else if(self.location.pathname.substr(0, 6) == '/blog/')
			{
				popup(4);
			}
			else if(self.location.pathname.substr(0, 10) == '/fastfind/')
			{
				if (alwaysPopup)
				{
					popup(13);
				}
			}
			else
			{
				popup(1);
			}
		}
	}
}
function popup(id)
{
	var win=window.open(page + "?id=" + id + "&ff=" + ff,"popunder",windowprops);
	window.focus();
	// This sets the number of minutes to expire the cookie
	var expMins = 30;
	var exp = new Date();
	exp.setTime(exp.getTime() + (expMins*60*1000));
	setc("adwas","true", exp);
}
function stoppopup()
{
	var expMins = 30;
	var exp = new Date();
	exp.setTime(exp.getTime() + (expMins*60*1000));
	setc("adwas","true", exp);
}
function getc(name)
{
	var rs=null;
	var mc=" " + document.cookie + ";";
	var sn=" " + name + "=";
	var sc=mc.indexOf(sn);
	var ec;
	if (sc!=-1)
	{
		sc+=sn.length;
		ec=mc.indexOf(";",sc);
		rs=unescape(mc.substring(sc,ec));
	}
	return rs;
}
function setc(name,value,expires,path,domain,secure)
{
	document.cookie=name + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	 ((secure) ? "; secure" : "");
}

