// AJAX library - see Ajax in 10 minutes book chapter 17
//
// Used in AUSMEDIC to regular change the Specials and New Products on the Home Page.
//
// This library is for the Client Side.
// See also "myAJAXinclude.js" which uses this library to set things up.
//
// Example of use:
// <html>
// <head>
// <script language="javascript" src="myAJAXlib.js"><end of script>
// ****************************************************************
// <script language="javascript">
// function cback(text)
// ********************
// {
// 		alert(text);
// }
// <end of script>
// </head>
//
// <body>
// <form name="form1">
// <input type="button" value="test" onClick="doAjax('check_for_messages.php','owner_id=derra&qtype=messages','ajax_call_back1','get','0');">
//                                            ***********************************************************************************************
// </form>
// </body>
// </html>

function createREQ()
{
	try 
	{
		req = new XMLHttpRequest(); /* e.g. Firefox */
	}
	
	catch(err1)
	{
		try
		{
			req = new ActiveXObject("Msxml2.XMLHTTP");
			/* some versions IE */
		}
		
		catch (err2)
		{
			try
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
				/* some versions IE */
			}
			
			catch (err3)
			{
				req = false;
			}
		}
	}
	return req;
}

function requestGET(url, query, req)
{
	myRand=parseInt(Math.random()*99999999);
	req.open("GET",url+'?'+query+'&rand='+myRand,true);
	req.send(null);
}

function requestPOST(url, query, req)
{
	req.open("POST", url, true);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	req.send(query);
}

function doCallback(callback,item)
{
	eval(callback + '(item)');
}

function doAjax(url, query, callback, reqtype, getxml)
{
	// create the XMLHTTPRequest object instance
	var myreq = createREQ();
	
	myreq.onreadystatechange = function()
	{
		if(myreq.readyState == 4)
		{
			if(myreq.status == 200)
			{
				var item = myreq.responseText;	// TEXT version
				
				if(getxml==1)
				{
					item = myreq.responseXML;	// XML version instead
				}
				
				doCallback(callback, item);
			}
		}
	}
	
	if(reqtype=='post')
	{
		requestPOST(url,query,myreq);
	}
	else
	{
		requestGET(url,query,myreq);
	}
}
