﻿//Global XMLHTTP Request object
var XmlHttp;
var aXmlHttp = new Array();
var openingHoursXmlHttp;

//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable  
function CreateXmlHttp()
{
	var o;
	//Creating object of XMLHTTP in IE
	try
	{
		o = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			o = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			o = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!o && typeof XMLHttpRequest != "undefined") 
	{
		o = new XMLHttpRequest();
	}
	return o;
}

function AJAXcallback(name, url, fn, syn)
{
	aXmlHttp[name] = CreateXmlHttp();
	if(aXmlHttp[name])
	{
	    if (syn == undefined)
	        bAsyn = true;
	    else
	        bAsyn = false;

		//Setting the event handler for the response
		if (bAsyn == true)
		    aXmlHttp[name].onreadystatechange = function(){HandleAJAXResponse(name, fn)};
		
		//Initializes the request object with GET (METHOD of posting), 
		//Request URL and sets the request as asynchronous.
		document.body.style.cursor = "wait";
		try
		{  
			aXmlHttp[name].open("GET", url, bAsyn);
		} 
		catch (e) 
		{
			document.body.style.cursor = "default";
			alert(e.description);
			return;
		}
		
		//Sends the request to server
		aXmlHttp[name].send(null);		
		
		if (bAsyn == false)
		{
		    fn.call();
		    document.body.style.cursor = "default";
		}

	}
}

function HandleAJAXResponse(name, fn)
{
	// To make sure receiving response data from server is completed
	if(aXmlHttp[name].readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(aXmlHttp[name].status == 200)
		{			
			fn.call();
			//document.body.style.cursor = "default";
		}
		else
		{
			//document.body.style.cursor = "default";
			alert("There was a problem retrieving data from the server." );
			__doPostBack.call('','');
		}
	}
	
	// don't reset the cursor until all of the XmlHttp objects have finished
	var finished=true;
	for(x in aXmlHttp)
	{
	    if(aXmlHttp[x].readyState!=4)
	    {
	        finished=false;
	        break;
	    }
	}
	if(finished)
	    document.body.style.cursor = "default";
}
