function IsHREFSameSiteAsThis(HREF)
{
	// get site name
	
	CurSite = GetCurrentSiteName();

	//alert(CurSite);
	//alert(HREF);

	if (HREF.indexOf(CurSite) !=-1)
		return true;
	if (HREF.indexOf("www.paypal.com") !=-1)
		return true;
	if (HREF.indexOf("javascript:") !=-1)
		return true;
	return false;
}

function GetCurrentSiteName()
{
	loc = location.href;
	
	start= loc.indexOf("://")
	if ( start == -1 )
		return "bad";
	start+=3;
	//alert(start);
		
	return loc.substring(start,loc.indexOf('/',start));
}

function SetNotLeaving()
{
	leaving=false;
}

function OnUnLoadExitPopup()
{
	var currentCount = getCookie("pageViewCount");
	///var alreadyDisplayed = getCookie("alreadyDisplayedPopup");
	// if the cookie wasn't found, this is your first visit
	if (!currentCount)
		currentCount = 0;
	else
		currentCount = parseInt(currentCount);
	currentCount--;
	//alert(currentCount);
	setCookie("pageViewCount", currentCount, ""/*, "/", ".mysite.com"*/);
	if (leaving && currentCount == 0 /**&& ! alreadyDisplayed**/)
	{
		//window.open("http://www.mysite.com/");
		windowprops = "left=50,top=50,width=460,height=250";
		window.open('tt_imgs/dl_Popup.html', "NS_DL_Popup", windowprops);
		//window.focus();		
		
		deleteCookie("pageViewCount"/*, "/", ".mysite.com"*/);
		// create an instance of the Date object
		//var now = new Date();
		// cookie expires in 7 days
		///now.setTime(now.getTime() + 7 * 24 * 60 * 60 * 1000);
		///setCookie("alreadyDisplayedPopup", "1", now/*, "/", ".mysite.com"*/);
	}
}

function OnLoadExitPopup()
{
	for (i=0;i<document.links.length;i++)
	{
		if(IsHREFSameSiteAsThis(document.links[i].href))
		//if (document.links[i].href.indexOf(test) !=-1)
			if(document.links[i].onclick==null) // don't fuck existing events. manually call SetNotLeaving in those handlers
				document.links[i].onclick=SetNotLeaving;
	}

    var currentCount = getCookie("pageViewCount");
    // if the cookie wasn't found, this is your first visit
    if (! currentCount)
		currentCount = 1;
    else
		currentCount = parseInt(currentCount) + 1;
	//alert(currentCount);
    setCookie("pageViewCount", currentCount, ""/*, "/", ".mysite.com"*/);
}
