var new_comment = { is_active: false };

function reveal_post_comment_inline(clear, edit) {
  var i, name, message, focus_set;
  var elem = document.getElementById('img_captcha_url');

  if (clear == true) clear_post_comment_inline();
  if (edit == false && elem) get_captcha();
  
  document.getElementById('inline_error_message').innerHTML = '';
  document.getElementById('post_comment_inline').style.display='block';

  focus_set = false;
  for (i=0; !focus_set && i<bw_list_of_fields.length; i++) {
    name = bw_list_of_fields[i];
    message = bw_form_errors[name];
    if (message != "") {
      window.setTimeout('document.comment.' + name + '.focus();', 1000);
      focus_set = true;
    }
  }

  if (!focus_set)
    window.setTimeout('document.comment.body.focus();', 1000);

  return true;
}

function clear_post_comment_inline() {
  var i;
  var elem = document.getElementById('img_captcha_url');

  if (elem) elem.src="/_images/blank_captcha.gif";
  document.comment.reset();
  document.getElementById('post_comment_inline').style.display='none';

  for (i=0; i<bw_list_of_fields.length; i++) {
    // response code is error.xxx, like error.contact_url
    // the corresponding element id is error_contact_url
    set_field_error_message(bw_list_of_fields[i], false);
  }
}

function get_captcha() {
  var myAjax = new Ajax.Request( "/ajax/index.cgi", {method: 'post', parameters: 'cmd=get_captcha', onLoading: get_captcha_busy, onComplete: get_captcha_response});
}

function get_captcha_busy() {
  if($('systemWorking') && Ajax.activeRequestCount>0)
    Effect.Appear('systemWorking',{duration:0.5,queue:'end'})
}

function get_captcha_response(originalRequest) {
  if($('systemWorking') && Ajax.activeRequestCount>0)
    Effect.Fade('systemWorking',{duration:0.5,queue:'end'});
	
  if (originalRequest.responseText.indexOf('JSON Response')) {
    eval(originalRequest.responseText);	// sets the img src and the form field captcha key value
    Effect.BlindDown('img_captcha_url_div');
  }   
}

function do_post_busy() {
  if($('systemPosting') && Ajax.activeRequestCount>0)
    Effect.Appear('systemPosting',{duration:0.5,queue:'end'})
}

function ajax_do_post_comment() {
  var i;

  if (new_comment.is_active) return false;
  new_comment.is_active = true;

  var finf = Form.getElements( document.comment );

  var fval = { };
  var fe;
  var t_parameters = 'cmd=post_comment&';
  
  for (i=0; i<finf.length; i++) {
    fe = finf[i];
    t_parameters += (i>0 ? '&' : '') + encodeURIComponent(fe.name) + '=' + encodeURIComponent($F(fe));

    fval[ fe.name ] = $F(f