// ************************************************************
// Individual Ajax Item 
// ************************************************************
function AjaxItem(parentName, position, url, parseMethod)
{
	// Tools for XML parsing
	this.tools = new AjaxTools();
	
	// Properties	
	this.httpRequest = null;
	this.url = url;
	this.parentName = parentName;
	this.position = position;

	// User-created Methods
	this.Parse = parseMethod;

	// Predefined Methods
	this.Load = ajaxItem_Load;
	
	function ajaxItem_Load()
	{
		this.httpRequest = this.tools.CreateXmlHttpRequest();

		if(this.httpRequest)
		{
			try
			{
				this.httpRequest.onreadystatechange = new Function(parentName + '.PreParse(' + this.position + ');');
				this.httpRequest.open('GET',this.url);
				this.httpRequest.send(null);
			}
			catch(e)
			{
				alert('Could not open URL');
				alert(e);
			}
		}
		else
		{
			alert('XmlHttpRequest object could not be loaded');
		}
	}
}

// ************************************************************
// Ajax Manager Object
// ************************************************************
function AjaxManager(objectName)
{
	this.name = objectName;

	// Properties
	this.items = new Array();
	
	// Methods
	this.Add = ajaxManager_Add;
	this.PreParse = ajaxManager_PreParse;
	
	function ajaxManager_Add(url, parseMethod)
	{
		// Creates a new ajax item
		var newItem = new AjaxItem(this.name, this.items.length, url, parseMethod);
		
		// Adds it to the storage
		this.items[this.items.length] = newItem;

		// Starts the Ajax process
		newItem.Load();
	}

	function ajaxManager_PreParse(pos)
	{
		if (this.items[pos].httpRequest.readyState == 4) // readyState = 4 -> Complete
		{
			this.items[pos].Parse();
		}
	}
}

