function showById(check_id, form_id)
{
     var form = document.getElementById(form_id);
     var check = document.getElementById(check_id);
     
     if (check.checked == true)
      form.style.display = 'block';
      
     else
      form.style.display = 'none';
}

/* získá info o variantě produktu */

function getVariantaInfo(id_varianty) 
{       
  
        var http_request = false;
        var request = "id_varianty="+id_varianty;
    
        if (window.XMLHttpRequest) {
            http_request = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            try {
              http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (eror) {
              http_request = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
  
        http_request.onreadystatechange = function() { changeVariantaInfo(http_request); };
        http_request.open('POST', '../scripts/getVariantaInfo.php', true);
        http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http_request.send(request);
        
}

// změní variatu
function changeVariantaInfo(http_request) {
        
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                        
                var xmlDoc = http_request.responseXML.documentElement;
                              
                var item_xml;
                
                // změna kat. cisla
                item_xml = xmlDoc.getElementsByTagName("katalogove_cislo")[0].childNodes[0].nodeValue;
                document.getElementById("katalogove_cislo").innerHTML = item_xml;
                
                
                // výpočet ceny a slevy
                var cena_s_dph;
                var sleva;
                var price_info;
                var table_price;
                var tr;
                var td;
                var span;
                var tbody;
                
                cena_s_dph = xmlDoc.getElementsByTagName("cena_s_dph")[0].childNodes[0].nodeValue;
                sleva = xmlDoc.getElementsByTagName("sleva")[0].childNodes[0].nodeValue;
                
                //odstraní tabulku s cenama
                table_price = document.getElementById("price");
                price_info = table_price.parentNode;
                table_price.parentNode.removeChild(table_price);
                
                // pokud je sleva
                if (sleva != 0)
                {
                  table_price = document.createElement("table");
                  table_price.setAttribute("id", "price");
                  
                  tbody = document.createElement("tbody");
                  
                  // ---- cena s DPH ----
                  tr = document.createElement("tr");
                  
                  //popis
                  td = document.createElement("td");
                  td.className = "blue";
                  span = document.createElement("span");
                  span.className = "normal";
                  span.innerHTML = "Původní cena s DPH:";
                  td.appendChild(span);
                  tr.appendChild(td);
                  //konec
                  
                  //obsah
                  td = document.createElement("td");
                  td.className = "cena";
                  span = document.createElement("span");
                  span.setAttribute("id", "cena_s_dph");
                  span.innerHTML = cena_s_dph;
                  td.appendChild(span);
                  tr.appendChild(td);
                  td = document.createElement("td");
                  td.className = "korun";
                  td.innerHTML = "Kč";
                  tr.appendChild(td);
                  //konec
                  
                  tbody.appendChild(tr);
                  // ---- konec ----
                  
                  // ---- sleva ----
                  tr = document.createElement("tr");
                  
                  //popis
                  td = document.createElement("td");
                  td.className = "blue";
                  td.innerHTML = "Sleva:";
                  tr.appendChild(td);
                  //konec
                  
                  //obsah
                  td = document.createElement("td");
                  td.className = "cena";
                  span = document.createElement("span");
                  span.setAttribute("id", "sleva");
                  span.innerHTML = sleva;
                  td.appendChild(span);
                  tr.appendChild(td);
                  td = document.createElement("td");
                  td.className = "korun";
                  td.innerHTML = "Kč";
                  tr.appendChild(td);
                  //konec
                  
                  tbody.appendChild(tr);
                  // ---- konec ----
                  
                  
                  // ---- cena po slevě ----
                  tr = document.createElement("tr");
                  
                  //popis
                  td = document.createElement("td");
                  td.className = "blue";
                  td.style.fontWeight = "bold";
                  span = document.createElement("span");
                  span.className = "normal";
                  span.innerHTML = "Cena s DPH po slevě:";
                  td.appendChild(span);
                  tr.appendChild(td);
                  //konec
                  
                  //obsah
                  td = document.createElement("td");
                  td.className = "cena";
                  span = document.createElement("span");
                  span.setAttribute("id", "cena_sleva");
                  span.innerHTML = cena_s_dph-sleva;
                  td.appendChild(span);
                  tr.appendChild(td);
                  td = document.createElement("td");
                  td.className = "korun";
                  td.style.color = "#D9527A";
                  td.innerHTML = "Kč";
                  tr.appendChild(td);
                  //konec
                  
                  tbody.appendChild(tr);
                  table_price.appendChild(tbody);
                  // ---- konec ----
                  
                  price_info.appendChild(table_price);
                }
                
                else
                {
                  table_price = document.createElement("table");
                  table_price.setAttribute("id", "price");
                  
                  tbody = document.createElement("tbody");
                  
                  // ---- cena po slevě ----
                  tr = document.createElement("tr");
                  
                  //popis
                  td = document.createElement("td");
                  td.className = "blue";
                  td.style.fontWeight = "bold";
                  span = document.createElement("span");
                  span.className = "normal";
                  span.innerHTML = "Cena s DPH:";
                  td.appendChild(span);
                  tr.appendChild(td);
                  //konec
                  
                  //obsah
                  td = document.createElement("td");
                  td.className = "cena";
                  span = document.createElement("span");
                  span.setAttribute("id", "cena_sleva");
                  span.innerHTML = cena_s_dph;
                  td.appendChild(span);
                  tr.appendChild(td);
                  td = document.createElement("td");
                  td.className = "korun";
                  td.style.color = "#D9527A";
                  td.innerHTML = "Kč";
                  tr.appendChild(td);
                  //konec
                  
                  tbody.appendChild(tr);
                  table_price.appendChild(tbody);
                  // ---- konec ----
                  
                  price_info.appendChild(table_price);
                }
                
                item_xml = xmlDoc.getElementsByTagName("pocet_kusu")[0].childNodes[0].nodeValue;
                document.getElementById("pocet_kusu").innerHTML = item_xml;
                
                //pokud je pocet kusu 0 => zneaktivnit přidání do košíku
                if (parseInt(item_xml) == 0 || !item_xml)
                {
                  document.getElementById("produkt_kosik_add").style.visibility = 'hidden';
                }
                
                else
                {
                  document.getElementById("produkt_kosik_add").style.visibility = 'visible';
                }
                
                item_xml = xmlDoc.getElementsByTagName("popis_velikosti")[0].childNodes[0].nodeValue;
                document.getElementById("popis_velikosti").innerHTML = item_xml;
            } 
        }
}

// fce pro získání nákladů přepravy a platby
function getDeliveryCosts()
{
  var doprava = document.getElementsByName('doprava');
  var platba = document.getElementsByName('platba');
  var doprava_id = 0;
  var platba_id = 0;
  
  for (var i=0; i<doprava.length; i++)
  {
    if (doprava[i].checked)
      doprava_id = doprava[i].value;
  }
  
  for (var i=0; i<platba.length; i++)
  {
    if (platba[i].checked)
      platba_id = platba[i].value;
  }
  
  // kontrola kombinace platba-doprava
  if (platba_id == '1')//pokud je dobírka
  {
    document.getElementById("doprava_2").style.visibility = 'hidden';
    document.getElementById("doprava_1").checked = 'checked';
    doprava_id = 1;
  }
  
  else
    document.getElementById("doprava_2").style.visibility = 'visible';
  
  
  getCost(doprava_id, platba_id);
  getDeliveryInfo(doprava_id) ;
}

function addDeliveryCosts(http_request) {
    
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
                  
            
            var cost = parseInt(http_request.responseText); 
            var cena_produktu = parseInt(document.getElementById('cena_produktu').childNodes[0].nodeValue.replace(/ /, ""));// získá cenu produktů v objednávce
           
            var cena_dopravy = document.getElementById('cena_dopravy');
            cena_dopravy.innerHTML = " + "+cost;
            
            var cena_celkem = document.getElementById('cena_celkem');
            cena_celkem.innerHTML = " = "+(cena_produktu+cost)+" Kč";
        } 
    }
}

function getCost(doprava_id, platba_id) 
{
    var http_request = false;
    var cena_produktu = parseInt(document.getElementById('cena_produktu').childNodes[0].nodeValue.replace(/ /, ""));// získá cenu produktů v objednávce
            
    var request = "cena_produktu="+cena_produktu+"&doprava_id="+doprava_id+"&platba_id="+platba_id;

    if (window.XMLHttpRequest) {
        http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
          http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (eror) {
          http_request = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    http_request.onreadystatechange = function() {addDeliveryCosts(http_request); };
    http_request.open('POST', '../scripts/getCosts.php', true);
    http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http_request.send(request);
    
}

function showDeliveryInfo(http_request) {
    
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
                  
            
            var delivery_info = http_request.responseText; 
            
            document.getElementById('delivery_info').innerHTML = delivery_info;
        
        } 
    }
}

function getDeliveryInfo(doprava_id) 
{
    var http_request = false;
            
    var request = "doprava_id="+doprava_id;

    if (window.XMLHttpRequest) {
        http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
          http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (eror) {
          http_request = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    http_request.onreadystatechange = function() {showDeliveryInfo(http_request); };
    http_request.open('POST', '../scripts/getDeliveryInfo.php', true);
    http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http_request.send(request);
    
}


function showByName(check_id, name)
{
 var check = document.getElementById(check_id);
 var name_array = document.getElementsByName(name);
 var display;
 
 if (check.checked == true)
  display = 'block';
  
 else
  display = 'none';
  
 for(var i=0; i<name_array.length;i++)
 {
   name_array[i].style.display = display;
 } 
}

// otevře nove okno pro maily
function openMailWindow(adresa)
{
      window.open("", "mail_window", "width=600,height=600,resizable=yes,left=0,top=0,scrollbars=yes");
      document.form_produkt_mail.action = adresa+"/modules/produkt_mailem.php";
      document.form_produkt_mail.target="mail_window";
}

// otevře nove okno pro maily
function openPrintWindow(adresa)
{
      window.open("", "print_window", "width=800,height=600,resizable=yes,left=0,top=0,scrollbars=yes");
      document.form_produkt_print.action = adresa+"/modules/produkt_print.php";
      document.form_produkt_print.target="print_window";
}


