document.addEventListener("DOMContentLoaded", function(event) {
  setInterval(function() {
    set_btn();
  }, 1000); //every 1 sec check for the page changes
});
 
 
 
function set_btn() {
  var booking_btns = document.getElementsByTagName('input'); //get all inputs
 
  for (let index = 0; index < booking_btns.length; index++) {
    if (booking_btns[index].type == 'submit' && booking_btns[index].value.includes('BOOK NOW')) { //if input is a submit button and has BOOK NOW in it override it with our own booking function.
      booking_btns[index].addEventListener('click', tbg_booking);
    }
 
  }
}
 
function tbg_booking(event) {
  event.preventDefault(); //stop the default behaviour
  //get the JS date of the check_in and check_out
  let check_in = jQuery("#nd_booking_archive_form_date_range_from").datepicker("getDate");
  let check_out = jQuery("#nd_booking_archive_form_date_range_to").datepicker("getDate");
 
  //values for the booking engine
  let id = 21505;
  let dc = 7025;
  let style = 16210;
 
  //create the link using the value and extracting the parts of the date
  let link = `https://book.travelbookgroup.com/premium/index2.html?gg=${('0' + check_in.getDate()).slice(-2)}&mm=${('0' + (check_in.getMonth() + 1)).slice(-2)}&aa=${check_in.getFullYear()}&ggf=${('0' + check_out.getDate()).slice(-2)}&mmf=${('0' + (check_out.getMonth() + 1)).slice(-2)}&aaf=${check_out.getFullYear()}&lingua_int=eng&id_albergo=${id}&dc=${dc}&?id_stile=${style}&countryCode=GB&tot_camere=1`;
 
  window.location = link;
}