// 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(response) {
	form_submission_count += 1;
	rtn = response.responseJSON;
	returnedJSON = rtn;
	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).visible()) {
						Effect.Pulsate('id_' + fieldname + '_label', { pulses: 3, duration: 2} );
					} else {
						$(errors).show();
					}
					$(errors).update(rtn[fieldname]);
					Element.addClassName('id_' + fieldname, 'fieldWithErrors');
				} else {
					Element.hide(errors);
					Element.removeClassName('id_' + fieldname, 'fieldWithErrors');
				}
			}
		if (first_field_with_error != "") {  // Focus the first field that had an error.
			Form.Element.activate('id_' + first_field_with_error);
			Element.scrollTo('id_' + first_field_with_error + "_label");
		}
		$('comment_submit_button').enable();
		$('comment_cancel_button').enable();
		Element.hide('form_posting_image');
	}
	// 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(response) {
	$('comment_submit_button').enable();
	$('comment_cancel_button').enable();
	Element.hide('form_posting_image');
	$('comment_ajax_errors').show();
	$('comment_ajax_errors').update(response.responseText);
	$('new_comment_link').visible();
	window.alert("Sorry, I was unable to save your comment.");
}

function post_comment_form() {
	$('comment_submit_button').disable();
	$('comment_cancel_button').disable();
	Element.show('form_posting_image');
	var frm = $('comment_form').request({ onSuccess: post_successful, onFailure: post_failed });
	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();
	parameters = {node_id: node_id};
	if (reply_title != undefined) {
		parameters = { node_id: node_id, reply_to_title: reply_title, reply_to_id: reply_id };
	}
	new Ajax.Updater('new_comment_form', '/comment/new_form', {
		method: 'post',
		parameters: parameters,
		evalScripts: true,
		asynchronous: true,
		onLoading: function(response) {
			Element.hide('new_comment_form');
			Effect.Appear('comment_form_loading');
		},
		onSuccess: function(response) {
			Effect.Fade('comment_form_loading');
			Effect.Appear('new_comment_form', {afterFinish: function() { Element.scrollTo('new_comment_form'); }});
		},
		onFailure: function(response) {
			window.alert('Sorry, I was unable to get the comment form.  Please try again.');
			Element.show('new_comment_link');
			Element.show('comment_ajax_errors');
			Element.update('comment_ajax_errors', response.responseText);
			Element.hide('comment_form_loading');
		}
	});
	return false;
}

// This function is called when the user clicks the cancel button.
function cancel_comment() {
	//Effect.SlideUp('new_comment_form');
	Effect.Fade('new_comment_form');
	Element.show('new_comment_link');
}
