function prepare_document () {
  $("ul.submenu").each(function() {
    var sel = 0;
    for (var i in locations) {
      if (this.id == "sm" + locations[i]) { sel = 1; }
    }
    if (!sel) { $(this).hide(); }
  
  
    //if(this.id != "sm" + this_rubric && this.id != "sm" + parent_rubric) { $(this).hide() }
  });
  
  $("[@id^=menu]").click(function() {
    $("[@id^=sm" + this.id.replace('menu', '') + "]").toggle();
    $("[@id^=li" + this.id.replace('menu', '') + "]").toggleClass("arrow");
    $(this).toggleClass("arrow");
    return false;
  });

  $(".footer").css('bottom', 0);
  
  $("input.input-datepicker").datepicker({
    inline: true,
    closeText: 'Закрыть',
	prevText: '&#x3c;Пред',
	nextText: 'След&#x3e;',
	currentText: 'Сегодня',
	monthNames: ['Январь','Февраль','Март','Апрель','Май','Июнь', 'Июль','Август','Сентябрь','Октябрь','Ноябрь','Декабрь'],
	monthNamesShort: ['Янв','Фев','Мар','Апр','Май','Июн', 'Июл','Авг','Сен','Окт','Ноя','Дек'],
	dayNames: ['воскресенье','понедельник','вторник','среда','четверг','пятница','суббота'],
	dayNamesShort: ['вск','пнд','втр','срд','чтв','птн','сбт'],
	dayNamesMin: ['Вс','Пн','Вт','Ср','Чт','Пт','Сб'],
	dateFormat: 'dd.mm.yy', 
	firstDay: 1,
	isRTL: false,
	yearRange: '1950:2020',
	buttonText: 'календарь',
	//buttonImage: '/img/icons/calendar.png',
	//buttonImageOnly: 'true',
	showOn: 'both'
  });
}

function select_date(link) {
  document.location = '/' + link + '01.' +
  document.calendar.month.options[document.calendar.month.options.selectedIndex].value + '.' +
  document.calendar.year.options[document.calendar.year.options.selectedIndex].value + '/';
}

function check_feedback_form() {
  var name     = document.getElementById('name').value;
  var index    = document.getElementById('age').value;
  var email    = document.getElementById('email').value;
  var question = document.getElementById('question').value;

  if (name && index && question && email) {
    var people_dop = document.getElementById('people_dop').value;
    if (people_dop) return true;
    var people = document.getElementById('people').options.selectedIndex;
    if (people) return true;
  }
  alert('Заполните, пожалуйста, все обязательные поля формы');
  return false;
}

function switch_submenu(id) {
  if (submenu_showed) { if (submenu_showed != id) {
    var ss = document.getElementById('sub' + submenu_showed);
    var ss_image = document.getElementById('isub' + submenu_showed);
    if (ss && ss_image) {
      ss.style.display = 'none';
      ss_image.src = ss_image.src.replace('arrow_sel', 'arrow');
      document.getElementById('bsub' + submenu_showed).className = 'bullet';
    }
  } }

  var item = document.getElementById('psub' + id);
  var submenu = document.getElementById('sub' + id);
  var image = document.getElementById('isub' + id);
  var bullet = document.getElementById('bsub' + id);

  if (submenu.style.display == 'block') {
    submenu.style.display = 'none';
    bullet.className = 'bullet';
    image.src = image.src.replace('arrow_sel', 'arrow');
    submenu_showed = false;
  } else {
    submenu.style.width = (item.offsetWidth - 22) + 'px';
    submenu.style.display = 'block';
    bullet.className = 'bulletsel';
    image.src = image.src.replace('arrow', 'arrow_sel');
    submenu_showed = id;
  }
  
  return false;
}

var subactive;
function subclose() {
        document.getElementById('submenu_' + subactive).style.display='none';
        arrow = document.getElementById('submenu_pic_' + subactive);
        if (arrow) {
          arrow.src = arrow.src.replace(/red.gif$/,'.gif');
        }
//        document.getElementById(subactiveid).style.background='02689C'
        document.body.onclick=null;
}

function subviewer(pressed){
        pr = 'submenu_' + pressed;
        id = 'submenu_top_' + pressed;
        var item=document.getElementById(id);
        var l=item.offsetLeft - 18;
        var t=item.offsetTop + 20;
//        var IE = navigator.appName.match(/(Microsoft Internet Explorer)/gi);
//        if (IE) {t += 0}
        while (item.offsetParent != null) {
                item = item.offsetParent;
                l+=item.offsetLeft;
                t+=item.offsetTop;
        }
        var pressedo = document.getElementById(pr);

        pressedo.style.left = l + 'px';
        pressedo.style.top  = t + 'px';
        pressedo.style.display='block';
        arrow = document.getElementById('submenu_pic_' + pressed);
        if (arrow) {
          arrow.src = arrow.src.replace(/.gif$/,'red.gif');
        }
        subactive=pressed;
        document.body.onclick=subclose;
}

function change_mo(link) {
  if(link != '-') {
    if (link == link.replace(/^http:\/\//,'')) {
      document.location=link;
    } else {
      window.open(link,'','status=1,toolbar=1,menubar=1,directories=1,resizable=1,location=1, scrollbars=1');
    }
  }
}


function viewImage(imgSrc,width,height,title) {
  if (!width || !height) {
    pic_window = window.open('', 'image', '');

    var str = '<html><head><title></title></head><body style="margin: 0; padding: 0;"><table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%"><tr><td align="center" valign="middle">' +
              '<img alt="' + title + '" src="' + imgSrc + '">' +
              '</td></tr></table></body></html>';
    pic_window.document.write(str);

    return;
  }

  var sWidth = screen.availWidth;
  var sHeight = screen.availHeight;

  var h = 30;
  if (title) {height = height + h;}
  var pic_window = window.open('','', 'width=' + width + ', height=' + height + ',left='+(sWidth/2-350)+',top='+(sHeight/2-500));
  var str = '<html><head><title>' + title + '</title></head><body style="margin: 0; padding: 0;">' +
            '<img alt="' + title + '" src="' + imgSrc + '">' +
            '<div style="heigth: ' + h + '; width: 100%; text-align: center; font: 10px Arial;">'+ title +'</div>' +
            '</body></html>';
  pic_window.document.write(str);
}

function resetForm(name) {
  var cleared_form = document[name];

  var i = 0;
  while (i < cleared_form.elements.length){
    var inp = cleared_form.elements[i];
    if (inp.type != 'button' && inp.type != 'submit' && inp.type != 'hidden')
      cleared_form.elements[i].value = '';
    if (inp.type == 'checkbox') inp.checked = false;
    i = i + 1;
  }
}

function change_img(img_id,old_img,new_img) {
 src=document.getElementById(img_id).src;
 eval("src = src.replace(/" + old_img + "/,new_img)");
 document.getElementById(img_id).src = src;
}