/*
ASP.NET 1.1 Lightweight Callback - based on ASP.NET 2.0 Callback mechanism
================================
(c)2006 Internova UK Ltd.
Author: Jamie Sanders

Usage:

1.	Create /ScriptCallBack/<Callback>.aspx that returns all 
	the response string as the first line of text on the page.

2.	Call WebForm_DoCallback('<Callback>'); from the webform 
	to perform lightweight callback.

3.	Create inline function <Callback>(response){ } to deal with 
	call back where response is the text generated by 
	<Callback>.aspx

*/
var pendingCallbacks = new Array();
function WebForm_DoCallback(callbackName, parameterList)
{
	var xmlhttp;
	var url;
		
	//TODO: append a parameter list to the url so the script callback page can use them
	url = '/ScriptCallback/' + callbackName + '.aspx?nocache=' + Math.random() + '&' + parameterList;
	
	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
		xmlhttp.onreadystatechange = WebForm_CallbackComplete;
	}
	else if (window.ActiveXObject)
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		if (xmlhttp)
		{
			xmlhttp.onreadystatechange = WebForm_CallbackComplete;
		}
	}
	
	//can add lots of things here that callback object may require
	var callback = new Object();
	callback.xmlhttp = xmlhttp;
	callback.callbackName = callbackName;
	
	WebForm_FillFirstAvailableSlot(pendingCallbacks, callback);
	

	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}
function WebForm_CallbackComplete()
{
	for (i = 0; i < pendingCallbacks.length; i++) 
	{
		callbackObject = pendingCallbacks[i];
		if (callbackObject && callbackObject.xmlhttp && (callbackObject.xmlhttp.readyState == 4))
		{
			WebForm_ExecuteCallback(callbackObject);
			pendingCallbacks[i] = null;
		}
	}
}
function WebForm_ExecuteCallback(callbackObject)
{
	var response = callbackObject.xmlhttp.responseText;
	window.eval(callbackObject.callbackName + '("' + response + '")');
}
function WebForm_FillFirstAvailableSlot(array, element) 
{
	var i;
	for (i = 0; i < array.length; i++) 
	{
		if (!array[i]) break;
	}
	array[i] = element;
	return i;
}
