/* Upload event handlers */

// When the user has selected a file, update the textbox with the file name
// Unfortunately Flash doesn't make the path available.
function fileDialogComplete(numFilesSelected, numFilesQueued) {
  try {
              
    var filename = this.getFile(filecount).name;                
    
    if (filecount > 0)
    {      
      this.cancelUpload(this.getFile(filecount - 1).id);
    }
    
    filecount = filecount + 1;
        
    jQuery('#publish_browse_filename').val(filename)
    
    if (test_for_document_type)
    {
    
    if (filename.charAt(filename.length - 1) == 'x')
    {
      jQuery('#office2007_warning').show();
      jQuery('#non_pdf_warning').hide();
    }
    else if (filename.indexOf('.pdf') < 0)
    {
      jQuery('#office2007_warning').hide();
      jQuery('#non_pdf_warning').show();
    }
    else
    {
      jQuery('#office2007_warning').hide();
      jQuery('#non_pdf_warning').hide();
    }
    }
            
	} catch (ex)  {
        this.debug(ex);
	}     
}

// Update the UI when the upload is started
function uploadStart(file) {
    uploading = true;
	try {
		var progress = new FileProgress();
		progress.setStart();
	}
	catch (ex) {}
	
	return true;
}

// Event triggered by the swf to say that some more of the file has been updated.
// Calculates the percentage and updates the UI
function uploadProgress(file, bytesLoaded, bytesTotal) {
	try {
		var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);

		var progress = new FileProgress();
		progress.setProgress(percent);
	} catch (ex) {
		this.debug(ex);
	}
}

// Triggered by the swf when the upload is completed (and the upload recipient returns a 200 OK)
// Updates the UI, and evals() the returned data. This allows our recipient page to, e.g., reload the
// page, or redirect to another page, etc.
function uploadSuccess(file, serverData) {
    uploading = false;
	try {
		var progress = new FileProgress();
		progress.setComplete();
    eval(serverData);
	} catch (ex) {
		this.debug(ex);
	}
}

// Triggered by the swf when the upload is interupted for some reason.
function uploadError(file, errorCode, message) {
    uploading = false;
	try {
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setError();
		progress.toggleCancel(false);

		switch (errorCode) {
		case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
			progress.setStatus("Upload Error: " + message);
			this.debug("Error Code: HTTP Error, File name: " + file.name + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
			progress.setStatus("Upload Failed.");
			this.debug("Error Code: Upload Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.IO_ERROR:
			progress.setStatus("Server (IO) Error");
			this.debug("Error Code: IO Error, File name: " + file.name + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
			progress.setStatus("Security Error");
			this.debug("Error Code: Security Error, File name: " + file.name + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
			progress.setStatus("Upload limit exceeded.");
			this.debug("Error Code: Upload Limit Exceeded, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
			progress.setStatus("Failed Validation.  Upload skipped.");
			this.debug("Error Code: File Validation Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + 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;
			}
			progress.setStatus("Cancelled");
			progress.setCancelled();
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
			progress.setStatus("Stopped");
			break;
		default:
			progress.setStatus("Unhandled Error: " + errorCode);
			this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		}
	} catch (ex) {
        this.debug(ex);
    }
}
