var aid = 1;

function addAttach() {
	newnode = $('attachbodyhidden').firstChild.cloneNode(true);
	var id = aid;
	var tags;
	tags = newnode.getElementsByTagName('input');
	for(i in tags) {
		if(tags[i].name == 'attach[]') {
			tags[i].id = 'attach_' + id;
			tags[i].onchange = function() {insertAttach(id)};
			tags[i].unselectable = 'on';
		}
		if(tags[i].name == 'attachid[]') {
			tags[i].value = id;
		}
	}
	tags = newnode.getElementsByTagName('span');
	for(i in tags) {
		if(tags[i].id == 'localfile[]') {
			tags[i].id = 'localfile_' + id;
		}
	}
	aid++;
	$('attachbody').appendChild(newnode);
	}

function insertAttach(id) {
   	var path = $('attach_' + id).value;
	//var ext = path.lastIndexOf('.') == -1 ? '' : path.substr(path.lastIndexOf('.') + 1, path.length).toLowerCase();
	//var re = new RegExp("(^|\\s|,)" + ext + "($|\\s|,)", "ig");//建立一个模式匹配上传文件扩展名
	var localfile = $('attach_' + id).value.substr($('attach_' + id).value.replace(/\\/g, '/').lastIndexOf('/') + 1);
	$('localfile_' + id).innerHTML = '&nbsp;<a href="###delAttach" onclick="delAttach(' + id + ')">[删除]</a> <a href="###insertAttach" title="插入" onclick="insertAttachtext(' + id + ');return false;">[插入]</a><span class="smalltxt">[' + id + ']</span> ' + localfile;
	$('attach_' + id).style.display = 'none';
	addAttach();
}

function insertAttachtext(id) {
		AddText('[attach]' + id + '[/attach]',false);
}

function AddText(txt) {
	obj=getEditorobj();
	selection = document.selection;
	checkFocus();
	if(!isUndefined(obj.selectionStart)) {
		var opn = obj.selectionStart + 0;
		obj.value = obj.value.substr(0, obj.selectionStart) + txt + obj.value.substr(obj.selectionEnd);
	} else if(selection && selection.createRange) {
		var sel = selection.createRange();
		sel.text = txt;
		sel.moveStart('character', -strlen(txt));
	} else {
		obj.value += txt;
	}
}

function delAttach(id) {
	$('attachbody').removeChild($('attach_' + id).parentNode.parentNode);
	$('attachbody').innerHTML == '' && addAttach();
}


function deleteattach(typename,mid,fid,sitefont){

	var con=confirm("您确定删除被选中附件的所有信息吗？");
		if (con==true) {
			if(sitefont==true){
					window.location.href="post.php?ipost=modify"+ typename + "&action=delete&mid=" + mid + "&fid=" + fid;
				}else{
					window.location.href="siteadmin.php?iframe="+ typename + "management&ipost=modify&action=delete&mid=" + mid + "&fid=" + fid;
				}
	}
}



function modifyattach(fid) {
	obj = $('attach'+fid);
	mobj = $('modifyattach'+fid);
	obj.style.display = 'none';
	mobj.innerHTML = '<input type="file" name="attachmodify[' + fid + ']" size="15" class="modifyfile"> <input type="button" value="取消" onclick="cancelmodify(\'' + fid + '\')">';
}

function cancelmodify(fid) {
	obj = $('attach'+fid);
	mobj = $('modifyattach'+fid);
	mobj.innerHTML = '';
	obj.style.display = '';
}