
var g_captchacnt = 0;


function check_comment_form() {

	dojo.byId('process_status').innerHTML = "Data Checking !!.........";

	var errmsg = check_comment_items();

	if (errmsg != "") {
		alert(errmsg);
		return false;
	}

	g_captchacnt = 0;
	var param = new Object();
	param.item_id = dojo.byId('item_id').value;
	param.author = dojo.byId('author').value;
	param.comment = dojo.byId('comment').value;

	callPHP(param, "httprequest.php?main_page=comment_confirm", res_confirm);

	return false;
}



function check_comment_items() {
	var errmsg = "";

	errmsg += check_anitem(dojo.byId('author').value, 'name', true, 0, 32, false);
	errmsg += check_anitem(dojo.byId('comment').value, 'comment', true, 0, 500, false);
	return errmsg;
}


function reinput_comment_form() {
	dojo.byId('confirm').innerHTML = "";
	dojo.style(dojo.byId("input"), "display", "block");
	dojo.byId('process_status').innerHTML = "";
	dojo.byId('errmsg-author').innerHTML = "";
	dojo.byId('errmsg-comment').innerHTML = "";
}


function update_comment_form() {

	var errmsg = "";
	errmsg += check_anitem(dojo.byId('captcha').value, 'Validation String', true, 0, 6, true);

	if (errmsg != "") {
		alert(errmsg);
		return false;
	}

	var param = new Object();
	param.captcha = dojo.byId('captcha').value;

	callPHP(param, "/tools/kcaptcha/ajax_blog_comment.php", res_kcaptch);
	return false;
}



function res_confirm(response) {

	var resulttext = "" + response.match(/<responseresult>[A-Za-z]+<\/responseresult>/);
	var result = "" + resulttext.replace(/<\/*responseresult>/g, "");

	if (result == "OK") {
		var htmltext = "" + response.replace(/<responseresult>[A-Za-z]+<\/responseresult>/, "");
		dojo.byId('confirm').innerHTML = htmltext;
		dojo.style(dojo.byId("input"), "display", "none");
		dojo.byId('process_status').innerHTML = "";
	} else {
		var errmsgtxt = "" + response.replace(/<responseresult>[A-Za-z]+<\/responseresult>/, "");
		var errmsgtxt_author = "" + errmsgtxt.match(/<errmsg_author>.*<\/errmsg_author>/);
		var errmsgtxt_comment = "" + errmsgtxt.match(/<errmsg_comment>.*<\/errmsg_comment>/);
		var errmsg_author = "" + errmsgtxt_author.replace(/<\/*errmsg_author>/g, "");
		var errmsg_comment = "" + errmsgtxt_comment.replace(/<\/*errmsg_comment>/g, "");
		var alertmsg = "There are errors !\n";
		if (errmsg_author) alertmsg +=  " *" + errmsg_author + "\n";
		if (errmsg_comment) alertmsg +=  " *" + errmsg_comment + "\n";
		alert(alertmsg);
		dojo.byId('process_status').innerHTML = "There are errors !";
		dojo.byId('errmsg-author').innerHTML = errmsg_author;
		dojo.byId('errmsg-comment').innerHTML = errmsg_comment;
	}

}



function res_kcaptch(response) {

	if (response == "OK") {
		var param = new Object();
		param.item_id = dojo.byId('item_id').value;
		param.author = dojo.byId('author').value;
		param.comment = dojo.byId('comment').value;

		callPHP(param, "httprequest.php?main_page=comment_update", res_comment_update);
		return false;
	} else {
		g_captchacnt++;
		if (g_captchacnt < 1000) {
			alert("Wrong Validation Code!\n" + g_captchacnt + " Times\n");
		}
	}

	return false;
}


function res_comment_update(response) {
	location.href = response;
}




