/* javascript for html, CSS and JS template
   Copyright by Anthony Vogelaar, Perth UK
   rev 0.10  2025-02-27  Initial release */

// ====================== Global variables and events ======================

const header = document.getElementById("header")

document.getElementById("btn1").addEventListener("click", onBtnClick)
document.getElementById("btn2").addEventListener("click", onBtnClick)
document.getElementById("btn3").addEventListener("click", onBtnClick)
document.getElementById("btn4").addEventListener("click", onBtnClick)

// =============================== domain =================================

function onLoad(ev) {
   console.log('running ....')
}


function onBtnClick(ev) {
   let id = this.id
   xhttpSend(id)
}


function xhttpSend(msg) {
   var xhr = new XMLHttpRequest()
   xhr.open("POST", "httpreq", true)
   xhr.send(msg)

   xhr.onreadystatechange = function () {
      if (this.readyState == 4 && this.status == 200) {
         header.innerHTML = xhr.responseText
      }
   }
}

// ============================== start ===================================

window.addEventListener("load", onLoad)

// ============================== end =====================================
