//----------- AJAX object implementation -----------


function AJAX_synchronous (xmlPayload, href)
{
	this.XML = xmlPayload
	this.href = href
	if (window.ActiveXObject)
	{
		this.xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP")
	}
	else
	{
		this.xmlhttp = new XMLHttpRequest()
	}
	this.xmlhttp.open (this.method, href, false)
	this.xmlhttp.send (this.XML)
	return this.xmlhttp  //.responseText
}

function AJAX_asynchronous (xmlPayload, href, returnFunction)
{
	this.XML = xmlPayload
	this.href = href
	this.returnFunction = returnFunction
	this.execute ()
}

function AJAX_execute ()
{
	if (window.ActiveXObject)
	{
		this.xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP")
	}
	else
	{
		this.xmlhttp = new XMLHttpRequest()
	}
	this.xmlhttp.open (this.method, this.href, true)

	var xmlhttp_obj = this.xmlhttp
	var _self = this;
	xmlhttp_obj.onreadystatechange = function()
	{
		if (_self.notifyFunction != null)
			_self.notifyFunction (xmlhttp_obj)
		if (xmlhttp_obj.readyState == 4)
		{
			if (xmlhttp_obj.status == 500)
			{
				_self.error500 (xmlhttp_obj.responseText)
			}
			else
			{
				if (xmlhttp_obj.responseText == '')
				{
					if (_self.DataExpected)
					{
						xmlhttp_obj.abort()
						_self.retry (1000)
					}
					else
					{
						if (_self.returnFunction != null)
							_self.returnFunction (xmlhttp_obj)
					}
				}
				else
				{
					if (_self.returnFunction != null)
						_self.returnFunction (xmlhttp_obj)
				}
			}
		}
	}
	this.xmlhttp.send (this.XML)
//	this.retry (3000)
}

function AKAX_500Error (str)
{
	//** in case of server error, display the server error message in a popup window
	var Win;
	try
	{
		Win = window.open('', 'AjaxError')
		Win.document.body.innerHTML = str
		Win.focus ()
	}
	catch(e)
	{
		//** if pop-up is blocked, inform user
		alert ('An error occurred, but the error message cannot be displayed because of your browser\'s pop-up blocker.\nPlease allow pop-ups from this Web site.')
	}
}

function AJAX_Error ()
{
	this.ReadyStates = new Array ("UNINITIALIZED", "Sending ... ", "LOADED", "INTERACTIVE", "COMPLETED")
	this.HTTP_Errors = new Array (
			400,"HTTP Error 400 - Bad Request - The page cannot be found.",
			401,"HTTP Error 401 - Access Denied.",
			403,"HTTP Error 403 - Forbidden. You are not authorized to view this page",
			404,"HTTP Error 404 - File not found. The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.",
			500,"HTTP Error 500 - Internal server error. The page cannot be displayed. There is a problem with the page you are trying to reach and it cannot be displayed.",
			501,"HTTP Error 501 - Not implemented.  The page cannot be displayed. The page you are trying to reach cannot be retrieved. ",
			502,"HTTP Error 502 - Bad Gateway. The page cannot be displayed. There is a problem with the page you are trying to reach and it cannot be displayed."
		)
//	this.HTTP_Status = new Array (
//		200,"Ok",
//		301,"Moved Permanently",
//		304,"Not Modified (page hasn't been modified)",
//		404,"Not Found",
//		403,"Forbidden",
//		401,"Unauthorized (wrong password)"
//		) //  .statusText property
}

function AJAX ()
{
	this.DataExpected == true
	this.method = "POST"						//* note: Firefox does not seem to work with GET

	this.sync = AJAX_synchronous			//* for synchronous AJAX calls
	this.async = AJAX_asynchronous		//* for asynchronous AJAX calls
	this.execute = AJAX_execute				//* used internally
	this.error500 = AKAX_500Error			//* used internally

	this.returnFunction = null					//* assign a call-back function
	this.notifyFunction = null					//* optional status notification function may provide feedback on status of AJAX call
	this.XML = ""

    this.retry = function (ms)
    {
        var _self = this;
		this.xmlhttp.abort()
        setTimeout(function(ms){_self.execute();}, ms);
    }
}

