//const saiahind=1.50;
const juustuhind=1.0;
const pontsikhind=2.0;
const magushind=3.0;
function calculate(val,saiatypp){
return (val*saiatypp).toFixed(2);
}
//kontrollib et kogus väli ei ole tühi
function validateForm() {
let x = document.forms[“kalk”][“kogus”].value;
if (x === “”) {
alert(“Palun sisesta kogus! “);
return false;
}
}
//arvutame hind mis sõltub tekstist tekstkaatist
function inputTextValue(){
tyhistaRadio();
tyhistaselectOption();
validateForm();
let answer=document.getElementById(“vastus”);
let kogus=document.getElementById(“kogus”);
let inputValue=kogus.value;
let saiatypp=document.getElementById(“saiatypp”).value
if (saiatypp===”juustu”){
answer.innerHTML=calculate(inputValue, juustuhind) + ” €”;
}
else if (saiatypp===”pontsik”){
answer.innerHTML=calculate(inputValue, pontsikhind) + ” €”;
}
else if (saiatypp===”magus”){
answer.innerHTML=calculate(inputValue, magushind) + ” €”;
}
else{
answer.innerHTML=”Saia tüüp ei ole määratud!”;
}
}
function tyhistaTextValue(){
document.getElementById(“saiatypp”).value=””;
}
//puhastab select valik
function tyhistaselectOption(){
document.getElementById(“saiatypp2”).selectedIndex=0;
}
function tyhistaRadio(){
let elem=document.getElementsByName(“saiatypp3”);
for(let i=0; i<elem.length; i++){
elem[i].checked=false;
}
}
function tyhistaCheckbox(){
let elem=document.getElementsByName("saiatypp3");
for(let i=0; i<elem.length; i++){
elem[i].checked=false;
}
}
//puhustab
function selectOptionChange(event){
tyhistaTextValue();
tyhistaCheckbox();
tyhistaRadio();
validateForm();
let answer=document.getElementById("vastus");
let kogus=document.getElementById("kogus");
let inputValue=kogus.value;
let saiatypp2=event.target.value
if (saiatypp2==="juustu"){
answer.innerHTML=calculate(inputValue, juustuhind) + " €";
}
else if (saiatypp2==="pontsik"){
answer.innerHTML=calculate(inputValue, pontsikhind) + " €";
}
else if (saiatypp2==="magus"){
answer.innerHTML=calculate(inputValue, magushind) + " €";
}
else{
answer.innerHTML="Saia tüüp ei ole määratud!";
}
}
//radio nuppu valikud
function radioChange(event){
tyhistaselectOption();
tyhistaTextValue();
validateForm();
tyhistaCheckbox();
let answer=document.getElementById("vastus");
let kogus=document.getElementById("kogus");
let inputValue=kogus.value;
if(event.target.id ==="juustu"){
answer.innerHTML=calculate(inputValue, juustuhind) +" euro"
}
else if(event.target.id==="magus"){
answer.innerHTML=calculate(inputValue, magushind) +" euro"
}
else if(event.target.id==="pontsik"){
answer.innerHTML=calculate(inputValue, pontsikhind) +" euro"
}
else{
answer.innerHTML="Saia tüüp määratud!";
}
}
function checkboxChange() {
let answer = document.getElementById("vastus");
let kogus = document.getElementById("kogus");
let inputValue = kogus.value;
let total = 0; // Initialize total price
// Check if the checkbox is checked and calculate price for each type
if (document.getElementById("juustu2").checked) {
total += inputValue * juustuhind;
}
if (document.getElementById("magus2").checked) {
total += inputValue * magushind;
}
if (document.getElementById("pontsik2").checked) {
total += inputValue * pontsikhind;
}
// Format the total price with two decimal places
let formattedTotal = total.toFixed(2);
// Update the answer
answer.textContent = "Hind on " + formattedTotal + " euro";
}
//arvutab kindla hinnaga saia hind
//function hind(){
//let answer=document.getElementById("vastus");
//let kogus=document.getElementById("kogus");
//answer.innerHTML="Hind on " + (kogus.value*saiahind).toFixed(2) + " €";
//toFixed(2) – 2 märki peaale komat
//