/* 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.
   ********************** */
var formChecker = null;
function swfUploadLoaded() {
	var btnSubmit = document.getElementById("btnSubmit");
	
	btnSubmit.onclick = doSubmit;
	btnSubmit.disabled = true;
	formChecker = window.setInterval(validateForm, 1000);
	
	validateForm();
}

function validateForm() {
	var txtFileName = document.getElementById("txtFileName");

	var isValid = true;
	if (txtFileName.value === "") {
		isValid = false;
	}

	document.getElementById("btnSubmit").disabled = !isValid;

}

function fileDialogStart() {
	var txtFileName = document.getElementById("txtFileName");
	txtFileName.value = "";

	this.cancelUpload();
}

function fileQueued(file) {
	try {
//		this.customSettings.tdFilesQueued.innerHTML = this.getStats().files_queued;
		var txtFileName = document.getElementById("txtFileName");
		txtFileName.value = file.name;
	} catch (ex) {
		this.debug(ex);
	}

}

function fileQueueError(file, errorCode, message)  {
	try {
		// Handle this error separately because we don't want to create a FileProgress element for it.
		switch (errorCode) {
		case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
			alert("You have attempted to queue too many files.\n" + (message === 0 ? "You have reached the upload limit." : "You may select " + (message > 1 ? "up to " + message + " files." : "one file.")));
			return;
		case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
			alert("The file you selected is too big.");
			this.debug("Error Code: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			return;
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
			alert("The file you selected is empty.  Please select another file.");
			this.debug("Error Code: Zero byte file, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			return;
		case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
			alert("The file you choose is not an allowed file type.");
			this.debug("Error Code: Invalid File Type, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			return;
		default:
			alert("An error occurred in the upload. Try again later.");
			this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			return;
		}
	} catch (e) {
	}
}

function fileDialogComplete(numFilesSelected, numFilesQueued) {
	validateForm();
}

function doSubmit(e) {
	if (formChecker != null) {
		clearInterval(formChecker);
		formChecker = null;
	}
	
	e = e || window.event;
	if (e.stopPropagation) {
		e.stopPropagation();
	}
	e.cancelBubble = true;
	
	try {
		swfu.startUpload();
	} catch (ex) {

	}
	return false;
}

function uploadStart(file) {
	try {
		this.customSettings.Tstats.style.display = "";
		this.customSettings.ButtonUp.style.display = "none";
		this.customSettings.UpStatus.style.display = "";
		this.customSettings.ProgressTable.style.display = "";

		this.customSettings.progressCount = 0;
		updateDisplay.call(this, file);
		
		var ButtonBrowse = document.getElementById("bbrowse");
		ButtonBrowse.style.visibility = "hidden";
//		ButtonBrowse.style.color = "#CCCCCC";
	}
	catch (ex) {
		this.debug(ex);
	}
	
}

function uploadProgress(file, bytesLoaded, bytesTotal) {
	try {
		this.customSettings.progressCount++;
		updateDisplay.call(this, file);
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadSuccess(file, serverData) {
	try {
		updateDisplay.call(this, file);
//		this.customSettings.ProGress.innerHTML = "100%";
//		this.customSettings.ProGress.style.width = "100%";	
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadComplete(file) {
//	this.customSettings.tdFilesQueued.innerHTML = this.getStats().files_queued;
//	this.customSettings.tdFilesUploaded.innerHTML = this.getStats().successful_uploads;
//	this.customSettings.tdErrors.innerHTML = this.getStats().upload_errors;

	this.customSettings.Cstatus.innerHTML = "<span class='title'><font color='#FF0000'>Complete!</font><span>";
	uploadDone(); // this line for post action.
}

function uploadDone() {
	try {
		document.forms[0].submit();
	} catch (ex) {
		alert("Error submitting form");
	}
}

function updateDisplay(file) {
	this.customSettings.tdCurrentSpeed.innerHTML = SWFUpload.speed.formatBPS(file.currentSpeed);
//	this.customSettings.tdAverageSpeed.innerHTML = SWFUpload.speed.formatBPS(file.averageSpeed);
//	this.customSettings.tdMovingAverageSpeed.innerHTML = SWFUpload.speed.formatBPS(file.movingAverageSpeed);
	this.customSettings.tdTimeRemaining.innerHTML = SWFUpload.speed.formatTime(file.timeRemaining);
	this.customSettings.tdTimeElapsed.innerHTML = SWFUpload.speed.formatTime(file.timeElapsed);
	this.customSettings.tdPercentUploaded.innerHTML = SWFUpload.speed.formatPercent(file.percentUploaded);
	this.customSettings.tdSizeUploaded.innerHTML = SWFUpload.speed.formatBytes(file.sizeUploaded);
	this.customSettings.TotalSize.innerHTML = SWFUpload.speed.formatBytes(file.size);
//	this.customSettings.tdProgressEventCount.innerHTML = this.customSettings.progressCount;

	var PercenT = SWFUpload.speed.formatPercent(file.percentUploaded);
	var WPercenT = PercenT.substring(-4,2);
//	var WPercenT = PercenT.substring(-1,3);
	this.customSettings.ProGress.innerHTML = WPercenT+"%";
	this.customSettings.ProGress.style.width = WPercenT+"%";
	if(PercenT == "100.00 %") {
		this.customSettings.ProGress.innerHTML = "100%";
		this.customSettings.ProGress.style.width = "100%";	
	}
}

