var formToBeSent = null;
var updateFunctionName = "";
var startFunctionName = "";
var endFunctionName = "";
var categoryId = null;
var interval = null;

//runtime parse javascript
function parse(src) { document.write('<script type="text/javascript" language="javascript" src="'+src+'"></script>'); }

parse('/dwr/engine.js');
parse('/dwr/interface/UploadService.js');

function monitorProgress(form, startFunction, updateFunction, endFunction)
{
	if (categoryId == null)
	{
		// Not configurated correctly: just upload the files
		return true;
	}
	formToBeSent = form;
	updateFunctionName = updateFunction;
	startFunctionName = startFunction;
	endFunctionName = endFunction;
	interval = setInterval("refreshUploadProgress()", 1000);
	eval(startFunctionName + "()");
	formToBeSent.submit();
	return false;
}

function refreshUploadProgress() {
	UploadService.getUploadInfo( this.updateProgress.bind(this) );
}

function updateProgress(data) {
	var percent = Math.round( ( data.bytesRead / data.totalSize ) * 100 );

	if (data.bytesRead == data.totalSize) // DONE
	{
		clearInterval(interval);
		eval(endFunctionName + "()");
	}
	else
	{
		eval(updateFunctionName + "('" + data.bytesRead
			+ "','" + data.totalSize + "','"
			+ percent + "');" );
	}
}
