/* Queue for updates. The same key can be used by multiple forms, distinguished by their superkey. */
sa_updatequeue = {
	queue: [],
	lock: false,
	
	add: function(key, data, superkey, finishedCallback)
	{
		if (this.queue.length > 1 && this.queue[this.queue.length-1].key == key && this.queue[this.queue.length-1].superkey == superkey)
			this.queue.pop();
		
		this.queue.push({
			key:				key,
			data:				data,
			superkey:			superkey,
			finishedCallback:	finishedCallback
		});
		
		if (!this.lock)
			this.execute();
	},
	
	execute: function()
	{
		this.lock = true;
		
		var o = this.queue[0];
		var callbacktarget = this;
		
		$.ajax({
			aync:		true,
			url:		'update.aspx',
			type:		'post',
			data:		o.data,
			dataType:	'script',
			complete:	function ()
			{
				callbacktarget.finished();
			}
		});
	},
	
	finished: function()
	{
		var o = this.queue.shift();
		
		var noSiblingsInQueue = true;
		for (var i = 0; i < this.queue.length; i++)
		{
			if (this.queue[i].superkey == o.superkey)
			{
				noSiblingsInQueue = false;
				break;
			}
		}

		o.finishedCallback(noSiblingsInQueue);

		if (this.queue.length == 0)
			this.lock = false;
		else
			this.execute();
	}
};

