/*****************************************************************************
 * Click recorder file - sends click data back to the server.
 *
 * History
 * -------
 *
 *	2005/11/14	jdp	Created
 *	2006/02/18	jdp	Modified for deployment on ev3qa (JPSpan -> pure XHR)
 *
 *****************************************************************************/

window.onload = cr_registerEvents;
var uaIsIE = (document.all) ? true : false;
var uaHasDOM = (document.documentElement) ? true : false;

/**
 * Sets stuff up.
 */
function cr_registerEvents()
{	
	if (uaIsIE)
		document.onmousedown = cr_recordClickIE;
	else
		window.onmousedown = cr_recordClickOther;
}

/**
 * Actual click recording method for decent browsers
 *
 * @param	ev	the event
 */
function cr_recordClickOther (ev)
{
	// get result array
	var clickRes = new Array();

	// x + y coordinates (remember to include scrolling...
	clickRes["x"] = ev.clientX + window.pageXOffset;
	clickRes["y"] = ev.clientY + window.pageYOffset;

	// dimension of window
	clickRes["dX"] = window.innerWidth;
	clickRes["dY"] = window.innerHeight;

	// send
	cr_sendToServer(clickRes);

	// we want the click to go through to the underlying layer
	return true;
}

/**
 * Actual click recording method for IE
 */
function cr_recordClickIE ()
{
	// grab event
	ev = window.event;
	var clickRes = new Array();

	// x + y coordinates (remember to include scrolling...)
	if (uaHasDOM)
	{
		// IE6/nonquirksmode uses documentElement rather than body...
		clickRes["x"] = ev.clientX + document.documentElement.scrollLeft;
		clickRes["y"] = ev.clientY + document.documentElement.scrollTop;
	}
	else
	{
		clickRes["x"] = ev.clientX + document.body.scrollLeft;
		clickRes["y"] = ev.clientY + document.body.scrollTop;
	}
	clickRes["dX"] = document.body.scrollWidth;
	clickRes["dY"] = document.body.scrollHeight;

	cr_sendToServer(clickRes);

	// we want the click to go through to the underlying layer
	return true;
}

/**
 * Sends data to server
 *
 * @param	aClick	data to send
 */
function cr_sendToServer (aClick)
{	
	// convert aClick to a string...
	var queryString = "x="+aClick['x']+"&y="+aClick['y']+"&dX="+aClick['dX']
					 +"&dY="+aClick['dY']+"&uri="+escape(window.location.href);

	// get xhr object
	var request = cr_getXmlHttpRequest();
	if (request == null)
	{
		return true;
	}

	// set up callback function
	request.onReadyStateChange = function (e) {} // empty function - who cares

	// open connection
	request.open("POST", "/clickRecorder.php", true);

	// and send
	request.send(queryString);
}

/**
 * Returns an XmlHttpRequest object appropriate to the UA.
 *
 * @return	an XmlHttpRequest or null if none available
 */
function cr_getXmlHttpRequest()
{
	var xhr = null;

	if (window.XMLHttpRequest)
	{
		try
		{
			xhr = new XMLHttpRequest();
		}
		catch (e)
		{}
	}
	else if (window.ActiveXObject)
	{
		try
		{
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e2)
			{}
		}
	}

	return xhr;
}
