//http://www.sitepoint.com/print/take-command-ajax
var loading = null;
var loadingStack = 0;

//PROBLEM WITH DISAPPEARING MENU WAS IN positionFooter()




//function makeHttpRequest(url, callback_function, return_xml)
function makeHttpRequest(url, method, data, callback_function, return_xml, async)
{
   loading = document.getElementById('loading');
   var http_request = false;
   if(async == null)
   		async = true;

   http_request = new XMLHttpRequest();

   if (!http_request) {
       alert('Unfortunatelly you browser doesn\'t support this feature.');
       return false;
   }

//START DYNAMIC CALL_BACK FUNCTION
   http_request.onreadystatechange = function() {
		readyStateChanged();
   }

   function readyStateChanged()     {
       if (http_request.readyState == 4) {
           if (http_request.status == 200) {
               loadingStack--;

//test(loading.style.display+' '+loadingStack);
               if(loadingStack <= 0)
			loading.style.display = 'none';


               if (return_xml) {
                   eval(callback_function + '(http_request.responseXML)');
               } else {
                   eval(callback_function + '(http_request.responseText)');
               }
           } else {
               alert('There was a problem with the request.(Code: ' + http_request.status + ')');
           }
       }
   }
//END DYNAMIC CALL_BACK FUNCTION
   //=============================================================

   var reqHeaderType = null;
   if(!method)
      	method = 'GET';
   //=============================================================
   if(method == 'GET')
      	reqHeaderType = 'text/xml';
   else if(method == 'POST')
      	reqHeaderType = 'application/x-www-form-urlencoded';
   //=============================================================
   if(!data)
      	data = '';
   if(data == null)
	data = ' ';
//test('['+data+']');
   //=============================================================
   http_request.open(method, url, async);
   http_request.setRequestHeader("Content-Type", reqHeaderType);
   http_request.send(data);



	//PROBLEM WITH DISAPPEARING MENU WAS IN positionFooter()
	loading = document.getElementById('loading');
	if (window.attachEvent)
		loading.style.position = 'absolute';
	else
		loading.style.position = 'fixed';

	var bodyRef = document.getElementsByTagName('body')[0];



   loadingStack++;
   loading.style.display = 'block';

   if(async == false)
   		readyStateChanged();
}



function getRequest(url, data, callback_function, return_xml)
{
	makeHttpRequest(url+'?'+data, 'GET', null, callback_function, return_xml);
}

function postRequest(url, data, callback_function, return_xml)
{
	makeHttpRequest(url, 'POST', data, callback_function, return_xml);
}






//==================================================================================
function makeHttpRequest2(url, method, data, callback_function, return_xml, async)
{
	loading = document.getElementById('loading');
   var http_request = false;
   if(async == null)
   		async = true;

   http_request = new XMLHttpRequest();

   if (!http_request) {
       alert('Unfortunatelly you browser doesn\'t support this feature.');
       return false;
   }

//START DYNAMIC CALL_BACK FUNCTION
   http_request.onreadystatechange = function() {
		readyStateChanged();
   }

   function readyStateChanged()     {
       if (http_request.readyState == 4) {
           if (http_request.status == 200) {
		loadingStack--;
		if(loadingStack <= 0)
			loading.style.display = 'none';


				if(callback_function instanceof Array)
				{
					for(var i=0;   i<callback_function.length;  i++)
					{
						cb = callback_function[i];
						if (return_xml) {
						   cb(http_request.responseXML);
						}
						else
						{
						   cb(http_request.responseText);
						}
					}

				}
				else
				{
					if (return_xml)
					{
						if(callback_function != null)
							callback_function(http_request.responseXML);
					} else {
						if(callback_function != null)
							callback_function(http_request.responseText);
					}
				}
           } else {
               alert('There was a problem with the request.(Code: ' + http_request.status + ')');
           }
       }
   }
//END DYNAMIC CALL_BACK FUNCTION
   //=============================================================

   var reqHeaderType = null;
   if(!method)
      	method = 'GET';
   //=============================================================
   if(method == 'GET')
      	reqHeaderType = 'text/xml';
   else if(method == 'POST')
      	reqHeaderType = 'application/x-www-form-urlencoded';
   //=============================================================
   if(!data)
      	data = null;
   if(data == null)
	data = ' ';
//test('['+data+']');
   //=============================================================
   http_request.open(method, url, async);
   http_request.setRequestHeader("Content-Type", reqHeaderType);
   http_request.send(data);



	//PROBLEM WITH DISAPPEARING MENU WAS IN positionFooter()
	loading = document.getElementById('loading');
	if (window.attachEvent)
		loading.style.position = 'absolute';
	else
		loading.style.position = 'fixed';
	var bodyRef = document.getElementsByTagName('body')[0];

	bodyRef.insertBefore(loading, bodyRef.lastChild);



   loadingStack++;
   loading.style.display = 'block';

   if(async == false)
   		readyStateChanged();
}




