//
// PREGO LIBRARY
// Powered by Prego (http://www.pregowebdesign.com)
//
// Copyright (c) 2006 George Brocklehurst
// You may only use this file as part of the web site with which it was provided.
// Within the context of that website, you may modify the contents as required.
// All other rights reserved.
//
// AJAX functions
//


// NEW REQUEST
// Creates an XML HTTP Request object
function pregoNewRequest()
{
	try
	{
		if(window.XMLHttpRequest)
		{
			try
			{
				request = new XMLHttpRequest();
			}
			catch(e)
			{
			}
		}
		else if(window.ActiveXObject)
		{
			try
			{
				request = new ActiveXObject("Msxml2.XMLHTTP.4.0");
			}
			catch(e)
			{
				try
				{
					request = new ActiveXObject("MSXML2.XMLHTTP");
				}
				catch(e)
				{
					try
					{
						request = new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch(e)
					{
					}
				}
			}
		}
	}
	catch(e)
	{
		alert("Your browser does not support the necessary functionality.");
		request = false;
	}
	
	return request;
}


// PREGO AJAX POST REQUEST
// Makes an AJAX POST request
//  parameters  -  The POST data to submit in the form 'var1=value1&var2=value2', values should URL encoded
//  url         -  The URL to submit the request to
//  callback    -  The function to call when the request is complete, the function should take three parameters: xml, text, error
//                 The callback function can also refer to the XMLHTTPRequest object as 'this' if it needs to
var pregoPostRequest;
var pregoPostCallback;
function pregoAjaxPostRequest(url, parameters, callback)
{
	pregoPostRequest = pregoNewRequest();
	pregoPostCallback = callback;
	//pregoPostRequest.pregoComplete = callback; // Not supported in IE
	pregoPostRequest.onreadystatechange = function()
	{
		if(pregoPostRequest.readyState == 4)
		{
			var error = (pregoPostRequest.status != 200) ? pregoPostRequest.status + ' - ' + pregoPostRequest.statusText : null;
			//pregoPostRequest.pregoComplete(pregoPostRequest.responseXML, pregoPostRequest.responseText, error);
			pregoPostCallback(pregoPostRequest.responseXML, pregoPostRequest.responseText, error);
		}
	}
	pregoPostRequest.open('POST', url, true);
	pregoPostRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	pregoPostRequest.setRequestHeader('Content-length', parameters.length);
	pregoPostRequest.setRequestHeader('Connection', 'close');
	pregoPostRequest.send(parameters);
}


// PREGO AJAX GET REQUEST
// Makes an AJAX GET request
//  url         -  The URL to submit the request to
//  callback    -  The function to call when the request is complete, the function should take three parameters: xml, text, error
//                 The callback function can also refer to the XMLHTTPRequest object as 'this' if it needs to
var pregoGetRequest;
var pregoGetCallback;
function pregoAjaxGetRequest(url, callback)
{
	pregoGetRequest = pregoNewRequest();
	pregoGetCallback = callback;
	//pregoGetRequest.pregoComplete = callback; // Not supported in IE
	pregoGetRequest.onreadystatechange = function()
	{
		if(pregoGetRequest.readyState == 4)
		{
			var error = (pregoGetRequest.status != 200) ? pregoGetRequest.status + ' - ' + pregoGetRequest.statusText : null;
			//pregoGetRequest.pregoComplete(pregoGetRequest.responseXML, pregoGetRequest.responseText, error);
			pregoGetCallback(pregoGetRequest.responseXML, pregoGetRequest.responseText, error);
		}
	}
	pregoGetRequest.open('GET', url, true);
	pregoGetRequest.send('');
}