// An array with all of the available fields.
var available_fields = [];

function initComments(userLoggedIn) {
	if (userLoggedIn) {
		available_fields = ["subject", "comment"];
	} else {
		available_fields = ["name", "email", "homepage", "captcha", "subject", "comment"];
	}
}

// Number of times the form has been submitted. This is used to refresh the captcha
// since after a few times the captcha actually expires.
var form_submission_count = 0;

//function clear_errors() {
//	for (var i=0; i < available_fields.length; i++) {
//		fieldname = available_fields[i];
//		errors = 'id_' + fieldname + '_errors';
//		Element.hide(errors);
//		Element.removeClassName('id_' + fieldname, 'fieldWithErrors');
//	}
//	window.alert('hree1');
//}

// This function is called when the comment is posted successfully.
function post_successful(rtn, textStatus, xhr) {
	form_submission_count += 1;
	if (rtn.successful) {  // if successful then reload to show the new comment.
		window.location.reload();
	} else {  // Otherwise we show the errors.
		first_field_with_error = ""
			for (var i=0; i < available_fields.length; i++) {
				fieldname = available_fields[i];
				errors = '#id_' + fieldname + '_errors';
				if (rtn[fieldname]) {
					if (first_field_with_error == "") { first_field_with_error = fieldname; }
					if ($(errors).is(":visible")) {
						$('#id_' + fieldname + '_label').effect("pulsate", {times: 2}, 500);
						//Effect.Pulsate('id_' + fieldname + '_label', { pulses: 3, duration: 2} );
					} else {
						$(errors).show();
					}
					$(errors).html(rtn[fieldname]);
					$('#id_' + fieldname).addClass('fieldWithErrors');
				} else {
					$(errors).hide();
					$('#id_' + fieldname).removeClass('fieldWithErrors');
				}
			}
		if (first_field_with_error != "") {  // Focus the first field that had an error.
			scrollTo($('#id_' + first_field_with_error + "_label"));
			$('#id_' + first_field_with_error).focus();
		}
		$('#comment_submit_button').removeAttr("disabled");
		$('#comment_cancel_button').removeAttr("disabled");
		$('#form_posting_image').hide();
	}
	// Regenerate the captcha if the form has been submitted three times.
	if (form_submission_count == 3) {
		form_submission_count = 0;
		captcha_regen_captcha();
	}
}

function post_failed(xhr, textStatus, error) {
	$('#comment_submit_button').removeAttr("disabled");
	$('#comment_cancel_button').removeAttr("disabled");
	$('#form_posting_image').hide();
	$('comment_ajax_errors').show();
	$('comment_ajax_errors').html(textStatus);
	$('new_comment_link').show();
	window.alert("Sorry, I was unable to save your comment.  Please report this to me on the 'Contact me' page.");
}

function post_comment_form() {
	$('#comment_submit_button').attr("disabled", "disabled");
	$('#comment_cancel_button').attr("disabled", "disabled");
	$('#form_posting_image').show();
	var frms = $("#comment_form");
	$.ajax({ url: frms[0].action, type: "POST", success: post_successful, error: post_failed, data: frms.serialize() });
	return false;
}

// reply_title and reply_id are only required for replies to comments.
function show_new_comment_form(node_id, reply_title, reply_id) {
	//clear_errors();
	data = {node_id: node_id};
	if (reply_title != undefined) {
		data = { node_id: node_id, reply_to_title: reply_title, reply_to_id: reply_id };
	}

	$("#new_comment_form").hide();
	$("#comment_form_loading").show();

	$.ajax({ url: "/comment/new_form", type: "POST",
			data: data, dataType: 'html',
			success: function(data, textStatus, xhr) {
				$("#comment_form_loading").hide();
				$("#new_comment_form").html(data);
				$("#new_comment_form").show()
				scrollTo($("#new_comment_form"));
			},
			error: function(xhr, textStatus, error) {
				window.alert('Sorry, I was unable to get the comment form.  Please try again.\n\nError: ' + textStatus);
				$("#new_comment_link").show();
				$("#comment_ajax_errors").show();
				$("#comment_ajax_errors").html("Text status: " +textStatus);
				$("#comment_form_loading").hide();
			}
	});
	return false;
}

// This function is called when the user clicks the cancel button.
function cancel_comment() {
	$('#new_comment_link').show();
	$("#new_comment_form").slideUp(200);
}

