// 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 = '';

// 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 = [];
	this.nEvents = 0;
}

FWEventQueue.prototype.SendNextEvent = function() {
	this.bWaiting = 1;
	objCurr = this.objQueue[this.nEvents - 1];
	if (objCurr.objForm) 
    {
        if (objCurr.objForm.length) 
        {
	        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);
	};
};

FWEventQueue.prototype.GenerateEvent = function(strURL, callback, object) {
	this.QueueEvent(new FWEvent(strURL, callback || null, object || null));
};

FWEventQueue.prototype.EventFinished = function(result) {
	var cEvent = this.objQueue[this.nEvents - 1];
	try {
		if (cEvent && cEvent.pfnCallback)
		{
			if (cEvent.cObject)
			{
				var vArray = [];
				vArray.push( result );
				cEvent.pfnCallback.apply( cEvent.cObject, vArray );
			}
			else
			{
				cEvent.pfnCallback( result );
			}
		}
	} catch(e) {
	}
	this.objQueue.pop();
	this.nEvents = this.objQueue.length;
	if(this.nEvents)
	{
		try
		{
			this.SendNextEvent();
		}
		catch(e)
		{
			// an error has occurred - so remove the event from the queue
			this.EventFinished();
		}
	}
	else
		this.bWaiting = 0;
};

FWEventQueue.prototype.EventFinishedOK = function (result) {
	this.EventFinished( result );
};

FWEventQueue.prototype.SubmitForm = function(strURL, objForm) {
	if(objForm) {
		var ev = new FWEvent(strURL);
		ev.objForm = objForm;
		this.QueueEvent(ev);
	}
};

FWEventQueue.prototype.GenerateValueEvent = function(strURL, objSrc) {
	if(objSrc) {
		var ev = new FWEvent(strURL);
		ev.objValue = objsrc;
		this.QueueEvent(ev);
	}
};

FWEventQueue.prototype.QueueEvent = function(ev) {
	var bGOM = (ev.pfnCallback && ev.pfnCallback.toString().indexOf("DoScreenFullyLoaded") > -1);

	if(this.nEvents) {
		// If a GOM or if the only event is running stick the event at the begining of the array as normal
		if(bGOM || (this.nEvents == 1 && this.bWaiting)) {
			this.objQueue.unshift(ev);
		} else {
			// this is NOT a GOM event so we want to stop when(if) we find a GOM event
			for(var i = this.nEvents - 1; i >= 0; i--) {
				if(this.objQueue[i].pfnCallback && this.objQueue[i].pfnCallback.toString().indexOf("DoScreenFullyLoaded") > -1) break;
			}
			this.objQueue.splice(i + 1, 0, ev);
		}
	} else {
		this.objQueue[0] = ev;
	}

	this.nEvents++;
	if (!this.bWaiting) this.SendNextEvent();
}

