function showReplies(comment_id, icon_prefix){
	if( !icon_prefix ) icon_prefix = "";

	var element = $j("#"+icon_prefix+comment_id+"_replies_text");
	var link = $j("#"+icon_prefix+comment_id+"_showRepliesLink");

	if( element.html() == "" ){
		element.load( g_webroot + "comments/ajax_getComments/"+comment_id, function(){
			element.css("display","block");
			link.html("hide replies");
		});
	} else {
		if( element.css("display") == "block" ){
			link.html("show replies");
			element.css("display","none");
		} else {
			link.html("hide replies");
			element.css("display","block");
		}
	}
}

function addReply(comment_id,icon_prefix){
	if( !icon_prefix ) icon_prefix = "";

	var element = $j("#"+icon_prefix+comment_id+"_replies");
	var link = $j("#"+icon_prefix+comment_id+"_addReplyLink");

	if( element.css("display") == "block" ){
		link.html("reply to this comment");
		element.css("display","none");
	} else {
		link.html("hide this form");
		element.css("display","block");
	}
}

function submitReply(elementID, icon_prefix){
	if( !icon_prefix ) icon_prefix = "";

	var text = $j("#"+icon_prefix+elementID+"_text");
	var fk = $j("#"+icon_prefix+elementID+"_fk");
	var fkt = $j("#"+icon_prefix+elementID+"_fkt");
	var button = $j("#"+icon_prefix+elementID+"_button");

	var error = false;
	if( empty.test(text.val()) ){
		hasError(text);
		displayError( text, "please enter a comment");
		error = true;
	} else {
		clearError(text);
		displayError( text, "");
	}
	if( error ){
		return;
	}
	disableElement(text);
	disableElement(button);

	$j.ajax({
		type: "POST",
		url: g_webroot + "comments/ajax_add",
		async:true,
		data: "data="+JSON.stringify({
			"text":encodeURIComponent(text.val()),
			"foreign_key":fk.val(),
			"foreign_key_type":fkt.val(),
			"comment_id":elementID,
			"c":false
		}),
		success: function(data){
			if( data == 1 ){
				//hide the form
				//$j("#"+elementID+"_replies").html("<div class='flash_message_success' style='font-weight:bold;padding: 12px 12px 12px 50px;'>Thanks for posting!<div style='font-weight:normal;color:#666'>please allow a few minutes for your comments to be displayed</div></div>");
				//var t = setTimeout('$j("#'+elementID+'_replies").fadeOut("slow")', 5000);
				showFlashMini("success","Thanks!","your comment has been posted",icon_prefix+elementID+"_replies")
			} else {
				enableElement(text);
				enableElement(button);

				if( data == "PERMISSION_ERROR" ){
					showFlashMini("error","Sorry!","You have to be logged in to post a comment.<br />"+login_link("<strong>Login Now!</strong>"), icon_prefix+elementID+"_text");
				} else if( data == "CAPTCHA_ERROR" ){
					hasError(captcha);
					displayError( captcha, "Please enter the characters to the right exactly as they appear");
				} else if( data == "EMPTY_TEXT_ERROR" ){
					hasError(text);
					displayError( text, "Please enter a comment.");
				} else if ( data == "EMPTY_CAPTCHA_ERROR" ){
					hasError(captcha);
					displayError( captcha, "Please enter the exact value from the image in the text box.");
				}
			}
		},
		error: ajax_communication_error
	});
}

function submitThumb(direction, elementID, icon_prefix){
	if( !icon_prefix ) icon_prefix = "";
	var element = $j("#"+icon_prefix+elementID+"_vote");
	var oppDirection = (direction=="U")?"D":"U";

	var chosenImage = $j("#"+icon_prefix+elementID+"_vote_"+direction);
	var unchosenImage = $j("#"+icon_prefix+elementID+"_vote_"+oppDirection);
	var chosenImageLink = $j("#"+icon_prefix+elementID+"_vote_"+direction+"_link");
	var unchosenImageLink = $j("#"+icon_prefix+elementID+"_vote_"+oppDirection+"_link");

	chosenImageLink.attr("href","javascript:void(0);");
	unchosenImageLink.attr("href","javascript:void(0);");
	if( oppDirection == "U"){
		unchosenImage.attr("src", g_img_url + "img/thumb_up_off.png");
	} else {
		unchosenImage.attr("src", g_img_url + "img/thumb_down_off.png");
	}

	$j.ajax({
		type: "POST",
		url: g_webroot + "comments/ajax_thumbs",
		data: "data="+JSON.stringify({"direction":direction,"id":elementID}),
		success: function(data){
			if( data == 1 ){
				var finalVal = 0;
				if( direction == "U" ){
					finalVal = parseInt(element.html()) + 1;
				} else {
					finalVal = parseInt(element.html()) - 1;
				}
				element.html( finalVal );
			}
		},
		error: ajax_communication_error
	});
}

function submitComment(elementID,icon_prefix){
	if( typeof icon_prefix == "undefined" ) icon_prefix = "";

	var text = $j("#"+icon_prefix+elementID+"_text");
	var captcha = $j("#"+icon_prefix+elementID+"_captcha");
	var fk = $j("#"+icon_prefix+elementID+"_fk");
	var fkt = $j("#"+icon_prefix+elementID+"_fkt");
	var cid = $j("#"+icon_prefix+elementID+"_cid");
	var button = $j("#"+icon_prefix+elementID+"_button");

	var error = false;
	if( empty.test(text.val()) ){
		hasError(text);
		displayError( text, "Please enter a comment");
		error = true;
	} else {
		clearError(text);
		displayError( text, "");
	}
	if( empty.test(captcha.val()) ){
		hasError(captcha);
		displayError( captcha, "Please enter the characters to the right exactly as they appear");
		error = true;
	} else {
		clearError(captcha);
		displayError( captcha, "");
	}

	if( error ){
		return;
	}

	disableElement(text);
	disableElement(captcha);
	disableElement(button);

	$j.ajax({
		type: "POST",
		url: g_webroot + "comments/ajax_add",
		async:true,
		data: "data="+JSON.stringify({
			"text":encodeURIComponent(text.val()),
			"captcha":encodeURIComponent(captcha.val()),
			"foreign_key":fk.val(),
			"foreign_key_type":fkt.val(),
			"comment_id":cid.val(),
			"c":true
		}),
		success: function(data){
			if( data == 1 ){
				//hide the form
				//$j("#"+elementID+"_wrapper").html("<div class='flash_message_success' style='font-weight:bold;padding: 12px 12px 12px 50px;'>Thanks for posting!<div style='font-weight:normal;color:#666'>please allow a few minutes for your comments to be displayed</div></div>");
				//var t = setTimeout('$j("#'+elementID+'_wrapper").fadeOut("slow")', 5000);
				showFlashMini("success","Thanks!","your comment has been posted",icon_prefix+elementID+"_wrapper");
			} else {
				enableElement(text);
				enableElement(captcha);
				enableElement(button);

				if( data == "PERMISSION_ERROR" ){
					showFlashMini("error","Sorry!","You have to be logged in to post a comment.<br />"+login_link("<strong>Login Now!</strong>"), icon_prefix+elementID+"_text");
				} else if( data == "CAPTCHA_ERROR" ){
					hasError(captcha);
					displayError( captcha, "Please enter the characters to the right exactly as they appear");
				} else if( data == "EMPTY_TEXT_ERROR" ){
					hasError(text);
					displayError( text, "Please enter a comment.");
				} else if ( data == "EMPTY_CAPTCHA_ERROR" ){
					hasError(captcha);
					displayError( captcha, "Please enter the exact value from the image in the text box.");
				}
			}
		},
		error: ajax_communication_error
	});
}
