﻿
var gbl_notifyUrl;
var gbl_userID;
var gbl_username;

function OnJSError(msg, url, lno)
{
	var url = encodeURI(gbl_notifyUrl + "?userID="+gbl_userID+"&username="+unescape(gbl_username)+"&url="+url+"&lineno="+lno+"&msg="+msg);

	var webrequest = null;
	
	if (window.ActiveXObject)
		webrequest = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		webrequest = new XMLHttpRequest();

	if (webrequest == null)
		return;	// client update unavailable
	
	webrequest.open ("GET", url, true);
	webrequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	webrequest.send('');
}	

function attachJSErrorNotifier(url, userID, username)
{
	gbl_notifyUrl = url;
	gbl_userID = userID;
	gbl_username = username;
	window.onerror = OnJSError;
}

function checkPinger(pingerUrl)
{
	var hasPingerFrame = false;
	var hasIsActiveFunction = false;
	var isActive = false;
	var lastMainRequest = null;
	
	try
	{
		hasPingerFrame = (top.frames['DatingPinger'] != null);
		hasIsActiveFunction = (hasPingerFrame && (typeof(top.frames['DatingPinger'].IsActive) != 'undefined'));
		isActive = (hasIsActiveFunction && top.frames['DatingPinger'].IsActive());
		
		if (hasPingerFrame && typeof(top.frames['DatingPinger'].lastmainrequest) != 'undefined')
			lastMainRequest = new String(top.frames['DatingPinger'].lastmainrequest);
	}
	catch(ex)
	{
	}

	if (!isActive)
	{
		var title = "unknown";
		
		if (hasPingerFrame)
		{
			var now = new Date();
			
			try
			{
				title = top.frames['DatingPinger'].document.title;
				content = top.frames['DatingPinger'].document.body.innerHTML;
			}
			catch (ex)
			{
			}

			var url = pingerUrl;
			url = url +'?pingframe='+hasPingerFrame+'&hasactfunc='+hasIsActiveFunction+'&isactive='+isActive;
			url = url + '&title='+title; //+'&content='+content+'&tick='+now.getTime();
			
			if (lastMainRequest != null)
				url = url + '&mainrequest='+lastMainRequest;
			
			top.frames['DatingPinger'].document.location.href = "about:blank";
			top.frames['DatingPinger'].document.location.href = encodeURI(url);
		}
	}
}
