

function getComments(liveSite, contextPath, threadName, currentLocale){
	var url = contextPath+"/"+liveSite+"/ajax-pages/lithiumcommentretrieveservlet.html"; 
		
	var params = {threadName: threadName, timestamp: (new Date()).getTime(), currentLocale: currentLocale};	
		
	jQuery.get(url, params, function(data) {
		jQuery('#commentsListDiv').html(data);
		
	});
}

function resetPostInput(commentText) {
	buttonClickable = true;
	document.body.style.cursor = "default";
	jQuery('.buttonbox,.buttonbox div').css('cursor','pointer');
	document.commentForm.commentText.value = commentText;
}

function postCommentAndUpdateList(liveSite, contextPath, threadName, typeHereText, pleaseEnterText, currentLocale) {
	if (!buttonClickable){
		return;
	}
	buttonClickable = false;
	
	document.body.style.cursor="wait";
	jQuery('.buttonbox,.buttonbox div').css('cursor','wait');
	
	var url = contextPath+"/"+liveSite+"/ajax-pages/lithiumcommentpostservlet.html"; 
	
	var commentText = document.commentForm.commentText.value;
	if (commentText.length > 400) {
		commentText = commentText.substr(0, 400);
	}
	
	if (commentText == "" || commentText == typeHereText) {
		alert(pleaseEnterText);
		resetPostInput(typeHereText);
	} else {
		var params = {threadName: threadName, commentText: commentText , currentLocale: currentLocale};	
		jQuery.post(url,params, refreshComments);
	}
}

function refreshCommentData(response, errorText, typeHereText, liveSite, contextPath, threadName, currentLocale) {
	if (response.indexOf("error") >= 0) {
		alert(errorText);
		resetPostInput(typeHereText);
	} else {
		getComments(liveSite, contextPath, threadName, currentLocale);
		resetPostInput(typeHereText);
	}
}

function clearForm(typeHereText) {	
	if (document.commentForm.commentText.value == typeHereText) {
		document.commentForm.commentText.value = "";
	} 
}

function onPostResult(data, textStatus) {
	reportAbuse.processResult(data, textStatus);
}

var reportAbuse = function() {
	var priv = {
		offenderId : "",
		postedDate : ""
	};
	return {
		showForm : function(offenderId, postedDate) {
			priv.offenderId = offenderId;
			priv.postedDate = postedDate;
			
			jQuery("#commentUrl").html(location.href);
			jQuery("#offenderId").html(priv.offenderId);
			showPopup('reportform');
		},
		postForm : function(currentLocale, contextPath, liveSite) {
			var data = {commentUrl: location.href, offenderId: priv.offenderId, postedDate: priv.postedDate};
			data.email = jQuery("#report_email").attr("value");
			data.email = data.email.replace(/_/g, "-underscore-");
			data.reason = jQuery("#report_reason").attr("reason");
			data.comments = jQuery("#report_comments").attr("value");
			data.currentLocale = currentLocale;
			var selectObj = document.getElementById("report_reason");
			if (selectObj) {
				if (selectObj.selectedIndex > 0) {
					data.reason = selectObj.options[selectObj.selectedIndex].text;
				}
			}
			var url = contextPath+"/"+liveSite+"/ajax-pages/lithiumreportabuseservlet.html"; 
	
			jQuery.post(url, data, onPostResult);
		},
		processResult : function(dataStr, textStatus) {
			var data = eval(dataStr);
			if (data == "success") {
				hidePopup('reportform');
				showPopup('reportthankyou');
				centerElement('closebutton');
			} else {
				jQuery(".errortxt").removeClass("errortxtact");
				for (var i in data) {
					var tagId = data[i];
					jQuery("#" + tagId).addClass("errortxtact");
				}
			}
		}
	};
}();
jQuery(this).ajaxError(function(event, request, ajaxOptions, thrownError) {
	if (request.statusText != "") {
//		alert("Command error: " + request.statusText);
	}
});


