function displayLayer(id) {
  var obj = document.getElementById(id).style;
  obj.display = (obj.display == 'none') ? 'block' : 'none';
  return false;
}

/**
* Подавливает загрузку документа во фрейме и загружает его в окне
* верхнего уровня. Рекомендуется вызывать в начале загрузки документа.
* @access public
*/
function killFrames(is_confirm) {
  if (top.frames.length > 0) {
    if (is_confirm) {
      if (!confirm('Frames detected.\r\nLoad page in top window?')) {
        return;
      }
    }
    top.document.location.href = document.location.href;
  }
}

/**
* Устанавливает имя заголовку документа окна верхнего уровня (тег <title>).
* Если параметр не передается, то берется текущее имя документа.
* Рекомендуется вызывать в начале загрузки документа.
* @access public
*/
function setTitle(title) {
  if (document.getElementById) { //DOM?
    top.frames.document.title = title ? title : document.title;
  }
}

//ported from PHP trim()
String.prototype.trim = function() {
  return this.replace(/(^\s*)|(\s*$)/g, '');
}

//ported from PHP sprintf()
sprintf=function(s){
  for(var i=1;i<arguments.length;i++)s=s.replace('%s',arguments[i]);
  return s;
}

/*
  Функция возвращает значение параметра из URL адреса.
  Если параметр url не задан, берется текущий URL адрес.

  @param  string  name
  @param  mixed   url (string; object location)
  @return mixed   (boolean false; string value)
  @access public
*/
function getParam(name, url) {
  var get = url ? String(url).replace(/^.*\?/,'').split('&') : location.search.substring(1).split('&');
  for (var i = 0; i < get.length; i++) {
    var p = get[i].split('=');
    if (p[0] == name) {
      return unescape(p[1]);
    }
  }
  return false;
}

/*
  Функция открывает новое окно браузера и возвращает его объект.
  @param   string  id
  @param   string  url
  @param   int     w    ширина
  @param   int     h    высота
  @param   int     l    отступ слева  (если l=0, то автоцентрирование)
  @param   int     t    отступ сверху (если t=0, то автоцентрирование)
  @param   bool    sb   прокрутка
  @param   bool    st   статусная строка
  @param   bool    rz   изменяемый размер
  @param   bool    fl   fullscreen
  @param   string  html
  @return  object window
  @access  public
  @version 3.0.0
*/
function winPopUp(name, url, w, h, l, t, sb, st, rz, fl, html) {
  var y = 'yes', n = 'no';
  var l = l ? l : (screen.width  - w) / 2;
  var t = t ? t : (screen.height - h) / 2;
  //окно уже открыто?
  if (top[name] != null && typeof(top[name]) == 'object' && !top[name].closed) {
    if (top[name].document.location.href != url) {
      top[name].document.location.href = url;
    }
  }
  else {
    top[name] = window.open(url, name, 'toolbar=no,location=no,directories=no,status='+(st?y:n)+',menubar=no,scrollbars='+(sb?y:n)+',left='+l+',top='+t+',resizable='+(rz?y:n)+',width='+w+',height='+h+',fullscreen='+(fl?y:n));
  }
  top[name].resizeTo(w, h);
  top[name].moveTo(l, t);
  if (html) {
    with (top[name].document) {
      open();
      write(html);
      close();
    }
  }
  top[name].focus();
  return top[name];
}

/*
  Перенаправление на страницу декодирования email адреса
  @param   string  с  зашифрованный email адрес
  @return  void
*/
function mailto(c) {
  document.getElementById('mailto').src = 'mailto/' + escape(c);
}


/*
  Подсвечивает текст (слово) в заданном слое
  @param   string  text
  @param   string  className
  @param   string  id         id слоя
  @access  public
  @version 1.0.3
*/
function highlight(text, className, id) {
  var obj = document.getElementById(id)
  if (! obj) {
    window.status = sprintf('HighLight: layer id="%s" does not exists!', id);
    return false;
  }

  if (!text) return;
  function searchWithinNode(node, text, className){
    var skip = 0;

    if (node.nodeType == 3) {  /* 1 - Element node; 3 - Text node. */
      var pos = 0;
      while (pos != -1) {
        pos = node.data.toLowerCase().indexOf(text.toLowerCase(), pos);
        if (pos > -1) {
          var prev_char = (pos > 0) ? node.data.substr(pos - 1, 1) : '';
          var next_char = node.data.substr(pos + text.length, 1);
          if ( (!prev_char || /^([^0-9a-zа-яёЁ][0-9a-zа-яёЁ]+)$/i.test(prev_char+text)) &&
               (!next_char || /^([^0-9a-zа-яёЁ][0-9a-zа-яёЁ]+)$/i.test(next_char+text)) ) {
            var spannode = document.createElement('SPAN');
            spannode.className = className;
            var middlebit = node.splitText(pos);
            var endbit = middlebit.splitText(text.length);
            var middleclone = middlebit.cloneNode(true);
            spannode.appendChild(middleclone);
            middlebit.parentNode.replaceChild(spannode, middlebit);
            skip = 1;
          }
          pos++;
        }
      }//while
    }
    else if (node.nodeType == 1 && node.childNodes && !/(script|style|textarea)/i.test(node.tagName) ) {
      //alert(node.tagName);
      for (var child = 0; child < node.childNodes.length; child++) {
        child += searchWithinNode(node.childNodes[child], text, className);
      }
    }
    return skip;
  }
  searchWithinNode(obj, text, className);
};

