/* Demo Note:  This demo uses a FileProgress class that handles the UI for displaying the file name and percent complete.
The FileProgress class is not part of SWFUpload.
*/


/* **********************
   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 uStatus = new uploadStatus(file, this.customSettings.statusTarget);
	} catch (ex) {
		this.debug(ex);
	}
}

function fileQueueError(file, errorCode, message) {
	try {
		if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
			alert("Estas intentando subir demasiados archivos.\n" + (message === 0 ? "Has alcanzado el número máximo de archivos." : "Debes seleccionar " + (message > 1 ? "hasta " + message + " archivos." : "un archivo.")));
			return;
		}
		
		var uStatus = new uploadStatus(file, this.customSettings.statusTarget);
		switch (errorCode) {
		case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
			uStatus.addUploadError("Error Code: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
			uStatus.addUploadError("Error Code: Zero byte file, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
			uStatus.addUploadError("Error Code: Invalid File Type, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		default:
			if (file !== null) {
				uStatus.addUploadError("Error Code: Invalid File Type, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			}
			//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;
		}
		/* I want auto start the upload and I can do that here */
		this.startUpload();
	} catch (ex)  {
        this.debug(ex);
	}
}

function uploadStart(file) {
	
	try {
		/* I don't want to do any file validation or anything,  I'll just update the UI and
		return true to indicate that the upload should start.
		It's important to update the UI here because in Linux no uploadProgress events are called. The best
		we can do is say we are uploading.
		 */
		
		var uStatus = new uploadStatus(file, this.customSettings.statusTarget);
		uStatus.startUploadAdvice (file);
		
	}
	catch (ex) {
		this.debug(ex);
	}
	
	return true;
}

function uploadProgress(file, bytesLoaded, bytesTotal) {
	return;
}

function uploadSuccess(file, serverData) {
	try {
		var uStatus = new uploadStatus(file, this.customSettings.statusTarget);
		uStatus.addUploadSuccess (file);
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadError(file, errorCode, message) {
	try {
		var uStatus = new uploadStatus(file, this.customSettings.statusTarget);
		switch (errorCode) {
			case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
				uStatus.addUploadError("Error: HTTP Error, Archivo: " + file.name + ", Mensaje: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
				uStatus.addUploadError("Error: fallo al subir archivo, Archivo: " + file.name + ", Tama&ntilde;o: " + file.size + ", Mensaje: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.IO_ERROR:
				uStatus.addUploadError("Error: Error I/O, Archivo: " + file.name + ", Mensaje: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
				uStatus.addUploadError("Error: Error de seguridad, Archivo: " + file.name + ", Mensaje: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
				uStatus.addUploadError("Excedido el l&iacute;mite de subida.");
				break;
			case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
				uStatus.addUploadError("Error: Validaci&oacute;n de archivo fallida, Archivo: " + file.name + ", Tama&ntilde;o: " + file.size + ", Mensaje: " + message);
				break;
			case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
				// If there aren't any files left (they were all cancelled) disable the cancel button
				if (this.getStats().files_queued === 0) {
					//document.getElementById(this.customSettings.cancelButtonId).disabled = true;
				}
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
				break;
			default:
				uStatus.addUploadError("Error: " + errorCode + ", Archivo: " + file.name + ", Tama&ntilde;o: " + file.size + ", Mensaje: " + message);
				break;
		}		

	} catch (ex) {
        this.debug(ex);
    }
}

function uploadComplete(file) {
	if (this.getStats().files_queued === 0) {
		//document.getElementById(this.customSettings.cancelButtonId).disabled = true;
	}
	var uStatus = new uploadStatus(file, this.customSettings.statusTarget);
	uStatus.allUploadsComplete (file);
}

// This event comes from the Queue Plugin
function queueComplete(numFilesUploaded) {
	//var status = document.getElementById("divStatus");
	//status.innerHTML = numFilesUploaded + " file" + (numFilesUploaded === 1 ? "" : "s") + " uploaded.";
}

