function commentReset(){
	$('comment-author').value = '';
	$('comment-email').value = '';
	$('comment-url').value = '';
	$('comment-delete-key').value = '';
	$('comment-text').value = '';
//	$('comment-captcha-input').value = '';
}

function commentDelete(){
	var blog_id = $('blog_id').value;
	var diary_id = $('diary_id').value;
	var accept_comment = $('accept_comment').value;
	var del_id = $F('delete-id');
	var del_key = $F('delete-key');
	var myajax = new Ajax.Request("xmlRequest.php",{
		method: 
			'post',
		parameters:
			"mode=delete"+ 
			"&blog_id=" + blog_id+ 
			"&diary_id=" + diary_id+ 
			"&accept_comment=" + accept_comment+ 
			"&del_id=" + del_id +
			"&del_key=" + del_key ,
		onSuccess:
			commentRead
	});
}

function commentWrite(){
	var blog_id = $('blog_id').value;
	var diary_id = $('diary_id').value;
	var accept_comment = $('accept_comment').value;
	var name = $F('comment-author');
	var email = $F('comment-email');
	var url = $F('comment-url');
	var text = $F('comment-text');
	var del_key = $F('comment-delete-key');
	var captcha_input = $F('comment-captcha-input');
	var captcha_key = $F('comment-captcha-key');
	var myajax = new Ajax.Request("xmlRequest.php",{
		method: 
			'post',
		parameters:
			"mode=save"+ 
			"&blog_id=" + blog_id+ 
			"&diary_id=" + diary_id+ 
			"&accept_comment=" + accept_comment+ 
			"&name=" + encodeURIComponent(name) + 
			"&email=" + encodeURIComponent(email) +
			"&url=" + encodeURIComponent(url) +
			"&text=" + encodeURIComponent(text) + 
			"&del_key=" + del_key +
			"&captcha_input=" + captcha_input + 
			"&captcha_key=" + captcha_key ,
		onLoading:
			function(){
				$('comments-status').innerHTML = '';
				$('comments-status').innerHTML = "送信中";
			},
		onFailure:
			function(){
				$('comments-status').innerHTML = '';
				$('comments-error').innerHTML = "送信エラーが発生しました。";
			},
		onSuccess:
			writeResult
	});
}

function commentRead(){
	var blog_id = $('blog_id').value;
	var diary_id = $('diary_id').value;
	var comment_page = 1;
	var myajax = new Ajax.Request("xmlRequest.php",{
		method: 
			'post',
		parameters:
			"mode=load"+ 
			"&blog_id=" + blog_id + 
			"&diary_id=" + diary_id + 
			"&page=" + comment_page, 
//		onLoading:
//			showStatus("読込み中"),
		onComplete:showResult
	});
}

function commentBack(){
	var blog_id = $('blog_id').value;
	var diary_id = $('diary_id').value;
	var comment_page = $('com-nowpage').value;
	if (comment_page >1)  comment_page --;
	var myajax = new Ajax.Request("xmlRequest.php",{
		method: 
			'post',
		parameters:
			"mode=update"+ 
			"&blog_id=" + blog_id + 
			"&diary_id=" + diary_id + 
			"&page=" + comment_page, 
		onLoading:
			function(){
				showStatus("読込み中")
			},
		onFailure:
			function(){
				showStatus("受信エラーが発生しました")
			},
		onSuccess:
			showResult
	});
}

function commentNext(){
	var blog_id = $('blog_id').value;
	var diary_id = $('diary_id').value;
	var comment_page = $('com-nowpage').value;
	comment_page ++;
	var myajax = new Ajax.Request("xmlRequest.php",{
		method: 
			'post',
		parameters:
			"mode=update"+ 
			"&blog_id=" + blog_id + 
			"&diary_id=" + diary_id + 
			"&page=" + comment_page, 
		onLoading:
			function(){
				showStatus("読込み中")
			},
		onFailure:
			function(){
				showStatus("受信エラーが発生しました")
			},
		onSuccess:
			showResult
	});
}

function writeResult(request){
	res = request.responseText.split(";");

	$('comments-status').innerHTML = '';

	if(res[0] == 0 || res[0] == 3) {
//		$('comment-author').value = '';
//		$('comment-email').value = '';
//		$('comment-url').value = '';
		$('comment-text').innerHTML = '';
		$('comment-text').value = '';
	}
	if(res[0] == 0)
		commentRead();
	else
		$('comments-error').innerHTML = res[1];
}

function showResult(request){
	$('comments-content').innerHTML = request.responseText;
	$('comments-error').innerHTML = '';
	if($('comments-loading1'))
				$('comments-loading1').innerHTML = '';
	if($('comments-loading2'))
				$('comments-loading2').innerHTML = '';
	if($('comment-back-up'))
		Event.observe('comment-back-up',"click",commentBack);
	if($('comment-next-up'))
		Event.observe('comment-next-up',"click",commentNext);
	if($('comment-back-dn'))
		Event.observe('comment-back-dn',"click",commentBack);
	if($('comment-next-dn'))
		Event.observe('comment-next-dn',"click",commentNext);
}

function showStatus(request){
	if($('comments-loading1'))
		$('comments-loading1').innerHTML = request;
	if($('comments-loading2'))
		$('comments-loading2').innerHTML = request;
	$('comments-error').innerHTML = '';
}

Event.observe(window,"load",function(){
	commentRead();
	Event.observe('comment-reset',"click",commentReset);
	Event.observe('comment-post',"click",commentWrite);
	if(document.getElementById("comment-delete"))
		Event.observe('comment-delete',"click",commentDelete);


});

/* prevent memory leaks in IE */
Event.observe(window, 'unload', Event.unloadCache, false);
