// There are two patterns that can be used with this simple AJAX API. The code below, demonstrating both patterns assumes
// that the server request is made as follows: http://www.ajax.com/response.asp?name1=value1&name2=value2&..., where www.ajax.com is the domain for the
// server to which the requests are being made. This domain must be the same as that of the requesting page.
//
// 1. When not polling, i.e. for individual requests.
//
//		var result;
//
//		result = ajaxInitialize('http://www.ajax.com/response.asp) // Called just one time in program initialization.
//
//		if(result == false)
//			ProcessErrors(); // This would only happen if the http object could not be created, e.g. on very old browsers.
//
//		ajaxRequest("name1=value1&name2=value2&..."); // Called for each request.
//
// After calling ajaxRequest(), the function ajaxResponse(data), which you must code will be called when the response is received. 'data' 
// will simply be a string, which you must parse to get your data.
//
// The server file that answers the request can be as simple as the following:
//
// <%
// Dim string
// 'Do something and assign the result to string
// Response.Expires=-1 ' To prevent caching!
// Response.Write(string)
// %>
//
// The other pattern is when you want to poll the server.
//
//		'The following 3 functions are called just one time in program initialization.
//
//		ajaxSetUrl('http://www.ajax.com/response.asp', 'name1', 'name2', ...);
//		ajaxSetParameters(value1, value2, ....);
//		ajaxStartTimer(msec); // msec = polling interval in milliseconds.
//
// After executing the three statements above, ajaxResponse(data) will be called at the specified intervals. To stop polling,
// call: ajaxStopTimer().

var httpObj;
var timerID;
var paramNames;
var paramValues;
var url;

function getHttpObject()
{
	var obj;
	
	try
	{
		obj = new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			obj = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				obj = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				return false;
			}
		}
	}
	httpObj = obj;
	return true;
}
function onreadystatechangeEventHandler()
{
	if(httpObj.readyState == 4)
	{
		ajaxResponse(httpObj.responseText);
	}
}
function ajaxPoll()
{
	var fullUrl;

	fullUrl = url + '?randID=' + Math.random();

	for(i = 0; i < paramNames.length; i++)
		fullUrl = fullUrl + '&' + paramNames[i] + '=' + paramValues[i];

	httpObj.open("GET", fullUrl, true);

	httpObj.onreadystatechange = onreadystatechangeEventHandler;
	
	httpObj.send(null);
}
// Assuming the complete server response url is "http://www.ajax.com/response.asp?name1=value1&name2=value2&..." call ajaxInitialize
// as follows: ajaxInitialize('http://www.ajax.com/response.asp', 'name1', 'name2', ...)

function ajaxInitialize()
{
	var result;

	result = getHttpObject();

	return(result);
}
function ajaxRequest(qs)
{
	var fullUrl;

	fullUrl = url + '?randID=' + Math.random() + "&" + qs;
	httpObj.open("GET", fullUrl, false);
	httpObj.onreadystatechange = onreadystatechangeEventHandler;
	httpObj.send(null);
}
// Assuming the complete server response url is "http://www.ajax.com/response.asp?name1=value1&name2=value2&..." call ajaxInitialize
// as follows: ajaxInitialize('http://www.ajax.com/response.asp', 'name1', 'name2', ...)

function ajaxSetUrl(newUrl)
{
	url = newUrl;
}
// Assuming the complete server response url is "http://www.ajax.com/response.asp?name1=value1&name2=value2&..." call ajaxSetParameters
// as follows: ajaxSetParameters('value1', 'value2', ...)

function ajaxSetParameters()
{
	var arg;
	var i;

	arg = ajaxSetParameters.arguments;
	
	for(i = 0; i < paramNames.length; i++)
		paramValues[i] = arg[i];
}
function ajaxStartTimer(msec)
{
	timerID = setInterval(ajaxPoll, msec);
}
function ajaxStopTimer()
{
	clearInterval(timerID);
}			
function ajaxSetHttpHeader(name, value)
{
	httpObj.setRequestHeader(name, value);
}