var currFocus;
var ExistAttaInfo = new Array();
var oldDelAttas = new Array();
var attaIdx = 0;
var IsIE;

function IsIEBrowser() {
	if (navigator.userAgent.indexOf("MSIE") != -1) {
		return true;
		} else {
		return false;
		}
		
}

// 增加附件函数 ()，增加到 idfilespan,基数为 attaIdx 。
function add() {

	addfile("idfilespan",attaIdx);
	attaIdx++;
	return false;
}

function exist(fileId) {
      var file = document.getElementById(fileId);
	  var result = false;
	  if ( file !=null ) {
			if ( file.value != null && file.value != "") {
				result = existFile( file);
			}//if
	  }
	  if( result)
	  {
		alert("此附件已粘贴, 如果在原有附件列表中并已删除。");
		//--attaIdx;
		//document.getElementById( "idAttachOper"+ attaIdx).click();
	  }
}//end of function

function getfilename( attaName ) {
	var s = attaName.lastIndexOf( '\\' );
	return attaName.substr(s+1, attaName.length - s -1);
}

function existFile( file)
{
	var form = document.submitinput;
	for ( var i=0; i <form.elements.length ; i ++ ) {				    
		if ( form.elements[i].type == "file" && form.elements[i].name != file.name ) {
			if ( file.value == form.elements[i].value ) {
				return true;
			}
		}
	}//for
	for (var i=0; i<ExistAttaInfo.length; i++) {
		var theName = ExistAttaInfo[i];
		if ( theName != null && theName != "" && theName == getfilename(file.name) ) {
			return true;
		}
	}
	return false;
}
//----------------------------------------addfile(spanId,index)----------------------------------------------
function addfile(spanId,index)
{
       var strIndex = "" + index;
	   var fileId = "attachfile"+ strIndex;
	   var brId = "idAttachBr" + strIndex;
	   
	   if (strIndex>10){alert('最多只能增加10个附件！');}
							else {
	   addInputFile(spanId,fileId);

	   adddel(spanId,index);

	   addbr(spanId,brId);
	   //document.getElementById( "attachfile"+ strIndex).click();
	   return;
							}
}
//-------------------------------------------sub function----------------------------------------------------
function addInputFile(spanId,fileId)
{
	  var span = document.getElementById(spanId);
	  if ( span !=null ) {
	                if ( !IsIE ) {
						var fileObj = document.createElement("input");
						if ( fileObj != null ) {
							
							fileObj.type="text";
							fileObj.name = fileId;
							fileObj.id = fileId;
							fileObj.size="50";
							var clickEvent = "exist('" + fileId + "')";
							fileObj.setAttribute("onclick",clickEvent,0);  
							span.appendChild(fileObj);
							
						}//if fileObj
					}// !IsIE


					if ( IsIE ) {
						var fileTag = "<input type='text' id ='" + fileId + "' name='" + fileId + "' size=50 class=input1>";
						var fileTagg = "<input type=button onClick=window.open('Web_File_AddUpload.asp?formname=theForm&editname=" + fileId + "&uppath=Upfiles','','status=no,scrollbars=no,top=20,left=110,width=400,height=126') class=button2 value=上传 />";
						var fileObj = document.createElement(fileTag);
						var fileObjg = document.createElement(fileTagg);
						span.appendChild(fileObj);
						span.appendChild(fileObjg);
					}//IsIE if
			
	  }//if span
}

function addbr(spanId,brId)
{
	  var span = document.getElementById(spanId);
	  if ( span !=null ) {
			var brObj = document.createElement("br");
			if ( brObj !=null ) {
				brObj.name = brId;
				brObj.id = brId;
				span.appendChild(brObj);
            }//if
     }//if
	 return;
}

function adddel(spanId,index)
{
      var strIndex = "" + index;
	  var delId = "idAttachOper" + strIndex;
	  var span = document.getElementById(spanId);
	  if ( span != null ) {
			var oTextNode = document.createElement("SPAN");
			oTextNode.style.width = "5px";
			span.appendChild(oTextNode);
		    if (IsIE) {
	        var tag = "";
			var delObj = document.createElement(tag);
			if ( delObj != null ) {
				span.appendChild(delObj);
				
			}//if
           
			}// Is IE
			
	        if (!IsIE) {
				var delObj = document.createElement("input");
				if ( delObj != null ) {
					delObj.name = delId;
					delObj.id = delId;
					delObj.type = "button";
					var clickEvent = "return delfile('" + spanId + "',"+strIndex+");";
					delObj.setAttribute("onclick",clickEvent);  
					span.appendChild(delObj);
				}//if
			}// !IsIE if
			if( delObj != null) delObj.value = "删除";
		}//main if
		return;
}

function delfile(spanId,index)
{
	   var strIndex = "" + index;
	   var fileId = "attachfile"+ strIndex;
	   var brId = "idAttachBr" + strIndex;
	   var delId = "idAttachOper" + strIndex;
	   //first,get the element
       var span = document.getElementById(spanId);
	   //alert(  "del span: " + span  );
	   if ( span == null ) return false;

	   var fileObj = document.getElementById(fileId);
	   if ( fileObj == null ) return false;

	   var brObj = document.getElementById(brId);
	   if ( brObj ==null ) return false;

	   var delObj = document.getElementById(delId);
	   //alert(  "del delId: " + delObj  );
	   if ( delObj == null ) return false;

       //second,create the replace element
	   var temp= document.createElement("SPAN");
	   //third,replace it
	    span.replaceChild(temp,fileObj);
		span.replaceChild(temp,brObj);
		
		span.removeChild(delObj.previousSibling);
		var attach = document.getElementById("attach");
		if(span.getElementsByTagName("INPUT").length == 1) attach.childNodes[0].nodeValue='添加附件';
		// End
		span.replaceChild(temp,delObj);	
		
		return false;
}
IsIE=IsIEBrowser();