function popup(name,w,h)
{
  if(!w) w = 400;
  if(!h) h = 300;
  win=window.open('blank.html',name,'width='+w+', height='+h+', scrollbars=auto, menubar=0, statusbar=0');
  win.focus();
}

// Функции JS, используемые Чайкой

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=\"щелкните, чтобы закрыть окно\"></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 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 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;
}

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 code_email(login, server, domain) {
  eml = login + "@" + server + "." + domain;
  return eml;
}

function code_mailto(login, server, domain, subj) {
  eml = "mailto:" + code_email(login, server, domain);
  if (subj) eml += subj;

  window.location.href = eml;
}

// state - показать(1) / спрятать (0) / переключить (2)
function setDIVDisplay(divId, state) {
  state1 = false; state2 = false;

  display = document.getElementById(divId).style.display;
  vis = document.getElementById(divId).style.visibility;

  if(state === false) state = 2;

  if (state == 2) {
    if (display != "none")
      state1 = "none";
    else
      state1 = "block";

    if (vis) {
      if (vis != "hidden")
        state2 = "hidden";
      else
        state2 = "visible";
    }
  }
  else {
    state1 = (state == 1) ? "block" : "none";
    state2 = (state == 1) ? "visible" : "hidden";
  }

  document.getElementById(divId).style.display = state1;

  if (vis)
    document.getElementById(divId).style.visibility = state2;
}

// 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";
    divcount[i].style.visibility = state ? "visible" : "hidden";
 }
}

// отменяет вызов функции родительского элемента
function cancelBubble() {
  if (typeof(event.stopPropagation) == 'function') event.stopPropagation();  // Mozilla
  if (typeof(event.cancelBubble) == 'boolean') event.cancelBubble = true;  // IE & Opera
}

// проверка расширения на соответствие какому-либо типу
function checkExt(ext, str) {
  eval("ext_pattern = /.+\.(" + ext + ")/;");

  if (ext_pattern.test(str)) {
    return true;
  }
  else {
    alert("Недопустимый формат файла");
  }

  return false;
}

function stripPath(flname) {
  slash_idx = flname.lastIndexOf("\\");
  ret = flname.slice(slash_idx + 1);

  return ret;
}
