/*
* holder for all AJAX polling objects on a given page
* such that each content section has an AJAX object 
* in the array
*/
var AJAXcomponents = new Array();

/*
*/
function createPollableObject(HTMLdiv,method,script)
{
	var compIndex = AJAXcomponents.length; // get next available index
	var objectArray = new Array(); // holder for its ajax related stuff
	objectArray['vars'] = new Array(); // holder for variables
	objectArray['ajax'] = new sack(script); // initialize ajax object with script to execute
	objectArray['ajax'].method = method;
	objectArray['ajax'].onCompletion = function(){
		showContent(document.getElementById(HTMLdiv),compIndex);};
	objectArray['ajax'].onLoading = showPollerWorking;
		
	AJAXcomponents[compIndex] = objectArray;
		
	return compIndex; 
	
}

function setPollObjectVar(objectID, name, value)
{
	AJAXcomponents[objectID]['vars'][name] = value;
}

/*
* each AJAX object on the page is run once
* this function should be called over some interval
* the interval calling takes place in the main page,
* like "setInverval('pollContent()',timeInMilliSec)"
*/
function pollContent()
{
	for (comp in AJAXcomponents) {
		// first set up request variables
		for (variable in AJAXcomponents[comp]['vars']) {
			//alert(variable + " = " + AJAXcomponents[comp]['vars'][variable]);
			AJAXcomponents[comp]['ajax'].setVar(variable,AJAXcomponents[comp]['vars'][variable]);
		}
		// now send http request
		AJAXcomponents[comp]['ajax'].runAJAX();
	}
}

function showPollerWorking()
{
	var messageElement = document.getElementById("message");
	messageElement.innerHTML = "[ <em>WORKING</em> ]";

}

/*
* This is called automatically after the AJAX call returns
* It is triggered by each content section independently of the others 
* It locates the <div> and fills it with the appropriate HTML
*/
function showContent(divElement, componentIndex)
{
	var messageElement = document.getElementById("message");
	messageElement.innerHTML = "";
	
	var component = AJAXcomponents[componentIndex]['ajax'];
	if (component.responseStatus[0] != 200) return;
	var data = component.response;
	//alert(data);
	divElement.innerHTML = data;
}

