﻿function get_region_name(subregion_id)
{
	for (var i = 0; i < tbl_subregions.length; i++)
	{	
		if (tbl_subregions[i].sub_region_id == subregion_id)
			return tbl_subregions[i].region_name;
	}
	return "";
}

function get_vis_for_region (polygon_id, subregion_id)
{
	for (var i = 0; i < tbl_polygons.length; i++)
	{
		if (tbl_polygons[i].polygon_id == polygon_id)
			return tbl_polygons[i].meta_polygon || (tbl_polygons[i].sub_region_id == subregion_id);
	}
	return false;
}

function get_select_value(obj)
{
  return obj.options[obj.selectedIndex].value;
}

function update_region_name(sender)
{
	
	update_subregion(get_select_value(sender));
}



function update_subregion(subregion_id)
{

	var placeholder = document.getElementById('region_placeholder');
		

	placeholder.innerHTML = '<strong>Раздел календаря:</strong> ' + get_region_name (subregion_id);
	
	var polygonsElement = document.getElementById('polygon_select');
	
	var selected = polygonsElement.options[polygonsElement.selectedIndex].value;
	
	
	while (polygonsElement.options.length> 0)
	{
		polygonsElement.removeChild(polygonsElement.lastChild);
	}
	
	for (var i = 0; i < tbl_polygons.length; i++)
	{
		if (tbl_polygons[i].meta_polygon || (tbl_polygons[i].sub_region_id == subregion_id))
			{
				var id = tbl_polygons[i].polygon_id;
				var opt = document.createElement('option');
				opt.setAttribute('value', id);
				if (id == selected)
				{
					opt.setAttribute('selected', 'selected');
				}
				opt.innerHTML = tbl_polygons[i].polygon_name;
				polygonsElement.appendChild(opt);
			}
	}
}

function get_time_value(time_prefix)
{
    var day = document.getElementById(time_prefix + '_day').value;
  var month = get_select_value(document.getElementById(time_prefix + '_month'));
  var year = get_select_value(document.getElementById(time_prefix + '_year'));
  var date = new Date();
  date.setFullYear(year, parseInt(month) - 1, day);
  return date;
}

var weekday=new Array(7);
weekday[0]="Воскресенье";
weekday[1]="Понедельник";
weekday[2]="Вторник";
weekday[3]="Среда";
weekday[4]="Четверг";
weekday[5]="Пятница";
weekday[6]="Суббота";

function update_time_placeholder(time_prefix, time2_prefix)
{
  var date = get_time_value (time_prefix);
  
  var placeholder = document.getElementById(time_prefix + '_placeholder');
	placeholder.innerHTML = weekday[date.getDay()];
	
	var length = document.getElementById(time2_prefix).value;
	date.setDate(date.getDate() + parseInt(length) - 1);
	

	
	var placeholder2 = document.getElementById(time2_prefix + '_placeholder');
	placeholder2.innerHTML = weekday[date.getDay()];
}

function install_search_plugin() {
 if (window.external && ("AddSearchProvider" in window.external)) {
   // Firefox 2 and IE 7, OpenSearch
   window.external.AddSearchProvider("http://kogda-igra.ru/opensearch.xml");
 } else {
   // No search engine support (IE 6, Opera, etc).
   alert("Поисковые плагины не поддерживаются вашим браузером.");
 }
}
function init()
{
  var search_link = document.getElementById("add_search_form");
  if (search_link && window.external && ("AddSearchProvider" in window.external))
  {
    if (("IsSearchProviderInstalled" in window.external) && window.external.IsSearchProviderInstalled("http://kogda-igra.ru/"))
    {

      return;
    }
     search_link.style.visibility = "visible";
  }
}

window.onload = init;