/**********************************************************************************************************************
***********************************************************************************************************************
Comment-Related Functions
***********************************************************************************************************************
**********************************************************************************************************************/

$(document).ready(function() {
	oBoxComment = new infoBox("divBoxComment");
	bSavingComment = false;
});

function togglecomments(obj, id) {
	var suffix = "_"+obj+"_"+id;
	if ($("#commentarea"+suffix+":hidden").attr("id")) {
		fillcommentarea(obj, id);
		$("#commentarea"+suffix).show();
	} else {
		$("#commentarea"+suffix).hide();
	}
}


function fillcommentarea(obj, id) {
	$.post(ajaxpath, {
			method:			"fillcommentarea"
			, obj:			obj
			, objid:		id
			, _ct:			new Date().getTime()
		}, function(xml) {
			if (parseInt($("errorcode", xml).text())>0) {
				oBoxInfo.doall($("msg", xml).text() + closelink("oBoxInfo"), "infoBoxGreen", "infoBoxRed", 5000);
			} else {
				$("#spanNumComments_"+obj+"_"+id).html($("numcomments", xml).text());
				$("#commentarea_"+obj+"_"+id).html($("html", xml).text());
			}
		});
}


function togglecommentform(obj, id) {
	var suffix = "_"+obj+"_"+id;
	if ($("#frmComment"+suffix+":hidden").attr("id")) {
		$("#frmComment"+suffix).show();
		$("#atoggleform"+suffix).hide();
		refreshcaptcha(obj, id);
	} else {
		$("#frmComment"+suffix).hide();
		$("#atoggleform"+suffix).show();
	}
}


function refreshcaptcha(obj, id) {
	var suffix = "_"+obj+"_"+id;
	var seed = Math.random();
	seed = Math.ceil(seed * 10000);		// generate a random seed for our captcha image between 1 and 10000
	$("#seed"+suffix).val(seed);
	$("#captcha"+suffix).val("");
	$("#captcha"+suffix).focus();
	$("#divCAPTCHA"+suffix).html("<img width=\"100\" height=\"20\" src=\"" + PATH_ROOT_URL_FULL + "/images/captcha.php?seed="+seed+"&_ct=" + new Date().getTime() + "\" />");
}


function submitcomment(obj, id) {
	var suffix = "_" + obj + "_" + id;
	if (!bSavingComment) {
		bSavingComment = true;
		iComment = setTimeout("submitcommenttimedout()", AJAX_SAVE_TIMEOUT*1000);
		oBoxComment.doall("Saving...", "infoBoxRed infoBoxGreen", "", AJAX_SAVE_TIMEOUT*1000);
		$.post(ajaxpath, {
				method:			"savecomment"
				, obj:			obj
				, objid:		id
				, name:			$("#commentname"+suffix).val()
				, body:			$("#commentbody"+suffix).val()
				, seed:			$("#seed"+suffix).val()
				, captcha:		$("#captcha"+suffix).val()
				, _ct:			new Date().getTime()
			}, function(xml) {
				clearTimeout(iComment);
				if (parseInt($("errorcode", xml).text())>0) {
					refreshcaptcha(obj, id);
					oBoxComment.doall($("msg", xml).text() + closelink("oBoxComment"), "infoBoxGreen", "infoBoxRed", 5000);
				} else {
					fillcommentarea(obj, id);
					oBoxComment.doall("Comment saved.", "infoBoxRed", "infoBoxGreen", 2000);
				}
				bSavingComment = false;
			});
	}

}


function submitcommenttimedout() {
	clearTimeout(iComment);
	oBoxComment.doall(AJAX_SAVE_TIMEOUT + " seconds expired without a response from the server.  Please try again.", "infoBoxGreen", "infoBoxRed", 5000);
}





