function openWindow(url, nm, wt, ht, top, left) {
  if (!wt) wt = 600;
  if (!ht) ht = 600;

  if (!top) top = screen.height ? (screen.height - ht)/2 - 30 : 0;
  if (!left) left = screen.width ? (screen.width - wt)/2 - 30 : 0;

  myWin = window.open(url, nm, "width=" + wt + ",height=" + ht + ",top=" + top + ",left=" + left + ",toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes");
  myWin.window.focus();

/* Эта хрень банально не работает:
   if (document.body.scrollHeight > document.body.clientHeight)
   myWin.resizeTo(document.body.clientHeight+document.body.scrollLeft, document.body.clientWidth);

  if (document.body.scrollWidth > document.body.clientWidth)
    myWin.resizeTo(document.body.clientHeight, document.body.scrollWidth);
*/
}

function bbcode(formname, bbcode, prompttext) {
  inserttext = prompt(tag_prompt + "\n[" + bbcode + "]xxx[/" + bbcode + "]", prompttext);

  if ((inserttext != null) && (inserttext != "")) {
    formname.comment_text.value += "[" + bbcode + "]" + inserttext + "[/" + bbcode + "] ";
    formname.comment_text.focus();
  }
}


function ban_ip (ip, user_name, comment_id) {
  prompt_descr = "Введите комментарий (без пробелов) для этого бана, если хотите.\nНажатие \"Отмена\" отменяет процедуру бана; \"OK\" - подтверждает.";
  prompt_text = "";

  text = prompt(prompt_descr, prompt_text);

  if (text != null) {
    openWindow('/admin/comments.php?action=ban&ban_ip=' + ip + '&text=' + text + '&user_name=' + user_name + '&comment_id=' + comment_id);
  }
}

function addToFav(pageLoc, titleString, tableWidth, inText) {
  favLink = (inText) ? "-&nbsp;&nbsp;to Favorits" : "<img src=img/ico-fav.gif width=19 height=15 alt='to \"Favorits\"' border=0 hspace=4>";
  hpLink = (inText) ? "&nbsp;&nbsp;-&nbsp;&nbsp;сделать стартовой" : "<img src=img/ico-homepage.gif width=16 height=15 alt='Сделать стартовой' border=0 hspace=0>";

  if (document.all) {
    //document.write("<table border=0 align=left width=" + tableWidth + " cellspacing=0 cellpadding=0><tr><td align=right width=" + tableWidth + "><font size=2>");
    document.write("<a href=\"javascript:window.external.addFavorite('" + pageLoc + "','" + titleString + "');\">" + favLink + "</a>");
    //document.write("</font></td></tr><tr><td align=right width=" + tableWidth + "><font size=2>");
    //document.write("<a href=\"" + pageLoc + "\" onclick=\"this.style.behavior='url(#default#homepage)';this.setHomePage('" + pageLoc + "');\">" + hpLink + "</a>");
    //document.write("</font></td></tr></table><br clear=left>");
  }
}

function formSubmit(formName) {
  eval('document.' + formName + '.submit()');
}

function frameResize() {
  width = self.document.body.scrollWidth;
  height = self.document.body.scrollHeight;
  self.resizeTo(width, height + 5);
}

// Сохранить локальный путь добавляемого документа
function saveLastPath(formname) {
  if (formname.last_path.value=formname.d_body.value) return true;
  return false;
}

// Проверить на заполненность поля формы
function checkForm(frm_field, msg) {
  if (frm_field.value == "") {
    alert(msg);
    return false;
  }

  return true;
}

// state - показать(1) / спрятать (0) / переключить (2)
function setDIVDisplay(divId, state) {
  if (state > 1) {
    state = (document.getElementById(divId).style.display == "none") ? ("block") : ("none");
  }
  else {
    state = (state == 1) ? "block" : "none";
  }

  document.getElementById(divId).style.display = state;
}

// parent - элемент, который содержит слои. Используй document для всех слоев.
function setAllDIVDisplay(parent, state) {
  var divcount = parent.getElementsByTagName("div");

  for (var i=0; i < divcount.length; i++) {
    divcount[i].style.display = state ? "block" : "none";
  }
}

function setValue(elementId, value) {
  document.getElementById(elementId).value = value;
}

function setTrValue(elementId, state) {
  if (state > 1) {
    state = (eval('document.' + elementId + '.style.display') == "none") ? ("block") : ("none");
  }
  else
    state = (state == 1) ? "block" : "none";

  eval('document.' + elementId + '.style.display = ' + state);
}

function openImage(imgName, imgTitle, wt, ht, top, left) {
  wscroll = "no";

  if (imgTitle == "::")
    ht_addon =30;
  else
  	//надо расчитывать от длины imgTitle и wt
    ht_addon =80;

    wwt=wt + 40;
    wht=ht + ht_addon;

   if (wht > screen.height - 60)  {
    wht = screen.height - 60;
    wscroll="yes"
  }
 if (wwt > screen.width) {
    wwt=screen.width;
    wscroll="yes"
  }

//  if (!top) top = (screen.height) ? Math.floor((screen.height - ht) / 2) - 30 : 0;
//  if (!left) left = (screen.width) ? Math.floor((screen.width - wt) / 2) - 30 : 0;
  
  if (!top) top = Math.floor((screen.height - wht) / 2) - 40
  if (!left) left = Math.floor((screen.width - wwt) / 2)
  if (top < 0) top = 0;
  if (left < 0) left = 0;

  image=window.open("", "fullImage_" + wt, "width=" + wwt + ",height=" + wht + ",top=" + top + ",left=" + left + ",scrollbars=" + wscroll + ",toolbar=no,location=no,status=no,menubar=no,resizable=yes");
  image.focus();

  // интересная штука: clientHeight не равен размеру, заданному в window.open, а вот clientWidth -- равен
//client_mult = image.document.body.clientHeight / ht;
//  wt = Math.floor(image.document.body.clientWidth * client_mult);
//  ht = image.document.body.clientHeight;

  image.document.open();
  image.document.write("<html>\n<head>\n<title>" + imgTitle + "</title>\n");
  image.document.write("<meta name=\"robots\" content=\"noindex,nofollow\">\n");
  image.document.write("<body>\n\n");
  image.document.write("<center><a href=\"javascript: window.close();\"><img src=\"" + imgName + "\" width=" + wt + " height=" + ht + " border=0 alt=\"Click mouse for closing this window\"></a></center>\n\n");
  if (imgTitle != "::") image.document.write("<p style=\"font-weight: bold; text-align: center;\">" + imgTitle + "</p>\n");
  image.document.write("\n</body></html>");
  image.document.close();
}

function checkExt(ext, str) {
  eval("ext_pattern = /.+\.(" + ext + ")/;");

  if (ext_pattern.test(str)) {
    return true;
  }
  else {
    alert("Файлы этого типа не поддерживаются");
  }

  return false;
}
