/** odeslání XMLHttp požadavku
* @param function obsluha funkce zajišťující obsluhu při změně stavu požadavku, dostane parametr s XMLHttp objektem
* @param string method GET|POST|...
* @param string url URL požadavku
* @param string [content] tělo zprávy
* @param array [headers] pole předaných hlaviček ve tvaru { 'hlavička': 'obsah' }
* @return bool true v případě úspěchu, false jinak
* @copyright Jakub Vrána, http://php.vrana.cz
*/
function send_xmlhttprequest(obsluha, method, url, content, headers) {
	var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
	if (!xmlhttp) {
		return false;
	}
	xmlhttp.open(method, url);
	xmlhttp.onreadystatechange = function() {
		obsluha(xmlhttp);
	};
	if (headers) {
		for (var key in headers) {
			xmlhttp.setRequestHeader(key, headers[key]);
		}
	}
	xmlhttp.send(content);
	return true;
}

function modely_xml(xmlhttp) {
	if (xmlhttp.readyState == 4) {
		var f = document.getElementById('form');
		if (xmlhttp.status != 200) {
			f.submit();
		} else {
			var modely = xmlhttp.responseXML.getElementsByTagName('model');
			f['model_id'].options[0].text = (modely.length > 0 ? '- vyberte -' : '- vyberte značku -');
			for (var i=0; i < modely.length; i++) {
				var o = document.createElement('option');
				o.value = modely[i].getAttribute('id');
				o.text = modely[i].firstChild.data;
				f['model_id'].add(o, o.length);
			}
		}
	}
}

function znacka_onchange(f) {
	f['model_id'].options.length = 1;
	f['model_id'].options[0].text = '(stahuje se)';
	if (!send_xmlhttprequest(modely_xml, 'GET', 'modely_xml.php?znacka=' + f['znacka_id'].value)) {
		f.submit();
	}
}

function show(elem_id) {
  if (document.getElementById(elem_id)) {
	document.getElementById(elem_id).style.display = '';
  }
}

function hide(elem_id) {
  if (document.getElementById(elem_id)) {
	document.getElementById(elem_id).style.display = 'none';
  }
}

function return_max_moznou_castku_dobropisu(rezervace_id, faktura_id, faktura_typ) {
	var url = 'http://www.autoplace.cz/account_new/ajax/max-mozna-castka-dobropisu.php';
	var pars = 'rezervace_id='+rezervace_id+'&faktura_typ='+faktura_typ+'&faktura_id='+faktura_id;
	var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: function(originalRequest){
	var newData = originalRequest.responseText;
	document.getElementById('castka_czk').value = newData;
	document.getElementById('max_castka_czk_span').innerHTML = newData;
  document.getElementById('max_castka_czk').value = newData;
  }} );
}

function add_muj_favorit(vuz_id) {
	$.ajax({
	  type: 'GET',
	  url: 'http://www.autoplace.cz/js/ajax/add_muj_favorit.php',
	  data: 'vuz_id='+vuz_id
	  /*success: function(msg){
	    alert( "Data Saved: " + msg );
	  }*/
	});
	
	/*var url = 'http://www.autoplace.cz/js/ajax/add_muj_favorit.php';
	var pars = 'vuz_id='+vuz_id;
	alert(url+'?'+pars);
	var myAjax = new Ajax2.Request( url, {method: 'get', parameters: pars, onComplete: function(originalRequest){
	document.getElementById('muj_favorit_checkbox'+vuz_id).onlick = 'delete_muj_favorit('+vuz_id+')';
	}} );*/
}

function delete_muj_favorit(vuz_id) {
	$.ajax({
	  type: 'GET',
	  url: 'http://www.autoplace.cz/js/ajax/delete_muj_favorit.php',
	  data: 'vuz_id='+vuz_id
	  /*success: function(msg){
	    alert( "Data Saved: " + msg );
	  }*/
	});
		
	/*var url = 'http://www.autoplace.cz/js/ajax/delete_muj_favorit.php';
	var pars = 'vuz_id='+vuz_id;
	var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onComplete: function(originalRequest){
	document.getElementById('muj_favorit_checkbox'+vuz_id).onlick = 'add_muj_favorit('+vuz_id+')';	
	}} );*/
}

