//上傳附件
function uploadfile(formid, sid) {
	var theform = $(formid)[0];
	var oldaction = theform.action;
	var oldtarget = theform.target;
	
	$('#showmsg').html('<font color="red">正在處理上傳檔案....</font>');
	
	theform.action = 'app.php?app=discuss&sid=' + sid + '&ac=upload';
	theform.target = 'phpframe';
	theform.submit();
	
	theform.action = oldaction;
	theform.target = oldtarget;
}

//設定
function setAttachment(sid, aid, filename, filedisc, file, isimage, image){
	if (isimage == 1) {
		var html = '<p id="attr_' + aid + '"><span class="upr">描述：<input type="text" name="filedisc[]" title="' + aid + '" size="40" value="' + filedisc + '" onblur="savaDescription(this,' + sid + ',' + aid +')" /></span><span class="upl"><input type="hidden" name="aids[]" value="' + aid + '" /> <a href="javascript:;" onclick="insertImg(\'' + file + '\', \'' + image + '\', \'' + aid + '\', \'' + sid + '\')">插入</a> | <a href="javascript:;" onclick="delAttr(' + sid + ',' + aid + ')">刪除</a> ' + filename + ' </span></p>';
	} else {
		var html = '<p id="attr_' + aid + '"><span class="upr">描述：<input type="text" name="filedisc[]" title="' + aid + '" size="40" value="' + filedisc + '" onblur="savaDescription(this,' + sid + ',' + aid +')" /></span><span class="upl"><input type="hidden" name="aids[]" value="' + aid + '" /> <a href="javascript:;" onclick="insertFile(\'' + filename + '\', \'' + file + '\', \'' + aid + '\', \'' + sid + '\')">插入</a> | <a href="javascript:;" onclick="delAttr(' + sid + ',' + aid + ')">刪除</a> ' + filename + ' </span></p>';
	}
	var container = $('#attrcontainer');
	container.append($(html));
	$('#showmsg').html('<font color="red">處理完畢.</font>');
	$('input[@name=upfile]').attr('value','');
}

//儲存描述
function savaDescription(obj, sid, aid) {
	var description = obj.value;
	var url = 'app.php?app=discuss&sid=' + sid + '&ac=ajax&op=savedescription&aid=' + aid + '&rnd=' + new Date().getTime();
	
	$.ajax({
		url : url,
		dataType : 'json',
		type     : 'POST',
		data     : '&description=' + description,
		success : function(json){
			//不用處理
		}
	});
}

//刪除附件
function delAttr(sid,aid){
	var url = "app.php?app=discuss&sid=" + sid + '&ac=ajax&op=delattr&aid=' + aid + '&rnd=' + new Date().getTime();
	
	$.ajax({
		url : url,
		dataType : 'json',
		success : function(json){
			alert(json.data);
			if(json.success){
				$('#attr_' + aid).remove();
			}
		}
	});
}

//插入圖片
function insertImg(file, image, aid, sid) {
	var url = "app.php?app=discuss&sid=" + sid + '&ac=ajax&op=insertattas&aid=' + aid + '&rnd=' + new Date().getTime();
	
	$.ajax({
		url : url,
		dataType : 'json',
		success : function(json){
			alert(json.data);
			if(json.success){
				edit_insert('<p><img src="' + image + '" alt="' + file + '" onclick="showimgs(alt);" /></p>');
			}
		}
	});
}

//插入附檔
function insertFile(filename, file, aid, sid) {
	var url = "app.php?app=discuss&sid=" + sid + '&ac=ajax&op=insertattas&aid=' + aid + '&rnd=' + new Date().getTime();
	
	$.ajax({
		url : url,
		dataType : 'json',
		success : function(json){
			alert(json.data);
			if(json.success){
				edit_insert('<p class="upfileother"><a href="' + file + '" />' + filename + '</a></p>');
			}
		}
	});
}
