var original_text = ''; // нужна как глобальная переменная!

// заполняет форму ответа комментария именем пользователя
function answerComment(name) {
 $('CommentText').value += name + ', ';
 $('CommentText').focus();
}

// убирает любые html тэги
function stripHTML(text){
 var re= /<\S[^><]*>/g
 return text.replace(re, "")
}

function show_inline_editor(id) {
  text = $('comment'+id+'_text').innerHTML;
  original_text = text;
  text = text.replace(/[\n\r]/gi, '');
  text = text.replace(/\<br \/\>/gi, '\n');
  text = text.replace(/\<br\>/gi, '\n');
  text = stripHTML(text);
  form_id = 'comment'+id+'_form';
  form = '<form id="'+form_id+'" onsubmit="submit_inline_form(' + id + '); return false;" method="post" action="/comments/edit/'+id+'/">';
  form += '<textarea style="width:100%; height:80px" name="data[Comment][text]">' + text + '</textarea>';
  form += '<br><input type="submit" value="Редактировать >>>" style="margin-top:5px">';
  form += '&nbsp;&nbsp;&nbsp;<input type="button" onclick="cancel_comment(' + id + ')" value="Отменить" style="margin-top:5px">';
  $('comment'+id+'_text').innerHTML = form;
  $('editComment'+id).style.display = 'none';
}

function submit_inline_form(id) {
 new Ajax.Updater(
  'comment' + id + '_text',
  '/comments/edit/' + id + '/', 
  {
   parameters:Form.serialize('comment' + id + '_form'),
   onComplete: function() {
    $('editComment'+id).style.display = 'inline';
    $('comment' + id + '_wait').innerHTML = '';
   },
   onCreate: show_wait(id)
  }
 ); 
}
 
// убирает форму редактирования из поля и возвращает оригинальный текст
function cancel_comment(id) {
 $('comment'+id+'_text').innerHTML = original_text;
 $('editComment'+id).style.display = 'inline';
}

function show_wait(id) {
 $('comment' + id + '_wait').innerHTML += '<div style="color:red; padding-top:10px"><img src="/images/ajax-loader.gif" align="absmiddle"> Подождите, идет загрузка...</div>';
}

