// ************************************************************
// Individual Ajax Item 
// ************************************************************
function AjaxItem(parentName, position, url, parseMethod, requestBegin, requestEnd, requestError, parameters, post)
{
	// Tools for XML parsing
	this.tools = new AjaxTools();
	
	// Properties	
	this.httpRequest = null;
	this.url = url;
	this.parentName = parentName;
	this.position = position;
	this.parameters = parameters;

	// User-created Methods
	this.Parse = parseMethod;
	this.RequestBegin = requestBegin;
	this.RequestEnd = requestEnd;
	this.RequestError = requestError;
	this.PostData = post;

	// Predefined Methods
	this.Load = ajaxItem_Load;
	
	function ajaxItem_Load()
	{
		this.httpRequest = this.tools.CreateXmlHttpRequest();

		if(this.httpRequest)
		{
			try
			{
				if(this.RequestBegin)
				{
					this.RequestBegin();
				}
				
				if(post)
				{
					this.httpRequest.open('POST', this.url, true);
					this.httpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					
					this.httpRequest.onreadystatechange = new Function(parentName + '.PreParse(' + this.position + ');');
					this.httpRequest.send(post);
				}
				else
				{
					this.httpRequest.onreadystatechange = new Function(parentName + '.PreParse(' + this.position + ');');
					this.httpRequest.open('GET',this.url);
					this.httpRequest.send(null);
				}
			}
			catch(e)
			{
				if(this.RequestError)
				{
					this.RequestError();
				}
					
				alert('Could not open URL');
				alert(e);
			}
		}
		else
		{
			if(this.RequestError)
			{
				this.RequestError();
			}
				
			alert('XmlHttpRequest object could not be loaded');
		}
	}
}

// ************************************************************
// Ajax Manager Object
// ************************************************************
function AjaxManager(objectName, usecache)
{
	this.name = objectName;

	// Properties
	this.items = new Array();
	this.usecache = usecache;
	
	// Methods
	this.Add = ajaxManager_Add;
	this.PreParse = ajaxManager_PreParse;
	
	function ajaxManager_Add(url, parseMethod, requestBegin, requestEnd, requestError, parameters, post)
	{
		// If client-side caching was enabled for this manager
		if(this.usecache)
		{
			// Looks at the items, and sees if we have one with the same URL
			for(var i = 0 ;i < this.items.length; i++)
			{
				// Found a cached item
				if(this.items[i].url == url && (this.items[i].PostData == post || !this.items[i].PostData))
				{
					if(this.items[i].RequestBegin)
						this.items[i].RequestBegin();
					
					// Creates a parsecache method
					this.items[i].ParseCache = parseMethod;
					
					// Runs the parser
					this.items[i].ParseCache();
					
					if(this.items[i].RequestEnd)
						this.items[i].RequestEnd();

					// Ends the request					
					return;
				}
			}
		}
		// Creates a new ajax item
		var newItem = new AjaxItem(this.name, this.items.length, url, parseMethod, requestBegin,
									requestEnd, requestError, parameters, post);
		
		// 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();
			
			if(this.items[pos].RequestEnd != null)
			{
				this.items[pos].RequestEnd();
			}
		}
	}
}