var doc = document;

// zobrazeni/skryti selectboxu pro misto akce se znamkou
function akce_change() {
  var akce = doc.getElementById('akce');
  var akce_slozka = doc.getElementById('akce_slozka');
  var akce_presunout_str = doc.getElementById('akce_presunout_str'); 
    
  if (akce.value == 'smazat') {
    if (akce_slozka) { 
      akce_slozka.style.display = 'none';
      akce_slozka.disabled = true;
    }
    if (akce_presunout_str) { 
      akce_presunout_str.style.display = 'none';
    }         
  } else if (akce.value == 'presunout') {  
    if (akce_slozka) { 
      akce_slozka.style.display = '';
      akce_slozka.disabled = false;
    }
    if (akce_presunout_str) { 
      akce_presunout_str.style.display = '';
    }          
  }
}

function akce_potvrzeni(hlaska) {  
  if (doc.getElementById('akce').value == 'presunout' || doc.getElementById('akce').value == 'smazat') {
    return confirm(hlaska);
  }
}

function zaskrtnout_vse(pocet_polozek, id_name) {
  for (i=1; i<=pocet_polozek; i++) {
    if (doc.getElementById(id_name+i)) {
      doc.getElementById(id_name+i).checked = true;
    } 
  }
}

function odskrtnout_vse(pocet_polozek, id_name) {
  for (i=1; i<=pocet_polozek; i++) { 
    if (doc.getElementById(id_name+i)) {
      doc.getElementById(id_name+i).checked = false;
    } 
  }
}

function popup(page) {
  var args = 'width=820,height=600,scrollbars=yes,status=yes,resizable=1,location=no,left=30,top=30';
  window.open(page,'_blank',args);
}

function kalendar(opener_id, mesic, rok) {
  var args = 'scrollbars=yes,status=no,resizable=1,location=no,width=280,height=150,top=100,left=200';
  if (mesic && rok) {
    rok_mesic = 'rok='+rok+'&mesic='+mesic+'&';
  } else {
    rok_mesic = '';
  }
  var page = 'http://www.smscz.cz/spalovny-odpadu/modules/kalendar.php?'+rok_mesic+'opener_id='+opener_id;
  window.open(page,'_blank',args);
}  

function show_hide(elem_id) {
  if (doc.getElementById(elem_id).style.display == 'none') {
    doc.getElementById(elem_id).style.display = '';
  } else {
    doc.getElementById(elem_id).style.display = 'none';
  }
}

function check_max_length(event, input, max_length) {
  var key = event.which;  

  if(key >= 33 || key == 13) {  
    return (input.value.length <= max_length);
  } else {
    return true;
  }
}

/* AJAX */
function get_obrazek_html(obrazek_id, input, close) {
  var text = '';
	var url = 'http://www.smscz.cz/spalovny-odpadu/modules/ajax/get_obrazek_html.php';
	var pars = 'obrazek_id='+obrazek_id;
	var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: function(originalRequest){
	var text = originalRequest.responseText;
	opener.document.getElementById(input).innerHTML = text;
	if (close) {
	  unset_session_prirazeni('obrazky');
	  self.close();
	}
  }} );
}

function get_soubor_html(soubor_id, input, close) {
  var text = '';
	var url = 'http://www.smscz.cz/spalovny-odpadu/modules/ajax/get_soubor_html.php';
	var pars = 'soubor_id='+soubor_id;
	var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: function(originalRequest){
	var text = originalRequest.responseText;
	opener.document.getElementById(input).innerHTML = text;
	if (close) {
	  unset_session_prirazeni('soubory');
	  self.close();
	}
  }} );
}

function unset_session_prirazeni(typ) {
  var text = '';
	var url = 'http://www.smscz.cz/spalovny-odpadu/modules/ajax/unset_session_prirazeni.php';
	var pars = typ+'';
	var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: function(originalRequest){
	//var text = originalRequest.responseText;
	//opener.document.getElementById(elem).value = text;
  }} );
}

function prirad_obrazky(pocet_obrazku) {
  var obrazky_id = '';
  for (var i = 1; i <= pocet_obrazku; i++) {
    if (doc.getElementById('obrazek'+i).checked == true) {
      obrazky_id += '-'+doc.getElementById('obrazek'+i).value;
    }
  }
  if (obrazky_id != '') {
    obrazky_id = obrazky_id.substr(1);
    var url = 'http://www.smscz.cz/spalovny-odpadu/modules/ajax/prirad_obrazky.php';
  	var pars = 'obrazky_id='+obrazky_id;
  	var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: function(originalRequest){
  	window.location.reload();
  	opener.location.reload();
    }} );
  }
}

function prirad_soubory(pocet_souboru) {
  var soubory_id = '';
  for (var i = 1; i <= pocet_souboru; i++) {
    if (doc.getElementById('soubor'+i).checked == true) {
      soubory_id += '-'+doc.getElementById('soubor'+i).value;
    }
  }
  if (soubory_id != '') {
    soubory_id = soubory_id.substr(1);
    var url = 'http://www.smscz.cz/spalovny-odpadu/modules/ajax/prirad_soubory.php';
  	var pars = 'soubory_id='+soubory_id;
  	var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: function(originalRequest){
  	window.location.reload();
  	opener.location.reload();
    }} );
  }
}
/* KONEC - AJAX */


/* PRO MAPU */
function marker_save() {
  doc.getElementById('latitude').value = marker.getPoint().lat();
  doc.getElementById('longitude').value = marker.getPoint().lng();
  doc.getElementById('map_zoom').value = map.getZoom();	
}

function unset_marker() {
  doc.getElementById('latitude').value = 0;
  doc.getElementById('longitude').value = 0;
  doc.getElementById('map_zoom').value = 0;
  var point = new GLatLng(0, 0);	
  marker.setPoint(point);      	  
	map.setCenter(point, 0);
}

function search_address(address) {
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(address, function (point) {
		if (!point) {
			alert('NENALEZENO: ' + address);
		} else {
			marker.setPoint(point);
			map.setCenter(point, 15);
			marker_save();
		}
	});
}
/* KONEC - PRO MAPU */

