﻿// JScript File

function Pinger(timeToNextPing, interval, url)
{
	this.Pinger_OnReadyStateChange = Pinger_OnReadyStateChange;
	this.Pinger_SendRequest = Pinger_SendRequest;
	this.Pinger_StartTimer = Pinger_StartTimer;
	
	this._timeToNextPing = timeToNextPing;
	this._interval = interval;
	this._url = url;
	this._successCount = 0;
	this._failureCount = 0;
	this._dateLastPing = 0;
	
	var instance = this;
	this._timerID = window.setTimeout(function() { instance._timerID = 0; instance.Pinger_SendRequest(); }, timeToNextPing);
}

function Pinger_SendRequest()
{
	try
	{
		var webrequest = null;
		
		if (window.ActiveXObject)
			webrequest = new ActiveXObject("Microsoft.XMLHTTP");
		else if (window.XMLHttpRequest)
			webrequest = new XMLHttpRequest();

		if (webrequest == null)
			return;
		
		var instance = this;
		
		webrequest.open ("GET", this._url + "?nocache=" + parseInt((new Date()).getTime()), true);
		webrequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		webrequest.onreadystatechange = function () { instance.Pinger_OnReadyStateChange(webrequest) };
		
		var pingerStatus = this._successCount + "|" + this._failureCount + "|" + this._dateLastPing;
		webrequest.setRequestHeader("PingerStatus", pingerStatus);
		
		webrequest.send('');
	}
	catch (ex)
	{
		this.Pinger_StartTimer();
	}
}

function Pinger_OnReadyStateChange(webrequest)
{
	if (webrequest.readyState == 4)
	{
		if (webrequest.status == 200)
		{
			this._successCount++;
			
			var xml = webrequest.responseXML;
			var messages = xml.getElementsByTagName("Message");
			
			try
			{
				for (i = 0; i < messages.length; i++)
				{
					var templatename = null;
					var sound = null;
					var args = new Array();
			        
					var message = messages[i];
			        
					for (attribindex = 0; attribindex < message.attributes.length; attribindex++)
					{
						var attrib = message.attributes[attribindex];
			            
						switch (attrib.name)
						{
							case "templatename":
							{
								templatename = attrib.value;
								break;
							}
						}
					}
			        
					var argnodes = message.getElementsByTagName("Argument");
			            
					for (argindex = 0; argindex < argnodes.length; argindex++)
					{
						if (argnodes[argindex].hasChildNodes())
							args.push (argnodes[argindex].firstChild.nodeValue);
					}

					if (typeof(notifier) != 'undefined')
					{
						var script = "notifier.Enqueue ('" + templatename + "',";
		                
						for (argindex = 0; argindex < args.length; argindex++)
						{
							if (argindex > 0)
								script = script + ",";
		                        
							script = script + "'" + escape(args[argindex]) + "'";
						}
		                
						script = script + ");";
		                
						try
						{
							eval(script);
						}
						catch (ex)
						{
						}
					}
				}
			}
			catch (ex)
			{
			}
		}
		else
		{
			this._failureCount++;
		}
		
		this._dateLastPing = new Date().getTime();
		this.Pinger_StartTimer();
	}
}

function Pinger_StartTimer()
{
	var instance = this;
	this._timerID = window.setTimeout(function() { instance.Pinger_SendRequest() }, this._interval);
}
