var iComment = 0;
var bSavingComment = false;


function togglecomments(commenttypeid, filterid) {
	
	var suffix = "_"+commenttypeid+"_"+filterid;
	if ($("#commentarea"+suffix+":hidden").attr("id")) {
		fillcommentarea(commenttypeid, filterid);
		$("#commentarea"+suffix).show();
	} else {
		$("#commentarea"+suffix).html("");
		$("#commentarea"+suffix).hide();
	}
}
	 
function checkLoginOnly(){
	if (!(bLoggedIn) ) {
		
		r = confirm( "You must be logged in to your online account to rate other users. Click OK to log in or create a free account, or click 'Cancel' to return to the page.");
		if (r){
			window.location="/community/profile/login.cfm";
		}
    }
}
function  checkProfile() {
	var r = false; 
	if ((bLoggedIn) && ( !bProfileExists)) {
		r=confirm("You must create an online profile to contact this user. Click OK to create your profile, or click 'Cancel' to return to the page.");
		if (r){
			window.location="/community/profile/login.cfm";
		}
	}
	else if (!(bLoggedIn) ) {
		r = confirm("You must be logged in to your online account and MY FLW profile to contact other users. Click OK to log in or create a free account, or click 'Cancel' to return to the page.");
		if (r){
			window.location="/community/profile/login.cfm";
		}
	} else r = true;
	//if (!r) {return false}
	return r;
}		

function fillcommentarea(commenttypeid, filterid, limit) {
	limit = (limit || 0);
	$.post(ajaxpath, {
			method:				"fillcommentarea"
			, commenttypeid:	commenttypeid
			, filterid:			filterid
			, limit:			limit
			, _ct:				new Date().getTime()
		}, function(response) {
			try {
				var json = eval("("+response+")");
				if (json.RESULTS_ARRAY.length) {
					alert(json.RESULTS_ARRAY.join("\n"));
				} else {
					var suffix = "_"+commenttypeid+"_"+filterid;
					$("#spanNumComments"+suffix).html(""+json.NUMCOMMENTS);
					$("#commentarea"+suffix).html(json.HTML);
				}
			}
			catch (e) {
				
				// if we can't get comments, fail
			}
		});
}


function togglecommentform(commenttypeid, filterid) {
	
	var profileOK=checkProfile();
	
	if (profileOK) {
	var suffix = "_"+commenttypeid+"_"+filterid;
	
		if ($("#frmComment"+suffix+":hidden").attr("id")) {
			$("#frmComment"+suffix).show();
			$("#atoggleform"+suffix).hide();
			refreshcaptcha("captcha"+suffix, true);
		} else {
			$("#frmComment"+suffix).hide();
			$("#atoggleform"+suffix).show();
		}
	}
}


function submitcomment(commenttypeid, filterid) {
	var suffix = "_" + commenttypeid + "_" + filterid;
	
	if (!bSavingComment) {
		bSavingComment = true;
		$("#btnSubmitComment"+suffix).attr("disabled", "disabled");
		iComment = setTimeout("submitcommenttimedout('" + suffix + "')", SAVE_TIMEOUT*1000);
		$.post(ajaxpath, {
				method:				"savecomment"
				, commenttypeid:	commenttypeid
				, filterid:			filterid
				, message:			$("#commentField"+suffix).val()
				, seed:				$("#seedcaptcha"+suffix).val()
				, captcha:			$("#captchacaptcha"+suffix).val()
				, _ct:				$("#_ctcaptcha"+suffix).val()
			}, function(response) {
				try {
					clearTimeout(iComment);
					var json = eval("("+response+")");
					if (json.RESULTS_ARRAY.length) {
						var newSuffix = "captcha" +suffix  ;
						alert(json.RESULTS_ARRAY.join("\n"));
						refreshcaptcha(newSuffix, true);		// do this so they can't keep trying at the same captcha
					
					} else {
						//alert("Your comment will need to be approved " )
						fillcommentarea(commenttypeid, filterid);
					}
				}
				catch (e) {
					// if we can't get comments, fail
				}
				bSavingComment = false;
				$("#btnSubmitComment"+suffix).removeAttr("disabled");
			});
	}

}


function submitcommenttimedout(suffix) {
	clearTimeout(iComment);
	alert(SAVE_TIMEOUT + " seconds expired without a response from the server.  Please try again.");
	bSavingComment = false;
	$("#btnSubmitComment"+suffix).removeAttr("disabled");
}
