function get(name){
	c=document.cookie+";";
	s=c.indexOf(name),m=name.length;
	return (s!=-1)? unescape(c.substring(s+m+1,c.indexOf(";",s))):0;
}
function writeC(name,value){
	temp=new Date();
	temp.setTime(temp.getTime()+60*60*24*1000*60);
	gmt=(value==0)? 0:temp.toGMTString();
	document.cookie=name+"="+escape(value)+"; expires="+gmt+";";
}
function v(id,h){
	obj=document.getElementById(id);
	if(obj && obj.style.visibility!=undefined)
		obj.style.visibility=(h==0)? "hidden":"visible";
}
function pageLoadFunc(){
	if(document.getElementById("ACCESS_INFO") && document.getElementById("ACCESS_INFO").firstChild){
		access_flg=get("access_flg");
		if(access_flg=="all")
			document.getElementById("ACCESS_INFO").firstChild.nodeValue="既に認証済みです。";
		else if(access_flg=="read")
			document.getElementById("ACCESS_INFO").firstChild.nodeValue="記事の閲覧のみ可能な権限で、既に認証済みです。";
	}
	if(!document.getElementById("NAME"))
		return;
	if(document.getElementById("sltMODE")){
		editMode();
		return;
	}
	sendname=get("sendname");
	email=get("email");
	pass=get("pass");
	if(document.getElementById){
		with(document){
			getElementById("NAME").value=(sendname!=0)? sendname:"";
			getElementById("MAIL").value=(email!=0)? email:"";
			getElementById("PASS").value=(pass!=0)? pass:"";
			getElementById("COOKIE").checked=(sendname!=0)? 1:0;
		}
	}
}
function check(){
	err="";
	if(document.getElementById)
		with(document){
			if(document.getElementById("sltMODE") && getElementById("sltMODE").value=="del")
				return true;
			sendname=getElementById("NAME").value;
			sendcomment=getElementById("COMMENT").value;
			if(document.getElementById("COOKIE")){
				if(getElementById("COOKIE").checked==1){
					writeC("sendname",sendname);
					writeC("email",getElementById("MAIL").value);
					writeC("pass",getElementById("PASS").value);
				}
				else{
					writeC("sendname",0);
					writeC("email",0);
					writeC("pass",0);
				}
			}
			if(sendname=="" || sendcomment=="" || getElementById("PASS").value=="")
				err="名前、コメント、パスワードは必須です。\n";
			if(sendcomment.length<3)
				err+="コメントは3文字以上でお願いします。\n";
			if(document.getElementById("TITLE")){
				sendtitle=getElementById("TITLE").value;
				if(sendtitle=="")
					err="タイトルは必ず書き込んで下さい。\n";
				if(sendtitle.length<3)
					err+="タイトルは3文字以上でお願いします。\n";
				if(sendtitle.indexOf(" ")==0 || sendtitle.indexOf("　")==0)
					err+="タイトルの最初の文字はスペース以外にして下さい。\n";
			}
		if(err!=""){
			window.alert(err);
			return false;
		}
	}
	return true;
}
function editMode(){
	with(document){
		if(document.getElementById){
			if(getElementById("sltMODE").value=="del"){
				getElementById("SUB").value="記事を削除する";
				v("main_form",0);
			}
			else{
				getElementById("SUB").value="編集した内容を投稿する";
				v("main_form",1);
			}
		}
	}
}
window.onload=pageLoadFunc;
