function view_photo (imgSrc, imgWidth, imgHeight, imgTitle) {
  if (!imgWidth || !imgHeight) {
    imgWindow = window.open('', 'image', '');
    var html = '<html><head><title></title></head><body style="margin: 0; padding: 0;">' +
               '<img alt="' + imgTitle + '" src="' + imgSrc + '">' +
               '</body></html>';
    imgWindow.document.write(html);
    return;
  }

  var screenWidth = screen.availWidth;
  var screenHeight = screen.availHeight;
  var titleHeight = 30;
  if (imgTitle) { imgHeight = imgHeight + titleHeight; }
  var windowLeft, windowTop;
  if ((screenWidth - imgWidth) > 0) { windowLeft = (screenWidth - imgWidth) / 2 } else { windowLeft = 0; }
  if ((screenHeight - imgHeight) > 0) { windowTop = (screenHeight - imgHeight) / 2 } else { windowTop = 0; }
  var imgWindow = window.open('', 'image',
                  'width=' + imgWidth + ', height=' + imgHeight +
                  ', left=' + windowLeft + ', top=' + windowTop
  );
  var html = '<html><head><title>' + imgTitle + '</title></head><body style="margin: 0; padding: 0;">' +
             '<img alt="' + imgTitle + '" src="' + imgSrc + '">' +
             '<div style="heigth: ' + titleHeight + 'px; width: 100%; text-align: center; font: 10px Arial;">'+ imgTitle +'</div>' +
             '</body></html>';
  imgWindow.document.write(html);
  imgWindow.document.close();
  return;
}

function load_ajax_news(start_num) {
  if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();
    request.onreadystatechange = show_news(request);
    request.open("GET", '/wz_ajax_news?start=' + start_num + '&random=' + Math.random(), true);
    request.send(null);
  } else if (window.ActiveXObject) {
    request = new ActiveXObject("Microsoft.XMLHTTP");
    request.onreadystatechange = show_news(request);
    request.open("GET", '/wz_ajax_news?start=' + start_num + '&random=' + Math.random(), true);
    request.send();
  }
  
  return false;
}

function show_news(request) {
  return function() {
    if(request.readyState == 4) {
      if(request.status == 200) {
        document.getElementById('ajax-news-block').innerHTML = request.responseText;
      } else {
        document.getElementById('ajax-news-block').innerHTML = '<span class="alert">Ошибка загрузки!</span>';
      }
    }
  }
}

function switch_deputies_form(number) {
  document.getElementById('dep' + sel_dep_form + 'block').style.display = 'none';
  document.getElementById('dep' + sel_dep_form + 'a').style.display = 'inline';
  document.getElementById('dep' + sel_dep_form + 's').style.display = 'none';
  document.getElementById('dep' + sel_dep_form + 'li').className = '';
  
  document.getElementById('dep' + number + 'block').style.display = 'block';
  document.getElementById('dep' + number + 'a').style.display = 'none';
  document.getElementById('dep' + number + 's').style.display = 'inline';
  document.getElementById('dep' + number + 'li').className = 'sel';
  
  sel_dep_form = number;
  
  document.getElementById('footer').style.bottom = '0';
  
  return false;
}

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 get_villages(location_id) {
  if (location_id == '') {
    clear_villages();
  } else {
    load_ajax('/wz_ajax_load?type=village&location_id=' + location_id + '&random=' + Math.random(), load_villages);
  }
}

function get_committees(location_id) {
  if (location_id == '') {
    clear_committees();
  } else {
    load_ajax('/wz_ajax_load?type=committee&location_id=' + location_id + '&random=' + Math.random(), load_committees);
  }
}

function load_villages(request) {
  return function() {
    var row =  document.getElementById('villages_row');
  
    if(request.readyState == 4) {
      if(request.status == 200) {
        //row.innerHTML = request.responseText;
	row.getElementsByTagName('td')[0].innerHTML = 'Поселения';
	row.getElementsByTagName('td')[1].innerHTML = request.responseText;
      } else {
        //row.innerHTML = '<td class="simple"></td><td class="simple"><span class="alert">Ошибка загрузки!</span></td>';
	row.getElementsByTagName('td')[1].innerHTML = '<span class="alert">Ошибка загрузки!</span>';
      }
    }
  }
}

function load_committees(request) {
  return function() {
    var row = document.getElementById('committees_row');
  
    if(request.readyState == 4) {
      if(request.status == 200) {
        //row.innerHTML = request.responseText;
	row.getElementsByTagName('td')[0].innerHTML = 'Комитеты';
	row.getElementsByTagName('td')[1].innerHTML = request.responseText;
      } else {
        //row.innerHTML = '<td class="simple"></td><td class="simple"><span class="alert">Ошибка загрузки!</span></td>';
	row.getElementsByTagName('td')[1].innerHTML = '<span class="alert">Ошибка загрузки!</span>';
      }
    }
  }
}

function clear_villages() {
  var row = document.getElementById('villages_row');
  row.getElementsByTagName('td')[0].innerHTML = '';
  row.getElementsByTagName('td')[1].innerHTML = '';
}

function clear_committees() {
  var row = document.getElementById('committees_row');
  row.getElementsByTagName('td')[0].innerHTML = '';
  row.getElementsByTagName('td')[1].innerHTML = '';
}

function load_ajax(get, func) {
  if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();
    request.onreadystatechange = func(request);
    request.open("GET", get, true);
    request.send(null);
  } else if (window.ActiveXObject) {
    request = new ActiveXObject("Microsoft.XMLHTTP");
    request.onreadystatechange = func(request);
    request.open("GET", get, true);
    request.send();
  }

  return false;
}

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 switch_block (block_id) {
  var block = document.getElementById(block_id);
  if (block.style.display == 'none') { block.style.display = 'block'; }
  else { block.style.display = 'none'; }
}