
function cancelQueue(instance) {
	document.getElementById(instance.customSettings.cancelButtonId).style.display = 'none';
	instance.stopUpload();
	var stats;
	do {
		stats = instance.getStats();
		instance.cancelUpload();
	} while (stats.files_queued !== 0);
	
}

function fileDialogStart() { }

function fileQueued(file) {
	try {
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setStatus(SWFUpload.msg('progress_pending'));
		progress.toggleCancel(true, this);
	} catch (ex) {
		this.debug(ex);
	}

}

function fileQueueError(file, errorCode, message) {
	try {
		if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
			alert(SWFUpload.msg('queue_exceeded1') + "\n" + (message === 0 ? SWFUpload.msg('queue_exceeded2') : SWFUpload.msg('queue_exceeded3') + (message > 1 ? SWFUpload.msg('queue_exceeded4') + message + SWFUpload.msg('queue_exceeded5') : SWFUpload.msg('queue_exceeded6'))));
			return;
		}
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setError();
		progress.toggleCancel(false);
		switch (errorCode) {
		case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
			progress.setStatus(SWFUpload.msg('queue_limit_size1'));
			this.debug(SWFUpload.msg('error_code') + SWFUpload.msg('queue_limit_size2') + SWFUpload.msg('file_name') + file.name + ", " + SWFUpload.msg('file_size') + file.size + ", " + SWFUpload.msg('mesage') + message);
			break;
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
			progress.setStatus(SWFUpload.msg('queue_limit_zero1'));
			this.debug(SWFUpload.msg('error_code') + SWFUpload.msg('queue_limit_zero2') + SWFUpload.msg('file_name') + file.name + ", " + SWFUpload.msg('file_size') + file.size + ", " + SWFUpload.msg('mesage') + message);
			break;
		case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
			progress.setStatus(SWFUpload.msg('queue_limit_type1'));
			this.debug(SWFUpload.msg('error_code') + SWFUpload.msg('queue_limit_type2') + SWFUpload.msg('file_name') + file.name + ", " + SWFUpload.msg('file_size') + file.size + ", " + SWFUpload.msg('mesage') + message);
			break;
		case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
			alert(SWFUpload.msg('queue_limit_exceeded1') +  (message > 1 ? SWFUpload.msg('queue_limit_exceeded2') +  message + SWFUpload.msg('queue_limit_exceeded3') : SWFUpload.msg('queue_limit_exceeded4')));
			break;
		default:
			if (file !== null) {
				progress.setStatus(SWFUpload.msg('unhandled_error'));
			}
			this.debug(SWFUpload.msg('error_code') + errorCode + ", " + SWFUpload.msg('file_name') + file.name + ", " + SWFUpload.msg('file_size') + file.size + ", " + SWFUpload.msg('mesage') + message);
			break;
		}
	} catch (ex) {
        this.debug(ex);
    }
}

function fileDialogComplete(numFilesSelected, numFilesQueued) {
	try {
		if (this.getStats().files_queued > 0) {
			document.getElementById(this.customSettings.cancelButtonId).style.display = '';
		}
		this.startUpload();
	} catch (ex)  {
        this.debug(ex);
	}
}

function uploadStart(file) {
	try {
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setStatus(SWFUpload.msg('progress_uploading'));
		progress.toggleCancel(true, this);
	}
	catch (ex) {
	}
	return true;
}

function uploadProgress(file, bytesLoaded, bytesTotal) {
	try {
		var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setProgress(percent);
		progress.setStatus(SWFUpload.msg('progress_uploading'));
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadSuccess(file, serverData) {
	try {
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setComplete();
		progress.setStatus(SWFUpload.msg('progress_complete'));
		progress.toggleCancel(false);

	} catch (ex) {
		this.debug(ex);
	}
}

function uploadComplete(file) {
	try {
		if (this.getStats().files_queued === 0) {
			document.getElementById(this.customSettings.cancelButtonId).style.display = 'none';
		} else {	
			this.startUpload();
		}
	} catch (ex) {
		this.debug(ex);
	}

}

function uploadError(file, errorCode, message) {
	try {
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setError();
		progress.toggleCancel(false);

		switch (errorCode) {
		case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
			progress.setStatus(SWFUpload.msg('upload_error1') + message);
			this.debug(SWFUpload.msg('error_code') + SWFUpload.msg('upload_error2') + SWFUpload.msg('file_name') + file.name + ", " + SWFUpload.msg('mesage') + message);
			break;
		case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
			progress.setStatus(SWFUpload.msg('configuration_error1'));
			this.debug(SWFUpload.msg('error_code') + SWFUpload.msg('configuration_error2') + SWFUpload.msg('file_name') + file.name + ", " + SWFUpload.msg('mesage') + message);
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
			progress.setStatus(SWFUpload.msg('upload_failed1'));
			this.debug(SWFUpload.msg('error_code') + SWFUpload.msg('upload_failed2') + SWFUpload.msg('file_name') + file.name + ", " + SWFUpload.msg('file_size') + file.size + ", " + SWFUpload.msg('mesage') + message);
			break;
		case SWFUpload.UPLOAD_ERROR.IO_ERROR:
			progress.setStatus(SWFUpload.msg('server_io1'));
			this.debug(SWFUpload.msg('error_code') + SWFUpload.msg('server_io2') + SWFUpload.msg('file_name') + file.name + ", " + SWFUpload.msg('mesage') + message);
			break;
		case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
			progress.setStatus(SWFUpload.msg('security_error1'));
			this.debug(SWFUpload.msg('error_code') + SWFUpload.msg('security_error2') + SWFUpload.msg('file_name') + file.name + ", " + SWFUpload.msg('mesage') + message);
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
			progress.setStatus(SWFUpload.msg('upload_limit_exceeded1'));
			this.debug(SWFUpload.msg('error_code') + SWFUpload.msg('upload_limit_exceeded2') + SWFUpload.msg('file_name') + file.name + ", " + SWFUpload.msg('file_size') + file.size + ", " + SWFUpload.msg('mesage') + message);
			break;
		case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND:
			progress.setStatus(SWFUpload.msg('file_not_found1'));
			this.debug(SWFUpload.msg('error_code') + SWFUpload.msg('file_not_found2') + SWFUpload.msg('file_name') + file.name + ", " + SWFUpload.msg('file_size') + file.size + ", " + SWFUpload.msg('mesage') + message);
			break;
		case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
			progress.setStatus(SWFUpload.msg('file_validation1'));
			this.debug(SWFUpload.msg('error_code') + SWFUpload.msg('file_validation2') + SWFUpload.msg('file_name') + file.name + ", " + SWFUpload.msg('file_size') + file.size + ", " + SWFUpload.msg('mesage') + message);
			break;
		case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
			if (this.getStats().files_queued === 0) {
				document.getElementById(this.customSettings.cancelButtonId).style.display = 'none';
			}
			progress.setStatus(SWFUpload.msg('progress_cancelled'));
			progress.setCancelled();
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
			progress.setStatus(SWFUpload.msg('progress_stopped'));
			break;
		default:
			progress.setStatus(SWFUpload.msg('unhandled_error') + ": " + error_code);
			this.debug(SWFUpload.msg('error_code') + errorCode + ", " + SWFUpload.msg('file_name') + file.name + ", " + SWFUpload.msg('file_size') + file.size + ", " + SWFUpload.msg('mesage') + message);
			break;
		}
	} catch (ex) {
        this.debug(ex);
    }
}