
//	CAJAX_Queue - tries to give you several AJAX objects for queing
function CAJAX_Queue(objectName)
{
	//	Vars
	this.oName=objectName;
	this.ajaxes=null;
	
	this.Init=CAJAX_Queue_Init;
	this.InitAjaxes=CAJAX_Queue_InitAjaxes;

	this.GetFreeSlot=CAJAX_Queue_GetFreeSlot;
	this.ClearFinishedSlots=CAJAX_Queue_ClearFinishedSlots;
	
	this.MakeGetRequest=CAJAX_Queue_MakeGetRequest;
	
	//////////////
	//	Initialize
	this.Init();
}

//	Init
function CAJAX_Queue_Init()
{
	this.InitAjaxes();
}

//	Inits empty ajaxes array
function CAJAX_Queue_InitAjaxes()
{
	this.ajaxes=new Array();
}

//	Makes a request to a free alax slot
function CAJAX_Queue_MakeGetRequest(url,strCallback)
{
	var slot=this.GetFreeSlot();

	//	Create a new ajax object in this slot
	var tempOName=this.oName+'.ajaxes['+slot+']';
	this.ajaxes[slot]=new CAJAX(tempOName,slot);
	
	//	Run request on that object
	this.ajaxes[slot].MakeGetRequest(url,strCallback);

	return;
}

/////////////////////////////////
//	Grab the first free ajax slot
function CAJAX_Queue_GetFreeSlot()
{
	this.ClearFinishedSlots();
	
	var len=this.ajaxes.length;
	var slot=-1;

	//	Try to find the first null slot	
	for(var a=0;a<len;a++){
		if(this.ajaxes[a]==null){
			slot=a;
			break;
		}
	}
	
	//	If no null slots were found, just make a new slot at the end
	if(slot==-1){
		slot=len;
	}
	
	return slot;
}

//////////////////////////////////
//	Clears out finished ajax slots
function CAJAX_Queue_ClearFinishedSlots()
{	
	var len=this.ajaxes.length;
	
	for(var a=0;a<len;a++){
		if(this.ajaxes[a]){
			if(this.ajaxes[a].canDelete==true){
				delete this.ajaxes[a];
				this.ajaxes[a]=null;
			}
		}
	}
	
	return;
}

//+++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++
//+++++++++++++++++++++++++++++++++++

//	Attempt at aiding ajax proggy-ing
function CAJAX(objectName,myIndex)
{
	//	Vars
	this.oName=objectName;
	this.myIndex=myIndex;
	this.http=null;
	this.responseCallback=null;
	this.canDelete=false;
	
	//	Proto
	this.Init=CAJAX_Init;
	this.MakeHTTPObject=CAJAX_MakeHTTPObject;
	this.MakeGetRequest=CAJAX_MakeGetRequest;
	this.ProcessResponse=CAJAX_ProcessResponse;
	
	//	Debug
	this.DebugWindow=CAJAX_DebugWindow;
	
	//	Call init !
	this.Init();
}

///////////////////
//	Inits the class
function CAJAX_Init()
{
	this.http=this.MakeHTTPObject();
}

//////////////////////////////////////
//	Returns you an ajax request object
function CAJAX_MakeHTTPObject()
{
	var tmpXmlHttpObject;
	
	//	Make the right thing, depending on the browser
	if(window.XMLHttpRequest){
		
		// Mozilla, Safari
		tmpXmlHttpObject = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) { 
		
		// IE
		tmpXmlHttpObject = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	return tmpXmlHttpObject;
}

/////////////////////////////////
//	Makes a request to the server,
//	and once the data has been grabbed,
//	calls your callback
function CAJAX_MakeGetRequest(url,strCallback)
{	
	var toEval;
	
	//	Set callback
	this.responseCallback=strCallback;
	
	//	Make conn to server, specifying GET method
	//	... also passing full url
	try{
		this.http.open('get',url);
	}
	catch(err){
		this.DebugWindow("Alax failed to open "+url,err);
		return;
	}
	
	//	Assign handler and be sneaky about it with eval
	toEval=
		'this.http.onreadystatechange = function(){'
		+this.oName+'.ProcessResponse();'
		+'};';
	eval(toEval);
	//alert(toEval);
		
	/*
	var tempOName=this.oName;
	this.http.onreadystatechange = function(){
		var oName=tempOName;
		var toEval=oName+'.ProcessResponse();';
		try{
			eval(toEval);
		}
		catch(e){
			alert('CAJAX_MakeGetRequest failure: '+e.name+'\r\n\r\n'+e.message+'\r\n\r\n'+toEval);
		}
	};
	*/

	//	Actually send the request to the server
	try{
		this.http.send(null);
	}
	catch(err){
		this.DebugWindow("Ajax failed to send data",err);
		return;
	}
	
	//this.DebugWindow('Made request for '+strCallback+' to '+url);
	
	return;
}

/////////////////////////////////////
//	Callback for ajax's returned data
function CAJAX_ProcessResponse()
{
	var tempState;
	var tempStatus;
	var toEval;
	
	//	First off, if we didnt even set a callback,
	//	just return
	if(!this.responseCallback){
		return;
	}
	
	//	These errors seem to get thrown an awful
	//	lot, especially when the page is first loading,
	//	so i'm muting it here.
	try{
		tempState=this.http.readyState;
	}
	catch(err){
		return;
	}
	try{
		tempStatus=this.http.status;
	}
	catch(err){
		return;
	}
	
	//	Check if the response has been received from the server
	if(tempState == 4){

		//	Status 200 is ok, so perform CB with data in it		
		if(tempStatus==200){
			toEval=this.responseCallback+'('+this.oName+'.http.responseText);';
		}
		else{
			toEval=this.responseCallback+'(null);';
		}
		
		//	Perform eval then flag for deletion
		eval(toEval);
		this.canDelete=true;
    }
}


///////////////////////////////////////////////
//	Outs stuff to a debug window, if one exists
function CAJAX_DebugWindow(str1,oError)
{
	var o;
	var toSet;
	
	o=document.getElementById('DebugWindow');
	if(!o){
		return false;
	}
	
	toSet=o.value;
	toSet+='\n---------------------\n';	
	toSet+="[Slot "+this.myIndex+"] (CAJAX) ";
	toSet+=str1;
	
	//	Maybe add some info on the error
	if(oError){
		toSet+=
			"\n\n"
			+"Error name: "+oError.name
			+"\n"
			+"Error message: "+oError.message;
	}
	
	o.value=toSet;
	
	return true;
}
