//	Brad Spencer Ajax Functions
// Copyright B.Spencer April 2008
// -------------------------------------------------------------------------
// This is a higher level ajax function that executes a server-side
// then also generated an XML file that is then l;oaded into the DOM 
// loadXml (see below).
//--------------------------------------------------------------------------
function ajaxGet(url, p1, p2, file)
{
	var bnum=0;
	ajaxRequest=null;
	isIE=false;
	try
	{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
		} catch (e)	{
			// Internet Explorer Browsers
			try
			{	isIE =true;
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try
					{
						ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
						} catch (e)	{
						// Something went wrong
						//alert("Your browser is too old to support AJAX applications!");
						return false;
					}
				}
		}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function()
	{	if(ajaxRequest.readyState == 4)  {return ajaxRequest;}}
	
	//alert('php program request = '+url+p1+p2+file)
	ajaxRequest.open("GET", url+p1+p2+file, false);
	if(isIE){ ajaxRequest.send(); }
	else { ajaxRequest.send(null); }

	// now go and get the XML file created
	var query="";
	var fileNm = file.split("file=");
	var query="";
	//alert("file in ajaxGet = "+fileNm[1])
	ajaxRequest = loadXml(fileNm[1], query);
	return ajaxRequest;
}
//--------------------------------------------------------------------------
function ajaxPost(url, p1, p2, file)
{
	var bnum=0;
	ajaxRequest=null;
	isIE=false;
	try
	{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
		} catch (e)	{
			// Internet Explorer Browsers
			try
			{	isIE =true;
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try
					{
						ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
						} catch (e)	{
						// Something went wrong
						//alert("Your browser is too old to support AJAX applications!");
						return false;
					}
				}
		}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function()
	{	if(ajaxRequest.readyState == 4)  { return ajaxRequest;}}
	
	//alert(url+p1+p2+file)
	ajaxRequest.open("POST", url, false);
	ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	if(isIE){ ajaxRequest.send(p1+p2+file); }
	else { ajaxRequest.send(p1+p2+file); }

	// now go and get the XML file created
	var query="";
	var fileNm = file.split("file=");
	var query="";
	
	//alert("file in ajaxPost = "+fileNm[1])
	ajaxRequest = loadXml(fileNm[1], query);
	return ajaxRequest;
}

function makeXml(url, p1, async)
{
	var bnum=0;
	var ajaxRequest01=null;
	isIE=false;
	try
	{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest01 = new XMLHttpRequest();
		} catch (e)	{
			// Internet Explorer Browsers
			try
			{	isIE =true;
				ajaxRequest01 = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try
					{
						ajaxRequest01 = new ActiveXObject("Microsoft.XMLHTTP");
						} catch (e)	{
						// Something went wrong
						//alert("Your browser is too old to support AJAX applications!");
						return false;
					}
				}
		}
	// Create a function that will receive data sent from the server
	ajaxRequest01.onreadystatechange = function()
	{	if(ajaxRequest01.readyState == 4)  {return false;}}
	//alert('inside makeXml ' +url+p1)
	if (async == null){async = false}
	//alert ('makeXml : '+url+p1)
	ajaxRequest01.open("GET", url+p1, async);
	if(isIE){ ajaxRequest01.send(); }
	else { ajaxRequest01.send(null); }
	if (ajaxRequest01.status!= 200) { return false;}
	return true;
}

function fetchXml(url, p1, async, type)
{
	//alert (p1)
	var bnum=0;
	ajaxRequest=null;
	isIE=false;
	if (async == null){async = false}
	if(type != 'GET' && type != 'POST') {type='POST';}
	try
	{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
		} catch (e)	{
			// Internet Explorer Browsers
			try
			{	isIE =true;
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try
					{
						ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
						} catch (e)	{
						// Something went wrong
						//alert("Your browser is too old to support AJAX applications!");
						return false;
					}
				}
		}
	// Create a function that will retrieve data sent from the server
	ajaxRequest.onreadystatechange = function()
	{	if(ajaxRequest.readyState == 4)  {return ajaxRequest;}}

	// for a GET request
	if (type == 'GET' || type == 'get')
	{
		ajaxRequest.open("GET", url+p1, async);
		if(isIE){ ajaxRequest.send(); }
		else { ajaxRequest.send(null); }
	}
	// for a POST request
	else if (type == 'POST' || type == 'post')
	{
		ajaxRequest.open("POST", url, false);
		//ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		//ajaxRequest.send(p1); 

		ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajaxRequest.setRequestHeader("Content-length", p1.length);
		ajaxRequest.setRequestHeader("Connection", "close");
		ajaxRequest.send(p1);

	}
	// done so return object
	//if (ajaxRequest.status != 200) {alert('error in fetchXml for '+type+' request'); return ajaxRequest;}
	return ajaxRequest;
}	

function makeFile(url, p1, async, type)
{
	var bnum=0;
	ajaxRequest=null;
	isIE=false;
	if (async == null){async = false}
	if(type != 'GET' && type != 'POST') {type='POST';}
	try
	{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
		} catch (e)	{
			// Internet Explorer Browsers
			try
			{	isIE =true;
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try
					{
						ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
						} catch (e)	{
						// Something went wrong
						//alert("Your browser is too old to support AJAX applications!");
						return false;
					}
				}
		}
	// Create a function that will retrieve data sent from the server
	ajaxRequest.onreadystatechange = function()
	{	if(ajaxRequest.readyState == 4)  {return ajaxRequest;}}

	// for a GET request
	//alert(type)
	if (type == 'GET' || type == 'get')
	{
		ajaxRequest.open("GET", url+p1, async);
		if(isIE){ ajaxRequest.send(); }
		else { ajaxRequest.send(null); }
	}
	// for a POST request
	else if (type == 'POST' || type == 'post')
	{
		//alert (url)
		//alert(p1)
		ajaxRequest.open("POST", url, false);
		ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		ajaxRequest.setRequestHeader("Content-length", p1.length);
		ajaxRequest.setRequestHeader("Connection", "close");
		ajaxRequest.send(p1); 
	}
	// done so return object
	//alert(ajaxRequest.status)
	if (ajaxRequest.status!= 200) {alert('error in makeFile for '+type+' request'); return ajaxRequest;}
	return ajaxRequest;
}	

//-----------------------------------------------------------------------------
function loadXml(url, query, async)
{
//-----------------------------------------------------------------------------
// This is a basic ajax funtion to load an XML file into DOM or execute a 
// server-side script such as delete a file.
//
//	useage = var Request = loadXml(xmlFile, extensions, async);
//
//	where xmlFile is a serverside xml file or script
//	where extensions are attached attributes required by remote script
//	where async is a true false to process asynchronously or synchronously (true/false/)
//
//-----------------------------------------------------------------------------	
	Request='';
	Request=null;
	isIE=false;
	try
	{
		// Opera 8.0+, Firefox, Safari
		Request = new XMLHttpRequest();
		} catch (e)	{
			// Internet Explorer Browsers
			try
			{	isIE =true;
				Request = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try
					{
						Request = new ActiveXObject("Microsoft.XMLHTTP");
						} catch (e)	{
						// Something went wrong
						alert("Your browser is too old to support AJAX applications!");
						return false;
					}
				}
		}
	// Wait until onready state indicates job done
	Request.onreadystatechange = function(){if(Request.readyState == 4) { return Request;}}
	
	// go ahead and execute the remote service
	//alert("loadXml = "+url+query)
	if (async == null){async = false}
	Request.open("GET", url+query, async);
	if(isIE){ Request.send(); }
	else { Request.send(null); } 
	return Request;
}


//-----------------------
function ajaxGet02(url, p1, p2, file)
{
	var bnum=0;
	ajaxRequest=null;
	isIE=false;
	try
	{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
		} catch (e)	{
			// Internet Explorer Browsers
			try
			{	isIE =true;
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try
					{
						ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
						} catch (e)	{
						// Something went wrong
						//alert("Your browser is too old to support AJAX applications!");
						return false;
					}
				}
		}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function()
	{	if(ajaxRequest.readyState == 4)  {return ajaxRequest;}}
	
	//alert(url+p1+p2+file)
	ajaxRequest.open("GET", url+p1+p2+file, false);
	if(isIE){ ajaxRequest.send(); }
	else { ajaxRequest.send(null); }

	// now go and get the XML file created
	var query="";
	var fileNm = file.split("file=");
	var query="";
	//alert("file in ajaxGet = inc/"+fileNm[1])
	ajaxRequest = loadXml02("inc/"+fileNm[1], query);
	
	return ajaxRequest;
}

//-----------------------------------------------------------------------------
function loadXml02(url, query, async)
{
//-----------------------------------------------------------------------------
// This is a basic ajax funtion to load an XML file into DOM or execute a 
// server-side script such as delete a file.
//
//	useage = var Request = loadXml(xmlFile, extensions, async);
//
//	where xmlFile is a serverside xml file or script
//	where extensions are attached attributes required by remote script
//	where async is a true false to process asynchronously or synchronously (true/false/)
//
//-----------------------------------------------------------------------------	
	Request=null;
	isIE=false;
	try
	{
		// Opera 8.0+, Firefox, Safari
		Request = new XMLHttpRequest();
		} catch (e)	{
			// Internet Explorer Browsers
			try
			{	isIE =true;
				Request = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try
					{
						Request = new ActiveXObject("Microsoft.XMLHTTP");
						} catch (e)	{
						// Something went wrong
						alert("Your browser is too old to support AJAX applications!");
						return false;
					}
				}
		}
	// Wait until onready state indicates job done
	Request.onreadystatechange = function(){if(Request.readyState == 4) { return Request;}}
	
	// go ahead and execute the remote service
	//alert("loadXml = "+url+query)
	if (async == null){async = false}
	Request.open("GET", url+query, async);
	if(isIE){ Request.send(); }
	else { Request.send(null); } 
	//alert (Request)
	return Request;
}
