
var comment_form = "comment_form_container";

function write_comment_form()
{
	var form_area = $('#'+comment_form);

	if (form_area.html() && !form_area.html().match(/thanks/i)) {
		form_area.toggle();
		return false;
	}

	form_area.html('<img src="images/ajax_working.gif" alt="loading..." />').show();

    $.ajax({
		type: 'GET',
		url: "_comment_form.php",
		data: {
			uf_item_id : '7-' + gallery.currentDetailImage.id,
			ajax_parent: location.pathname.match(/\/([^\/]+?)$/)[1]
		},
		success: write_form_success,
		dataType:'html'
	});
}

function write_form_success(content)
{
    var form_area = $('#'+comment_form);

	form_area.html(content);

	if (form_area.length && form_area.html().match(/login/)) {
		showLoginForm();
		form_area.hide();
		return;
	}
	if (form_area.length && form_area.html().match(/thanks/i)) {
		form_area.hide();
		gallery.loadComments();;
		return;
	}
	var form = form_area.find('form');
	form.submit(checkCommentForm);

	//error messages go away when addressed
	form.find('input, textarea').change(function()
	{
		var my_error_div = $('#e_' + this.id);
		if (my_error_div && my_error_div.html())  {
			my_error_div.slideUp().html('');
		}
	});
}

function checkCommentForm() //{{{
{
	var errors = false;
	$(this).find('.error').each(function()
	{
		var check_input = $('#comment_' + this.id.match(/e_comment_(.+)/)[1]);
		if (check_input.val().length < 1) {
			$(this).html(this.title).show();
			errors = true;
		}
		if (check_input.val().length > 4000) {
			$(this).html('Sorry, comments must be less than 4000 characters long.').show();
			errors = true;
		}
	});

	if (!errors) {
		var form = $('#'+comment_form + ' form');
		$('input[@type=submit], input[@type=image]', form).get(0).disabled = true;

		form.ajaxSubmit({
			url: '_comment_form.php'
				+ this.action.substring(this.action.indexOf('?')),
			//target: comment_form,
			success: write_form_success
		});
	}

	return false;
} //}}}


