//
var rqHttp			= getHTTPObject();  
var rqHttpCb		= null;
var rqHttpObj		= null;
var rqHttpPending	= new Array();
function RQHttpObj(url,cb,obj)
{
	this.m_url	= url;
	this.m_cb	= cb;
	this.m_obj	= obj;
	this.M_send	= function()
	{	RQAjaxCall( this.m_url, this.m_cb, this.m_obj);
	}
}

function getHTTPObject() 
{  
  // will store the reference to the XMLHttpRequest object
	var xmlHttp;

	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest)
	{
		try
		{	xmlHttp = new XMLHttpRequest();
		}
		catch(e)
		{
			xmlHttp = false;
		}
	} 
	// branch for IE/Windows ActiveX version
	else if(window.ActiveXObject)
	{
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				xmlHttp = false;
			}
		}
	}

	// return the created object or display an error message
	if (!xmlHttp)
	{	alert("Error creating the XMLHttpRequest object.");
	}
	else 
		return xmlHttp;
}

//---------------------------------------------
//  RQStartLoader
//  shows a loader
//  Second parameter (optional) is the id of an element to hide
//---------------------------------------------
function RQStartLoader(id,objHide)
{	var o;
	if( o=getObj(id))
	{
		o.style.display ="inline";
	}
	if( objHide && (o=getObj(objHide)))
		o.style.display = "none";
}
//---------------------------------------------
//  RQStopLoader
//  stops (hide) a loader
//---------------------------------------------
function RQStopLoader(id)
{	var o;
	if( o=getObj(id))
	{
		o.style.display ="none";
	}
}

//---------------------------------------------
//  RQAjaxTreatQueue
//---------------------------------------------
function RQAjaxTreatQueue()
{
	//RQDebug("RQAjaxTreatQueue - nb="+rqHttpPending.length);

	if( rqHttpPending.length)
	{
		// if still busy, we wait some more
		if( rqHttpCb || !(rqHttp.readyState == 4 || rqHttp.readyState == 0) )
			setTimeout(RQAjaxTreatQueue,400);
		else
		{
			var objToSend = rqHttpPending[0];
			objToSend.M_send();
			rqHttpPending.shift();
		}

	}
}

//---------------------------------------------
//  RQAjaxCall
//  Calls a script using GET method
//  For a global function: pass function pointer and no obj.  Example: RQAjaxCall("script.php", myCallback)
//  For an object method : pass method name and obj instance. Example: RQAjaxCall("script.php", "M_myMethod", this)
//---------------------------------------------
function RQAjaxCall(url,cb,obj)
{
	if( !rqHttp)
	{	RQError("RQAjaxCall - rqHttp is invalid");
		return;
	}
	if( rqHttpCb || !(rqHttp.readyState == 4 || rqHttp.readyState == 0) )
	{
		//RQDebug("RQAjaxCall - object is busy");
		rqHttpPending.push( new RQHttpObj(url,cb,obj));
		setTimeout(RQAjaxTreatQueue,400);
	} 
	else
	{
		//RQDebug("RQAjaxCall("+url+")");
		rqHttpCb = cb;									// store the callback, that will mark the rqHttp object busy
		rqHttpObj = obj;
		rqHttp.open("GET", url, true);
		rqHttp.onreadystatechange = rqAjaxCallback;
		rqHttp.send(null);
	}
}

//---------------------------------------------
//  RQAjaxPost
//  Calls a script using POST method
//---------------------------------------------
function RQAjaxPost(url,formId,cb,obj)
{
	if( !rqHttp)
	{	RQError("RQAjaxPost - rqHttp is invalid");
		return;
	}
	if( rqHttpCb || !(rqHttp.readyState == 4 || rqHttp.readyState == 0) )
	{
		//RQDebug("RQAjaxPost - object is busy");
		// TODO: setup a timeout to resend the command
	} 
	else
	{

		// append form data to the URL
		var formObj = getObj(formId);
		var postVars="";
		var sep="";
		if( formObj)
		{	var i
			for( i=0; i<formObj.length; i++)
			{	if( formObj[i].name)
				{	
					if(formObj[i].type=="radio" || formObj[i].type=="checkbox")
					{	if( formObj[i].checked)
						{	postVars += sep+escape(formObj[i].name)+"="+escape(formObj[i].value);
							sep="&";
						}
					}
					else
					{	postVars += sep+escape(formObj[i].name)+"="+escape(RQGetInputValue(formObj[i]));
						sep="&";
					}
				}
			}
		}
		//RQDebug("RQAjaxPost("+url+")");

		rqHttpCb = cb;									// store the callback, that will mark the rqHttp object busy
		rqHttpObj = obj;
		rqHttp.open("POST", url, true);
		rqHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		rqHttp.onreadystatechange = rqAjaxCallback;
		rqHttp.send(postVars);
	}
}


// rqAjaxCallback
// callback that receives answers from ajax calls
//---------------------------------------------
function rqAjaxCallback()
{
	// transaction has completed?
	if (rqHttp.readyState == 4) 
	{

			// exemple of XML response treatment
				/*
				// extract the XML retrieved from the server
				xmlResponse = rqHttp.responseXML;
				// obtain the document element (the root element) of the XML structure
				xmlDocumentElement = xmlResponse.documentElement;
				// get the text message, which is in the first child of
				// the the document element
				helloMessage = xmlDocumentElement.firstChild.data;
				// update the client display using the data received from the server
				*/
			
		// call the user callback
		if( rqHttpCb && rqHttpObj && typeof(rqHttpObj[rqHttpCb])=='function')
		{
			//RQDebug("RQAjaxCallAnswer "+rqHttpCb+" with object");
			if( rqHttp.status == 200)
				rqHttpObj[rqHttpCb](rqHttp.responseText,false);
			else
			{	RQError("There was a problem accessing the server: "+rqHttp.statusText)
				rqHttpObj[rqHttpCb](null,true);
			}


		}
		else if(rqHttpCb && typeof(rqHttpCb)=='function')
		{
			//RQDebug("RQAjaxCallAnswer received for "+rqHttpCb);
			if( rqHttp.status == 200)
				rqHttpCb(rqHttp.responseText,false);
			else
			{	RQError("There was a problem accessing the server: "+rqHttp.statusText)
				rqHttpCb(null,true);
			}
		}
		else
			RQError("RQAjax : "+rqHttpCb+" is not a valid callback");
		rqHttpCb = null;		// set the http request object free
		rqHttpObj= null;


	}

}

// --------------------------------------------------
// rqAjaxIncludeScript
// dynamically includes a script passed as parameter
// --------------------------------------------------
function rqAjaxIncludeScript(script,id)
{
	var el;
	var insert=true;
	if( id)
		el = document.getElementById(id);
	if(el)
	{	// remove ancient script
		while (el.firstChild)
			el.removeChild(el.firstChild);
		insert = false;
	
	}
	else
	{
		// create a new script node
		el=document.createElement("script");
		el.setAttribute('name',id);
		el.charset="UTF-8";
	}
	// create the script content
	var	scriptNode = document.createTextNode(script);
	el.appendChild(scriptNode);
	if( insert)
	{	var head=document.getElementsByTagName('head')[0];
		head.appendChild(el);
	}

}


//  RQAjaxCallPlugin
function RQAjaxCallPlugin(pluginName,vars,cb,obj)
{
	var url='?rqcode='+sRqSessionCode+'&rqmod='+pluginName+(vars?"&"+vars:"");
	RQAjaxCall(url,cb,obj);
}
