     // 
     // Base AJAX functions
     //

     // url: page to request
     // params: args to post in request
     // callbackFunction: function to call when the request is complete - must receive two arguments
     // target: argument passed on to the callback function (usually target element to display response)
     function ajax(url, params, callbackFunction, target)
     {
	var request = new XMLHttpRequest();
  	request.open("POST", url, true);
  	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request.setRequestHeader("Content-length", params.length);
	request.setRequestHeader("Connection", "close");

  	request.onreadystatechange = function()
  	{
	  if (request.readyState == 1) { toggleloading(1); }

    	  if (request.readyState == 4 && request.status == 200)
    	  {
	    if (request.responseText) { callbackFunction(request.responseText, target); }
	    toggleloading(0);
	  }
	};

  	request.send(params);
     }

     // Get an XMLHttpRequest object
     if( !window.XMLHttpRequest ) XMLHttpRequest = function()
     {
	try{ return new ActiveXObject("Msxml2.XMLHTTP.6.0") }catch(e){}
	try{ return new ActiveXObject("Msxml2.XMLHTTP.3.0") }catch(e){}
	try{ return new ActiveXObject("Msxml2.XMLHTTP") }catch(e){}
	try{ return new ActiveXObject("Microsoft.XMLHTTP") }catch(e){}
	throw new Error("Could not find an XMLHttpRequest alternative.")
     };

     // Used to display the results of an ajax request
     function ajax_generic(response, targetElement)
     {
        var target = document.getElementById(targetElement);
        if (target && target.innerHTML != response)
        {
           if (targetElement == "Main")
           {
             var hold = target.style.visibility;
             target.style.visibility = "hidden";
             target.innerHTML = response;
             fixPageSize();
             target.style.visibility = hold;
           }
           else
           {
             target.innerHTML = response;
           }
        }
     }  


     // _____________________________________________________________


     // 
     // Global functions
     //

     function getBrowserSize()
     {
           // Gets page width, height and window width, height
           // Core code from - quirksmode.org
           var xScroll, yScroll;
	   if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	   } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	   } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	   }
	
	   var windowWidth, windowHeight;
	   if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	   } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	   } else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	   }	
	
	   // for small pages with total height less then height of the viewport
	   if(yScroll < windowHeight){
		pageHeight = windowHeight;
           } else { 
		pageHeight = yScroll;
	   }

 	   // for small pages with total width less then width of the viewport
	   if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	   } else {
		pageWidth = xScroll;
	   }

           return new Array(windowWidth, windowHeight, pageWidth, pageHeight);
     }

     function fixPageSize()
     {
           var bodyHeight = document.getElementById("Main").offsetHeight + 68;
           if (bodyHeight > 600)
              document.getElementById("BodyArea").style.height = bodyHeight + "px";
           else 
              document.getElementById("BodyArea").style.height = "600px";
     }

     // escapes the & symbol to be sent through an ajax request
     function prepare(arg)
     {
        arg = arg.replace(/=/g, "%3D");
        return arg.replace(/&/g, "%26");
     }

     var toggleloading_var = 0;
     function toggleloading(arg)
     {
	if (arg == 1) { toggleloading_var += 1; }
	else if (toggleloading_var > 0) { toggleloading_var -= 1; }

	var ajax_loader = document.getElementById("ajax-loader");
	if(toggleloading_var > 0)
	{
	  ajax_loader.style.display="block";
	} else {
	  ajax_loader.style.display="none";
	}
     }