var root_http = 'http://www.autoplace.cz/account/';
var api_http = 'http://www.autoplace.cz/';

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 == 'zmenit_zaplaceno_prace' || doc.getElementById('akce').value == 'smazat_prace') {
    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, name) {
  var args = 'width=820,height=600,scrollbars=yes,status=yes,resizable=1,location=no,left=30,top=30';
  if (!name) {
	name = 'prirazeni_polozek';  
  }
  popup_window = window.open(page,name,args);
  if (window.focus) {
	popup_window.focus();
  }
}

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/zemedelske-stroje/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 = 'block';
  } else {
    doc.getElementById(elem_id).style.display = 'none';
  }
}  

function show(elem_id) {
  if (doc.getElementById(elem_id)) {
    doc.getElementById(elem_id).style.display = '';
  }
}

function hide(elem_id) {
  if (doc.getElementById(elem_id)) {
    doc.getElementById(elem_id).style.display = 'none';
  }
} 

function enable(elem_id) {
  if (doc.getElementById(elem_id)) {
    doc.getElementById(elem_id).disabled = false;
  }
}

function disable(elem_id) {
  if (doc.getElementById(elem_id)) {
    doc.getElementById(elem_id).disabled = true;
  }
}

function set_value(elem_id, value) {
	if (doc.getElementById(elem_id)) {
		doc.getElementById(elem_id).value = value;
	}
}

function get_value(elem_id) {
	if (doc.getElementById(elem_id)) {
		return doc.getElementById(elem_id).value;
	}
}

function set_html(elem_id, value) {
	if (doc.getElementById(elem_id)) {
		doc.getElementById(elem_id).innerHTML = value;
	}
}

function get_html(elem_id) {
	if (doc.getElementById(elem_id)) {
		return doc.getElementById(elem_id).innerHTML;
	}
}

function focus_elem(elem_id) {
	if (doc.getElementById(elem_id)) {
		doc.getElementById(elem_id).focus();
	}
}

function redirect(url, timeout) {
	if (!timeout) {
		window.location = url;
	} else {
		setTimeout('window.location = url', timeout);
	}
}
   
function aktualizuj_celkove_ceny() {
	var vuz_cena = intval(doc.getElementById('vuz_cena').value);
	var doplnkova_vybava_cena = 0;
	var prislusenstvi_cena = 0;
	var pojisteni_cena = intval(doc.getElementById('pojisteni_cena_input').value);
	var zaruka_cena = 0;
	var bonus_cena = intval(doc.getElementById('bonus_cena_input').value);
	var cena_celkem = 0;
	
	//celkova cena doplnkove vybavy
	var i = 0;
	while (doc.getElementById('doplnkova_vybava_id_'+i)) {
	  if (doc.getElementById('doplnkova_vybava_id_'+i).checked) {
		doplnkova_vybava_cena += intval(doc.getElementById('doplnkova_vybava_cena_'+i).value);
	  }
	  
	  i++;
	}

	// celkova cena prislusenstvi
	var i = 0;
	while (doc.getElementById('prislusenstvi_id_'+i)) {
	  if (doc.getElementById('prislusenstvi_id_'+i).checked) {
		prislusenstvi_cena += intval(doc.getElementById('prislusenstvi_cena_'+i).value);
	  }
	  
	  i++;
	}

	// cena zaruky
	var tachometr = intval(doc.getElementById('tachometr').value);
	var tachVal = doc.getElementById('tachVal').innerHTML;
	var zaruka_roky = intval(doc.getElementById('zaruka_roky').value);
	
	if (zaruka_roky) {
    	if (tachVal == 'mil') {
    		tachometr = tachometr * 1.609344;
    	}
    	
    	if (tachometr >= 160000) {
    		zaruka_cena = zaruka_roky*10000;
    	} else {
    		zaruka_cena = zaruka_roky*6000;
    	}
    }
	

	// ceny celkem
	doc.getElementById('doplnkova_vybava_cena').innerHTML = nformat(doplnkova_vybava_cena);
	doc.getElementById('prislusenstvi_cena').innerHTML = nformat(prislusenstvi_cena);
	doc.getElementById('pojisteni_cena').innerHTML = nformat(pojisteni_cena);
	doc.getElementById('zaruka_cena').innerHTML = nformat(zaruka_cena);
	doc.getElementById('bonus_cena').innerHTML = nformat(bonus_cena);
	doc.getElementById('vuz_cena_str').innerHTML = nformat(vuz_cena);
	
	celkem_cena = vuz_cena + doplnkova_vybava_cena + prislusenstvi_cena + pojisteni_cena + zaruka_cena + bonus_cena;
	doc.getElementById('celkem_cena').innerHTML = nformat(celkem_cena);
} 

function show(elem_id) {
  if (doc.getElementById(elem_id)) {
	doc.getElementById(elem_id).style.display = '';
  }
}  

function hide(elem_id) {
  if (doc.getElementById(elem_id)) {
	doc.getElementById(elem_id).style.display = 'none';
  }
}     

function disable_enable(elem_id) {
  if (doc.getElementById(elem_id)) {
	if (doc.getElementById(elem_id).disabled) {
	  doc.getElementById(elem_id).disabled = false;
  	} else {
  	  doc.getElementById(elem_id).disabled = true;	
  	}
  }
}     

function disable(elem_id) {
  if (doc.getElementById(elem_id)) {
	doc.getElementById(elem_id).disabled = true;
  }
}  

function enable(elem_id) {
  if (doc.getElementById(elem_id)) {
	doc.getElementById(elem_id).disabled = false;
  }
}  

function is_checked(elem_id) {
  return doc.getElementById(elem_id).checked;
}

function get_value(elem_id) {
  if (doc.getElementById(elem_id)) {
	return doc.getElementById(elem_id).value;
  }
}

function check_elem(elem_id) {
	if (doc.getElementById(elem_id)) {
		doc.getElementById(elem_id).checked = true;
	}
}

function uncheck_elem(elem_id) {
	if (doc.getElementById(elem_id)) {
		doc.getElementById(elem_id).checked = false;
	}
}

function select_option(select_id, option_value) {
	if (doc.getElementById(select_id)) {
		var options = doc.getElementById(select_id).options;
		
		for (var i=0; i < options.length; i++) {
			if (options[i].value == option_value) {
				options[i].selected = true;
			} else {
				options[i].selected = false;
			}
		}
	}
}

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;
  }
}      

function save_parametr(hodnota_akce, hodnota_id) {
  doc.getElementById('hodnota_akce').value = hodnota_akce;
  doc.getElementById('hodnota_id').value = hodnota_id; 
  doc.getElementById('form').submit();
}

/* AJAX */
function get_faktury_option(objednavka_id, faktura_typ) {
	jQuery.ajax({
		type: "GET",
		url: root_http+'ajax/get_faktury_option.php',
		data: 'objednavka_id='+objednavka_id+'&faktura_typ='+faktura_typ,
		success: function(text){
		document.getElementById('faktura_id_td').innerHTML = text;
		}
	});
}   

function get_prodejny_option(admin_login) {
	jQuery.ajax({
		type: "GET",
		url: root_http+'ajax/get_prodejny_option.php',
		data: 'admin_login='+admin_login,
		success: function(text){
			document.getElementById('prodejna_id_div').innerHTML = text;
		}
	});
}    

function get_administratori_option(prodejna_id) {
	jQuery.ajax({
		type: "GET",
		url: root_http+'ajax/get_administratori_option.php',
		data: 'prodejna_id='+prodejna_id,
		success: function(text){
			document.getElementById('user_id_td').innerHTML = text;
		}
	});
}     

function get_okresy_option(stat) {
	jQuery.ajax({
		type: "GET",
		url: root_http+'ajax/get_okresy_option.php',
		data: 'stat='+stat,
		success: function(text){
			document.getElementById('user_id_td').innerHTML = text;
		}
	});
}

function get_modely_option(znacka_id, zobrazeni) {
	jQuery.ajax({
		type: "GET",
		url: root_http+'ajax/get_modely_option.php',
		data: 'znacka_id='+znacka_id+'&zobrazeni='+zobrazeni,
		success: function(text){
			document.getElementById('model_id_span').innerHTML = text;
		}
	});
}   

function objednavky_dokumenty_zobrazit(dokument, objednavka_id) {
	jQuery.ajax({
		type: "GET",
		url: root_http+'ajax/get_dokument_pdf_url.php',
		data: 'dokument='+dokument,
		success: function(text){
			document.getElementById('dokument_zobrazit_'+objednavka_id).href = text;
		}
	});
}      

function objednavky_dokumenty_editovat(dokument, objednavka_id) {
	jQuery.ajax({
		type: "GET",
		url: root_http+'ajax/get_dokument_edit_url.php',
		data: 'dokument='+dokument,
		success: function(text){
			document.getElementById('dokument_editovat_'+objednavka_id).href = text;
		}
	});
}        

function make_prihlaseni(prodejna_id) {
	jQuery.ajax({
		type: "GET",
		url: root_http+'ajax/make_prihlaseni.php',
		data: 'prodejna_id='+prodejna_id,
		success: function(text){
			// nic
		}
	});
}          

function send_zapomneli_jste_heslo_zakaznik(email) {
	if (email) {
		jQuery.ajax({
			type: "GET",
			url: root_http+'ajax/send_zapomneli_jste_heslo_zakaznik.php',
			data: 'email='+email,
			success: function(text){
				alert(text);
			}
		});
	} else {
		alert('Musíte vyplnit e-mail.');
	}
}   

function fill_kontaktni_udaje_zakaznik(zakaznik, elem_prefix) {
	var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
	if (!xmlhttp) {
		return false;
	}
	
	var zakaznik_id = zakaznik.substring(zakaznik.lastIndexOf(' ID: ')+5); 
	
	set_value(elem_prefix+(elem_prefix ? '_' : '')+'zakaznik_id', intval(zakaznik_id));

	if (intval(zakaznik_id)) {
		xmlhttp.open('GET', root_http+'ajax/fill_kontaktni_udaje_zakaznik.php?zakaznik_id='+zakaznik_id, true);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {						
				var kontaktni_udaje = xmlhttp.responseXML.getElementsByTagName('kontaktni_udaj');
				
				for (var i=0; i < kontaktni_udaje.length; i++) {
					var elem_name = kontaktni_udaje[i].getAttribute('name');
					var elem_id = elem_prefix+(elem_prefix ? '_' : '')+'kontaktni_udaje_'+elem_name;
					var elem_value = kontaktni_udaje[i].getAttribute('value');
					
					if (elem_name == 'osoba_typ') {
						if (elem_value == 'po') {
							uncheck_elem(elem_id+'_fo');
							check_elem(elem_id+'_po');
						} else {
							uncheck_elem(elem_id+'_po');
							check_elem(elem_id+'_fo');
						}
					}
					else
					if (elem_name == 'stat') {
						select_option(elem_id, elem_value);
						okres_elem_id = elem_prefix+(elem_prefix ? '_' : '')+'kontaktni_udaje_okres_id_tr';
						if (elem_value == 'cr') {
							show(okres_elem_id);
						} else {
							hide(okres_elem_id);
						}
					}
					else
					if (elem_name == 'okres_id') {
						select_option(elem_id, elem_value);
					}
					else
					{
						set_value(elem_id, elem_value);
					}
				}
			}
		};
		/*f (headers) {
			for (var key in headers) {
				xmlhttp.setRequestHeader(key, headers[key]);
			}
		}*/
		//xmlhttp.send(content);
		xmlhttp.send();
		return true;
	} else {
		return false;
	}
}

function exists_zakaznik_email(email, prefix) {
	jQuery.ajax({
		type: "GET",
		url: root_http+'ajax/exists_zakaznik_email.php',
		data: 'email='+email,
		success: function(text){
			var exists_email = intval(text);
			
			var elem_id = (prefix ? prefix+'_' : '')+'user_not_required';
			
			var i = 1;
			while(doc.getElementById(elem_id+i)) {
				if (exists_email) {
					hide(elem_id+i);
				} else {
					show(elem_id+i);
				}
				i++;
			}
		}
	});
}
/* KONEC - AJAX */

function add_radek_polozek_faktury(parent_id, parent_typ) { // parent = objednavka nebo pronajem 
  pocet_radku = get_pocet_radku_polozek_faktury();
  id = pocet_radku; // jeden radek navic (toprow), cislovani od 0
  
  if (id%2) {
	row_class = 'odd_row'; 
  } else {
	row_class = 'even_row'
  }
        
  tabulka = document.getElementById('polozky_table');
  radek = document.createElement('tr');
  radek.setAttribute('class', row_class);
  sloupec_popis = document.createElement('td');
  sloupec_cena = document.createElement('td'); 
  sloupec_dph = document.createElement('td'); 
  sloupec_akce = document.createElement('td');
  sloupec_akce.setAttribute('align', 'center');   
  input_popis = document.createElement('input');  
  input_popis.setAttribute('type', 'text');      
  input_popis.setAttribute('id', 'polozka_popis_'+id);      
  input_popis.setAttribute('name', 'polozky['+id+'][popis]');      
  input_popis.setAttribute('value', '');             
  input_popis.setAttribute('class', 's4');        
  input_cena = document.createElement('input');
  input_cena.setAttribute('type', 'text');      
  input_cena.setAttribute('id', 'polozka_cena_'+id);      
  input_cena.setAttribute('name', 'polozky['+id+'][cena]');      
  input_cena.setAttribute('value', '');            
  input_cena.setAttribute('class', 's01');          
  select_dph = document.createElement('select');
  select_dph.setAttribute('id', 'polozka_dph_'+id);      
  select_dph.setAttribute('name', 'polozky['+id+'][dph]');        
  option0_dph = document.createElement('option'); 
  option0_dph.setAttribute('value', 'null');    
  option0_dph_text = document.createTextNode('zvláštní režim'); 
  option0_dph.appendChild(option0_dph_text);                 
  option1_dph = document.createElement('option'); 
  option1_dph.setAttribute('value', '0');    
  option1_dph_text = document.createTextNode('0%'); 
  option1_dph.appendChild(option1_dph_text);             
  option2_dph = document.createElement('option'); 
  option2_dph.setAttribute('value', '10');  
  option2_dph_text = document.createTextNode('10%'); 
  option2_dph.appendChild(option2_dph_text);         
  option3_dph = document.createElement('option'); 
  option3_dph.setAttribute('value', '19');  
  option3_dph_text = document.createTextNode('19%'); 
  option3_dph.appendChild(option3_dph_text);         
  option4_dph = document.createElement('option'); 
  option4_dph.setAttribute('value', '20');    
  option4_dph.setAttribute('selected', 'selected');    
  option4_dph_text = document.createTextNode('20%'); 
  option4_dph.appendChild(option4_dph_text);
  select_dph.appendChild(option0_dph);    
  select_dph.appendChild(option1_dph);  
  select_dph.appendChild(option2_dph);  
  select_dph.appendChild(option3_dph);  
  select_dph.appendChild(option4_dph); 
  input_tabulka = document.createElement('input');
  input_tabulka.setAttribute('type', 'hidden');      
  input_tabulka.setAttribute('id', 'polozka_tabulka_'+id);      
  input_tabulka.setAttribute('name', 'polozky['+id+'][tabulka]');      
  input_tabulka.setAttribute('value', '');       
  input_polozka_id = document.createElement('input');
  input_polozka_id.setAttribute('type', 'hidden');      
  input_polozka_id.setAttribute('id', 'polozka_polozka_id_'+id);      
  input_polozka_id.setAttribute('name', 'polozky['+id+'][polozka_id]');      
  input_polozka_id.setAttribute('value', '');        
  input_typ = document.createElement('input');
  input_typ.setAttribute('type', 'hidden');      
  input_typ.setAttribute('id', 'polozka_typ_'+id);      
  input_typ.setAttribute('name', 'polozky['+id+'][typ]');      
  input_typ.setAttribute('value', '');   
  if (parent_typ == 'objednavka') {
	  odkaz_pridat = document.createElement('a');
	  odkaz_pridat.setAttribute('href', 'javascript: popup(\''+root_http+'objednavka-polozky.php?objednavka_id='+parent_id+'&radek_id='+id+'\')');    
	  ikonka_pridat = document.createElement('img');
	  ikonka_pridat.setAttribute('src', root_http+'pics/icons/add.gif');    
	  ikonka_pridat.setAttribute('alt', 'přidat položku z objednávky');    
	  ikonka_pridat.setAttribute('title', 'přidat položku z objednávky');    
	  ikonka_pridat.setAttribute('style', 'border: none;');    
	  odkaz_pridat.appendChild(ikonka_pridat); 
  }
  odkaz_smazat = document.createElement('a');
  odkaz_smazat.setAttribute('href', 'javascript: smazat_polozku_faktury('+id+')');    
  ikonka_smazat = document.createElement('img');
  ikonka_smazat.setAttribute('src', root_http+'pics/icons/delete.gif');    
  ikonka_smazat.setAttribute('alt', 'smazat položku');    
  ikonka_smazat.setAttribute('title', 'smazat položku');    
  ikonka_smazat.setAttribute('style', 'border: none;'+(parent_typ == 'objednavka' ? ' margin-left: 10px;' : ''));    
  odkaz_smazat.appendChild(ikonka_smazat);      
  text_kc = document.createTextNode(' Kč'); 
  sloupec_popis.appendChild(input_popis);        
  sloupec_cena.appendChild(input_cena);          
  sloupec_cena.appendChild(text_kc);            
  sloupec_dph.appendChild(select_dph);   
  if (parent_typ == 'objednavka') {
	  sloupec_akce.appendChild(odkaz_pridat);
  }
  sloupec_akce.appendChild(odkaz_smazat);       
  sloupec_akce.appendChild(input_tabulka);       
  sloupec_akce.appendChild(input_polozka_id);       
  sloupec_akce.appendChild(input_typ);
  radek.appendChild(sloupec_popis);       
  radek.appendChild(sloupec_cena);;       
  radek.appendChild(sloupec_dph);       
  radek.appendChild(sloupec_akce);
  tabulka.appendChild(radek);
}

function add_radek_zalohove_faktury(parent_id, parent_typ) { // parent = objednavka nebo pronajem 
  pocet_radku = get_pocet_radku_faktur();
  id = pocet_radku; // jeden radek navic (toprow), cislovani od 0
  
  if (id%2) {
	row_class = 'odd_row'; 
  } else {
	row_class = 'even_row'
  }
        
  tabulka = document.getElementById('faktury_table');
  radek = document.createElement('tr');
  radek.setAttribute('class', row_class);
  sloupec_cislo = document.createElement('td'); 
  sloupec_popis = document.createElement('td');
  sloupec_pdf = document.createElement('td'); 
  sloupec_cena = document.createElement('td'); 
  sloupec_akce = document.createElement('td');
  sloupec_akce.setAttribute('align', 'center');   
  input_cena = document.createElement('input');
  input_cena.setAttribute('type', 'text');      
  input_cena.setAttribute('id', 'polozka_cena_'+id);      
  input_cena.setAttribute('name', 'polozky['+id+'][cena]');      
  input_cena.setAttribute('value', '');            
  input_cena.setAttribute('class', 's01'); 
  input_id = document.createElement('input');
  input_id.setAttribute('type', 'hidden');      
  input_id.setAttribute('id', 'faktura_id_'+id);      
  input_id.setAttribute('name', 'zalohove_faktury_id[]');      
  input_id.setAttribute('value', '');     
  odkaz_pridat = document.createElement('a');
  odkaz_pridat.setAttribute('href', 'javascript: popup(\''+root_http+'prirazeni-faktur.php?'+parent_typ+'_id='+parent_id+'&radek_id='+id+'&faktury_typ=zalohove\')');    
  ikonka_pridat = document.createElement('img');
  ikonka_pridat.setAttribute('src', root_http+'pics/icons/add.gif');    
  ikonka_pridat.setAttribute('alt', 'přidat zálohovou fakturu');    
  ikonka_pridat.setAttribute('title', 'přidat zálohovou fakturu');    
  ikonka_pridat.setAttribute('style', 'border: none;');    
  odkaz_pridat.appendChild(ikonka_pridat);        
  odkaz_smazat = document.createElement('a');
  odkaz_smazat.setAttribute('href', 'javascript: smazat_fakturu('+id+')');    
  ikonka_smazat = document.createElement('img');
  ikonka_smazat.setAttribute('src', root_http+'pics/icons/delete.gif');    
  ikonka_smazat.setAttribute('alt', 'smazat položku');    
  ikonka_smazat.setAttribute('title', 'smazat položku');    
  ikonka_smazat.setAttribute('style', 'border: none; margin-left: 10px;');    
  odkaz_smazat.appendChild(ikonka_smazat);      
  text_kc = document.createTextNode(' Kč'); 
  sloupec_cena.appendChild(input_cena);          
  sloupec_cena.appendChild(text_kc);            
  sloupec_akce.appendChild(odkaz_pridat);        
  sloupec_akce.appendChild(odkaz_smazat);       
  sloupec_akce.appendChild(input_id);   
  radek.appendChild(sloupec_cislo);       
  radek.appendChild(sloupec_popis);       
  radek.appendChild(sloupec_pdf);        
  radek.appendChild(sloupec_cena);      
  radek.appendChild(sloupec_akce);
  table.appendChild(radek);
}

function get_pocet_radku_polozek_faktury() {  
  table = document.getElementById('polozky_table');
  rows = table.getElementsByTagName('tr');
  
  return rows.length;
}

function get_pocet_radku_faktur() {  
  table = document.getElementById('faktury_table');
  rows = table.getElementsByTagName('tr');
  
  return rows.length;
}

function paste_polozku_faktury(radek_id, popis, cena, dph, tabulka, polozka_id, typ) {
  opener.document.getElementById('polozka_popis_'+radek_id).value = popis;
  opener.document.getElementById('polozka_cena_'+radek_id).value = cena;
  opener.document.getElementById('polozka_tabulka_'+radek_id).value = tabulka;
  opener.document.getElementById('polozka_polozka_id_'+radek_id).value = polozka_id;
  opener.document.getElementById('polozka_typ_'+radek_id).value = typ;

  var select_dph = opener.document.getElementById('polozka_dph_'+radek_id);
  
  for (var i = 0; i < select_dph.options.length; i++) {
	  if (select_dph.options[i].value == dph) {
		  select_dph.options[i].selected = true;  
	  }
  }	  
  
  var polozky_cena_celkem = 0;
  var i = 0;
  
  while (opener.document.getElementById('polozka_cena_'+i)) {
	if (intval(opener.document.getElementById('polozka_cena_'+i).value)) {
	  polozky_cena_celkem += intval(opener.document.getElementById('polozka_cena_'+i).value);
	}
		
    i++;
  }

  opener.document.getElementById('polozky_cena_celkem').innerHTML = polozky_cena_celkem;
  
  self.close();
}

function paste_fakturu(radek_id, id, typ, cislo, popis, cena, typ_f) {
  opener.document.getElementById('faktura_id_'+radek_id).value = id;
  if (opener.document.getElementById('faktura_typ_'+radek_id)) {
	opener.document.getElementById('faktura_typ_'+radek_id).innerHTML = typ;
  }
  if (opener.document.getElementById('faktura_cislo_'+radek_id)) {
	opener.document.getElementById('faktura_cislo_'+radek_id).value = cislo;
  }
  if (opener.document.getElementById('faktura_popis_'+radek_id)) {
	opener.document.getElementById('faktura_popis_'+radek_id).innerHTML = popis;
  }
  opener.document.getElementById('faktura_cena_'+radek_id).value = cena;
  if (opener.document.getElementById('faktura_typ_f_'+radek_id)) {
	opener.document.getElementById('faktura_typ_f_'+radek_id).innerHTML = typ_f;
  }
  if (opener.document.getElementById('faktura_cislo_str_'+radek_id)) {
	opener.document.getElementById('faktura_cislo_str_'+radek_id).innerHTML = cislo;
  }
  if (opener.document.getElementById('faktura_pdf_'+radek_id)) {
	opener.document.getElementById('faktura_pdf_'+radek_id).innerHTML = '<a href="'+api_http+'dokumenty/faktury/faktura-'+cislo+'.pdf" onclick="target=\'_blank\';"><img src="'+root_http+'pics/files_icons/pdf.gif" alt="zobrazit PDF" title="zobrazit PDF" style="border: none;" /></a>';
  }
  
  /*var faktury_cena_celkem = 0;
  var i = 0;
  
  while (opener.document.getElementById('faktury_cena_'+i)) {
	if (intval(opener.document.getElementById('faktury_cena_'+i).value)) {
		faktury_cena_celkem += intval(opener.document.getElementById('faktury_cena_'+i).value);
	}
		
    i++;
  }

  opener.document.getElementById('faktury_cena_celkem').innerHTML = faktury_cena_celkem;*/
  
  self.close();
}

function aktualizuj_cenu_polozek_faktur() {
  var polozky_cena_celkem = 0;
  var i = 0;
  
  while (doc.getElementById('polozka_cena_'+i)) {
	if (intval(doc.getElementById('polozka_cena_'+i).value)) {
	  polozky_cena_celkem += intval(doc.getElementById('polozka_cena_'+i).value);
	}
	
    i++;
  }

  doc.getElementById('polozky_cena_celkem').innerHTML = polozky_cena_celkem;
}

function aktualizuj_cenu_faktur() {
  var faktury_cena_celkem = 0;
  var i = 0;
  
  while (doc.getElementById('faktura_cena_'+i)) {
	if (intval(doc.getElementById('faktura_cena_'+i).value)) {
		faktury_cena_celkem += intval(doc.getElementById('faktura_cena_'+i).value);
	}
	
    i++;
  }

  doc.getElementById('faktury_cena_celkem').innerHTML = faktury_cena_celkem;
}

function smazat_polozku_faktury(id) {
  doc.getElementById('polozka_popis_'+id).value = '';
  doc.getElementById('polozka_cena_'+id).value = '';
  doc.getElementById('polozka_tabulka_'+id).value = '';
  doc.getElementById('polozka_polozka_id_'+id).value = '';
  doc.getElementById('polozka_typ_'+id).value = '';
  doc.getElementById('polozka_dph_'+id).options[4].selected = true;
}

function smazat_fakturu(id) {
  doc.getElementById('faktura_id_'+id).value = '';
  if (doc.getElementById('faktura_typ_'+id)) {
	doc.getElementById('faktura_typ_'+id).innerHTML = '';
  }
  if (doc.getElementById('faktura_cislo_'+id)) {
	doc.getElementById('faktura_cislo_'+id).value = '';
  }
  if (doc.getElementById('faktura_popis_'+id)) {
	doc.getElementById('faktura_popis_'+id).innerHTML = '';
  }
  doc.getElementById('faktura_cena_'+id).value = '';
  if (doc.getElementById('faktura_typ_f_'+id)) {
	doc.getElementById('faktura_typ_f_'+id).innerHTML = '';
  }
  if (doc.getElementById('faktura_cislo_str_'+id)) {
	doc.getElementById('faktura_cislo_str_'+id).innerHTML = '';
  }
  if (doc.getElementById('faktura_cislo_str_'+id)) {
	doc.getElementById('faktura_pdf_'+id).innerHTML = '';
  }
}

function show_table_typ_prodeje(financovani) {
  if (financovani == 'hotovost' || financovani == 'operativni_leasing') {
	hide('table_uver_leasing');
	hide('table_operativni_leasing');
	disable('akontace');
	disable('uverova_smlouva');
	disable('banka_id');
  }
  else
  if (financovani == 'uver' || financovani == 'leasing') {
	show('table_uver_leasing');
	hide('table_operativni_leasing');
	enable('akontace');
	enable('uverova_smlouva');
	enable('banka_id');  
  }
}

function calculate_zaruka_cena (delka_kontraktu, tachometr) {
	zaruka = 0;
	
	if (delka_kontraktu == 12) {
		if (tachometr < 160000) {
			zaruka = 6000;
		} else {
			zaruka = 12000;
		}
	} 
	else
	if (delka_kontraktu == 24) {
		if (tachometr < 160000) {
			zaruka = 10000;
		} else {
			zaruka = 20000;
		}
	}
	
	set_value('zaruka', zaruka);
}

function add_row_soubory_poznamky_komunikace(tabulka_id) {   
	  tabulka = document.getElementById(tabulka_id);
	  
	  var id = tabulka.getElementsByTagName('tr').length-1; // pocet radku tabulky
	  
	  radek = document.createElement('tr');
	  radek.setAttribute('id', 'poznamky_komunikace_soubory_radek'+id);  
	  sloupec_soubor = document.createElement('td'); 
	  sloupec_popis = document.createElement('td');
	  sloupec_akce = document.createElement('td');
	  sloupec_akce.setAttribute('align', 'center');   
	  input_soubor = document.createElement('input');  
	  input_soubor.setAttribute('type', 'file');   
	  input_soubor.setAttribute('id', 'poznamky_komunikace_soubory_soubor'+id);       
	  input_soubor.setAttribute('name', 'poznamka_soubory['+id+']');   
	  input_popis = document.createElement('input');
	  input_popis.setAttribute('type', 'text'); 
	  input_popis.setAttribute('id', 'poznamky_komunikace_soubory_popis'+id);         
	  input_popis.setAttribute('name', 'poznamka_soubory['+id+'][popis]');      
	  input_popis.setAttribute('value', '');            
	  input_popis.setAttribute('class', 's4'); 
	  odkaz_smazat = document.createElement('a');
	  odkaz_smazat.setAttribute('href', 'javascript: delete_row_soubory_poznamky_komunikace('+id+');');   
	  odkaz_smazat.setAttribute('title', 'odstranit');      
	  ikonka_smazat = document.createElement('img');
	  ikonka_smazat.setAttribute('src', root_http+'pics/icons/delete.gif');    
	  ikonka_smazat.setAttribute('alt', 'odstranit');   
	  ikonka_smazat.setAttribute('style', 'border: none;');    
	  odkaz_smazat.appendChild(ikonka_smazat);       
	  sloupec_soubor.appendChild(input_soubor);     
	  sloupec_popis.appendChild(input_popis); 
	  sloupec_akce.appendChild(odkaz_smazat);    
	  radek.appendChild(sloupec_soubor);
	  radek.appendChild(sloupec_popis);     
	  radek.appendChild(sloupec_akce);
	  tabulka.appendChild(radek);	
}

function delete_row_soubory_poznamky_komunikace(id) {
	hide('poznamky_komunikace_soubory_radek'+id);
	disable('poznamky_komunikace_soubory_soubor'+id);
	disable('poznamky_komunikace_soubory_popis'+id);
}

function intval (mixed_var, base) {
    var type = typeof( mixed_var );
 
    if (type === 'boolean') {        
      return (mixed_var) ? 1 : 0;
    } else if (type === 'string') {
      tmp = parseInt(mixed_var, base || 10);
      return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
    } else if (type === 'number' && isFinite(mixed_var) ) {        
      return Math.floor(mixed_var);
    } else {
      return 0;
    }
}

function nformat(nStr){
  nStr += '';
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1))
    x1 = x1.replace(rgx, '$1' + ' ' + '$2');
  return x1 + x2;
}

function validate_email(email) {
	var atpos = email.indexOf("@");
	var dotpos = email.lastIndexOf(".");
	if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length) {
	    return false;
	} else {
		return true;
	}
}
