var g_px = 0;
var g_py = 0;
var g_backspaceStamp = 0;

// Keep track of where on the page the mouse is when the page exits.  This won't track over iframes (but will hopefully get up the edge).
function adclicks_getMouse(e) {
	try {
		if (document.all) {
			g_px = event.clientX;
			g_py = event.clientY;
//alert("move");
		}
		else {
//alert("move");
			g_px = e.pageX;
			g_py = e.clientY;
		}
	}
	catch (e) {
		// eat any errors
//		alert("adclicks_getMouse error: " + e.message);
	}
}	// adclicks_getMouse


// Keep track of where on the page the mouse is when the page exits.  This won't track over iframes (but will hopefully get up the edge).
function adclicks_keyPress(e) {
	try {
		if (document.all) {
			if (event.keyCode == 8)
				g_backspaceStamp = (new Date()).getTime();
//alert("adclicks_keyPress:" + event.keyCode);
		}
		else {
			if (e.keyCode == 8)
				g_backspaceStamp = (new Date()).getTime();
		}
	}
	catch (e) {
		// eat any errors
//		alert("adclicks_keyPress error: " + e.message);
	}
}	// adclicks_keyPress


function adclicks_findY(obj) {
	var y = 0;
	while (obj) {
		y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return(y);
}

function adclicks_findX(obj) {
	var x = 0;
	while (obj) {
		x += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return(x);
}

function adclicks_doPageExit(e) {
	try {
		// Check if leaving the page due to a backspace (go back) and not an ad click.
		if ((new Date()).getTime() - g_backspaceStamp < 1000) {
//			alert("go back");
		}
		else {
			var ad = document.getElementsByTagName("iframe");
			for (var i=0; i<ad.length; i++) {
				var sNetwork = "";
				if (ad[i].src.toLowerCase().indexOf('ypn-js.overture.com') > -1)
					sNetwork = "Yahoo";
				else if (ad[i].src.toLowerCase().indexOf('pagead2.googlesyndication.com') > -1)
					sNetwork = "Google";
	
				// Check if this IFrame is an ad frame.
				if (sNetwork != "") {
					var adLeft = adclicks_findX(ad[i]);
					var adTop = adclicks_findY(ad[i]);
					var inFrameX = (g_px > (adLeft - 10) && g_px < (parseInt(adLeft) + parseInt(ad[i].width) + 15));
					var inFrameY = (g_py > (adTop - 10) && g_py < (parseInt(adTop) + parseInt(ad[i].height) + 10));
//alert("network = " + sNetwork + "\ng_px = " + g_px + "\ng_py = " + g_py + "\nHoriz = " + (adLeft - 10) + " .. " + (parseInt(adLeft) + parseInt(ad[i].width) + 15) + "\nVert = " + (adTop - 10) + " .. " + (parseInt(adTop) + parseInt(ad[i].height) + 10) + "\ninFrameX = " + inFrameX + "\ninFrameY = " + inFrameY);
					
					if (inFrameY && inFrameX) {
						var img = new Image(); 
						img.src = '/ajax-ad-counter.asp?page=' + escape(document.location) + '&title=' + escape(document.title) + '&ad=' + escape(window.status) + '&network=' + escape(sNetwork); 
//						img.src = '/ajax-ad-counter.asp?page=' + escape(document.location) + '&title=' + escape(navigator.userAgent) + '&ad=' + escape(window.status) + '&network=' + escape(sNetwork); 
					}
				}
			}	// for
		}	// if backspace
	}
	catch (e) {
		// eat any errors
//		alert("Hit error: " + e.message);
	}
}	// adclicks_doPageExit


try {
	if (document.all) {
		window.onbeforeunload = adclicks_doPageExit;
		window.document.onmousemove = adclicks_getMouse;
		window.document.onkeydown = adclicks_keyPress;	// the keyPress event didn't pickup a backspace.
	}
	else {
		window.addEventListener('beforeunload', adclicks_doPageExit, false);
		window.addEventListener('mousemove', adclicks_getMouse, true);
		window.addEventListener('keydown', adclicks_keyPress, true);
	}
}
catch (e) {
	// eat any errors
//	alert("hook error: " + e.message);
}
