var casovac;

if (typeof XMLHttpRequest == "undefined")
  XMLHttpRequest = function() {
    try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch(e) {};
    try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch(e) {};
    try { return new ActiveXObject("Msxml2.XMLHTTP"); }     catch(e) {};
    try { return new ActiveXObject("Microsoft.XMLHTTP"); }  catch(e) {};
 
    throw new Error("This browser does not support XMLHttpRequest or XMLHTTP.");
  };

function zv_ajax_den()
{
	typ_dostihu = $('input[name=typ_dostihu]:checked').val() 
	stat = $('input[name=stat]:checked').val()
	rok = document.getElementById('zv_rok').options[document.getElementById('zv_rok').selectedIndex].value;
	url_c="/ajax/ajax.php?cmd=zv_den&lang=" + e4e_lang + "&typ_dostihu=" + typ_dostihu + "&rok=" + rok + '&stat=' + stat;
	ajax_request(url_c, 'den_dostihu');
	url_c="/ajax/ajax.php?cmd=zv_dostih&lang=" + e4e_lang;
	ajax_request(url_c, 'dostih');
	url_c="/ajax/ajax.php?cmd=zv_show_all_dostih&lang=" + e4e_lang;
	ajax_request(url_c, 'show_all_dostih');
}

function zv_ajax_dostih()
{
	id_den = document.getElementById('zv_id_den').options[document.getElementById('zv_id_den').selectedIndex].value;
	typ_dostihu = document.getElementById('zv_typ_dostihu').options[document.getElementById('zv_typ_dostihu').selectedIndex].value;
	url_c="/ajax/ajax.php?cmd=zv_dostih&lang=" + e4e_lang + "&id_den=" + id_den + "&typ_dostihu=" + typ_dostihu;
	ajax_request(url_c, 'dostih');
	url_c="/ajax/ajax.php?cmd=zv_show_all_dostih&lang=" + e4e_lang
	ajax_request(url_c, 'show_all_dostih');
}

function zv_ajax_show_dostih()
{
	id_dostih = document.getElementById('zv_id_dostih').options[document.getElementById('zv_id_dostih').selectedIndex].value;
	typ_dostihu = document.getElementById('zv_typ_dostihu').options[document.getElementById('zv_typ_dostihu').selectedIndex].value;
	url_c="/ajax/ajax.php?cmd=zv_show_dostih&lang=" + e4e_lang + "&id_dostih=" + id_dostih + "&typ_dostihu=" + typ_dostihu;
	ajax_request(url_c, 'show_dostih');
}

function zv_ajax_show_all_dostih()
{
	id_den = document.getElementById('zv_id_den').options[document.getElementById('zv_id_den').selectedIndex].value;
	typ_dostihu = $('input[name=typ_dostihu]:checked').val() 
	url_c="/ajax/ajax.php?cmd=zv_show_all_dostih&lang=" + e4e_lang + "&id_den=" + id_den + "&typ_dostihu=" + typ_dostihu;
	ajax_request(url_c, 'show_all_dostih');
}

function zj_ajax_show_jezdci()
{
  if(casovac)
	{
		clearTimeout(casovac);
	}
	casovac = setTimeout('zj_ajax_show_jezdci2()', 1000);
}

function zj_ajax_show_jezdci2()
{
	jezdec = document.getElementById('zj_jezdec').value;
	url_c="/ajax/ajax.php?cmd=zj_show_jezdci&lang=" + e4e_lang + "&jezdec=" + encode(jezdec);
	stop_type('zj_jezdec');
	ajax_request(url_c, 'show_jezdci','zj_jezdec');
}

function zj_ajax_show_sezona()
{
	jezdec = document.getElementById('zj_jezdec_id').value;
	rok = document.getElementById('zj_rok').options[document.getElementById('zj_rok').selectedIndex].value;
	url_c="/ajax/ajax.php?cmd=zj_show_sezona&lang=" + e4e_lang + "&jezdec=" + jezdec + "&rok=" + rok;
	ajax_request(url_c, 'show_sezona');
}

function zs_ajax_show_sezona()
{
	staj = document.getElementById('zs_staj_id').value;
	rok = document.getElementById('zs_rok').options[document.getElementById('zs_rok').selectedIndex].value;
	url_c="/ajax/ajax.php?cmd=zs_show_sezona&lang=" + e4e_lang + "&staj=" + staj + "&rok=" + rok;
	ajax_request(url_c, 'show_sezona');
}

function zt_ajax_show_treneri()
{
  if(casovac)
	{
		clearTimeout(casovac);
	}
	casovac = setTimeout('zt_ajax_show_treneri2()', 1000);
}
function zt_ajax_show_treneri2()
{
	trener = document.getElementById('zt_trener').value;
	url_c="/ajax/ajax.php?cmd=zt_show_treneri&lang=" + e4e_lang + "&trener=" + encode(trener);
	stop_type('zt_trener');
	ajax_request(url_c, 'show_treneri', 'zt_trener');
}

function zs_ajax_show_staje()
{
  if(casovac)
	{
		clearTimeout(casovac);
	}
	casovac = setTimeout('zs_ajax_show_staje2()', 1000);
}
function zs_ajax_show_staje2()
{
	staj = document.getElementById('zs_staj').value;
	url_c="/ajax/ajax.php?cmd=zs_show_staje&lang=" + e4e_lang + "&staj=" + encode(staj);
	stop_type('zs_staj');
	ajax_request(url_c, 'show_staje', 'zs_staj');
}

function zt_ajax_show_sezona()
{
	trener = document.getElementById('zt_trener_id').value;
	rok = document.getElementById('zt_rok').options[document.getElementById('zt_rok').selectedIndex].value;
	url_c="/ajax/ajax.php?cmd=zt_show_sezona&lang=" + e4e_lang + "&trener=" + trener + "&rok=" + rok;
	ajax_request(url_c, 'show_sezona');
}

function zp_ajax_show_den()
{
	id_den = document.getElementById('zp_id_den').options[document.getElementById('zp_id_den').selectedIndex].value;
	url_c="/ajax/ajax.php?cmd=zp_show_den&lang=" + e4e_lang + "&id_den=" + id_den;
	ajax_request(url_c, 'show_prihlasky');
}

function zs_ajax_show_den()
{
	id_den = document.getElementById('zs_id_den').options[document.getElementById('zs_id_den').selectedIndex].value;
	url_c="/ajax/ajax.php?cmd=zs_show_den&lang=" + e4e_lang + "&id_den=" + id_den;
	ajax_request(url_c, 'show_startky');
}



function select_ajax(name, query)
{
	document.getElementById(name).value='';
	filter = document.getElementById(name + '_ajax').value;
	url_c="/ajax/ajax.php?cmd=select_ajax&query=" + query + "&filter=" + encode(filter) + "&name=" + name;
	ajax_request(url_c, name + '_content');
}

function zf_select_ajax()
{
	document.getElementById('id_subjektu_content').style.display='block';
	document.getElementById('id_subjektu').value='';
	typ_subjektu = document.getElementById('typ_subjektu').options[document.getElementById('typ_subjektu').selectedIndex].value;
	filter = document.getElementById('id_subjektu_ajax').value;
	url_c="/ajax/ajax.php?cmd=zf_select_ajax&typ_subjektu=" + typ_subjektu + "&filter=" + encode(filter);
	ajax_request(url_c, 'id_subjektu_content');
}

function start_ajax(type, name, layer, cmd, name_val)
{
	value = '';
	if(document.getElementById(name_val))
	{
		value = "&value=" + document.getElementById(name_val).value;
	}
	if(type == 'input')
	{
		val = document.getElementById(name).value;
	}
	if(type == 'select')
	{
		val = document.getElementById(name).options[document.getElementById(name).selectedIndex].value;
	}
	url_c="/ajax/ajax.php?cmd=" + cmd + "&val=" + val + "&name=" + layer + value;
	ajax_request(url_c, layer);
}


function get_result(result,layer,name)
{
  document.getElementById(layer).innerHTML=result;
  start_type(name);
  $('a.thickbox').unbind('click');
  $('area.thickbox').unbind('click');
  $('input.thickbox').unbind('click');
	tb_init('a.thickbox, area.thickbox, input.thickbox');
	$('.date-pick').datePicker({startDate:'01/01/1000'});
}

function ajax_request(url_c, layer, name)
{
	document.getElementById(layer).innerHTML='<p><img src="/images/loadingAnimation.gif" alt="please wait, loading" /></p>';
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open("GET", url_c, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.responseText) {
				get_result(xmlhttp.responseText, layer, name);
			}
		}
	}
	xmlhttp.send(null);
}

function send_value(name)
{
	from = document.getElementById(name + '_ajax_send');	
	val_id = from.options[from.selectedIndex].value;
	val_name = from.options[from.selectedIndex].text;
	document.getElementById(name).value = val_id;
	document.getElementById(name + '_ajax').value = val_name;
}

function stop_type(name)
{
	$('#' + name).attr("disabled", true);
}

function start_type(name)
{
	if(name!='undefined')
	{
		$('#' + name).removeAttr("disabled");
		$('#' + name).focus();
	}
}

function encode(val)
{
	val = encodeURIComponent(val);
	return val;
}

