function changSpanDisplay(id){
	id = objCheck(id);
	var spans = id.getElementsByTagName('span');
	for (var i=0;i<spans.length;i++) {
		if (spans[i].style.display == 'none') {
			spans[i].style.display = '';
		} else {
			spans[i].style.display = 'none';
		}
	}
}
function showEidt(dtid,u){
	var li = getObj('dt_'+dtid);
	li.onmouseover = '';
	li.onmouseout = '';
	var spans = li.getElementsByTagName('span');
	for (var i=0;i<spans.length;i++) {
		spans[i].style.display = 'none';
	}
	var a = li.getElementsByTagName('a');
	for (var i=0;i<a.length;i++) {
		a[i].style.display = 'none';
	}

	var text= elementBind('input','','input');
	text.setAttribute('type','text');
	text.value	= a[i-1].innerHTML;
	li.appendChild(text);
	text.focus();

	var ok	= elementBind('input','','bt3');
	ok.setAttribute('type','button');
	ok.value= '確定';

	var cancel	= elementBind('input','','bt','padding:.2em 0;margin:0 0 0 .3em');
	cancel.setAttribute('type','button');
	cancel.value= '取消';

	ok.onclick	= function () {
		var name = char_cv(text.value);
		if (name.length<1) {
			showDialog('error','類型名稱不能為空');
			return false;
		}
		ajax.send('mode.php?m=o&q=ajax&action=eidtdiarytype','u='+u+'&name='+name+'&dtid='+dtid,function(){
			var rText = ajax.request.responseText;
			if (rText == 'success') {
				cancelEdit(li,name);
			} else {
				ajax.guide();
			}
		});
	}

	cancel.onclick	= function () {
		cancelEdit(li);
	}
	li.appendChild(ok);
	li.appendChild(cancel);
}
function cancelEdit(li,name){
	li = objCheck(li);
	li.onmouseover = function () {
		changSpanDisplay(li);
	}
	li.onmouseout = function () {
		changSpanDisplay(li);
	}
	var input = li.getElementsByTagName('input');
	while (input[0]) {
		delElement(input[0]);
	}
	var a = li.getElementsByTagName('a');
	for (var i=0;i<a.length;i++) {
		if (name) {
			a[i].innerHTML	= name;
		}
		a[i].style.display	= '';
	}
}
function addType(id,u){/*創建日志分類*/
	if (getObj('dt_add')) {
		changeCreatInputDisplay('dt_add');
		return true;
	}
	creatDiaryTypeInput(id,u);
}
function creatDiaryTypeInput(id,u){/*創建日志分類*/
	var parent	= getObj(id);
	var li	= addChild(parent,'li','dt_add');//生成<li id='dt_add'></li>

	var text= elementBind('input','','input','width:90px;height:17px;');//生成<input class="input" type="text">
	text.setAttribute('type','text');
	li.appendChild(text);//生成<li id='dt_add'><input class="txa dtwidth" type="text"></li>
	text.focus();

	var ok	= elementBind('input','','bt3','padding:2px;margin:0 0 0 5px');//生成<input class="bt3" type="button" value="確定" onclick="">
	ok.setAttribute('type','button');
	ok.value= '確定';
	ok.onclick	= function () {
		var name = char_cv(text.value);
		if (name.length<1) {
			showDialog('error','類型名稱不能為空');
			return false;
		}
		ajax.send('mode.php?m=o&q=ajax&action=adddiarytype','u='+u+'&name='+ajax.convert(name),function(){
			var rText = ajax.request.responseText.split('\t');//類似php中的explode
			if (rText[0] == 'success') {
				var dtid = rText[1] - 0;
				if (isNaN(dtid)==false) {
					creatDiaryType('dt_null',dtid,name,u);
					delElement('dt_add');
				} else {
					showDialog('error','非法錯誤');
				}
			} else {
				ajax.guide();
			}
		});
	}
	li.appendChild(ok);//生成<li id='dt_add'><input class="bt3" type="button" value="確定" onclick=""></li>
	var cancel	= elementBind('input','','bt','padding:2px 2px 1px;margin:0 0 0 5px');//生成<input class="bt" type="input" value="取消" style="padding:.2em 0;margin:0 0 0 .3em" onclick="">
	cancel.setAttribute('type','button');
	cancel.value= '取消';
	cancel.onclick	= function () {
		text.value = '';
		li.style.display = 'none';
	}
	li.appendChild(cancel);
}

function changeCreatInputDisplay(id){
	var dt_add = getObj(id);
	if (dt_add.style.display =='none') {
		dt_add.style.display = '';
	} else {
		dt_add.style.display = 'none';
	}
}
function creatDiaryType(latter,dtid,name,u){/*創建日志分類*/
	latter = objCheck(latter);
	var parent = latter.parentNode;//ul
	var li = elementBind('li','dt_'+dtid);//生成<li id='dt_1' onmouseover="changSpanDisplay(this);" onmouseout="changSpanDisplay(this);"></li>
	li.onmouseover = function () {
		changSpanDisplay(li);
	}
	li.onmouseout = function () {
		changSpanDisplay(li);
	}
	li.innerHTML = '<span class="fr gray o-del" style="cursor: pointer;display:none" onclick="pwConfirm(\'是否確認刪除\',this,function(){delDiaryType(\''+dtid+'\',\''+u+'\')})">刪除</span><span class="fr pdr gray" style="cursor: pointer;display:none" onclick="showEidt(\''+dtid+'\',\''+u+'\');">編輯</span><a href="mode.php?m=o&q=diary&dtid='+dtid+'">'+name+'</a>';
	parent.insertBefore(li,latter);//在ul中，id="dt_null"的li前動態插入新li一行
}
function delDiaryType(dtid,u){/*刪除日志分類*/
	ajax.send('mode.php?m=o&q=ajax&action=deldiarytype','u='+u+'&dtid='+dtid,function(){
		var rText = ajax.request.responseText;
		if (rText == 'success') {
			delElement('dt_'+dtid);
		} else {
			ajax.guide();
		}
	});
}

function add_dtid(u,id) {/*寫日志頁分類添加*/
	if (isNaN(u) && winduid != u) {
		showDialog('error','非法操作');
	}
	ajax.send('mode.php?m=o&q=ajax&action=adddiarytype','u='+u+'&b=1',function(){
		var rText = ajax.request.responseText.split('\t');
		if (rText[0]=='success') {
			if (rText[1]=='1') {
				read.setMenu(create_dtid(id));
				read.menupz();
			} else {
				showDialog('error','非法操作');
			}
		}
	});
}

function create_dtid(id) {/*寫日志頁分類添加*/
	selid = objCheck(id);
	var maindiv	= elementBind('div','','','width:300px;height:100%');
	var title = elementBind('div','','h');
	title.innerHTML = '增加日志分類';
	maindiv.appendChild(title);
	var innerdiv = addChild(maindiv,'div','','menu-text p10');
	var ul = addChild(innerdiv,'ul','');
	var li = addChild(ul,'li');
	li.innerHTML = '分類名稱';

	var text= elementBind('input','','input','margin-left:10px');
	text.setAttribute('type','text');
	li.appendChild(text);
	var innername = document.createTextNode(' 小于20字節');
	li.appendChild(innername);
	text.focus();

	var footer	= addChild(maindiv,'div','','bdtA tr2','padding:5px');
	var tar	= addChild(footer,'div','','tar');
	var ok	= elementBind('input','','bt3');
	ok.setAttribute('type','button');
	ok.setAttribute('value','確定');

	ok.onclick	= function () {
		var name = char_cv(text.value);
		if (name.length<1) {
			showDialog('error','類型名稱不能為空');
			return false;
		}
		ajax.send('mode.php?m=o&q=ajax&action=adddiarytype','u='+winduid+'&name='+ajax.convert(name),function(){
			var rText = ajax.request.responseText.split('\t');
			if (rText[0] == 'success') {
				var dtid = rText[1] - 0;
				if (isNaN(dtid)==false) {
					var option = elementBind('option');
					option.innerHTML = name;
					//var innername = document.createTextNode(name);
					//option.appendChild(innername);
					option.value = dtid;
					option.selected = 'selected';
					selid.insertBefore(option,null);
					showDialog('success','分類添加成功!',2);
				} else {
					showDialog('error','非法錯誤');
				}
			} else {
				ajax.guide();
			}
		});
	}

	var cansel	= elementBind('input','','bt','margin-left:8px');
	cansel.type	= 'button';
	cansel.value= '取消';
	cansel.onclick	= closep;

	tar.appendChild(ok);
	tar.appendChild(cansel);

	return maindiv;
}

function optionsel(id) {/*權限選擇*/
	copy = objCheck('if_copy');
	if (isNaN(id)) {
		showDialog('error','非法操作');
	}
	if (id == '0') {
		copy.disabled = '';
		copy.checked = 'checked';
	} else if (id == '1') {
		copy.disabled = '';
		copy.checked = 'checked';
	} else if (id == '2') {
		copy.disabled = 'disabled';
		copy.checked = '';
	}
}

function delDiary(id,u,read){/*刪除日志*/
	var url = 'mode.php?m=o&q=diary&u='+u;
	ajax.send('mode.php?m=o&q=ajax&action=deldiary&id='+id,'',function(){
	var rText = ajax.request.responseText.split('\t');
	if (rText[0] == 'success') {
		var element = document.getElementById('diary_'+id);
		if (element) {
			element.parentNode.removeChild(element);
			if (typeof(read) == 'string') {
				window.location.href = url;
			}
		} else {
			alert(ajax.request.responseText);
			window.location.href = url;
		}
	} else {
		ajax.guide();
	}
});
}

function Copydiary(id) {/*日志轉載*/
	ajax.send('mode.php?m=o&q=ajax&action=copydiary&id='+id,'',function(){
		var rText = ajax.request.responseText;
		if (rText=='success') {
			read.setMenu(create_copy());
			read.menupz();
		} else {
			ajax.guide();
		}
	});
}

function create_copy() {/*轉載提示*/
	var maindiv	= elementBind('div','','','width:300px;height:100%');
	var title = elementBind('div','','h');
	title.innerHTML = '轉載提示';
	maindiv.appendChild(title);
	var innerdiv = addChild(maindiv,'div','','menu-text p10');
	var ul = addChild(innerdiv,'ul','');
	var li = addChild(ul,'li');
	li.innerHTML = '轉載成功，日志已存在我的日志中，是否要去瀏覽？';

	var footer	= addChild(maindiv,'div','','tr2','padding:5px;');
	var tar	= addChild(footer,'div','','tar');
	var ok	= elementBind('input','','bt3');
	ok.setAttribute('type','button');
	ok.setAttribute('value','瀏覽');

	ok.onclick	= function () {
		window.location.href = 'mode.php?m=o&q=diary';
	}

	var cansel	= elementBind('input','','bt','margin-left:8px');
	cansel.type	= 'button';
	cansel.value= '關閉';
	cansel.onclick	= closep;

	tar.appendChild(ok);
	tar.appendChild(cansel);

	return maindiv;
}

function ajaxpage(url,type,u) {/*瀏覽日志*/
	var tourl = 'mode.php?m=o&q=diary&u='+u;
	ajax.send(url,'',function() {
		var rText = ajax.request.responseText.split('\t');
		if (rText[0] == 'success') {
			if (rText[1]) {
				window.location.href = tourl + '&did=' + rText[1];
			} else {
				ajax.request.responseText = type == 'next' ? '已經是最後一篇日志' : '已經是第一篇日志';
				ajax.guide();
			}
		} else {
			ajax.guide();
		}
	});
	return false;
}