var otazky = new Array()
var spravne = new Array()


function Otazka() {
  this.parametry = new Array()
  this.vystupy = new Array()
  this.zadani = ""
   
  
  this.setParametry = function(retez) {
  var pole = retez.split("\n");
  
  for (var i= 0; i< pole.length; i++) {
  
    this.parametry = this.parametry.concat(pole[i].split("$"))

    }
}
  this.setVystupy = function(retez) {
  var pole = retez.split("\n");
   this.vystupy = new Array()
    for (var i= 0; i< pole.length; i++) {
  
    this.vystupy = this.vystupy.concat(pole[i].split("$"))
    
    }
}
  
  
  
}

window.onload = function() {
    generateQuestions();
}

function generateQuestions() {
  var section = document.querySelector("section.genpriklady");
  var header =  section.appendChild(document.createElement("h2"));
  
  header.innerHTML = "Příklady k procvičení"
  
    
  for (var i = 0; i < examples.length; i++) {
      var ex = section.appendChild(document.createElement("div"))
      ex.className = "question";
      ex.setAttribute("data-index", i);
      ex.innerHTML = generovatNahled(i); //examples[i].task
    
  }
  
}


 function log(arg) {  return Math.log(arg)/Math.LN10 } 
 function ln(arg) { return Math.log(arg) }
 function sin(arg) { return Math.sin(arg) }
 function cos(arg) { return Math.cos(arg) }
 function tg(arg) { return Math.tan(arg) }
 function exp(arg) { return Math.exp(arg) }
 function sqrt(arg) { return Math.sqrt(arg) }
 function pow(arg1, arg2) { return Math.pow(arg1, arg2) }

  function des_carka(cislo) {
  
    var vystup = "" + cislo;
    vystup = vystup.replace(/\./g,",");
  
    if (vystup.charAt(0) ==",") vystup = "0" + vystup;
    return vystup
  }


function nahodne(spodni, horni) {

   return spodni + Math.random()*(horni-spodni)
   }
   
function zaokrouhlit(cislo, mista) {
   var cislo2 = Number(cislo)
   var pocet = Number(mista);
   return Math.round(cislo2 * Math.pow(10, pocet))/ Math.pow(10, pocet); 
   }
   
function interpol(poleX, poleY, x0) {
    var imin =0;
    var imax=0;
        
    for (var i = 1; i< poleX.length; i++) {
      if (poleX[i-1] < x0 && poleX[i] > x0) {
        imin = i-1;
        imax = i;
        break;
        
      }
    }
    
    return poleY[imin] + (poleY[imax]-poleY[imin])/(poleX[imax]-poleX[imin])*(x0 - poleX[imin])  
      
   }


function regenerate(node) {
    var question = node.parentNode.parentNode
    var index = parseInt(question.getAttribute("data-index"));
    
    question.innerHTML = generovatNahled(index);

}

function formatovatCislo(cislo) {
   cislo = Number(cislo);
  
    return cislo 
   }

function generovatNahled(index) {
  
    var vystup ="";
    var otazka = examples[index];   
    var task = otazka.task //priklady[index*3 + 2]
   
    otazka.used = [];
    
    for (var i= 0; i< otazka.param_in.length; i++) {
   
        var vals = otazka.param_in[i].split(";");
        var newParam = { name: vals[0], value: 0};
        
        for (var j = 0; j < otazka.used.length; j++) {
            var par = otazka.used[j]; 
            vals[1] = vals[1].replace(new RegExp(par.name, "g"), par.value )
        }
        
        nahrada = new RegExp("<" + newParam.name + ">","g");
        newParam.value = eval(vals[1]);
                
        if (vals[2].indexOf("nz") < 0) {
            newParam.value = zaokrouhlit(newParam.value, vals[2])
        }
       
       otazka.used.push(newParam);
       task = task.replace(nahrada, newParam.value) 
      
    }
      
    //vstupni policka 
    
    spravne[index] = new Array()
      
      for (i= 0; i<  otazka.param_out.length; i++) { 
          
          var vals = otazka.param_out[i].split(";");
          var newParamOut = { name: vals[0], value: 0, tol:0};
          
          for (var j = 0; j < otazka.used.length; j++) {
            var par = otazka.used[j]; 
            vals[1] = vals[1].replace(new RegExp(par.name, "g"), par.value )
          }
          newParamOut.value = eval(vals[1]);
          var rounded = parseInt(vals[2])
      
        var nazev = ":n" + (i+1);
      
        newParamOut.value = zaokrouhlit( newParamOut.value, rounded )
        var tolerance = parseFloat(vals[3])
        newParamOut.tol = tolerance;
        
        var nahrada = new RegExp("<" + newParamOut.name + ">","g")
     
      
      task = task.replace(nahrada,"  ")
      
    
      spravne[index].push(newParamOut);
      
        otazka.used.push(newParamOut);
      }
     task += "
 
" task += "
" task += "
" return vystup + task; } function checkAnswers(index) { var ok = true; var vystup =""; var question = document.querySelectorAll(".question")[index] var fb = question.querySelector(".feedback") var inputs = question.querySelectorAll("input[type=text]"); for (var i =0; i< inputs.length; i++) { var pname = inputs[i].name; pname = pname.substring(pname.indexOf("_") + 1); var hodnota = inputs[i].value; hodnota = hodnota.replace(/\s/g, ""); hodnota = hodnota.replace(/,/g, "."); hodnota = Number(hodnota); if (isNaN(hodnota)) { fb.innerHTML = "Špatně. Zadejte číslo!"; return; } var spr = getCorrect(index, pname); var tolerance = spr.tol; var spravna = spr.value; alert(spravna) if (hodnota > spravna + tolerance || hodnota < spravna - tolerance) ok = false; } if (ok) vystup = "Správně." else vystup = "Špatně."; fb.innerHTML = vystup; } function getCorrect(index, name) { for(var i = 0; i < spravne[index].length; i++) { if (spravne[index][i].name == name) return spravne[index][i]; } return null; } //pomocne funkce function getValue(nodeId) { return document.getElementById(nodeId).value; } function setValue(nodeId, code) { document.getElementById(nodeId).value = code; } function setHTML(nodeId,code) { document.getElementById(nodeId).innerHTML = code; } function getHTML(nodeId) { return document.getElementById(nodeId).innerHTML; } function show(nodeId) { document.getElementById(nodeId).style.visibility = "visible"; } function hide(nodeId) { document.getElementById(nodeId).style.visibility = "hidden"; } function showBlock(nodeId) { document.getElementById(nodeId).style.display = "block"; } function hideBlock(nodeId) { document.getElementById(nodeId).style.display = "none"; } function disable (nodeId) { document.getElementById(nodeId).disabled = true; } function enable (nodeId) { document.getElementById(nodeId).disabled = false; }