
function showHide(id) {
	if(document.getElementById){
		var obj = document.getElementById(id);
		if (obj.style.display == 'inline') {
		obj.style.display='none';
		} else {
		obj.style.display='inline';
		}
	}
}

function showIt(idOption) {
	if(document.getElementById){
		var obj = document.getElementById(idOption);	
		obj.style.display='block';
	}
}

function hideIt(idOptionHide) {
	if(document.getElementById){
		var obj = document.getElementById(idOptionHide);	
		obj.style.display='none';
	}
}

function dodaciDolozka(dolozka) {
											
	vybrano = dolozka.selectedIndex;
	if (dolozka.options[vybrano].value == 'empty') {
		for (c=1;c<=7;c++) {
			hideIt(dolozka.options[c].value);
		}
		showIt('dolozkaMsg');
		document.getElementById('dolozkaMsg').innerHTML = '<br /><strong>Nesmí být --prázdné--</strong>';
		return false;
	} else {
		for (c=1;c<=7;c++) {
			hideIt(dolozka.options[c].value);
		}
		hideIt('dolozkaMsg');
		showIt(dolozka.options[vybrano].value);
		
		return false;
	}
	
	return true;
}
// checkboxy
var howMany = 0;
function checkAll(howMany) {	
	for (var j = 1; j <= howMany; j++) {
		box = eval("document.mojeZasilky.stop" + j); 
		if (box.checked == false) box.checked = true;
	}
}
function uncheckAll(howMany) {	
	for (var j = 1; j <= howMany; j++) {
		box = eval("document.mojeZasilky.stop" + j); 
		if (box.checked == true) box.checked = false;
	}
}

// Od Pospíšila...
var Countries = new Array();
Countries['CZ'] = 'Česká republika';
Countries['B'] = 'Belgie';
Countries['BG'] = 'Bulharsko';
Countries['DK'] = 'Dánsko';
Countries['EST'] = 'Estonsko';
Countries['FIN'] = 'Finsko';
Countries['F'] = 'Francie';
Countries['I'] = 'Itálie';
Countries['IRL'] = 'Irsko';
Countries['CY'] = 'Kypr';
Countries['LV'] = 'Litva';
Countries['LT'] = 'Lotyšsko';
Countries['L'] = 'Lucembursko';
Countries['H'] = 'Maďarsko';
Countries['M'] = 'Malta';
Countries['D'] = 'Německo';
Countries['NL'] = 'Nizozemí';
Countries['PL'] = 'Polsko';
Countries['P'] = 'Portugalsko';
Countries['A'] = 'Rakousko';
Countries['RO'] = 'Rumunsko';
Countries['GR'] = 'Řecko';
Countries['SK'] = 'Slovensko';
Countries['SLO'] = 'Slovinsko';
Countries['E'] = 'Španělsko';
Countries['S'] = 'Švédsko';
Countries['GB'] = 'Velká Británie';
Countries['IS'] = 'Island';
Countries['FL'] = 'Lichtenštejnsko';
Countries['N'] = 'Norsko';
Countries['CH'] = 'Švýcarsko';

// <fux>
var itemnr = 2;
function additem() {
  var newitem =  '<div id="item_'+itemnr+'"  class=both style="float: left; padding: 5px; width: 95%;color: maroon;">';
      newitem += '<div style="margin: 0; padding: 0;	float: left; text-align: left; color: maroon;">';
      newitem += '<select name="typ_baleni[]" id="typ_baleni_'+itemnr+'" onchange="check_dimensions('+itemnr+')">';
      newitem += '<option value="X">Prosím, vyberte</option>';
      newitem += '<option value="europaleta">EURO-paleta</option>';
      newitem += '<option value="atyp_paleta">Atyp. paleta</option>';
      newitem += '<option value="balik">Balík</option>';
      newitem += '<option value="bedna">Bedna</option>';
      newitem += '<option value="volne_lozene">Volně ložené</option>';
      newitem += '<option value="volne_lozeny_kus">Volně ložený kus</option>';
      newitem += '<option value="klec">Klec</option>';
      newitem += '<option value="krabice">Krabice</option>';
      newitem += '<option value="nadoba">Nádoba</option>';
      newitem += '<option value="prepravka">Přepravka</option>';
      newitem += '<option value="pytel">Pytel</option>';
      newitem += '<option value="sud">Sud</option>';
      newitem += '<option value="svazek">Svazek</option>';
      newitem += '<option value="tlakova_nadoba">Tlaková nádoba</option>';
      newitem += '<option value="celovozova">Colovozová zásilka</option>';
      newitem += '<option value="kotouc">Kotouč</option>';
      newitem += '<option value="stroj">Stroj</option>';
      newitem += '</select><br />';
      newitem += '<input id="pocet_kusu_'+itemnr+'" type="text" size="8" name="pocet_kusu[]" value=" "> ks';
      newitem += '<br />Popis (nepovinné):<br /><textarea name="popis_zbozi[]" style="margin-top:0px;" cols="11" rows="1" onkeyup="resizeTextarea(this)" onmouseup="resizeTextarea(this)"></textarea></div>';
      newitem += '<div style="margin: 0; padding: 0;	float: right;	text-align: right; font-weight: bold; color: maroon; line-height: 1.4em;">';
      newitem += '<a href="#" onclick="removeitem('+itemnr+');return false;" style="background-color:red;padding:0.2em;color:white;line-height:2em;">Odstranit</a>';
      newitem += '<br />Rozměry (1 ks)<br />';
      newitem += '<span class="theRed">*</span>D: <input id="delka_'+itemnr+'" type="text" size="4" name="delka[]" value="0" style="height: 1.1em; padding: 0; margin: 0;" />m<br />';
      newitem += '<span class="theRed">*</span>Š: <input id="sirka_'+itemnr+'" type="text" size="4" name="sirka[]" value="0" style="height: 1.1em; padding: 0; margin: 0;" />m<br />';
      newitem += '<span class="theRed">*</span>V: <input id="vyska_'+itemnr+'" type="text" size="4" name="vyska[]" value="0" style="height: 1.1em; padding: 0; margin: 0;" />m<br />';
      newitem += '</div>';
      newitem += '</div>';
  //document.getElementById('formitems').innerHTML += newitem;
  $('formitems').insert({bottom:newitem});
	++ itemnr;
}

function removeitem(itemid) {
  $('item_'+itemid).remove();
  //document.getElementById('item_'+itemid).parentNode.removeChild(document.getElementById('item_'+itemid)); 
}

function check_dimensions(cargoid) {
  wraptype = document.getElementById('typ_baleni_'+cargoid).value;
  if (wraptype == 'europaleta') {
    document.getElementById('delka_'+cargoid).value = '1,2';
    document.getElementById('sirka_'+cargoid).value = '0,8';
    document.getElementById('vyska_'+cargoid).value = '';
    document.getElementById('delka_'+cargoid).readOnly = true;
    document.getElementById('sirka_'+cargoid).readOnly = true;
  } else {
    document.getElementById('delka_'+cargoid).readOnly = false;
    document.getElementById('sirka_'+cargoid).readOnly = false;
    document.getElementById('delka_'+cargoid).value = '';
    document.getElementById('sirka_'+cargoid).value = '';
    document.getElementById('vyska_'+cargoid).value = '';
  }
}
// </fux>


function checkMail(mailaddr){
	var filter = /^[a-zA-Z0-9][a-zA-Z0-9_\.\-]*\@[a-zA-Z0-9][a-zA-Z0-9\-]*\.+[a-zA-Z0-9]{2,6}$/;
	return (filter.test(mailaddr));
}


function getDigits(s) {
  return s.replace(/\D/g, '');
}


function checkTel(telcis) {
	var shorttel = /^[0-9]{9}$/;
	var longtel  = /^[0-9]{12}$/;
	return ((shorttel.test(telcis)) || (longtel.test(telcis)));
}


function formatPsc(psc) {
  psc = psc.toUpperCase();
  return psc.replace(/\s/g, '');
}


function checkPsc(psc) {
	var charpsc = /^[A-Z]{4}$/;
	var numpsc  = /^[0-9]{4,6}$/;
	return ((charpsc.test(psc) || numpsc.test(psc)))  ;
}


function checkPrice() {
  if (document.getElementById("cena") == null) return;
  cena = document.getElementById('cena').value.replace(/,/g, '.');
  if (cena == '.') cena = '0.';
  while (isNaN(cena * 1)) {
    cena = cena.substring(0,cena.length-1);
  }
  document.getElementById('cena').value = cena;
  cenasdph = To2Dec(Math.ceil(cena * 1.19 * 10)/10);
  document.getElementById('cena_s_dph').innerHTML = cenasdph;
}


function To2Dec(num) {
  var D, C, T;
  with (Math) { 
    D = floor(num); 
    C = round(100 * (num - D)); 
    T = C%10;
  }
  return D + "." + (C-T)/10 + T; 
}


function showitem(item) {
  document.getElementById(item).style.display='block';
}
function hideitem(item) {
  document.getElementById(item).style.display='none';
}


function markcountries() {
  var aCountryMPZs = new Array();
  var aCountryStrings = new Array();
  for (key in Countries) {
    if ($('cntr_'+key).checked) {
      aCountryMPZs.push(key);
      aCountryStrings.push(Countries[key]);
    }
  }
  $('staty').value = aCountryMPZs.join(";");
  $('trasa').innerHTML = aCountryStrings.join(", ");
}


function setdefaultcountry(){
  if (document.getElementById("staty") == null) return;
  if (document.getElementById('staty').value == '') {
    document.getElementById('cntr_'+'CZ').checked = true;
    markcountries();
  }
}


function checkForm() {
  var retval = true;
  var es = '';
  // Test mailove adresy
  if (checkMail(document.vlozit.email.value)) es = '';  else {
    retval = false;
    es = 'E-mailová adresa má chybný formát!';
  }
  document.getElementById('usermail_errors').innerHTML = es;

  // test telefonniho cisla
  var mobtel = getDigits(document.vlozit.mobil.value);
  document.vlozit.usermob.value = mobtel;
  if (checkTel(mobtel)) es = ''; else {
    es = 'Telefonní číslo má chybný formát!';
    retval = false;
  }
  document.getElementById('usermob_errors').innerHTML = es;

  // test PSC
  var psc = formatPsc(document.vlozit.userpsc.value);
  document.vlozit.userpsc.value = psc;
  if (checkPsc(psc)) es = '';  else {
    es = 'Směrovací číslo má chybný formát!';
    retval = false;
  }
  document.getElementById('userpsc_errors').innerHTML = es;

  // prepis vybranych prujezdnich zemi
  markcountries();
  return retval;
}


function forminit() {
  checkPrice();
  setdefaultcountry();
}

function changeLorfp() {
  try {
    
    var html =  '<label><strong>E-mail pro zaslání hesla:</strong><br /><input type="text" name="email_pwd" /></label><br />';
        html += '<input type="submit" name="pozadavek_zap_heslo" value="Poslat heslo" />';
    $('lorfp').innerHTML = html;
    return false;
  } catch (e) {
    return true;
  }
}
