﻿


var ConnectionStatus = { Unknown: 0, Ready: 1, ConnectionFailed: 2, ConnectionTimeOut: 3 };

function ServerConnection(target, taskCompleted, taskFailed)
{
	this.Socket = null;
	this.Response = "";
	this.Target = target;
	this.TaskCompletedCallback = taskCompleted;
	this.TaskFailedCallback = taskFailed;
	this.TimeOut = null;
	this.Id = 0;
	this.Status = ConnectionStatus.Unknown;

	this.Create = function ()
	{
		this.Socket = null;
		if (window.XMLHttpRequest)
			this.Socket = new XMLHttpRequest();
		else
			this.Socket = new ActiveXObject("Microsoft.XMLHTTP");
		if (this.Socket != null)
		{
			this.Socket.onreadystatechange = this.StateChanged;
			this.Socket.Manager = this;
		}
	}

	this.Send = function (objects)
	{
		this.TimeOut = null;
		var url = this.Target + "?";
		for (var i = 0; i < objects.GetLength(); i++)
		{
			url += objects.GetName(i) + "=" + objects.GetValue(i);
			if (i < objects.GetLength() - 1)
				url += "&";
		}
		this.Create();
		this.Socket.open("GET", url, false);
		this.Socket.send(null);
		this.TimeOut = setTimeout("Network.TimeOutExceeded(" + this.Id + ")", 5000);
	}

	this.TimeOutExceeded = function ()
	{
		clearTimeout(this.TimeOut);
		if (this.Socket.readyState != 4 && this.Status != ConnectionStatus.Ready)
			this.TaskFailedCallback(ConnectionStatus.ConnectionTimeOut);
	}

	this.StateChanged = function ()
	{
		if (this.readyState == 4 && this.responseText.length > 0)
		{
			this.Status = ConnectionStatus.Ready;
			this.Manager.Response = this.responseText;
			this.Manager.TaskCompletedCallback(ConnectionStatus.Ready);
		}
	}
}

function NameValueCollection()
{
	this.Items = new Array();

	this.GetLength = function ()
	{
		return this.Items.length;
	}
	this.Add = function (object)
	{
		this.Items.push(object);
	}
	this.GetName = function (index)
	{
		return this.Items[index].Name;
	}
	this.GetValue = function (index)
	{
		return this.Items[index].Value;
	}
}

function NameValueItem(name, value)
{
	this.Name = name;
	this.Value = value;
}