uploadObject = function(iNum, maxFiles) {
    this.isUploadFile = "false";
    this.isUploadFileAll = "false";
    this.countRows = null;
    this.idRowSelected = null;
    this.fileId = iNum;
    this.sessionId = null;
		this.inxupload = 0;
		this.maxupload = maxFiles;
    this.pathUploadHandler = null;
    this.pathGetInfoHandler = null;
    this.pathGetIdHandler = null;
    this.progressRun = null;
    this.MAX_FILE_SIZE = null;
    this.UPLOAD_IDENTIFIER = null;
}

uploadObject.prototype.setServerHandlers = function(uploadHandler, getInfoHandler, getIdHandler) {
    this.pathUploadHandler = uploadHandler;
    this.pathGetInfoHandler = getInfoHandler;
    this.pathGetIdHandler = getIdHandler;

}

uploadObject.prototype.create = function(htmlObject) {

    this.parentObject = document.getElementById(htmlObject);

    this.parentObject.style.position = "relative";
    this.parentObject.innerHTML = "<iframe src='about:blank' id='UploadFrame' name='UploadFrame'  style='display:none'></iframe>";

    this.containerDiv = document.createElement("div");
    this.containerDiv.style.cssText = "position:absolute;overflow-y:auto;height:190px;background-color:#FFFFFF;border:1px solid #878E95;top:10px;left:10px;z-index:10;width:410px";
    this.parentObject.appendChild(this.containerDiv);

    this.container = document.createElement("div");
    this.container.style.position = "relative";

    var str = "<table style='background-color:#EDEEEF;border: 1px solid #7A7C80;' border='0'>" +
              "<tr><td style='width:420px' colspan=3 align='center' id = 'cellContainer' >" +
              "<div style='height:200px;'></div>" +
              "</td></tr>" +
              "<tr><td style=';width: 80px; height: 32px;' align='left'></td>" +
              "<td style='width: 200px; height: 32px;' align='left'>" +
              "<img _onclick='UploadControl.prototype.uploadAllItems()' _ID='ImageButton3'  src='images/upload/btn_upload.gif' style='cursor:pointer'/></td>" +
              "<td style='width: 140px; height: 32px;' align='right'>" +
              "<img _onclick='return UploadControl.prototype.removeAllItems()' _ID='ImageButton3'  src='images/upload/btn_clean.gif' style='cursor:pointer;margin-right:20px'/></td></tr></table>" +

              "<div _id='fileContainer' style='width:84px;overflow:hidden;height:32px;left:0px;direction:rtl;position:absolute;top:211px'>" +
              "<img style='z-index:2' src='images/upload/btn_add.gif'/>" +
              "<input type='file' id='file1'  name='file1' value='' class='hidden' style='cursor:pointer;z-index:3;left:7px;position:absolute;height:25px;top:0px'/></div>";

    this.container.innerHTML = str;

    var self = this;
    this.container.childNodes[0].rows[1].cells[1].childNodes[0].onclick = function() {
        self.uploadAllItems()
    };
    this.container.childNodes[0].rows[1].cells[2].childNodes[0].onclick = function() {
        self.removeAllItems()
    };
    this.fileContainer = this.container.childNodes[1];
    this.fileContainer.childNodes[1].onchange = function() {
        self.addFile()
    };

    this.uploadForm = document.createElement("form");

    this.uploadForm.method = "post";
    this.uploadForm.encoding = "multipart/form-data";
    this.uploadForm.target = "UploadFrame";
    this.container.appendChild(this.uploadForm);

    this.MAX_FILE_SIZE = document.createElement("input");
    this.MAX_FILE_SIZE.type = "hidden";
    this.MAX_FILE_SIZE.name = "MAX_FILE_SIZE";
    this.MAX_FILE_SIZE.value = '200000000';
    this.uploadForm.appendChild(this.MAX_FILE_SIZE);

    this.UPLOAD_IDENTIFIER = document.createElement("input");
    this.UPLOAD_IDENTIFIER.type = "hidden";
    this.UPLOAD_IDENTIFIER.name = "UPLOAD_IDENTIFIER";
    this.uploadForm.appendChild(this.UPLOAD_IDENTIFIER);

    this.parentObject.appendChild(this.container);
    this.tblListFiles = null;

    this.currentFile = this.fileContainer.childNodes[1];
    this.progressRun = this.createProgressRun();

}

uploadObject.prototype.createXMLHttpRequest = function() {
    var xmlHttp = null;
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }

		return xmlHttp
}

uploadObject.prototype.getFileName = function(path) {
    var arr = path.split("\\");
		var newname = this.fileId + "--" + arr[arr.length - 1];
		return newname;
}

uploadObject.prototype.selectItemInExplorer = function(currentId) {
    var currentRow = this.getCurrentRowListFiles(currentId);

    if (this.idRowSelected) {
        var row = this.getCurrentRowListFiles(this.idRowSelected);
        if (row) {
            if (row.id != currentRow.id) {
                currentRow.style.background = "#F7F7F7";
                this.idRowSelected = currentId;
                row.style.background = "#FFFFFF";
            } else {
                currentRow.style.background = "#FFFFFF";
                this.idRowSelected = "";
            }
        } else {
            currentRow.style.background = "#F7F7F7";
            this.idRowSelected = currentId;
        }

    } else {
        currentRow.style.background = "#F7F7F7";
        this.idRowSelected = currentId;
    }

}

uploadObject.prototype.selectItemInMozilla = function(currentId) {

    var currentRow = this.getCurrentRowListFiles(currentId);

    if (this.idRowSelected) {
        var row = this.getCurrentRowListFiles(this.idRowSelected);
        if (row) {
            if (row.id != currentRow.id) {
                currentRow.style.background = "#F7F7F7";
                this.idRowSelected = currentId;
                row.style.background = "#FFFFFF";
            } else {
                currentRow.style.background = "#FFFFFF";
                this.idRowSelected = "";
            }
        } else {
            currentRow.style.background = "#F7F7F7";
            this.idRowSelected = currentId;
        }

    } else {
        currentRow.style.background = "#F7F7F7";
        this.idRowSelected = currentId;
    }

}

uploadObject.prototype.addFile = function() {

		var currentId = this.createId();

		var file = this.currentFile;
		file.disabled = true;
		file.style.display = "none";
		this.uploadForm.appendChild(file);

		var newInputFile = document.createElement("input");
		newInputFile.type = "file";
		newInputFile.className = "hidden";
		newInputFile.style.cssText = "cursor:pointer;z-index:3;left:7px;position:absolute;height:30px";
		newInputFile.id = "file" + (currentId + 1);
		newInputFile.name = "file" + (currentId + 1);
		this.currentFile = newInputFile;
		var self = this;
		newInputFile.onchange = function() {
				return self.addFile()
		};
		this.fileContainer.appendChild(newInputFile);

		var fileName = this.getFileName(file.value);
		var imgFile = this.getImgFile(fileName);

		//create table ListFiles
		var containerData = this.containerDiv;
		if (this.tblListFiles == null) {
				this.tblListFiles = this.createTblListFiles();
				containerData.appendChild(this.tblListFiles);
		}
		var rowListFiles = this.tblListFiles.insertRow(this.tblListFiles.rows.length);
		rowListFiles.setAttribute("fileItemId", currentId);
		rowListFiles.setAttribute("id", "rowListFiles" + currentId);
		rowListFiles.setAttribute("isUpload", "false");

		if (navigator.appName.indexOf("Explorer") != -1) {
				rowListFiles.onclick = function() {
						self.selectItemInExplorer(currentId)
				}
		} else {
				rowListFiles.onclick = function(event) {
						self.selectItemInMozilla(currentId)
				}
		}

		var cellListFiles = document.createElement("td");
		cellListFiles.align = "center";
		rowListFiles.appendChild(cellListFiles);


		//create table "content"
		var tblContent = document.createElement("table");
		cellListFiles.appendChild(tblContent);
		tblContent.style.cssText = ";border-bottom: 1px solid #E2E2E2;";
		tblContent.cellPadding = "0px";
		tblContent.cellSpacing = "0px";
		tblContent.border = "0px";
		tblContent.id = "tblContent" + currentId;

		var rowList = tblContent.insertRow(tblContent.rows.length);

		var cellList = document.createElement("td");
		cellList.rowSpan = 2;
		cellList.align = "center";
		if (navigator.appName.indexOf("Explorer") != -1) {
				var span = document.createElement("span");
				span.style.cssText = "width: 40px; height: 40px; display: inline-block; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader (src='" + imgFile + " ')";
				cellList.appendChild(span);
		} else {
				cellList.innerHTML = "<img  src='" + imgFile + "'  />";
		}
		cellList.style.cssText = ";width:60px;";
		rowList.appendChild(cellList);

		//add name of file
		cellList = document.createElement("td");
		cellList.align = "left";
		cellList.vAlign = "bottom";
		cellList.style.cssText = ";width:300px;height:30px;";
		cellList.innerHTML = "<div style='overflow: hidden;height: 12px;width:280px;'><div class='fileName' style='height: 12px;width:280px;'>" + fileName + "</div></div> ";
		cellList.className = "filenName";
		rowList.appendChild(cellList);

		// add link Remove
		cellList = document.createElement("td");
		cellList.style.cssText = ";width:140px;height:30px";
		cellList.innerHTML = "<a href='#remove' class='link'>Remove</a>";
		cellList.firstChild.onclick = function() {
				self.removeItem(currentId)
		};
		cellList.vAlign = "bottom";
		cellList.align = "center";
		rowList.appendChild(cellList);
		rowList = tblContent.insertRow(tblContent.rows.length);

		//  progress bar
		cellList = document.createElement("td");
		cellList.align = "left";
		cellList.style.cssText = ";width:300px;height:30px;";
		//cellList.appendChild(tblProgressBar);
		rowList.appendChild(cellList);

		// add link Upload
		cellList = document.createElement("td");
		cellList.style.cssText = ";width:140px;height:30px;";
		cellList.innerHTML = "<a href='#upload' class='link'>Upload</a>";
		cellList.firstChild.onclick = function() {
				self.uploadFile(currentId)
		};
		cellList.vAlign = "middle";
		cellList.align = "center";
		rowList.appendChild(cellList);
		this.inxupload++;

		if (this.inxupload > this.maxupload) {
			self.removeItem(currentId);
			alert("Maximum Files Reached!");
		}
}

uploadObject.prototype.getImgFile = function(fileName) {
    var srcImgPic = "images/upload/ico_image.png";
    var srcImgVideo = "images/upload/ico_video.png";
    var srcImgSound = "images/upload/ico_sound.png";
    var srcImgArchives = "images/upload/ico_zip.png";
    var srcImgFile = "images/upload/ico_file.png";

    var valueImgPic = "jpg,jpeg,gif,png,bmp,tiff";
    var valueImgVideo = "avi,mpg,mpeg,rm,move";
    var valueImgSound = "wav,mp3,ogg";
    var valueImgArchives = "zip,rar,tar,tgz,arj";

    var ext = "_";
    var ext0 = fileName.split(".");
    if (ext0.length > 1) ext = ext0[ext0.length - 1].toLowerCase();

    if (valueImgPic.indexOf(ext) != -1) {
        return srcImgPic;
    }

    if (valueImgVideo.indexOf(ext) != -1) {
        return srcImgVideo;
    }

    if (valueImgSound.indexOf(ext) != -1) {
        return srcImgSound;
    }

    if (valueImgArchives.indexOf(ext) != -1) {
        return srcImgArchives;
    }

    return srcImgFile;
}

uploadObject.prototype.getFileType = function(fileName) {
    var srcTypePic = "Image";
    var srcTypeVideo = "Video";
    var srcTypeSound = "Sound";
    var srcTypeArchives = "Archived";
    var srcTypeFile = "Other";

    var valueImgPic = "jpg,jpeg,gif,png,bmp,tiff";
    var valueImgVideo = "avi,mpg,mpeg,rm,move";
    var valueImgSound = "wav,mp3,ogg";
    var valueImgArchives = "zip,rar,tar,tgz,arj";

    var ext = "_";
    var ext0 = fileName.split(".");
    if (ext0.length > 1) ext = ext0[ext0.length - 1].toLowerCase();

    if (valueImgPic.indexOf(ext) != -1) {
        return srcTypePic;
    }

    if (valueImgVideo.indexOf(ext) != -1) {
        return srcTypeVideo;
    }

    if (valueImgSound.indexOf(ext) != -1) {
        return srcTypeSound;
    }

    if (valueImgArchives.indexOf(ext) != -1) {
        return srcTypeArchives;
    }

    return srcTypeFile;
}

uploadObject.prototype.createId = function() {
    var count = this.countRows;
    if (!count) {
        count = 0;
    }
    this.countRows = parseInt(count) + 1;
    return this.countRows;
}

uploadObject.prototype.createTblListFiles = function() {

    var tblListFiles = document.createElement("table");
    tblListFiles.id = "tblListFiles";
    tblListFiles.style.cssText = "background-color:#FFFFFF;";
    tblListFiles.cellPadding = "0";
    tblListFiles.cellSpacing = "0";
    tblListFiles.border = "0";

    return tblListFiles;
}

uploadObject.prototype.removeItem = function(id) {
		this.inxupload--;
    this.getCurrentRowListFiles(id).parentNode.removeChild(this.getCurrentRowListFiles(id));
}

uploadObject.prototype.removeAllItems = function() {
    if (this.isUploadFile == "false") {

        if (this.tblListFiles != null) {
						this.inxupload = 0;
            var count = this.tblListFiles.rows.length;

						if (count > 0) {
                for (var i = 0; i < count; i++) {
                    this.tblListFiles.deleteRow(0);
                }
            }

        }
    }
}

uploadObject.prototype.uploadAllItems = function() {
    var flag = -1;

    if (this.tblListFiles != null) {
			
        if (this.tblListFiles.rows.length > 0) {
					
            for (var i = 0; i < this.tblListFiles.rows.length; i++) {

                if (this.tblListFiles.rows[i].attributes["isUpload"].value == "false") {
                    flag = i;
                    break;
                }

            }

            if (flag != -1) {
                this.isUploadFileAll = "true";
                var fileItemId = this.tblListFiles.rows[i].attributes["fileItemId"].value;
                this.uploadFile(fileItemId);
            } else {
                this.isUploadFileAll = "false";
            }

        }

    }
}

uploadObject.prototype.createProgressRun = function () {

		var srcImgProgress = "images/upload/pb_running.gif";
    var tblProgress = document.createElement("table");
    tblProgress.cellPadding = "0";
    tblProgress.cellSpacing = "0";
    tblProgress.border = "0";
    tblProgress.style.cssText = "height:10px;width:153px;display:none;";
    tblProgress.id = "progress";

    var row = tblProgress.insertRow(tblProgress.rows.length);
    var cell1 = document.createElement("td");
    cell1.style.cssText = "font-size: 1px;border: 1px solid #A9AEB3;"
    cell1.innerHTML = "<img src=" + srcImgProgress + " style = 'width:150px;height:8px;'/>";
    row.appendChild(cell1);

    return tblProgress;

}

uploadObject.prototype.endLoading = function (id) {
    this.isUploadFile = "false";
    this.progressRun.style.display = "none";
    this.container.appendChild(this.progressRun);
    this.getCurrentInputFile(id).parentNode.removeChild(this.getCurrentInputFile(id));

}

uploadObject.prototype.startRequest = function(id) {

	try {
			var xmlHttp = this.createXMLHttpRequest();
			xmlHttp.open("POST", this.pathGetIdHandler, false);
			xmlHttp.send("a=0");

			if (xmlHttp.status == 200) {
				
					if(!xmlHttp.responseText) {
						throw "error";
					}
	
					this.sessionId = xmlHttp.responseText;
					this.UPLOAD_IDENTIFIER = xmlHttp.responseText;
	
			} else {
					throw "error";
			}
  } catch(e) {
       throw e;
       return;
  }
}


uploadObject.prototype.sendIdSession = function (id) {

	try {
			var xmlHttp = this.createXMLHttpRequest();
			xmlHttp.open("post", this.pathGetInfoHandler , false);
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			xmlHttp.send("sessionId="+this.sessionId);

			if (xmlHttp.status == 200) {
					
					if (xmlHttp.responseText) {
							if(isNaN(parseInt(xmlHttp.responseText))) {
									 throw "error";
							}

							if (parseInt(xmlHttp.responseText) != -1) {
									var self=this;

									try {
											window.setTimeout( function () {self.sendIdSession(id)} , 500);
									} catch(e) {

									}

							} else if (parseInt(xmlHttp.responseText) == -1) {
									this.endLoading(id);

									var tblContent = this.getCurrentTblContent(id);
									tblContent.rows[1].cells[0].innerHTML += "<font class='text' >Done</font>";
									tblContent.rows[1].cells[0].vAlign = "top";

									if (this.isUploadFileAll == "true") {
											this.uploadAllItems();
									}
							}
          }
      } else {
          throw "error";
      }
  } catch(e) {
			this.endLoading(id);
			this.isUploadFileAll = "false";
			var tblContent = this.getCurrentTblContent(id);
			tblContent.rows[1].cells[0].innerHTML += "<font class='text' >ERROR</font>";
			tblContent.rows[1].cells[0].vAlign = "top";

      return;
  }

}

uploadObject.prototype.getCurrentRowListFiles = function(id) {
	
    for (var i = 0; i < this.tblListFiles.rows.length; i++) {
			
        if (this.tblListFiles.rows[i].id == "rowListFiles" + id) {
            return this.tblListFiles.rows[i];
        }
    }
}

uploadObject.prototype.getCurrentTblContent = function(id) {
	
    for (var i = 0; i < this.tblListFiles.rows.length; i++) {
			
        if (this.tblListFiles.rows[i].cells[0].firstChild.id == "tblContent" + id) {
            return this.tblListFiles.rows[i].cells[0].firstChild;
        }
    }
}

uploadObject.prototype.getCurrentInputFile = function(id) {

    var collInputs = this.uploadForm.getElementsByTagName("input");

    for (var i = 0; i < collInputs.length; i++) {
			
        if (collInputs[i].id == "file" + id) {
            return collInputs[i];
        }
    }

}

uploadObject.prototype.uploadFile = function (id) {
		var fileName = this.getFileName(this.getCurrentInputFile(id).value);
		var fileType = this.getFileType(fileName);

		if ( this.isUploadFile == "false" ) {
				if (navigator.appName.indexOf("Explorer") != -1) {
						 this.selectItemInExplorer(id);
				} else {
						 this.selectItemInMozilla(id);
				}

				var tblContent = this.getCurrentTblContent(id);
				tblContent.rows[0].cells[2].removeChild(tblContent.rows[0].cells[2].firstChild)
				tblContent.rows[1].cells[1].removeChild(tblContent.rows[1].cells[1].firstChild)
				tblContent.parentNode.parentNode.attributes["isUpload"].value = "true";

				this.isUploadFile = "true";
				this.getCurrentInputFile(id).disabled = false;
				this.progressRun.style.display = "inline";
				this.getCurrentRowListFiles(id).cells[0].firstChild.rows[1].cells[0].appendChild(this.progressRun);

				try {
						this.startRequest(id);
						this.sendIdSession(id);

				} catch(e) {
						this.endLoading(id);
						this.isUploadFileAll = "false";
						var tblContent = this.getCurrentTblContent(id);
						tblContent.rows[1].cells[0].innerHTML += "<font class='text' >ERROR</font>";
						tblContent.rows[1].cells[0].vAlign = "top";

						return;
				}

				this.uploadForm.action = this.pathUploadHandler + "?sessionId=" + this.sessionId + "&fileName=" + fileName + "&fileType=" + fileType +"&userFile=" + this.getCurrentInputFile(id).id;
				this.uploadForm.submit();
    }
}
