var
  currPostBlock = null,
  comment_always = 1;

function togglePostBlock(el) {
  if (el.className == 'disabled' || chkSaveRequestActive()) return;
  if (currPostBlock == null || currPostBlock.id != 'post_block') {
    if (currPostBlock != null) closePostBlock();
    currPostBlock = $('post_block');
    currPostBlock.style.display = 'block';
    startUpdateTimeField('post_moment_text');
    el.saveCaption = el.innerHTML;
    el.innerHTML = 'Скрыть форму';
    currPostBlock.editBtn = el;
    $('post_title').value = '';
    $('post_txt').value = '';
    $('post_submit_btn').disabled = false;
    $('post_title').focus();
    if (blk = $('post_captcha_img')) blk.src='/captcha.html?'+Math.random();

    initTextEditor($('post_txt'),'html');
  } else {
    closePostBlock();
  }
}

var
  currUpdateTimeField = null;

function startUpdateTimeField(id) {
  if (currUpdateTimeField) stopUpdateTimeField();
  currUpdateTimeField = $(id);
  if (currUpdateTimeField) {
    currUpdateTimeField.updateIntervalId = setInterval(updateTimeField, 1000);
    updateTimeField();
  }
}

function updateTimeField() {
  var dt = getCurrentTime('skipSec');
  if (dt != currUpdateTimeField.innerHTML) currUpdateTimeField.innerHTML = dt;
}

function stopUpdateTimeField() {
  if (currUpdateTimeField) {
    if (currUpdateTimeField.updateIntervalId) {
      clearInterval(currUpdateTimeField.updateIntervalId);
      currUpdateTimeField.updateIntervalId = null;
    }
    currUpdateTimeField = null;
  }
}

function closePostBlock() {
  if (currPostBlock != null) {
    stopUpdateTimeField();
    if (currPostBlock.id == 'post_block') {
      $('post_title').value = '';
      $('post_txt').value = '';
      $('post_moment').value = '';
      $('post_moment_off_block').style.display = 'block';
      $('post_moment_on_block').style.display = 'none';
      removeTextEditor('post_txt');
    }
    else
      removeTextEditor(findSubChild(currPostBlock,'TEXTAREA','post_txt').id);

    if (currPostBlock.id == editBlockLoadingId) {
      if (editBlockLoader) editBlockLoader.cancel(editBlockLoadingId);
      editBlockLoadingId = '';
    }
    if (currPostBlock.okBtn) currPostBlock.okBtn = null;
    if (currPostBlock.editBtn) {
      if (currPostBlock.editBtn.className == 'disabled') {
        currPostBlock.editBtn.className = currPostBlock.editBtn.saveClass;
      }
      if (currPostBlock.editBtn.saveCaption) {
        currPostBlock.editBtn.innerHTML = currPostBlock.editBtn.saveCaption;
      }
      currPostBlock.editBtn = null;
    }
    if (currPostBlock.editBlock) {
      currPostBlock.removeChild(currPostBlock.editBlock);
      currPostBlock.editBlock = null;
    }
    if (currPostBlock.postBlock) {
      currPostBlock.postBlock.style.display = 'block';
      currPostBlock.postBlock = null;
    }
    if (currPostBlock.momentBlock) {
      currPostBlock.momentBlock.style.display = '';
      currPostBlock.momentBlock = null;
    }

    if (currPostBlock.editNoteId) currPostBlock.editNoteId = null;
    if (currPostBlock.commentId) currPostBlock.commentId = null;
    currPostBlock.style.display = 'none';
    currPostBlock = null;
  }
}


function toggleEditBlock_bak(el, noteId, commentId) {
  if (el.className == 'disabled' || chkSaveRequestActive()) return;
  var id = noteId + (commentId ? '_'+commentId : '');
  if (currPostBlock == null || currPostBlock.editNoteId != id) {
    if (currPostBlock != null) closePostBlock();
    var container = $('post_container_'+id);
    container.editNoteId = id;
    var editBlock = $('post_block').cloneNode(true);
    editBlock.style.display = 'block';
    fixEditBlockIds(editBlock, id);
    container.appendChild(editBlock);
    container.editBlock = editBlock;
    if (commentId) $('post_add_'+id).value = '1';
    var form = $('post_form_'+id)
    var inp = document.createElement('INPUT');
    inp.type = 'hidden';
    inp.name = (commentId ? 'comment_id' : 'note_id');
    inp.value = (commentId ? commentId : noteId);
    form.appendChild(inp);
//  $('post_moment_label_'+id).style.display = 'none';
//  $('post_title_label_'+id).style.display = 'none';
//  $('post_txt_label_'+id).style.display = 'none';
//  if (fld = $('post_keywords_label_'+id)) fld.style.display = 'none';
//  if (fld = $('post_txt_short_label_'+id)) fld.style.display = 'none';
    $('post_submit_btn_'+id).value = 'Сохранить';
    $('post_title_'+id).value = $('title_'+id).innerHTML;
    $('post_txt_'+id).value = br2eol($('txt_'+id).innerHTML);
    var fld;
    if (commentId) {
      if (fld = $('post_moment_block_'+id)) fld.style.display = 'none';
      if (fld = $('post_keywords_block_'+id)) fld.style.display = 'none';
      if (fld = $('post_txt_short_block_'+id)) fld.style.display = 'none';
    } else {
      fld = $('moment_'+id);
      fld.style.display = 'none';
      container.momentBlock = fld;
      $('post_moment_text_'+id).innerHTML = fld.innerHTML;
      if ((inp = $('post_keywords_'+id)) && (fld = $('keywords_'+id))) 
        inp.value = br2eol(fld.innerHTML);
      if ((inp = $('post_txt_short_'+id)) && (fld = $('txt_short_'+id))) 
        inp.value = br2eol(fld.innerHTML);
    }
    el.saveCaption = el.innerHTML;
    el.innerHTML = 'Отменить';
    container.editBtn = el;
    container.postBlock = $('post_'+id);
    container.postBlock.style.display = 'none';
    container.style.display = 'block';
    currPostBlock = container;
    setTimeout('$("post_title_'+id+'").focus()', 0);
  } else {
    closePostBlock();
  }
}

var
  editBlockLoader = null,
  editBlockLoadingId = '';
  editBlockLoadingType = '';

function toggleEditBlock(el, noteId, commentId, forumId) {
  if (el.className == 'disabled' || chkSaveRequestActive()) return;
  var id = noteId + (commentId ? '_'+commentId : '');
  if (currPostBlock == null || currPostBlock.editNoteId != id) {
    if (currPostBlock != null) closePostBlock();
    var container = $('post_container_'+id);
    container.editNoteId = id;
    var editBlock = $('post_block').cloneNode(true);
    editBlock.style.display = 'block';
    fixEditBlockIds(editBlock, id);
    container.appendChild(editBlock);
    container.editBlock = editBlock;
    if (commentId) $('post_add_'+id).value = '1';
    var form = $('post_form_'+id)
    var inp = document.createElement('INPUT');
    inp.type = 'hidden';
    inp.name = (commentId ? 'comment_id' : 'note_id');
    inp.value = (commentId ? commentId : noteId);
    form.appendChild(inp);
//  $('post_moment_label_'+id).style.display = 'none';
//  $('post_title_label_'+id).style.display = 'none';
//  $('post_txt_label_'+id).style.display = 'none';
//  if (fld = $('post_keywords_label_'+id)) fld.style.display = 'none';
//  if (fld = $('post_txt_short_label_'+id)) fld.style.display = 'none';
    $('post_submit_btn_'+id).value = 'Сохранить';
//  $('post_title_'+id).value = $('title_'+id).innerHTML;  <--
//  $('post_txt_'+id).value = br2eol($('txt_'+id).innerHTML); <--
    var fld;
    if (commentId) {
//    if (fld = $('post_moment_block_'+id)) fld.style.display = 'none';
//    if (fld = $('post_keywords_block_'+id)) fld.style.display = 'none';
//    if (fld = $('post_txt_short_block_'+id)) fld.style.display = 'none';
    } else {
//    container.momentBlock = $('moment_'+id);
//    $('post_moment_text_'+id).innerHTML = container.momentBlock.innerHTML;
//    if ((inp = $('post_keywords_'+id)) && (fld = $('keywords_'+id))) 
//      inp.value = br2eol(fld.innerHTML);  <--
//    if ((inp = $('post_txt_short_'+id)) && (fld = $('txt_short_'+id))) 
//      inp.value = br2eol(fld.innerHTML);  <--
    }
    el.saveClass = el.className || '';
    el.className = 'disabled';
//  el.saveCaption = el.innerHTML;
//  el.innerHTML = 'Отменить';
    container.editBtn = el;
    container.postBlock = $('post_'+id);
//  container.postBlock.style.display = 'none';
//  container.style.display = 'block';
    currPostBlock = container;
//  setTimeout('$("post_title_'+id+'").focus()', 0);

    if (editBlockLoader == null) {
      editBlockLoader = new RemoteFileLoader('editBlockLoader');
      editBlockLoader.showRequestIndicator = true;
      editBlockLoader.onCopyContent = editBlockLoaded;
    }
    var url = '/blog.html?edit='+$('post_add').value +
              '&'+(commentId ? 'comment_id='+commentId : 'note_id='+noteId) +
              (typeof(forumId)!='undefined' ? '&forum_id='+forumId : '');
    editBlockLoader.loadInto(url, id);
    editBlockLoadingId = id;
    editBlockLoadingType = (commentId ? 'comment' : 'note');
  } else {
    closePostBlock();
  }
}

function cntEOL(s) {
  var n = 0, r = 0, a = 0;
  for (var i = 0; i < s.length; i++) {
    if (s.charCodeAt(i) == 13) n++;
    else if (s.charCodeAt(i) == 10) n++;
    else if (s.charCodeAt(i) == 32) a++;
  }
  return (n + '/' + r + '/' + a);
}

function editBlockLoaded(doc, text, id) {
  if (editBlockLoadingId != id) return;
  editBlockLoadingId = '';
  if (text == null || currPostBlock == null) return;
  var src = document.createElement('DIV');
  src.innerHTML = text;
  var prm = $_('edit_blog_error', src);
  if (prm) {
    alert('Ошибка: '+prm.innerHTML);
    closePostBlock();
    return true;
  }
  if (prm = $_('edit_blog_header', src)) $('post_title_'+id).value = unescapeHTML(prm.innerHTML);
  if (prm = $_('edit_blog_txt', src))    $('post_txt_'+id).value = unescapeHTML(prm.innerHTML);
  var fld, inp;
  if (id.indexOf('_') < 0) {  // not a comments
    if ((inp = $('post_keywords_'+id)) 
    &&  (prm = $_('edit_blog_keywords', src))) inp.value = unescapeHTML(prm.innerHTML);
    if ((inp = $('post_txt_short_'+id))
    &&  (prm = $_('edit_blog_txt_short', src))) inp.value = unescapeHTML(prm.innerHTML);
    currPostBlock.momentBlock = fld = $('moment_'+id);
    fld.style.display = 'none';
    fld = $('post_moment_text_'+id);
    if (prm = $_('edit_blog_moment', src)) setBlockUnixTime(fld, prm.innerHTML)
    else if(fld) fld.innerHTML = currPostBlock.momentBlock.innerHTML;
  } else {
    if (fld = $('post_moment_block_'+id)) fld.style.display = 'none';
    if (fld = $('post_keywords_block_'+id)) fld.style.display = 'none';
    if (fld = $('post_txt_short_block_'+id)) fld.style.display = 'none';
  }

  var el = currPostBlock.editBtn;
  el.className = el.saveClass;
  el.saveCaption = el.innerHTML;
  el.innerHTML = 'Отменить';

  currPostBlock.postBlock.style.display = 'none';
  currPostBlock.style.display = 'block';
  setTimeout('$("post_title_'+id+'").focus()', 0);
  initTextEditor($('post_txt_'+id),(editBlockLoadingType == 'comment' ? 'html-light' :'html'));

  return true;
}

function toggleCommentBlock(el, noteId, commentId) {
  if (userLogin == '' && comment_always) {
    loginSignupUser();
    return;
  }
  if (el.className == 'disabled' || chkSaveRequestActive()) return;
  var id = noteId + (commentId ? '_'+commentId : '');
  if (currPostBlock == null || currPostBlock.commentId != id) {
    if (currPostBlock != null) closePostBlock();
    var container = $('comment_container_'+id);
    container.commentId = id;
    var editBlock = $('post_block').cloneNode(true);
    editBlock.style.display = 'block';
    fixEditBlockIds(editBlock, id);
    container.appendChild(editBlock);
    container.editBlock = editBlock;
    $('post_add_'+id).value = '1';
    var blk;
    if (blk = $('post_keywords_block_'+id)) blk.style.display = 'none';
    if (blk = $('post_keywords_'+id)) blk.parentNode.removeChild(blk);
    if (blk = $('post_txt_short_block_'+id)) blk.style.display = 'none';
    if (blk = $('post_txt_short_'+id)) blk.parentNode.removeChild(blk);
    if (blk = $('post_captcha_img_'+id)) blk.src='/captcha.html?'+Math.random();
    $('post_moment_block_'+id).style.display = 'none';
    var form = $('post_form_'+id)
    var inp = document.createElement('INPUT');
    inp.type = 'hidden';
    inp.name = 're_note_id';
    inp.value = noteId;
    form.appendChild(inp);
    if (commentId) {
      inp = document.createElement('INPUT');
      inp.type = 'hidden';
      inp.name = 're_comment_id';
      inp.value = commentId;
      form.appendChild(inp);
    }
    el.saveCaption = el.innerHTML;
    el.innerHTML = 'Скрыть форму';
    container.editBtn = el;
    container.style.display = 'block';
    currPostBlock = container;
    setTimeout('$("post_title_'+id+'").focus()', 0);
    initTextEditor($('post_txt_'+id),'html-light');
  } else {
    closePostBlock();
  }
}

var
  editBlockIds = [
    'note_id',
    'post_block',
    'post_form',
    'post_add',
    'post_moment_block',
    'post_moment_off_block',
    'post_moment_text',
    'post_moment_on_block',
    'post_moment_label',
    'post_moment',
    'post_moment_error',
    'post_title_label',
    'post_title',
    'post_title_error',
    'post_keywords_block',
    'post_keywords_label',
    'post_keywords',
    'post_keywords_error',
    'post_txt_short_block',
    'post_txt_short_label',
    'post_txt_short',
    'post_txt_short_error',
    'post_txt_label',
    'post_txt',
    'post_txt_error',
    'post_submit_btn',
    'post_error',
    'post_captcha_img',
    'post_captcha',
    'post_captcha_label',
    'post_captcha_error'
  ];

function fixEditBlockIds(block, noteId) {
  var e;
  for (var i = 0; i < editBlockIds.length; i++) {
    e = $_(editBlockIds[i], block);
    if (e) e.id = editBlockIds[i]+'_'+noteId;
  }
  if (e = $_('post_moment_label_'+noteId, block))    e.htmlFor = 'post_moment_'+noteId;
  if (e = $_('post_title_label_'+noteId, block))     e.htmlFor = 'post_title_'+noteId;
  if (e = $_('post_keywords_label_'+noteId, block))  e.htmlFor = 'post_keywords_'+noteId;
  if (e = $_('post_txt_short_label_'+noteId, block)) e.htmlFor = 'post_txt_short_'+noteId;
  if (e = $_('post_txt_label_'+noteId, block))       e.htmlFor = 'post_txt_'+noteId;
  if (e = $_('post_captcha_label_'+noteId, block))   e.htmlFor = 'post_captcha_'+noteId;
  if (e = $_('post_submit_btn_'+noteId, block)) {
//  e.disabled = true;
    e.disabled = false;
  }
  if (e = $_('post_title_'+noteId, block)) {
    e.value = '';
  }

}

function br2eol(s) {
  return s.replace(/<br\s?\/?>/ig, '\n');
}

function unescapeHTML(s) {
  return br2eol(s).replace(/&lt;/g,  '<').
                   replace(/&gt;/g,  '>').
                   replace(/&amp;/g, '&');
}

var
  saveRequestSender = null,
  saveRequestActive = false;

function chkSaveRequestActive() {
  if (saveRequestActive)
    alert('Данные отправляются.\nПожалуйста, дождитесь ответа сервера.');
  return saveRequestActive;
}

function submitPostForm(form, event) {
  var ta=form.txt;
  if (ta.style.display=='none')
     {
     var text = getEditorContent(ta.id);
     ta.value=text;
     }
  var el, noteId = (form.id.match(/^post_form/) ? RegExp.rightContext : '');
  if (el = $('post_error'+noteId)) el.innerHTML = '';
  if (el = $('post_title_error'+noteId)) el.innerHTML = '';
  if (el = $('post_txt_error'+noteId)) el.innerHTML = '';
  if (el = $('post_moment_error'+noteId)) el.innerHTML = '';
  if (el = $('post_captcha_error'+noteId)) el.innerHTML = '';
  if (el = $('post_error'+noteId)) el.innerHTML = '';
  if (el = $('post_captcha_img'+noteId)) {el.src='/i/loader.gif';el.alt='Защита';}
  el = $('post_moment_block'+noteId);
  var time = '';
  if (el.offsetHeight != 0) {  // is not comments
    el = $('post_moment'+noteId);
    if (el.offsetHeight > 0) {
     time = trim(el.value)
      if (time != '') {
        time = dateTimeToUnixTime(time);
        if (typeof(time) != 'number') {
          alert('Ошибка: '+time);
          cancelEvent(event, true);
          el.focus();
          return false;
        }
      }
    } else {
      var em = $('moment_ut'+noteId);
      time = (em ? em.innerHTML : '');
    }
  }
  if (time == '') time = getCurrentUnixTime();
  el = $('post_moment_input'+noteId);
  if (el == null) {
    el = document.createElement('INPUT');
    el.id = 'post_moment_input'+noteId;
    el.name = 'moment';
    el.type = 'hidden';
    form.appendChild(el);
  }
  el.value = time;
  var okBtn = findSubChild(form, 'INPUT', 'submit');
  if (okBtn) okBtn.disabled = true;
  currPostBlock.okBtn = okBtn;
  currPostBlock.editBtn.saveClass = currPostBlock.editBtn.className;
  currPostBlock.editBtn.className = 'disabled';
  if (saveRequestSender == null) {
    saveRequestSender = new RemoteFileLoader('saveRequestSender');
    saveRequestSender.showRequestIndicator = true;
    saveRequestSender.onCopyContent = saveRequestSent;
  }
  saveRequestActive = true;
  saveRequestSender.submitInto(form, form.id, event);
}

// return 'http://<search>.flexum.ru[:post]'
function getHostName() {
  return location.href.match(/^(\S*:\/\/.*?)\//)[1];
}

function saveRequestSent(doc, text, id) {
  saveRequestActive = false;
  if (text.match(/ id="note_save_result">Ok</i)) {
    if (isSingleNote) {
      var s = getHostName() + blogNoteURL;
      if (s != location.href) { 
        location.replace(s);
      } else {
        location.reload(false);
      }
    } else {
      location.reload(false);
    }
  } else {
    if (!text.match(/ id="errors_all">(.*?)</i)) {
      alert('Ошибка сохранения записи.\n'+trimHTML(text));
    } else {
      var errorsAll =      (text.match(/ id="errors_all">(.*?)</i) ? RegExp.$1 : '');
      var errorsMoment =   (text.match(/ id="errors_moment">(.*?)</i) ? RegExp.$1 : '');
      var errorsTitle =    (text.match(/ id="errors_title">(.*?)</i) ? RegExp.$1 : '');
      var errorsKeywords = (text.match(/ id="errors_keywords">(.*?)</i) ? RegExp.$1 : '');
      var errorsTxtShort = (text.match(/ id="errors_txt_short">(.*?)</i) ? RegExp.$1 : '');
      var errorsTxt =      (text.match(/ id="errors_txt">(.*?)</i) ? RegExp.$1 : '');
      var errorsCaptcha =  (text.match(/ id="errors_captcha">(.*?)</i) ? RegExp.$1 : '');
      var el, noteId = (id.match(/^post_form/) ? RegExp.rightContext : '');
      if (errorsAll      && (el = $('post_error'+noteId))) el.innerHTML = errorsAll;
      if (errorsMoment   && (el = $('post_moment_error'+noteId))) el.innerHTML = errorsMoment;
      if (errorsTitle    && (el = $('post_title_error'+noteId))) el.innerHTML = errorsTitle;
      if (errorsKeywords && (el = $('post_keywords_error'+noteId))) el.innerHTML = errorsKeywords;
      if (errorsTxtShort && (el = $('post_txt_short_error'+noteId))) el.innerHTML = errorsTxtShort;
      if (errorsTxt      && (el = $('post_txt_error'+noteId))) el.innerHTML = errorsTxt;
      if (errorsCaptcha  && (el = $('post_captcha_error'+noteId))) el.innerHTML = errorsCaptcha;
    }
    if (el = $('post_captcha_img'+noteId)) {el.src='/captcha.html?'+Math.random();el.alt='Защита';}
    if (el = $('post_captcha'+noteId)) el.value='';
    if (currPostBlock) {
      if (currPostBlock.okBtn) currPostBlock.okBtn.disabled = false;
      if (currPostBlock.editBtn) with (currPostBlock.editBtn) className = saveClass;
    }
  }
  return true;
}

var
  removeRequestSender = null;

function requestRemoveNote(el, action, noteId, commentId, forumId) {
  if (el.className == 'disabled') return;
  var msg = (action == 'remove_comment' ? 'этот комментарий?' :
            (action == 'remove_article' ? 'эту статью?' :
                                          'эту запись?'));
  if (!confirm('Вы действительно хотите удалить '+msg)) return;
  disableButtons(el.parentNode);
  var id = noteId + (commentId ? '_'+commentId : '');
  if (currPostBlock != null && (currPostBlock.editNoteId == id || currPostBlock.commentId == id)) {
    closePostBlock();
  }
  var reqId = action+'_'+id;
  if (removeRequestSender == null) {
    removeRequestSender = new RemoteFileLoader('removeRequestSender');
    removeRequestSender.showRequestIndicator = true;
    removeRequestSender.onCopyContent = removeRequestSent;
  }
  removeRequestSender.loadInto('/blog.html?'+action+'='+(commentId || noteId) +
                               (forumId ? '&forum_id='+forumId : ''), action+'_'+id);
}

function removeRequestSent(doc, text, id) {
  var isComment = (id.match(/^remove_comment_/));
  if (text.match(/ id="note_remove_result">Ok</i)) {
    var s = getHostName() + (isComment ? blogNoteURL : blogRootURL);
    if (s != location.href) { 
      location.replace(s);
    } else {
      location.reload(false);
    }
  } else {
    var msg = (text.match(/ id="errortxt">(.*?)</i) ? '\n'+RegExp.$1 : '');
    alert('Ошибка удаления записи.'+msg);
    var noteId = (id.match(/(_\d.*)/) ? RegExp.$1 : '');
    if (noteId) {
      var btn = $((isComment ? 'comment_btns' : 'note_btns') + noteId);
      if (btn) enableButtons(btn);
    }
  }
  return true;
}

function disableButtons(container) {
  var el = container.firstChild;
  while (el) {
    if (el.tagName && (el.tagName == 'SPAN' || el.tagName == 'A')) {
      el.saveClass = el.className || '';
      el.className = 'disabled';
    }
    el = el.nextSibling;
  }
}

function enableButtons(container) {
  var el = container.firstChild;
  while (el) {
    if (el.tagName && (el.tagName == 'SPAN' || el.tagName == 'A')) {
      el.className = el.saveClass;
      el.saveClass = null;
    }
    el = el.nextSibling;
  }
}

function switchMomentBlock(show) {
  show = (show == 'show');
  var id = (currPostBlock.id.match(/(_\d+)/) ? RegExp.$1 : '');
  $('post_moment_off_block'+id).style.display = (show ? 'none' : 'block');
  $('post_moment_on_block'+id).style.display = (show ? 'block' : 'none');
  if (show) {
    var f = $('post_moment'+id);
    var h = (id ? $('moment_ut'+id) : null);
    f.value = (h ? UnixTimeToDateTimeStr(h.innerHTML, false, true) : getCurrentTime());
    f.focus();
  }
}

function getCurrentTime(skipSec) {
  var dt = new Date();
  var yy = yearToYYStr(dt.getFullYear());
  var mm = addLeadingZero(dt.getMonth()+1);
  var dd = addLeadingZero(dt.getDate());
  var hh = addLeadingZero(dt.getHours());
  var nn = addLeadingZero(dt.getMinutes());
  var ss = addLeadingZero(dt.getSeconds());
  return (dd+'.'+mm+'.'+yy+' '+hh+':'+nn+(skipSec ? '' : ':'+ss));
}

function getCurrentUnixTime() {
  return (Math.round(Date.parse(new Date())/1000));
}

/*function noteTimeToMomentFormat(s) {
  if (s.match(/^(\d{2})\.(\d{2})\.(\d{4}) (\d{2}):(\d{2})$/))
    with (RegExp) s = $3+'-'+$2+'-'+$1+' '+$4+':'+$5;
  return s;
}*/

function dateTimeToUnixTime(s) {
  s = trim(s);
  if (s.match(/^(\d{1,2})\.(\d{1,2})\.(\d{1,4})\s+(\d{1,2}):(\d{1,2})(|:(\d{1,2}))$/)) {
    var dd = RegExp.$1*1;
    var mm = RegExp.$2*1;
    var yy = RegExp.$3*1;
    if (yy < 100) yy += 2000;
    var hh = RegExp.$4*1;
    var nn = RegExp.$5*1;
    var ss = RegExp.$7*1 || 0;
//  alert('dateTimeToUnixTime('+s+'): '+yy+'-'+mm+'-'+dd+' '+hh+':'+nn+':'+ss);
    var d = new Date(yy, mm-1, dd, hh, nn, ss);
    if (d.getDate() != dd) return 'getDate() = '+d.getDate()+', дд = '+dd;
    if (d.getMonth()+1 != mm) return 'getMonth()+1 = '+(d.getMonth()+1)+', мм = '+mm;
    if (d.getFullYear() != yy) return 'getFullYear() = '+d.getFullYear()+', гг = '+yy;
    if (d.getHours() != hh) return 'getHours() = '+d.getHours()+', чч = '+hh;
    if (d.getMinutes() != nn) return 'getMinutes() = '+d.getMinutes()+', мм = '+nn;
    if (d.getSeconds() != ss) return 'getSeconds() = '+d.getSeconds()+', cc = '+ss;
    return Math.round(Date.parse(d)/1000);
  } else {
    return 'Поле даты должно иметь формат дд.мм.гг чч:мм:сс';
  }
}

function getCookieValue(name) {
  name = name.toLowerCase();
  var cookies = document.cookie.split(';');
  for (var i = 0; i < cookies.length; i++) {
    if (cookies[i].match(/^\s?(.*?)\=(.*)/)) {
      var n = RegExp.$1.toLowerCase();
      if (n == name) return RegExp.$2;
    }
  }
  return null;
}

function setCookieValue(name, value) {
  var d = new Date();
  d.setFullYear(d.getFullYear()+10);
  var s = name+'='+value + '; path=/' +
    (flexumHost ? '; domain=.'+flexumHost : '') + '; expires='+d.toUTCString();
  document.cookie = s;
}

function getBlogFlag() {
  var v = getCookieValue('blog_flag');
  if (v == null) return false;
  v = parseInt(v);
  return (isNaN(v) ? null : v);
}

function setBlogFlag(value) {
  var oldVal = parseInt(getCookieValue('blog_flag')) || 0;
  var newVal = (value ? (oldVal & (0xFF-blogCookieMask)) : (oldVal | blogCookieMask));
  if (oldVal != newVal) setCookieValue('blog_flag', newVal);
}

// false - show (toggle) / true - hide
function isCookieHelpFlagSet() {
  var v = getBlogFlag();
  if (!v) return false;
  var res = ((v & blogCookieMask) != 0);
  return res;
}

function toggleHelpBlock(dontSetCookie) {
  var helpBlock = document.getElementById('help_block');
  if (!helpBlock) return;
  var flag = (helpBlock.offsetHeight == 0);
  helpBlock.style.display = (flag ? 'block' : 'none');
  var helpBtn = document.getElementById('help_btn');
  if (helpBtn) helpBtn.style.display = (flag ? 'none' : '');
  if (dontSetCookie) return;
  setBlogFlag(flag);
}
