function forumMsg(id) {
  var forumtextprefix = "ftext";
  var forumtitleprefix = "ftitle";
  var forumaction = "faction";
  var forumeditid = "feditid";
  this.actionfield = window.document.getElementById(forumaction);
  this.editidfield = window.document.getElementById(forumeditid);
  this.title = window.document.getElementById(forumtitleprefix + id);
  this.text = window.document.getElementById(forumtextprefix + id);
  this.titlefield = window.document.getElementById(forumtitleprefix + "edit");
  this.textfield = window.document.getElementById(forumtextprefix + "edit");
  this.valid = (this.title && this.text && this.titlefield && this.textfield);
}

function forumReply(id, nick) {
  var msg = new forumMsg(id);
  if (msg.valid) {
    msg.titlefield.value = "RE: " + String(msg.title.innerText).replace(/\s*re:\s*/i, "");
    msg.textfield.value = nick + " rašė:" + "\n----------\n" + msg.text.innerText + "\n----------\n" + msg.textfield.value;
  }
}

function forumEdit(id) {
  var msg = new forumMsg(id);
  if (msg.valid && msg.actionfield && msg.editidfield) {
    msg.actionfield.value = "UpdateMess";
    msg.editidfield.value = id;
    msg.titlefield.value = msg.title.innerText;
    msg.textfield.value = msg.text.innerText;
  }
}

function forumValidate() {
  var msg = new forumMsg("");
  if (msg.titlefield && trimString(msg.titlefield.value) == "") {
    alert("Pavadinimas yra privalomas");
    msg.titlefield.focus();
    return false;
  }
  if (msg.textfield && trimString(msg.textfield.value) == "") {
    alert("Tekstas yra privalomas");
    msg.textfield.focus();
    return false;
  }
  return true;
}
