///////////////////////////////////////////////////////////////////////////////////////////////////////
// AjaxRequest class v0.1
//
// 	Class for creating asynchronous javascript http requests
//
// DESCRIPTION:
//
// 		Class used for creating and sending mainly asynchronous javascript http requests
// 	and getting responses in XML or text format. It is also used for sending variables to the
// 	server side scripts. On received server response it calls OnReceivedResponse method.
//
// EXAMPLE:
//
// 	var Req = new AjaxRequest();
// 	Req.mScriptPath = 'php/test.php';
// 	Req.OnReceivedResponse = function(Response)
// 	{
// 		document.write(Response);
// 	}
// 	Req.SendRequest();
//
// TODO:
//	all - ie port - EMERGENCY, objects collector (to remove mModuleName property in classes)
///////////////////////////////////////////////////////////////////////////////////////////////////////
function AjaxRequest()
{
	// Path to the server side script
	this.mScriptPath = '';

	// Method of the http request
	this.mMethod = 'GET';

	// Asynchronous request
	this.mAsynchronous = true;

	// Type of the response (XML or Text)
	this.mResponseType = 'XML';

	// Variables to be sent
	this.mSendVariables = '';

	// Function that will be executed after request is finished
	this.OnReceivedResponse = false;

	// Send request to the server
	this.SendRequest = function()
	{
		if (window.XMLHttpRequest) var xmlHttp = new XMLHttpRequest();
		else if (window.ActiveXObject) var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

		if (this.mMethod = 'GET') this.mScriptPath += '?' + this.mSendVariables;

		xmlHttp.open(this.mMethod, this.mScriptPath, this.mAsynchronous);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
		if (this.mMethod = 'POST') xmlHttp.send(this.mSendVariables);
		else xmlHttp.send(0);
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4)
			{
				switch (AjaxRequest.mResponseType)
				{
					case 'XML':
						AjaxRequest.OnReceivedResponse(xmlHttp.responseXML);
						break;
					case 'Text':
						AjaxRequest.OnReceivedResponse(xmlHttp.responseText);
						break;
					default:
						throw("AjaxRequest error! You haven't used right mResponseType!");
				}
			}
		}
	}
	
	var AjaxRequest = this;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////
// ClientStatusReporter class v0.1
//
//      Class that reports clients status in some interval
//
//      author: Tihomir Piha
//      version: v0.1
//      mail: tihomir.piha@uniqall.com
//      licence: see Licence.txt
//
// DESCRIPTION:
//
//              Detailed class
//      description
//
// EXAMPLE:
//
//      Class usage expample.
//
// TODO:
//
//      Things that should be done on the class.
//
// CHANGELOG:
//		t.URL + "?ClickId=" + _root.ClickId + "&Check=" + _root.Check + "&Reason=" + Reason;
///////////////////////////////////////////////////////////////////////////////////////////////////////
function ClientStatusReporter(LogoClickId, LogoCheck)
{
	this.mInterval = 5;

	var mLogoClickId = LogoClickId;

	var mLogoCheck = LogoCheck;

	this.StartReporting = function()
	{
		Request("OnLoad");
	}

	this.OnUnload = function()
	{
		Request("OnUnload");
	}

	var ClientStatusReport = this;


	function UpdateInterval(Res)
	{
		var List = Res.getElementsByTagName('interval');
		ClientStatusReport.mInterval = List[0].childNodes[0].nodeValue;
	}

	Request = function(Reason)
	{
		var Req = new AjaxRequest();
		Req.mScriptPath = '/scripts/clickupdate.php';
		Req.mSendVariables = 'ClickId=' + mLogoClickId + '&Check=' + mLogoCheck + '&Reason=' + Reason + '&Interval=' + ClientStatusReport.mInterval;
		Req.mMethod = 'GET';
		Req.OnReceivedResponse = function(Response)
		{
			UpdateInterval(Response);
			if (ClientStatusReport.mInterval) setTimeout('javascript:Request("OnTimer");', ClientStatusReport.mInterval * 1000);
		}
		Req.SendRequest();
	}


	
	document.onunload = function()
	{
		ClientStatusReport.OnUnload();
	}
}