// JavaScript Document

function addFiles(objectName, initNum, currentNum, maxNum, argFile, withDesc, argFileDesc, descWord, lang){
	this.objectName  = objectName;
	this.currentNum  = currentNum;
	this.initNum     = initNum;
	this.maxNum      = maxNum;
	this.argFile     = argFile;
	this.argFileDesc = argFileDesc;
	this.withDesc    = withDesc;
	this.descWord    = descWord;
	
	//------------
	// 语言部分
	//------------
	this._lang = {};
	this._lang = {};
	
	this._lang['chs'] = {};
	this._lang['chs']['explain'] = '说明';
	this._lang['chs']['delete']  = '删除';
	this._lang['chs']['add']     = '添加';
	this._lang['chs']['continue_add'] = '继续添加';
	this._lang['chs']['max_upload']  = '您最多可上传的';
	this._lang['chs']['amount']      = '数目 : ';
	
	this._lang['cht'] = {};
	this._lang['cht']['explain'] = '說明';
	this._lang['cht']['delete']  = '刪除';
	this._lang['cht']['add']     = '添加';
	this._lang['cht']['continue_add'] = '繼續添加';
	this._lang['cht']['max_upload']  = '您最多可上傳的';
	this._lang['cht']['amount']      = '數目: ';
	
	this._lang['eng'] = {};
	this._lang['eng']['explain'] = ' note ';
	this._lang['eng']['delete']  = ' Delete';
	this._lang['eng']['add']     = ' Add ';
	this._lang['eng']['continue_add'] = ' Continue to add ';
	this._lang['eng']['max_upload']  = ' The max amount of ';
	this._lang['eng']['amount']      = ' you can upload : ';
	
	this.lang = this._lang[lang];
	
}

addFiles.prototype.findTD = function(o){
	if (o.nodeName=="TR"|| o.nodeName=="TABLE") 
	{
		return;
	}

	if(o.nodeName=="TD")
	{
		return (o);	
	}
	else
	{
		return (o.parentElement);
	}
}

addFiles.prototype.addFile = function() {
 if(this.currentNum<this.maxNum)
 {                     
     this.currentNum++;
	 var tbl = document.getElementById(this.objectName+"Table");
     currRow = tbl.insertRow(-1);
     cellc = currRow.insertCell(0);
     cellc.innerHTML = this.genFileInput();
  }
}

addFiles.prototype.genFileInput = function(){
	var fileInput = '';
	 fileInput= "<input type='file' name='"+this.argFile+"[]'>";
	 if(this.withDesc)
	 {
		 fileInput += "&nbsp;" + this.descWord + this.lang['explain'] +": <input type='text' name='" + this.argFileDesc + "[]' />&nbsp;&nbsp;";
	 }
	 
	 fileInput += "<button onclick='"+ this.objectName + ".removeFile();' class='botheight'>"+ this.lang['delete'] + "</button><br />";
	 
	 return fileInput;
}

addFiles.prototype.removeFile = function(){

//-------------- start ----------------//
// 此代码仍然与Mozilla系列浏览器不兼容
// October 11, 2006 
// Blin
//----------------------------------
	if (navigator.userAgent.indexOf("MSIE") != -1){ // IE
		e = event.srcElement ;
	}else{  // Mozilla 
		 e = event.target;
	}
//---------- end -----------------//
		
  o = this.findTD(e);
  var tbl = document.getElementById(this.objectName+"Table");
  tbl.deleteRow(o.parentElement.rowIndex*1);
  
  this.currentNum--;
}


addFiles.prototype.printHtml = function(){

	document.write("<table name='"+this.objectName+"Table' id='" + this.objectName+"Table' border='0'><tr><td></td></tr></table>");
		
    if(this.initNum >=0 && this.initNum <= this.maxNum && this.currentNum < this.maxNum)
	{
		if(this.initNum==0){
			document.write("<a href='javascript:void("+ this.objectName + ".addFile());'>" + this.lang['add'] + this.descWord + "</a>  " + this.lang['max_upload'] + this.descWord + this.lang['amount'] + this.maxNum);
		}else{
			document.write("<a href='javascript:void("+ this.objectName + ".addFile());'>" + this.lang['continue_add'] + this.descWord + "</a> " +  this.lang['max_upload'] + this.descWord + this.lang['amount'] +this.maxNum);
		}
		

		for(var i=1; i<=this.initNum; i++)
		{
            this.addFile();
			
		}
	}

}
