
var			RDE_NOT_INITIALIZED=0;						// 0: request not ready yet.
var			RDE_PENDING=1;										// 1: request in process.
var			RDE_READY=2;											// 2: request ready with results.
var			RDE_FAILED=3;											// 3: request failed/timed out.

function CaRDERequest()
{
	this.rdeReqObj = new AJAXRequest();
	this.rdeReqObj.rdeState = RDE_NOT_INITIALIZED;
	this.rdeReqObj.rdeTimeout = 9999;
	this.rdeReqObj.rdeReqPath = "";
	this.rdeReqObj.rdeResult = null;
	this.rdeReqObj.onReadyFunc = function(rdeObj){};
	this.rdeReqObj.onErrorFunc = function(errObj){};

	// Set functions.
	this.setTimeout = function(timeout)
	{
		this.rdeReqObj.rdeTimeout = timeout;
	}
	this.setReadyFunc = function(readyFunc)
	{
		this.rdeReqObj.onReadyFunc = readyFunc;
	}
	this.setErrorFunc = function(errorFunc)
	{
		this.rdeReqObj.onErrorFunc = errorFunc;
	}

	// Load function.
	this.load = function(strRequest)
	{
		if (this.rdeReqObj.rdeState == RDE_PENDING)
			this.rdeReqObj.stop();

		try
		{
			this.rdeReqObj.rdeState = RDE_PENDING;
			this.rdeReqObj.rdeReqPath = strRequest;
			this.rdeReqObj.rdeReqStart = new Date();
			this.rdeReqObj.rdeResult = null;

			this.rdeReqObj.addEvent(EVENT_UPDATE, {func:this.rdeReqObj.caStateChangeFunc});
			this.rdeReqObj.addEvent(STATE_READY, {func:this.rdeReqObj.caOnRequestComplete});
			this.rdeReqObj.addEvent(EVENT_ERROR, {func:this.rdeReqObj.caOnRequestError});
			this.rdeReqObj.setURL(this.rdeReqObj.rdeReqPath);
	 		this.rdeReqObj.load();
		}
		catch (e)
		{
			this.caOnRequestError({"value": e.toString()});
		}
	}

// Do not directly use any of the functions under this line!
//----------------------------------------------------------------------------

	this.rdeReqObj.caOnRequestComplete = function()
	{
		var			reqTime=new Date().getTime()-this.rdeReqStart.getTime();
		var			rdeObj=null;


		// Request ready. Now look at the result.
		if (this.objXML.readyState != 4)
		{
			this.caOnRequestError({"value": "Internal error"});
			return;
		}

		if (this.objXML.status != 200 && this.objXML.status != 304)
		{
			this.caOnRequestError({"value": "No response from the RDE (status=" + this.objXML.status + ")"});
			return;
		}

		try
		{
			rdeObj = eval("(" + this.objXML.responseText + ")");
		}
		catch (e)
		{
			this.caOnRequestError({"value": e.toString()});
			return;
		}

		if (rdeObj == null || typeof rdeObj == "undefined")
		{
			this.caOnRequestError({"value": "Keine Empfehlung vorhanden."});
			return;
		}

		if (typeof rdeObj[0].global != "undefined")
		{
			if (rdeObj[0].global.controlgroup == "true")
			{
				this.caOnRequestError({"value": "Kontrollgruppe."});
				return;
			}
		}

		this.rdeResult = rdeObj;

//if (document.getElementById("navi"))
//caRDEShowResult(true);

		this.rdeState = RDE_READY;

		this.onReadyFunc(this);

//		alert("Request Time (ms): " + reqTime + "\n" + this.rdeReqObj.responseText);
	}

	this.rdeReqObj.caOnRequestUpdate = function()
	{
		var			reqTime=new Date().getTime()-this.rdeReqStart.getTime();


		if (reqTime >= this.rdeReqTimeout)
		{
			// Timeout reached.
			this.caOnRequestError({"value": "RDE response timed out (" + reqTime + "ms)"});
			this.stop();
		}
	}

	this.rdeReqObj.caOnRequestError = function(evt)
	{
//		alert(evt.value);

		if (this.rdeState != RDE_FAILED)
		{
			this.rdeState = RDE_FAILED;
			this.onErrorFunc(evt.value);
		}
	}

	this.caOnRequestError = function(evt)
	{
//		alert(evt.value);

		if (this.rdeReqObj.rdeState != RDE_FAILED)
		{
			this.rdeReqObj.rdeState = RDE_FAILED;
			this.rdeReqObj.onErrorFunc(evt.value);
		}
	}

	this.rdeReqObj.caStateChangeFunc = function()
	{
		this.caOnRequestUpdate();
	}
}

