function loadJSON(url) {
  var headID = document.getElementsByTagName("head")[0];         
  
  //check if we already have a JSONLoader then delete it
  var oldScript = document.getElementById('JSONLoader');
  if(oldScript) headID.removeChild(oldScript);
  
  //create a new JSONLoader
  var newScript = document.createElement('script');
  newScript.type = 'text/javascript';
  newScript.src = url;
  newScript.defer = true;
  newScript.id = 'JSONLoader';
  headID.appendChild(newScript);
}

//called every second
function processJSON(data){
  var txt=document.getElementById("requirements");
  
  var l_all_needed = document.getElementById("l_all_needed").innerHTML;
  var l_all_insufficient = document.getElementById("l_all_insufficient").innerHTML;
  var l_fp180_needed = document.getElementById("l_fp180_needed").innerHTML;
  var l_fp380_needed = document.getElementById("l_fp380_needed").innerHTML;
  var l_fp600_needed = document.getElementById("l_fp600_needed").innerHTML;
  var l_fp1900_needed = document.getElementById("l_fp1900_needed").innerHTML;
  
  var content = "";
  
  if (parseInt(data.fp180) >= 1){
     content = l_all_needed.replace(/__liters__/g, data.liters);
     content += l_fp180_needed.replace(/__fp180__/g, data.fp180);
     if (parseInt(data.fp380) >= 1){
        content += l_fp380_needed.replace(/__fp380__/g, data.fp380);
        if (parseInt(data.fp600) >= 1){
           content += l_fp600_needed.replace(/__fp600__/g, data.fp600);
           if (parseInt(data.fp1900) >= 1){
              content += l_fp1900_needed.replace(/__fp1900__/g, data.fp1900);
           }	
        }	
     }	
  }
  else{
     content = l_all_insufficient;
  }
  txt.innerHTML = content;
}

function initForm(){
   //set spray as default	
   document.forms["myform"].application[0].checked = true;
   
   var txt = document.getElementById("requirements");
   var l_requirements = document.getElementById("l_requirements").innerHTML;
   var content = l_requirements;
   txt.innerHTML = content;
}

function getData(){
   var volume = parseFloat(document.getElementById("volume").value);
   if (isNaN(volume)){
      volume = 0;
   }
   
   var application = "";
   for (var i=0; i < document.forms["myform"].application.length; i++){
      if (document.forms["myform"].application[i].checked){
         application = document.forms["myform"].application[i].value;
      }
   }
   if (volume > 0 && (application == "spray" || application == "injection")){
      var request = 'core/calc/calc.php?a=' + application + '&v=' + volume + '&callback=processJSON&rnd='+ Math.random();
      loadJSON(request);
   }
   else{
      var txt = document.getElementById("requirements");
      var l_requirements = document.getElementById("l_requirements").innerHTML;
      var content = l_requirements;
      txt.innerHTML = content;
   }
}

function calc(){
   var surface = parseFloat(document.getElementById("surface").value);
   if (isNaN(surface)){
      surface = 0;
   }
   var thickness = parseFloat(document.getElementById("thickness").value);
   if (isNaN(thickness)){
      thickness = 0;
   }
   document.getElementById("volume").value = surface * thickness /100;
   getData();
}

function clearAll(){
   document.getElementById("thickness").value = 0;
   document.getElementById("surface").value = 0;
   getData();
}


