/* Licence:
*   Use this however/wherever you like, just don't blame me if it breaks anything.
*
* Credit:
*   If you're nice, you'll leave this bit:
*
*   Class by Pierre-Alexandre Losson -- http://www.telio.be/blog
*   email : plosson@users.sourceforge.net
*/

if(typeof(lang) == "undefined") {
	lang = "tha";
}

function $(id){	
	return typeof id=="object"?id:document.getElementById(id);
}

function setVisible(obj, visible){
	obj.className=visible?"displayVisible":"displayInvisible";
}

//----------------------------
var submitted = false;
var numOfFileUploads=0;

var messages= {
	"eng" : {
		"upload-please-select-file" : "Please select a file to upload and submit again.",
		"upload-file-not-allow" : "You selected forbidden file types.\nPlease select a new file to upload and submit again.",
		"upload-page-title" : "File Insertion Page",
		"upload-title" : "Upload your file(s)",
		"upload-sub" : "",
		"upload-message" : "Browse to the files that you want to upload:",
		"upload-max-size" : "Maximum upload size",
		"extensions-allow" : "Extensions are support",
		"extensions-not-allow" : "Extensions are not support",
		"upload-during-title" : "Uploading your file(s)",
		"upload-during-sub" : "Please don't close the window or navigate away from this page.",
		"upload-error-title" : "Uploading has been error",
		"upload-error-sub" : "You were not able to upload files right now. Please try again later.",
		"upload-cancel-title" : "Uploading has been cancel",
		"upload-cancel-sub" : "",
		"upload-cancel-message" : "",
		"upload-success-title" : "Uploading successully.",
		"upload-success-sub" : "You successfully uploaded this file(s).",
		
		"delete-page-title" : "File Deleting Page.",

		"delete-success-title" : "Deleting result.",
		"delete-success-sub" : "",
		"delete-error-title" : "Deleting has been error.",
		"delete-error-sub" : "",
		
		"delete-message-success" : "file '{0}'  has been delete.",
		"delete-message-file-not-found" : "file '{0}' could not be found.",
		"delete-message-file-not-allow" : "file '{0}' has forbidden.",
		"delete-message-invalid-parameter" : "file '{0}' could not delete",
		"delete-message-error" : "file '{0}' could not delete"
	},

	"tha" : {
		"upload-please-select-file" : "กรุณาเลือกไฟล์ แล้วกดปุ่มอัปโหลด",
		"upload-file-not-allow" : "ไม่อนุญาตให้อัปโหลดไฟล์ชนิดนี้ กรุณาเลือกไฟล์อื่น แล้วกดปุ่มอัปโหลด",
		"upload-page-title" : "หน้าจอสำหรับเพิ่มไฟล์",
		"upload-title" : "อัปโหลดไฟล์",
		"upload-sub" : "",
		"upload-message" : "เรียกดูไฟล์ที่คุณต้องการอัปโหลด:",
		"upload-max-size" : "ขนาดไฟล์สูงสุด",
		"extensions-allow" : "ไฟล์ที่อนุญาต",
		"extensions-not-allow" : "ไฟล์ที่ไม่อนุญาต",
		"upload-during-title" : "กำลังอัปโหลดไฟล์",
		"upload-during-sub" : "กรุณาอย่าปิด หรือออกจากหน้านี้ จนกว่าการอัปโหลดจะเสร็จสิ้น",
		"upload-error-title" : "การอัปโหลดเกิดข้อผิดพลาด",
		"upload-error-sub" : "ไฟล์ของคุณอัปโหลดไม่สมบูรณ์. กรุณาอัปโหลดใหม่ภายหลัง",
		"upload-cancel-title" : "การอัปโหลดถูกยกเลิก",
		"upload-cancel-sub" : "",
		"upload-cancel-message" : "",
		"upload-success-title" : "ไฟล์ถูกอัพโหลดเสร็จสมบูรณ์",
		"upload-success-sub" : "คุณเพิ่มไฟล์ต่อไปนี้ได้เสร็จสมบูรณ์",
		
		"delete-page-title" : "หน้าจอสำหรับลบไฟล์",
		
		"delete-success-title" : "ผลการลบไฟล์",
		"delete-success-sub" : "",
		"delete-error-title" : "การลบไฟล์เกิดข้อผิดพลาด",
		"delete-error-sub" : "",
		
		"delete-message-success" : "ไฟล์ '{0}' ถูกลบเสร็จสมบูรณ์",
		"delete-message-file-not-found" : "ไฟล์ '{0}' ไม่มีอยู่ในระบบ",
		"delete-message-file-not-allow" : "ไฟล์ '{0}' ไม่ได้รับอนุญาตให้ลบ ",
		"delete-message-invalid-parameter" : "ไฟล์ '{0}' ไม่สามารถลบได้",
		"delete-message-error" : "ไฟล์ '{0}' ไม่สามารถลบได้"
	}
};

var delete_icon= {
	"success" : "<img src=\"IMAGES/UPLOAD/ICON/ICON-DELETE-SUCCESS.png\" width=\"16\" height=\"16\" align=\"absmiddle\" />",
	"file-not-found" : "<img src=\"IMAGES/UPLOAD/ICON/ICON-DELETE-ERROR.png\" width=\"16\" height=\"16\" align=\"absmiddle\" />",
	"file-not-allow" : "<img src=\"IMAGES/UPLOAD/ICON/ICON-DELETE-ERROR.png\" width=\"16\" height=\"16\" align=\"absmiddle\" />",
	"invalid-parameter" : "<img src=\"IMAGES/UPLOAD/ICON/ICON-DELETE-ERROR.png\" width=\"16\" height=\"16\" align=\"absmiddle\" />",
	"error" : "<img src=\"IMAGES/UPLOAD/ICON/ICON-DELETE-ERROR.png\" width=\"16\" height=\"16\" align=\"absmiddle\" />"
};

var req=0;

function updateProgress(uploadInfo) {
	var progressPercent = uploadInfo.percent;
	if (uploadInfo.inProgress==true) {
		$("progressBarText").innerHTML = formatString( "Upload in progress: {0}% [{1}/{2} bytes]", progressPercent, uploadInfo.bytesRead, uploadInfo.totalSize );
		$('progressStatusText').innerHTML = formatString( "Status : {0}", uploadInfo.status) ;
			
		$("progressBarBoxContent").style.width = parseInt((progressPercent *3.5), 10) + "px";

		setTimeout("refreshProgress()", 1000);

    } else if(uploadInfo.cancel==true) {
		//window.location.replace( window.location.href );
		
		cancelUpload();
		setVisible($("duringUpload"), false);
		setVisible($("postUpload"), true);
		$("titleText").innerHTML = messages[lang]["upload-cancel-title"];
    	$("pageSubText").innerHTML = messages[lang]["upload-cancel-sub"];
		$("titleIcon").innerHTML = "<img src=\"IMAGES/UPLOAD/ICON/ICON-WARNING.gif\" width=\"48\" height=\"48\" align=\"absmiddle\" />";
		setVisible($("buttonUpload"), false);
		setVisible($("buttonCancel"), true);
		
		return false;
		
	}
	return true;
}

function refreshProgress() {
   UploadMonitor.getUploadInfo(updateProgress);
}

function startProgress() {
	uploadInputChanged();
	if(numOfFileUploads>0) {
		submitted = true;
		$("titleText").innerHTML = messages[lang]["upload-during-title"];
		$("pageSubText").innerHTML = messages[lang]["upload-during-sub"];

		activeButton($("uploadButton"), false);
		activeButton($("cancelButton"), true);
		setVisible($("preUpload"), false);
		setVisible($("duringUpload"), true);

		// wait a little while to make sure the upload has started ..
		setTimeout("refreshProgress()", 1500);
		return true;
	} 
	alert(messages[lang]["upload-please-select-file"]);
	return false;
}


//---------------
function cancelProgress(){
	$("progressBarText").innerHTML = "Upload in progress: ";
    $("progressStatusText").innerHTML = "Status : Cancelled ";

	 UploadMonitor.cancelFileUpload();
	  setTimeout("refreshProgress()", 1500);
}

//Functions for CANCEL UPLOAD button
function uploadCanCancel() {
    return (window.stop || window.document.execCommand);
}

function cancelUpload() {
	if (window && window.stop) {
		window.stop();
	} else if (window && window.document && window.document.execCommand) {
		window.document.execCommand('Stop');
	}
}

function uploadInputChanged(obj){
	numOfFileUploads= 0;
	var _up = $("fileUploads");
	var c = _up.getElementsByTagName("input");
	for(var i=0; i<c.length; i++) {
		var a=c[i];
		if(a.getAttribute("type")==="file" && a.value!=null && a.value.length>0) {
			numOfFileUploads++;
		}
	}
	activeButton($("uploadButton"), (numOfFileUploads>0 && !submitted));
}

function initialFileFilter() {
	with(document.cscform) {
		fileFilter = __filter.value.split(",");
		restrict = (__blacklistenabled.value=="true");
	}
}

function fileIsAllow(filename) {
	var isMatch = false;
	for( var i=0; i<fileFilter.length; i++) {
		var isMatch = FilenameUtils.wildcardMatch( filename, fileFilter[i], false );
		if( isMatch ){
			break;
		}
	}
	if( restrict ) {
		return !isMatch;
	}
	return isMatch;
}

function chkFileUpload(e, obj) {
	var filename = obj.value;
	var fileExt = FilenameUtils.getExtension(filename);
	if( filename != "" ) {
		if( !fileIsAllow(filename) ) {
			alert( formatString(messages[lang]["upload-file-not-allow"], fileExt ));
			clearFileInput(obj.id);
			return false;
		} else {
			uploadInputChanged(obj);
		}
	} 
}

function clearFileInput(inputID) {
	var inputObj = $(inputID);
	var parentObj = inputObj.parentNode;
	var EmptyFileInput;
	if(window.ActiveXObject) {
		var nextObj = inputObj.nextSibling;
		EmptyFileInput = document.createElement(inputObj.outerHTML);
		parentObj.replaceChild(EmptyFileInput, inputObj);
	} else {
		//for FireFox that doesn't supports outerHTML property
		EmptyFileInput = document.createElement("INPUT");
		EmptyFileInput.setAttribute("type", "file");
		EmptyFileInput.setAttribute("id", inputObj.getAttribute("id"));
		EmptyFileInput.setAttribute("name", inputObj.getAttribute("name"));
		EmptyFileInput.setAttribute("size", inputObj.getAttribute("size"));
		EmptyFileInput.setAttribute("class", inputObj.getAttribute("class"));
		EmptyFileInput.setAttribute("onChange", inputObj.getAttribute("onChange"));
		EmptyFileInput.setAttribute("onKeyup", inputObj.getAttribute("onKeyup"));
		parentObj.replaceChild(EmptyFileInput, inputObj);
	}
	uploadInputChanged();
}
function removeFileInput(objToRemoveID) {
	var ObjToRemove = $(objToRemoveID);
	var parentObj = ObjToRemove.parentNode;
	parentObj.removeChild(ObjToRemove);
	uploadInputChanged();
}
function addFileInput(parentObjID) {
	var parentObj = $(parentObjID);
	uploadLastFileIndex++;
	var newFileIndex = uploadLastFileIndex;
	var newFileInputID = 'filediv' + newFileIndex;
	var newFileInput = document.createElement('div');
	newFileInput.setAttribute("id", newFileInputID);

	newFileInput.innerHTML = "<input type=\"file\" id=\"file" + newFileIndex + "\" name=\"file" + newFileIndex + "\" size=\"50\" class=\"fileUploadBox\" onChange=\"chkFileUpload(event, this);\" onKeyup=\"chkFileUpload(event, this);\"  />\n";
	newFileInput.innerHTML += "<a href=\"javascript:;\" onClick=\"removeFileInput('" + newFileInputID + "');\"><img src=\"IMAGES/UPLOAD/ICON/ICON-DELETE.png\" alt=\"Remove\" width=\"16\" height=\"16\" border=\"0\" align=\"absmiddle\" /></a>\n";
	newFileInput.innerHTML += "<a href=\"javascript:;\" onClick=\"clearFileInput('file" + newFileIndex + "');\"><img src=\"IMAGES/UPLOAD/ICON/ICON-CLEAR.png\" alt=\"Clear\" width=\"16\" height=\"16\" border=\"0\" align=\"absmiddle\" /></a>\n";
	parentObj.appendChild(newFileInput);
}