// 03/11/2003
// => added function/method callback (Mickael Putters)
//
// eventQueue.GenerateEvent( 'someURL' );				// Same thing as before
// eventQueue.GenerateEvent( 'someURL', func );			// Calls func( result )
// eventQueue.GenerateEvent( 'someURL', func, obj );	// Calls obj.func( result )

// global cargo variable (AS/BA)
var gcargo = new String('');


// Represents an Event
function FWEvent( strURL, pfnCallback, cObject)
{
	this.strURL			= strURL;
	this.objForm		= null;
	this.objValue		= null;
	this.bCalc			= 0;
	this.pfnCallback	= pfnCallback;
	this.cObject		= cObject;
};

// Represents an Event Queue
function FWEventQueue()
{
	this.bWaiting = 0;
	this.objQueue = new Array();
	this.nEvents = 0;
};


function FWEventQueue_SendNextEvent()
{
	this.bWaiting = 1;
	objCurr = this.objQueue[this.nEvents - 1];
	if (objCurr.objForm) 
    {
        if (objCurr.objForm.length > 0) 
        {
	        objDoc = window.top.frames['frm_send'].document;
	        objNewForm = objDoc.createElement("form");
	        objNewForm.name = 'event_form';
	        objNewForm.method = 'POST';
	        objNewForm.action = objCurr.strURL;

	        objDoc.documentElement.appendChild(objNewForm);

	        /*
	        * Transfer fields across
	        */

	        for (var i = 0; i < objCurr.objForm.length; i++) 
            {
	            var strValue;
	            var bQuotes = 0;
	            var dbType;

	            objInput = objDoc.createElement('input');
	            objInput.name = objCurr.objForm.elements[i].name;

	            if (objCurr.objForm.elements[i].tagName == 'TEXTAREA')
                {
	                var strValue = new String(objCurr.objForm.elements[i].innerText);
	                objInput.value = strValue.replace(/\n+/g, '`~');
	            }
	            else 
                {
	                objInput.value = objCurr.objForm.elements[i].value;
	                if (objCurr.objForm.elements[i].db_extra && objInput.value != top.frames['main'].strBlankDate)
	                    objInput.value = objInput.value + '|' + objCurr.objForm.elements[i].db_extra;
	            };
	            objNewForm.appendChild(objInput);
	        };
	        objNewForm.submit();
	    }
	    else
	        this.EventFinished();
	}
	else if (objCurr.objValue) 
    {
	    var strURL = objCurr.strURL + '&value=' + objCurr.objValue.value;
	    window.top.frames['frm_send'].location.replace(strURL);
	}
	else {
	    window.top.frames['frm_send'].location.replace(objCurr.strURL);
	};
};


function FWEventQueue_GenerateEvent( strURL, callback, object )
{
	if(this.nEvents > 0)
	{
		for(ev = this.nEvents; ev > 0; ev--)
			this.objQueue[ev] = this.objQueue[ev - 1];
	};

	// 03/11/2003 - Mickael Putters
	switch (arguments.length == 1)
	{
	case 2:
		this.objQueue[0] = new FWEvent( strURL, callback, null );
		break;

	case 3:
		this.objQueue[0] = new FWEvent( strURL, callback, object);
		break;

	default:
		this.objQueue[0] = new FWEvent( strURL, null, null);
		break;
	}

	this.nEvents++;
	if(!this.bWaiting)
		this.SendNextEvent();
};


function FWEventQueue_EventFinished( result )
{
	var cEvent = this.objQueue[this.nEvents - 1];

	if ((cEvent != null) && (cEvent.pfnCallback != null))
	{
		if (cEvent.cObject != null)
		{
			var vArray = new Array();
			vArray.push( result );
			cEvent.pfnCallback.apply( cEvent.cObject, vArray );
		}
		else
		{
			cEvent.pfnCallback( result );
		}
	}

	this.objQueue[this.nEvents - 1] = null;
	this.nEvents--;
	if(this.nEvents < 0) this.nEvents = 0;
	if(this.nEvents > 0)
	{
		try
		{
			this.SendNextEvent();
		}
		catch(e)
		{
			// an error has occurred - so remove the event from the queue
			this.EventFinished();
		}
		finally
		{
		}
	}
	else
		this.bWaiting = 0;
};


function FWEventQueue_EventFinishedOK( result )
{
	this.EventFinished( result );
};


function FWEventQueue_SubmitForm(strURL, objForm)
{
	if(objForm)
	{
		if(this.nEvents > 0)
		{
			for(ev = this.nEvents; ev > 0; ev--)
				this.objQueue[ev] = this.objQueue[ev - 1];
		};
		
		this.objQueue[0] = new FWEvent(strURL);
		this.objQueue[0].objForm = objForm;
		this.nEvents++;
		if(!this.bWaiting)
			this.SendNextEvent();
	};
};


function FWEventQueue_GenerateCalcEvent(objForm, strFormName)
{
//	if(objForm)
//	{
//		if(this.nEvents > 0)
//		{
//			for(ev = this.nEvents; ev > 0; ev--)
//				this.objQueue[ev] = this.objQueue[ev - 1];
//		};
//
//		this.objQueue[0] = new FWEvent(strFormName);
//		this.objQueue[0].objForm = objForm;
//		this.objQueue[0].bCalc = -1;	// Is a calculation
//		this.nEvents++;
//		if(!this.bWaiting)
//			this.SendNextEvent();
//	};
};

function FWEventQueue_GenerateValueEvent(strURL, objSrc)
{
	if(objSrc)
	{
		if(this.nEvents > 0)
		{
			for(ev = this.nEvents; ev > 0; ev--)
				this.objQueue[ev] = this.objQueue[ev - 1];
		};

		this.objQueue[0] = new FWEvent(strURL);
		this.objQueue[0].objValue = objSrc;
		this.nEvents++;
		if(!this.bWaiting)
			this.SendNextEvent();	
	};
};


FWEventQueue.prototype.SendNextEvent = FWEventQueue_SendNextEvent;
FWEventQueue.prototype.GenerateEvent = FWEventQueue_GenerateEvent;
FWEventQueue.prototype.EventFinished = FWEventQueue_EventFinished;
FWEventQueue.prototype.EventFinishedOK = FWEventQueue_EventFinishedOK;
FWEventQueue.prototype.SubmitForm = FWEventQueue_SubmitForm;
FWEventQueue.prototype.GenerateCalcEvent = FWEventQueue_GenerateCalcEvent;
FWEventQueue.prototype.GenerateValueEvent = FWEventQueue_GenerateValueEvent;
