function send_xmlhttprequest(obsluha, id, 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,id); };
  if (headers) {
    for (var key in headers) { xmlhttp.setRequestHeader(key, headers[key]); }
  }
  xmlhttp.send(content);
  return true;
}

//ajax fce pro nacteni obsahu
function read_content(url, id){
if(url=="" && document.getElementById(id)){
    document.getElementById(id).innerHTML="";
  } else {
    // odeslání požadavku na aktualizaci dat
    if (document.getElementById(id) && !send_xmlhttprequest(list_obsluha, id, 'GET', url)) return false;
  }
}

function list_obsluha(xmlhttp,id) {
  if (xmlhttp.readyState == 4) {
    if (id != ""  && document.getElementById(id)) document.getElementById(id).innerHTML=xmlhttp.responseText;
  }
}

function send_xmlhttprequest_standart(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 nactiList(parent,list,table,sublists){
  // odeslání požadavku na aktualizaci dat
  var stav = document.getElementById(parent).value;
  if (stav == 0){
    while (document.getElementById(list).childNodes.length > 0) {
      document.getElementById(list).removeChild(document.getElementById(list).childNodes[0])
    }
    id = 0;
    text = "-Vyberte-";
    document.getElementById(list).options[document.getElementById(list).options.length] = new Option(text,id);
  }
  if(sublists != ""){
    var arraySubListMe = sublists.split('-');
    for (key in arraySubListMe){
      item = arraySubListMe[key];
      if (document.getElementById(item)){
        while (document.getElementById(item).childNodes.length > 0) {
          document.getElementById(item).removeChild(document.getElementById(item).childNodes[0])
        }
        id = 0;
        text = "-Vyberte-";
        document.getElementById(item).options[document.getElementById(item).options.length] = new Option(text,id);
      }
    }
  }
  if (!send_xmlhttprequest_standart(listLocation, 'GET', '/interface/script/getList.php?id='+stav+'&next='+list+'&table='+table)) {
    return false;
  }
}

function listLocation(xmlhttp) {
  if (xmlhttp.readyState == 4) {
    // aktualizace odpovědí na základě aktuálního stavu
    var odpovedi = xmlhttp.responseXML.getElementsByTagName('polozka');
    if(odpovedi.length > 0){
      var list = odpovedi[0].getAttribute('prvek');
      while (document.getElementById(list).childNodes.length > 0) {
        document.getElementById(list).removeChild(document.getElementById(list).childNodes[0])
      }
      document.getElementById(list).options[document.getElementById(list).options.length] = new Option('-Vyberte-',0);
      for (var i=0; i < odpovedi.length; i++) {
        id = odpovedi[i].getAttribute('id');
        text = odpovedi[i].getAttribute('text');
        document.getElementById(list).options[document.getElementById(list).options.length] = new Option(text,id);
      }
    }
  }
}

function count(){
 var cena_demanded=parseFloat(document.getElementById('demandedpriceID').value,10);
 var cena_minus=parseFloat(document.getElementById('provisionminusID').value,10);
 var cena_plus=parseFloat(document.getElementById('provisionplusID').value,10);

 if (isNaN(cena_minus))cena_minus=0;
 if (isNaN(cena_plus))cena_plus=0;
 var money=cena_plus;
 var cena_minus_percent=parseFloat(document.getElementById('provisionpercentminusID').value,10);
 var cena_plus_percent=parseFloat(document.getElementById('provisionpercentID').value,10);
 if (isNaN(cena_minus_percent))cena_minus_percent=0;
 if (isNaN(cena_plus_percent))cena_plus_percent=0;
 var percent=cena_plus_percent ;
 if (isNaN(cena_demanded))cena_demanded=0;
 document.getElementById('priceID').value=Math.round((cena_demanded+money)*((100+percent)/100));
}
