//SubjectAjax For Discuz! 4.X  And  5.X
//Author: lu5266
//(C) lu5266  www.5466.ik8.com


var enterdo=false;

function $(id)  {
		return document.getElementById(id);
}

function htmlspecialchars(str){
	var f = new Array(new RegExp('&', 'g'),new RegExp('<', 'g'),new RegExp('>', 'g'));
	var r = new Array('&amp;','&lt;','&gt;');
	for (var i = 0; i < f.length; i++){
		str = str.replace(f[i], r[i]);
	}
	return str;
}

function sendsubject(edittid) {

	$('collapsedimg_'+edittid).style.display= '';

	var newtext = $('subject_'+edittid).value;
	var oldtext = $('tid_'+edittid).innerHTML;

	if( htmlspecialchars(newtext) ==  oldtext ||  newtext ==''  || newtext.length > 80 ) {		
		$('tid_'+edittid).style.visibility="visible";
		$('input_'+edittid).innerHTML = '';
		 return false;
	}

	document.subjectajaxform.tid.value = edittid;
	document.subjectajaxform.newsubject.value = newtext;
	document.subjectajaxform.submit();

}

function newsubject(edittid) {

	obj = $('tid_'+edittid);
	$('input_'+edittid).innerHTML = "<input id='subject_"+ edittid +"' type='text' size='80' value='" + obj.innerHTML 
		+"' onkeypress ='if(event.keyCode==13) {noLight(this); enterdo=true; sendsubject("+edittid
		+"); return false;}'  onblur='if(!enterdo) {noLight(this); sendsubject("+edittid+");}'   onfocus='highLight(this);' >";  
	obj.style.visibility="hidden";
	$('subject_'+edittid).focus();

}

function subjectresponse(rspText, edittid) {
	
	if(rspText > 0){
		showmessage(' 编辑标题成功!');	
		$('tid_'+edittid).style.visibility="visible";		
		$('tid_'+edittid).innerHTML = $('subject_'+edittid).value;
		$('input_'+edittid).innerHTML = '';
	}else {
		showmessage('非常负责任的告诉你: 编辑标题失败.');
		$('input_'+edittid).innerHTML = '';
		$('tid_'+edittid).style.visibility="visible";		
	}
	setTimeout('enterdo=false','1000');
}

function highLight(span){

	span.parentNode.style.border = "2px solid #D1FDCD";
	span.parentNode.style.padding = "0";
	span.style.border = "1px solid #54CE43";  
	
}

function noLight(span){

	span.parentNode.style.border = "0px";
	span.parentNode.style.padding = "2px";
	 span.style.border = "0px";  
	 
}

function showmessage(message) {

	$("popmsg").innerHTML = message;
	$("subjectajaxlayer").style.visibility="visible";
	setTimeout('$("subjectajaxlayer").style.visibility="hidden"','2000');

}

document.write("<div id='subjectajaxlayer'  style='position:absolute;top:expression(body.clientHeight/3+body.scrollTop); right:300; visibility:hidden ;'>"
	+"<table  align=\"center\"  width=\"300\" height=\"80\" class=\"tableborder\" >"
	+"<tr><td class=\"altbg1\" align=center><b><font color='blue'><span id='popmsg' ></span></font></b></td ></tr></table></div>");

document.write('<div><iframe  name="subjectajaxframe" width="0" height="0"></iframe>'
	+'<form name="subjectajaxform" action="subjectajax.php" method="post" target="subjectajaxframe">'
	+'<input type="hidden" name="tid"><input type="hidden" name="newsubject"></form></div>');

