/* **********************
   Event Handlers
   These are my custom event handlers to make my
   web application behave the way I went when SWFUpload
   completes different tasks.  These aren't part of the SWFUpload
   package.  They are part of my application.  Without these none
   of the actions SWFUpload makes will show up in my application.
   ********************** */
function fileQueued(file) {
	try {
		var progress = new FileProgress(file, this.customSettings.progressTarget, this.customSettings.nombre);
		progress.setStatus(this.customSettings.pendiente_txt);
		progress.toggleCancel(true, this);
		$('#'+this.customSettings.campo_nombre_txt).val(file.name);
		$('#'+this.customSettings.nombre).val(file.name);
		$('#'+this.customSettings.aviso).html('');

	} catch (ex) {
		this.debug(ex);
	}

}

function fileBrowse() {
	$('#'+this.customSettings.campo_nombre_txt).val("");

	this.cancelUpload();
	this.selectFile();
}

function fileQueueError(file, errorCode, message) {
	try {

		var progress = new FileProgress(file, this.customSettings.progressTarget, this.customSettings.nombre);
		progress.setError();
		progress.toggleCancel(false);

		switch (errorCode) {
		case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
			progress.setStatus(this.customSettings.archivo_tamanio_invalido.replace('{archivo}',file.name));
			$('#'+this.customSettings.aviso).html(this.customSettings.archivo_tamanio_invalido.replace('{archivo}',file.name));
			this.debug("Error Code: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
			progress.setStatus(this.customSettings.zero_bytes_txt);
			$('#'+this.customSettings.aviso).html(this.customSettings.zero_bytes_txt);
			this.debug("Error Code: Zero byte file, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
			progress.setStatus(this.customSettings.archivo_formato_invalido.replace('{archivo}',file.name));
			$('#'+this.customSettings.aviso).html(this.customSettings.archivo_formato_invalido.replace('{archivo}',file.name));
			this.debug("Error Code: Invalid File Type, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		default:
			if (file !== null) {
				progress.setStatus(this.customSettings.archivo_no_subido);
			}
			this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		}
	} catch (ex) {
        this.debug(ex);
    }
}

function fileDialogComplete(numFilesSelected, numFilesQueued) {
	try {
		if (numFilesSelected > 0) {
			document.getElementById(this.customSettings.cancelButtonId).disabled = false;
		}
		
	} catch (ex)  {
        this.debug(ex);
	}
}

function uploadStart(file) {
	try {
		var progress = new FileProgress(file, this.customSettings.progressTarget, this.customSettings.nombre);
		progress.setStatus(this.customSettings.subiendo_archivo_txt);
		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, this.customSettings.nombre);
		progress.setProgress(percent);
		progress.setStatus(this.customSettings.subiendo_archivo_txt);
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadSuccess(file, serverData) {
	try {
		var progress = new FileProgress(file, this.customSettings.progressTarget, this.customSettings.nombre);
		progress.setComplete();
		progress.setStatus(this.customSettings.completo_txt+". "+serverData);
		progress.toggleCancel(false);

	} catch (ex) {
		this.debug(ex);
	}
}

function uploadComplete(file) {
	if (this.getStats().files_queued === 0) {
		document.getElementById(this.customSettings.cancelButtonId).disabled = true;
		formulario.submit();
	}
}

// This event comes from the Queue Plugin
function queueComplete(numFilesUploaded) {
	var status = document.getElementById("divStatus");
	status.innerHTML = this.customSettings.archivos_subidos_txt.replace("{numero}", numFilesUploaded);
}
