function writeComment(id) {
	if ( document.getElementById ) {
		if ( document.getElementById(id).style.display == "none" ) {
			document.getElementById(id).style.display = "block";
		} else {
			document.getElementById(id).style.display = "none";
		}
	} else {
	}
}

/** トリムする.
 * @param szValue 検証する文字列.
 * @return トリム後の文字列.
 */
function trim( szValue ) {
	return String(szValue).replace(/^[ 　]*/gim, "").replace(/[ 　]*$/gim, "");
}
/** サニタイズする.
 * @param szValue サニタイズされる前の文字列.
 * @return サニタイズされた文字列.
 */
function sanitize( szValue ) {
	szValue = szValue.replace(/&/g, "&amp;");
	szValue = szValue.replace(/</g,"&lt;");
	szValue = szValue.replace(/>/g,"&gt;");
	szValue = szValue.replace(/"/g,"&quot;");
	szValue = szValue.replace(/'/g,"&#39;");
	return szValue;
}

/** コメントに記入する
 * @param year コメントを書く記事の年
 * @param month コメントを書く記事の月
 * @param day コメントを書く記事の日
 * @param diarycount コメントを書く記事の記事数
 * @param commentpolicy コメントポリシー
 */
function putComment(year, month, day, diarycount, commentpolicy) {
	//alert("コメント記入"+year+"年"+month+"月"+day+"日"+diarycount+"記事");
	var formname = ''+year+''+month+''+day+''+diarycount;
	var textname = ''+year+''+month+''+day+''+diarycount;
	var data;
	var password;
	data = document.forms["formcomment"+formname].elements["commentdata"+textname].value;
	username = document.forms["formcomment"+formname].elements["username"+textname].value;
	password = document.forms["formcomment"+formname].elements["pass"+textname].value;
	//実際に子ども達に触らせてみたら、誤ってユーザ名や内容を入力せずに、
	//コメントを登録してくれることがわかった.
	//従って、ユーザ名や内容が空の場合は登録せずに、エラーメッセージを
	//出力できるようにした.PHPで制御するほどでもないので、javascriptを切った場合はしょうがない
	//ことにする.[2007.07.19]
	if ( trim(username) == "" ) {
		alert("ユーザ名が未入力です.");
		return;
	} else if ( trim( data ) == "" ) {
		alert("空のデータは送信できません.");
		return;
	}
	//username = username.replace(/&/g,"＆");
	//ここからAjaxね。
	httpObj = createHttpRequest();
	if ( httpObj ) {
		//httpObj.open("GET", "./db/diarycommentsave.php?request="+encodeURI(data), true)
		httpObj.open("GET", "./db/diarycommentsave.php?year="+year+"&month="+month+"&day="+day+"&diarycount="+diarycount+"&request="+encodeURI(data)+"&username="+encodeURI(username)+"&password="+encodeURI(password)+"&commentpolicy="+commentpolicy, true);
		httpObj.send(null);
		//コメント欄を非表示にして、書いた内容を画面に表示する.
		var commentlist = 'comment'+year+''+month+''+day+''+diarycount;
		//サニタイズ[これはPHPの方で制御しているので、書き込み完了時のみの対策だけでよい.]
		data = sanitize(data);
		username = sanitize( username );
		document.getElementById(commentlist).style.display = "none";
		document.getElementById("wrotecomment"+year+''+month+''+day+''+diarycount).innerHTML = "<fieldset><legend>"+username+"</legend><div><div align = \"right\">NOW!!</div>"+data+"</div></fieldset>\n";
		document.getElementById("writecommentmessage"+year+''+month+''+day+''+diarycount).style.display = "none";
		//alert(data);
	} else {
		alert("httprequest violation has occured. Call MURAYAMA.");
		return;
	}
}

//XMLHttpRequestオブジェクト生成
function createHttpRequest(){
	//Win ie用
	if(window.ActiveXObject){
		try {
			//MSXML2以降用
			return new ActiveXObject("Msxml2.XMLHTTP") 
		} catch (e) {
			try {
				//旧MSXML用
				return new ActiveXObject("Microsoft.XMLHTTP")
			} catch (e2) {
				return null
			}
		}
	} else if(window.XMLHttpRequest){
		//Win ie以外のXMLHttpRequestオブジェクト実装ブラウザ用
		return new XMLHttpRequest() 
	} else {
		return null
	}
}

