var bbs_tim;
var asyncHttp=null;

function asyncHttpRequest(url,func){
	if(url=="")return;
	try{
		if (window.ActiveXObject){
			asyncHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}else{
			asyncHttp = new XMLHttpRequest();
		}
	}catch(e){
	}

	document.body.style.cursor="progress";
	asyncHttp.open("GET", url, true);
	asyncHttp.onreadystatechange = func;
	asyncHttp.send(null);
}

function asyncHttpPostRequest(url,data,func){
	if(url=="")return;
	try{
		if (window.ActiveXObject){
			asyncHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}else{
			asyncHttp = new XMLHttpRequest();
		}
	}catch(e){
	}

	asyncHttp.onreadystatechange = func;
	asyncHttp.open("POST", url, true);
	asyncHttp.setRequestHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
	asyncHttp.send(data);
//alert(data);
}

function showMessage(){
	if(asyncHttp==null)return;
	if ((asyncHttp.readyState == 4) && (asyncHttp.status == 200)){

		p=asyncHttp.responseText.indexOf('\n',0);
		str=asyncHttp.responseText.substr(0,p).replace('\t','<BR>\n');
		document.getElementById("userlist").innerHTML="<FONT COLOR=BLUE><B>只今の参加者</B></FONT><BR>"+str;
		document.getElementById("bbsbody").innerHTML="<TABLE BORDER=0 VALIGN=TOP ALIGN=LEFT>"+asyncHttp.responseText.substring(p,asyncHttp.responseText.length)+"</TABLE><BR><BR>";
		document.getElementById("bbsbody").scrollTop = document.getElementById("bbsbody").scrollHeight;
		document.body.style.cursor="auto";
		startInterval();
		asyncHttp=null;
		document.getElementById("btn").enabled=true;
	}
}
function getMessage(){
	if(asyncHttp!=null)return;
	document.getElementById("btn").enabled=false;
//	document.body.style.cursor="progress";
	asyncHttpRequest("http://nangoso.org/bbs/bbs_engine.php?t="+Math.floor(Math.random()*10000).toString()+"&u="+document.bbs.username.value,showMessage);
}

function writeMessage(){
	if(asyncHttp!=null)return;
	if(document.bbs.message.value.length==0)getMessage();
	if(document.bbs.username.value.length==0)return;
	clearTimeout(bbs_tim);
	document.getElementById("btn").enabled=false;
	setCookie("namae",document.bbs.username.value);
	setCookie("col",document.bbs.col.value);
	setCookie("tim",document.bbs.interval.value);
//	document.body.style.cursor="progress";
	asyncHttpPostRequest("http://nangoso.org/bbs/bbs_engine.php","t="+Math.floor(Math.random()*10000).toString()+"&u="+document.bbs.username.value+"&m="+document.bbs.message.value+"&c="+document.bbs.col.value,showMessage);
	document.bbs.message.value="";
	startInterval();
	document.bbs.message.focus();
	return false;
}

function setColor(){
	document.bbs.message.style.color=document.bbs.col.value;
	document.getElementById("commentcol").style.color=document.bbs.col.value;
}

function startInterval(){
	clearTimeout(bbs_tim);
	if(document.bbs.interval.value!=0)bbs_tim=setTimeout(getMessage,document.bbs.interval.value);
}

function getCookie(key){
	var tmp = document.cookie+";";
	var index1 = tmp.indexOf(key, 0);
	if(index1 != -1){
		tmp = tmp.substring(index1,tmp.length);
		var index2 = tmp.indexOf("=",0)+1;
		var index3 = tmp.indexOf(";",index2);
		return(unescape(tmp.substring(index2,index3)));
	}
	return("");
}

function setCookie(key, val){
	document.cookie = key+"="+escape(val)+";expires=Fri, 31-Dec-2030 23:59:59;";
}

function init_bbs(){
	try{
		document.bbs.username.value=getCookie("namae");
		if(getCookie("col")!=""){
			document.bbs.col.value=getCookie("col");
		}
		if(getCookie("tim")!=""){
			document.bbs.interval.value=getCookie("tim");
		}
	}catch(e){
	}
	setColor();
	getMessage();
	document.bbs.message.focus();
}

