/* This is an example of how to cancel all the files queued up.  It's made somewhat generic.  Just pass your SWFUpload
object in to this method and it loops through cancelling the uploads. */
function cancelQueue(instance) {
	document.getElementById(instance.customSettings.cancelButtonId).disabled = true;
	instance.stopUpload();
	var stats;
	
	do {
		stats = instance.getStats();
		instance.cancelUpload();
	} while (stats.files_queued !== 0);
	
	//下一步按鈕
	var btn_next = document.getElementById('btn_gonext');	//下一步按鈕
	btn_next.src = "/tw/minicard/images/next_on.gif";
	btn_next.className = "btn_Next_on";
	btn_next.disabled = false;	//下一步按鈕
	
}

/* **********************
   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 fileDialogStart() {
	/* I don't need to do anything here */
}
function fileQueued(file) {
	try {
		// You might include code here that prevents the form from being submitted while the upload is in
		// progress.  Then you'll want to put code in the Queue Complete handler to "unblock" the form
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setStatus("等待上傳...");
		progress.toggleCancel(true, this);
		
		//下一步按鈕
		var btn_next = document.getElementById('btn_gonext');	//下一步按鈕
		btn_next.src = "/tw/minicard/images/next_off.gif";
		btn_next.className = "btn_Next_off";
		btn_next.disabled = true;	//下一步按鈕

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

}

function fileQueueError(file, errorCode, message) {
	try {
		if (errorCode === SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
			alert("您已經超過上傳允許的最多檔案數量.\n" + (message === 0 ? "已經超過上傳限制." : "您可能選取 " + (message > 1 ? "大於 " + message + " 檔案." : "一個檔案.")));
			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("檔案太大.");
			this.debug("錯誤碼: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
			progress.setStatus("Cannot upload Zero Byte files.");
			this.debug("錯誤碼: Zero byte file, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
			progress.setStatus("Invalid File Type.");
			this.debug("錯誤碼: Invalid File Type, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
			alert("You have selected too many files.  " +  (message > 1 ? "You may only add " +  message + " more files" : "You cannot add any more files."));
			break;
		default:
			if (file !== null) {
				progress.setStatus("Unhandled Error");
			}
			this.debug("錯誤碼: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		}
	} catch (ex) {
        this.debug(ex);
    }
}

function fileDialogComplete(numFilesSelected, numFilesQueued) {
	try {
		if (this.getStats().files_queued > 0) {
			document.getElementById(this.customSettings.cancelButtonId).disabled = false;
			var vbtn = document.getElementById(this.customSettings.cancelButtonId);
			vbtn.className = "btn_CancelUpload_on";
		}
		
		/* I want auto start 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 */
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setStatus("上傳中...");
		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("上傳中...");
	} catch (ex) {
		this.debug(ex);
	}
}

function uploadSuccess(file, serverData) {
	var o_pkgcnt = document.getElementById("pkgcnt");	//本商品一個單位製作的張數
	var o_upload_nums = document.getElementById("upload_nums");	//先前已上傳的張數
	var infoobj = document.getElementById("upInfo");	//上傳資訊
	var uphint = document.getElementById("UploadHint");	//上傳提示
	var vpkgcnt = parseInt(o_pkgcnt.value);
	var vupload_nums = parseInt(o_upload_nums.value);
	//uploadCounter++;
	//o_upload_nums.value = vupload_nums + 1;	//更新已上傳數量
	
	try {
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setComplete();
		//取得傳回值
		vret = serverData;
		
		if (vret.indexOf(":", 0) >= 0) {
			vdata = vret.split(":");
			if (parseInt(vdata[0]) == 0) {	//上傳檔案成功, 成功才加
				//vupload_nums++;
				vupload_nums += parseInt(vdata[1]);	//傳回上傳處理成功的圖片數，因zip檔裡面有多張，如果是單一圖片都會是1
				//alert(vupload_nums);
				o_upload_nums.value = vupload_nums;	//更新已上傳數量
				progress.setStatus("上傳成功.");
				//progbar = "<hr style=\"color: #0000ff; background-color: #0000ff; text-align:left; margin: 0 auto 0 0; height: 5px; width: 100px\"/>";
			} else {	//失敗
				//vupload_nums--;
				o_upload_nums.value = vupload_nums;
				progress.setStatus(vdata[1]);
			}
		} else {
			progress.setStatus(vret);
			//vprocresult = vret;
			//progbar = "<hr style=\"color: #f00; background-color: #f00; text-align:left; margin: 0 auto 0 0; height: 5px; width: 100px\"/>";
		}
		
		//progress.setStatus("完成.");
		progress.toggleCancel(false);

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

function uploadComplete(file) {
	try {
		/*  I want the next upload to continue automatically so I'll call startUpload here */
		if (this.getStats().files_queued === 0) {
			document.getElementById(this.customSettings.cancelButtonId).disabled = true;
			//設定取消上傳按鈕無效
			var vbtn = document.getElementById(this.customSettings.cancelButtonId);
			vbtn.className = "btn_CancelUpload_off";
			//下一步按鈕
			var btn_next = document.getElementById('btn_gonext');	//下一步按鈕
			btn_next.src = "/tw/minicard/images/next_on.gif";
			btn_next.className = "btn_Next_on";
			btn_next.disabled = false;	//下一步按鈕
			
		} else {	
			this.startUpload();
		}
	} catch (ex) {
		this.debug(ex);
	}
	//加上更新訊息
	var o_pkgcnt = document.getElementById("pkgcnt");	//本商品一個單位製作的張數
	var o_upload_nums = document.getElementById("upload_nums");	//先前已上傳的張數
	var infoobj = document.getElementById("upInfo");	//上傳資訊
	var uphint = document.getElementById("UploadHint");	//上傳提示
	var vpkgcnt = parseInt(o_pkgcnt.value);
	var vupload_nums = parseInt(o_upload_nums.value);
	
	var pavg = parseInt(vpkgcnt / (vupload_nums));
		
	var vmsg = "目前合計已上傳 <span class='upInfo-number'> " + (vupload_nums ) + " </span>張影像，";
	vmsg += "每張影像約輸出 <span class='upInfo-number'>" + pavg + " </span>張卡片，餘數將從上傳的相片依序補足。";
	//vshowmsg = vmsg + infoobj.innerHTML;
	infoobj.innerHTML = vmsg;
	
	hintMsg = "您選擇的照片將會顯示在這個區域，最多可選擇 <strong>" + vpkgcnt + "</strong> 張。<br />\n";
	hintMsg += "您還可以上傳最多 " + (vpkgcnt - vupload_nums) + " 張不同的照片。";
	uphint.innerHTML = hintMsg;
	
}

function uploadError(file, errorCode, message) {
	try {
		//加上扣 數量
		var o_upload_nums = document.getElementById("upload_nums");	//先前已上傳的張數
		var vupload_nums = parseInt(o_upload_nums.value);
		vupload_nums--;
		o_upload_nums.value = vupload_nums;	//更新已上傳數量
	
		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("錯誤碼: HTTP Error, File name: " + file.name + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
			progress.setStatus("Configuration Error");
			this.debug("錯誤碼: No backend file, File name: " + file.name + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
			progress.setStatus("Upload Failed.");
			this.debug("錯誤碼: 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("錯誤碼: IO Error, File name: " + file.name + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
			progress.setStatus("Security Error");
			this.debug("錯誤碼: Security Error, File name: " + file.name + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
			progress.setStatus("Upload limit exceeded.");
			this.debug("錯誤碼: Upload Limit Exceeded, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND:
			progress.setStatus("File not found.");
			this.debug("錯誤碼: The file was not found, 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("錯誤碼: File Validation Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
			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: " + error_code);
			this.debug("錯誤碼: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		}
	} catch (ex) {
        this.debug(ex);
    }
}
