function str_replace(search, replace, subject) {
  return subject.split(search).join(replace);
}

function $(id) {
  if (document.getElementById(id)) return document.getElementById(id);
  else return null;
}

function loadSelectInt(min, max, def) {
  document.write('<option>' + def + '</option>');
  for (i=min; i<=max; i++)
    document.write('<option value="' + i + '">' + i + '</option>');
}

function scrollBackground(id, value, inc) {
  speed = 10;
  position = $(id).style.backgroundPosition;
  position = str_replace("px", "", position);
  position = str_replace(" ", "", position);
  if (position == '') position = 0;
  else position /= 10;
  if (inc) position = position + speed;
  else position = position - speed;
  $(id).style.backgroundPosition = position + 'px 0px';
  value = value - speed;
  if (value > 0)
    setTimeout('scrollBackground(\'' + id + '\', ' + value + ', ' + inc + ')', 1);
  else {
    if (inc) $(id).style.backgroundPosition = Math.floor(position / str_replace("px", "", $(id).style.width)) * str_replace("px", "", $(id).style.width) + 'px 0px';
    else $(id).style.backgroundPosition = Math.ceil(position / str_replace("px", "", $(id).style.width)) * str_replace("px", "", $(id).style.width) + 'px 0px';
  }
}

function checkEditField(id, caption) {
  result = true;
  if ($(id)) {
    if (($(id).getAttribute('type') == 'text')||($(id).getAttribute('type') == 'file')||($(id).getAttribute('type') == 'password')) {
      if ($(id).value == '') {
        $(id).focus();
        if ($(id).getAttribute('type') == 'text')
          alert('Текстовое поле "' + caption + '" обязательно для заполнения.');
        if ($(id).getAttribute('type') == 'password')
          alert('Поле ввода пароля "' + caption + '" обязательно для заполнения.');
        if ($(id).getAttribute('type') == 'file')
          alert('Поле "' + caption + '" обязательно для заполнения.');
        result = false;
      }
    }
  }
  return result;
}

function loadNixCSS(filename) {
  if ((navigator.userAgent.indexOf("Linux") != -1)||(navigator.userAgent.indexOf("Unix") != -1))
    document.write('<link href="' + filename + '" rel="stylesheet" type="text/css" />');
}

function checkEditForm(form) {
  result = true;
  elements = form.getElementsByTagName('label');
  for(i=0; i<elements.length; i++) {
    id = elements[i].getAttribute('for');
    required = elements[i].getAttribute('required');
    if (!required) required = 'true';
    if ((required == 'true') && result) result = checkEditField(id, str_replace(':', '', elements[i].innerHTML));
  }
  return result;
}

function previewImage(image) {
  if (!$('previewImage')) document.getElementsByTagName('body')[0].innerHTML += '<div id="previewImage"></div>';
  $('previewImage').innerHTML = '<img src="' + image + '" onclick="hideDiv(\'previewImage\')" />';
  showDiv('previewImage');
  $('previewImage').style.top = document.documentElement.scrollTop + 'px';

}

function initCodemirror() {
  elements = document.getElementsByTagName('textarea');
  for(i=0; i<elements.length; i++) {
    if (elements[i].id != '') {
      CodeMirror.fromTextArea(elements[i].id, {
        height: "350px",
        parserfile: "parsexml.js",
        stylesheet: "js/common/codemirror/css/xmlcolors.css",
        path: "js/common/codemirror/js/"
      });
    }
  }
}

