/* successCallback: function ( request_output, data )
	failCallback: function ( request status, request_output, data ) */
function ServerRequest( reqId, data, wait)
{
	this.Request=null
	this.wait=$(wait)
	this.data=data
	this.reqId=reqId
	this.params={}

	this.setReqId=function(nId){this.reqId=nId}
	this.setParam=function(p,v){this.params[p]=v}

	this.Execute=function(okCb,koCb, debug)
	{
		try{this.Request = new XMLHttpRequest()}
		catch(a){
			try{this.Request=new ActiveXObject("Msxml2.XMLHTTP")}
			catch(b){
				try{this.Request=new ActiveXObject("Microsoft.XMLHTTP")}
				catch(c){
					this.Request = null
					alert("Request is unavailable: something might not work! UPGRADE BROWSER")
					return false
				}
			}
		}
	
		var successCallback=okCb
		var failCallback=koCb

		var url=document.myurlpath+"request.php?s="+document.section+"&p="+document.site_prefix+"&l="+document.lang+"&RQST="+this.reqId
		for(var i in this.params)
			url+="&"+i+"="+this.params[i]

		if ( debug )
			alert("URL for "+this.reqId+" is: "+url)

		this.Request.open("GET",url,true)

		var w = this.wait?new Wait(this.wait):null
		if (w)w.Start()

		var OnRSC = function()
		{
			if ( debug ) alert("For "+OnRSC.reqId+"-> rs: "+OnRSC.Request.readyState+" - : "+OnRSC.Request.status)
			
		 	if ( OnRSC.Request.readyState==4 ) // done
		 	{
				if ( OnRSC.wait ) OnRSC.wait.Stop()
				if (OnRSC.Request.status==200) // request successfull..
				{
					try { eval( OnRSC.Request.responseText ) }
					catch (e) {
						alert("Error in processing request result ("+OnRSC.Request.responseText+"): "+e+"!")
						var request_output = {status:"ko",error:"unk error"}
					}

					if ( request_output.status == "ko" )
					{
						try { failCallback(request_output, OnRSC.Request.status, OnRSC.local_data) }
						catch (err) { alert(request_output.error+" - "+err) }
					 }
					else
					{
						try { successCallback(request_output, OnRSC.local_data) }
						catch (err) { alert("Error calling successCallback function ("+err+")") }
					}
					if ( debug && request_output.execution_time)
						alert("Total execution time: "+request_output.execution_time)
				}
				else
				{
					try{failCallback(null,OnRSC.Request.status, OnRSC.local_data)}
					catch(err){alert("Request error ("+OnRSC.Request.status+")! "+err+")")}
				}
			}
		}
		OnRSC.Request=this.Request
		OnRSC.reqId=this.reqId
		OnRSC.local_data=this.data;
		OnRSC.wait=w
		this.Request.onreadystatechange=OnRSC
		this.Request.send(null)
	}
}