const observer = lozad(); // lazy loads elements with default selector as '.lozad'
observer.observe();

var locationCourses={1:{title:"Melbourne (Collins Street)",courses:[{type:"group",value:"",caption:"Hospitality Training",style:"",url:""},{type:"course",value:"1",caption:"RSA | Responsible Service Of Alcohol",style:"",url:"/rsa-course/vic/melbourne"},{type:"course",value:"2",caption:"New Entrant Liquor License Training",style:"",url:"/new-entrant-liquor-license-training/vic/melbourne"},{type:"course",value:"4",caption:"FSH | Food safety Handler",style:"",url:"/food-safety-handler-course/vic/melbourne"},{type:"course",value:"5",caption:"FSS | Food safety Supervisor",style:"",url:"/food-safety-supervisor-fss-course/vic/melbourne"},{type:"course",value:"3",caption:"Barista Coffee Course",style:"",url:"/coffee-course/vic/melbourne"},{type:"course",value:"6",caption:"Prepare and serve espresso coffee (Full Unit)",style:"",url:"/prepare-serve-espresso-coffee/vic/melbourne"},{type:"course",value:"16",caption:"Prepare & serve non alcoholic beverages (Full unit)",style:"",url:"/prepare-serve-non-alcoholic-beverages/vic/melbourne"},{type:"course",value:"7",caption:"Waiter Training",style:"/waiter-course/vic/melbourne",url:""},{type:"course",value:"17",caption:"Provide Service to Customers (Full Unit)",style:"/provide-service-to-customers/vic/melbourne",url:""},{type:"course",value:"18",caption:"JOB CAMP Summer School",style:"/job-camp/vic/summer-school",url:""},{type:"group",value:"",caption:"First Aid and CPR",style:"",url:""},{type:"course",value:"8",caption:"CPR | Provide Cardiopulmonary Resuscitation",style:"",url:"/cpr-course/vic/melbourne"},{type:"course",value:"9",caption:"Provide First Aid",style:"",url:"/first-aid-course/vic/melbourne"},{type:"group",value:"",caption:"Work Safely with Construction",style:"",url:""},{type:"course",value:"10",caption:"White Card | Work Safely in the Construction Industry",style:"",url:"/white-card-course/vic/melbourne"},{type:"course",value:"11",caption:"Elevated Work Platform",style:"",url:"/ewp-elevated-work-platform/vic/melbourne"},{type:"course",value:"38",caption:"Licence to operate a forklift truck",style:"",url:"/forklift-course/vic/melbourne"},{type:"group",value:"",caption:"Rail Industry",style:"",url:""},{type:"course",value:"12",caption:"Safety Access the Rail Corridor",style:"",url:"/safety-access-the-rail-corridor-course/vic/melbourne"},{type:"group",value:"",caption:"Traffic Control",style:"",url:""},{type:"course",value:"13",caption:"Control Traffic With Stop Slow Bat",style:"",url:"/control-traffic-with-stop-slow-bat/vic/melbourne"},{type:"course",value:"14",caption:"Implement Traffic Management Plan",style:"",url:"/implement-traffic-management-plan/vic/melbourne"},{type:"group",value:"",caption:"Building, Pest and Pool inspection",style:"",url:""},{type:"course",value:"15",caption:"Inspect For and Report on Timber Pest",style:"",url:"/building-and-pest-inspection-course/vic/melbourne"},{type:"course",value:"16",caption:"Swimming Pool and Spa Barrier Inspection",style:"",url:"/pool-barrier-safety-course/vic/melbourne"},{type:"course",value:"17",caption:"Pre Purchase Building Inspection Course",style:"",url:"/pre-purchase-building-inspection-course/vic/melbourne"}]},2:{title:"Port Melbourne (Anderson Street)",courses:[{type:"group",value:"",caption:"First Aid and CPR",style:"",url:""},{type:"course",value:"8",caption:"CPR | Provide Cardiopulmonary Resuscitation",style:"",url:"/cpr-course/vic/melbourne"},{type:"course",value:"9",caption:"Provide First Aid",style:"",url:"/first-aid-course/vic/melbourne"},{type:"group",value:"",caption:"Work Safely with Construction",style:"",url:""},{type:"course",value:"10",caption:"White Card | Work Safely in the Construction Industry",style:"",url:"/white-card-course/vic/melbourne"},{type:"course",value:"11",caption:"Elevated Work Platform",style:"",url:"ewp-elevated-work-platform/vic/melbourne"},{type:"course",value:"38",caption:"Licence to operate a forklift truck",style:"",url:"/forklift-course/vic/melbourne"},{type:"group",value:"",caption:"Rail Industry",style:"",url:""},{type:"course",value:"12",caption:"Safety Access the Rail Corridor",style:"",url:"/safety-access-the-rail-corridor-course/vic/melbourne"},{type:"group",value:"",caption:"Traffic Control",style:"",url:""},{type:"course",value:"13",caption:"Category 1 - combined TC1 & TM1 Traffic controller for Low Volume Roads",style:"",url:"/traffic-controller-implementer-tc1-tmi1/melbourne"},{type:"course",value:"20",caption:"Category 2 - combined TC2 & TM2 Traffic Management for High Volume Roads",style:"",url:"/traffic-management-tc2-tmi2/melbourne"},{type:"course",value:"14",caption:"Implement Traffic Management Plan",style:"",url:"/implement-traffic-management-plan/vic/melbourne"},{type:"group",value:"",caption:"Building, Pest and Pool inspection",style:"",url:""},{type:"course",value:"15",caption:"Inspect For and Report on Timber Pest",style:"",url:"/building-and-pest-inspection-course/vic/melbourne"},{type:"course",value:"16",caption:"Swimming Pool and Spa Barrier Inspection",style:"",url:"/pool-barrier-safety-course/vic/melbourne"},{type:"course",value:"19",caption:"Pre Purchase Building Inspection Course",style:"",url:"/pre-purchase-building-inspection-course/vic/melbourne"},{type:"group",value:"",caption:"Hospitality Training",style:"",url:""},{type:"course",value:"1",caption:"RSA | Responsible Service Of Alcohol",style:"",url:"/rsa-course/vic/melbourne"},{type:"course",value:"2",caption:"New Entrant Liquor License Training",style:"",url:"/new-entrant-liquor-license-training/vic/melbourne"},{type:"course",value:"4",caption:"FSH | Food safety Handler",style:"",url:"/food-safety-handler-course/vic/melbourne"},{type:"course",value:"5",caption:"FSS | Food safety Supervisor",style:"",url:"/food-safety-supervisor-fss-course/vic/melbourne"},{type:"course",value:"3",caption:"Barista Coffee Course",style:"",url:"/coffee-course/vic/melbourne"},{type:"course",value:"6",caption:"Prepare and serve espresso coffee (Full Unit)",style:"",url:"/prepare-serve-espresso-coffee/vic/melbourne"},{type:"course",value:"16",caption:"Prepare & serve non alcoholic beverages (Full unit)",style:"",url:"/prepare-serve-non-alcoholic-beverages/vic/melbourne"},{type:"course",value:"7",caption:"Waiter Training",style:"/waiter-course/vic/melbourne",url:""},{type:"course",value:"17",caption:"Provide Service to Customers (Full Unit)",style:"/provide-service-to-customers/vic/melbourne",url:""},{type:"course",value:"18",caption:"JOB CAMP Summer School",style:"/job-camp/vic/summer-school",url:""}]},3:{title:"Sydney (Wattle Street Ultimo)",courses:[{type:"group",value:"",caption:"Hospitality Training",style:"",url:""},{type:"course",value:"1",caption:"RSA | Responsible Service Of Alcohol",style:"",url:"/rsa-course/nsw/sydney"},{type:"course",value:"2",caption:"Responsible Conduct of Gambling (RCG)",style:"",url:"/gaming-course/nsw/sydney"},{type:"course",value:"6",caption:"LT & ALT - Liquor Licensees Training",style:"",url:"/licencees-training-lt/nsw/sydney"},{type:"course",value:"11",caption:"ARCG - ARCG - Advanced Gambling Supervisor",style:"",url:"/arcg-advanced-responsible-of-gaming/nsw/sydney"},{type:"course",value:"4",caption:"FSH | Food safety Handler",style:"",url:"/food-safety-handler-course/nsw/sydney"},{type:"course",value:"5",caption:"FSS | Food safety Supervisor",style:"",url:"/food-safety-supervisor-fss-course/nsw/sydney"},{type:"course",value:"3",caption:"Barista Coffee Course",style:"",url:"/coffee-course/nsw/sydney"},{type:"course",value:"7",caption:"Waiter Training",style:"/waiter-course/nsw/sydney",url:""},{type:"group",value:"",caption:"First Aid and CPR",style:"",url:""},{type:"course",value:"8",caption:"CPR | Provide Cardiopulmonary Resuscitation",style:"",url:"/cpr-course/nsw/sydney"},{type:"course",value:"9",caption:"Provide First Aid",style:"",url:"/first-aid-course/nsw/sydney"},{type:"group",value:"",caption:"Work Safely with Construction",style:"",url:""},{type:"course",value:"10",caption:"White Card | Work Safely in the Construction Industry",style:"",url:"/white-card-course/nsw/sydney"}]},4:{title:"Parramatta (Church Street)",courses:[{type:"group",value:"",caption:"Hospitality Training",style:"",url:""},{type:"course",value:"1",caption:"RSA | Responsible Service Of Alcohol",style:"",url:"/rsa-course/nsw/parramatta"},{type:"course",value:"2",caption:"Responsible Conduct of Gambling (RCG)",style:"",url:"/gaming-course/nsw/parramatta"},{type:"course",value:"11",caption:"ARCG - ARCG - Advanced Gambling Supervisor",style:"",url:"/arcg-advanced-responsible-of-gaming/nsw/parramatta"},{type:"course",value:"4",caption:"FSH | Food safety Handler",style:"",url:"/food-safety-handler-course/nsw/parramatta"},{type:"course",value:"5",caption:"FSS | Food safety Supervisor",style:"",url:"/food-safety-supervisor-fss-course/nsw/parramatta"},{type:"course",value:"3",caption:"Barista Coffee Course",style:"",url:"/coffee-course/nsw/parramatta"},{type:"course",value:"7",caption:"Waiter Training",style:"/waiter-course/nsw/parramatta",url:""},{type:"group",value:"",caption:"First Aid and CPR",style:"",url:""},{type:"course",value:"8",caption:"CPR | Provide Cardiopulmonary Resuscitation",style:"",url:"/cpr-course/nsw/parramatta"},{type:"course",value:"9",caption:"Provide First Aid",style:"",url:"/first-aid-course/nsw/parramatta"},{type:"group",value:"",caption:"Work Safely with Construction",style:"",url:""},{type:"course",value:"10",caption:"White Card | Work Safely in the Construction Industry",style:"",url:"/white-card-course/nsw/parramatta"}]}};


function loadDateTime(caller){
if(caller==1){
var loc=document.getElementById('location-select').value;
var courseList=locationCourses[loc].courses;
let courseSelector = $('#course-select');
courseSelector.empty();
courseSelector.append($('<option></option>').attr('value', 0).attr('selected','selected').text('Select Course'));
courseList.forEach((course)=>{
if(course.type=='group'){
courseSelector.append($('<optgroup id="'+11+'" ></optgroup>').attr('label',course.caption));
}else{
courseSelector.append($('<option></option>').attr('value', course.url).text(course.caption));
}
});

}else{
let checkerButton = $('#book-now-button');
checkerButton.attr('href',document.getElementById('course-select').value);
checkerButton.removeClass('disabled');
}
}

function selectLocation(course){
$("#melbourne-link").attr("href","/"+course+"/vic/melbourne");
$("#port-melbourne-link").attr("href","/"+course+"/vic/melbourne");
$("#ringwood-link").attr("href","/"+course+"/vic/ringwood");
$("#dandenong-link").attr("href","/"+course+"/vic/dandenong");
$("#sunshine-link").attr("href","/"+course+"/vic/sunshine");
}

function bookingPopUp(){
var crs_value=document.getElementById('course-select').value;
var loc_value=document.getElementById('location-select').value;
var occ_value=document.getElementById('date-select').value;
var crs_text=$( "#course-select option:selected" ).text();
var loc_text=$( "#location-select option:selected" ).text();
var occ_text=$( "#date-select option:selected" ).text();

document.getElementById('socc').value=occ_value;
document.getElementById('occt').innerText=crs_text;
document.getElementById('occl').innerHTML= loc_text;
document.getElementById('occd').innerText= occ_text;
document.getElementById('occdt').innerText='';
document.getElementById('occp').innerText=''
}



function toggle_highlight_ID(id){
$('#'+id).toggleClass("bg-warning text-dark p-2 rounded-4");
}

var recs=20;

function loadMoreDates(rcb){
var page=$("#page-number").val();
$.ajax({
   type: "POST",
  url: window.location.href,
  data: "ajax=true&page="+(parseInt(page)+1+"&rcb="+rcb),
}).done(function(response) {
$("#list-body" ).append( response );
$("#page-number").val(parseInt(page)+1);
$("#rec-so-far").text(parseInt($("#rec-so-far").text())+20);
});
}


function updateLocationLinks(locations) {
    const unavailableText = '(Not available at this location at the moment)';
    for (const [location, data] of Object.entries(locations)) {
        const linkId = `${location}-link`;
        const linkElement = document.getElementById(linkId);

        if (!linkElement) continue;

        // Clean previously appended unavailable text
        linkElement.innerHTML = linkElement.innerHTML.replace(unavailableText, '');

        if (data.url && data.url.trim() !== "") {
            linkElement.href = data.url.startsWith("/") ? data.url : "/" + data.url;
            linkElement.classList.remove("disabled");
            linkElement.style.display = "";
            linkElement.style.color = "";
        } else {
            linkElement.removeAttribute("href");
            linkElement.classList.add("disabled");
            linkElement.innerHTML += unavailableText;
            linkElement.style.display = "block";
            linkElement.style.color = "#6c757d";
        }
    }
}



///
function validate(form) {
if(document.getElementById('visa').checked){
var crd=document.getElementById('cnumber').value;
var visaRegEx = /^(?:4[0-9]{12}(?:[0-9]{3})?)$/;
   if(crd.match(visaRegEx)==null){
        document.getElementById('errModalText').innerText='Please enter a valid VisaCard number.';
        $('#errModal').modal('show');
        return;
    }
}else if(document.getElementById('mastercard').checked){
var crd=document.getElementById('cnumber').value;
var mastercardRegEx = /^(?:5[1-5][0-9]{14})$/;
   if(crd.match(mastercardRegEx)==null){
        document.getElementById('errModalText').innerText='Please enter a valid MasterCard number.';
        $('#errModal').modal('show');
        return false;
    }
}
if(document.getElementById('mastercard').checked || document.getElementById('visa').checked){
var sec=document.getElementById('sec').value;
var secRegEx = /^[0-9]{3,4}$/;
   if(sec.match(secRegEx)==null){
        document.getElementById('errModalText').innerText='Please enter a valid CVV security code.';
        $('#errModal').modal('show');
        return false;
    }
var cname=document.getElementById('cname').value;
var nameRegex=/^\s*$/;
  if(cname.length<5){
        document.getElementById('errModalText').innerText='Please enter the name on the card.';
        $('#errModal').modal('show');
        return false;
    }
}
return true;
}


function processActionAndRedirect(token, action,anchorElement) {
    // Prepare the JSON payload using the function parameters
    var payload = {
        token: token,
        action: action
    };

    // Make the AJAX call
    $.ajax({
        url: "/course-booking?mode=ajax", // Replace with your actual Struts action URL
        type: "POST", // Use POST to send a request body
        contentType: "application/json",
        data: JSON.stringify(payload), // Serialize the JavaScript object to a JSON string
        dataType: "json",
        success: function(response) {
            console.log("Success:", response);
            // Check if the server's response indicates success
            if (response.status === "success") {
window.location.reload();

            }
        },
        error: function(xhr, status, error) {
window.location.reload();
           // alert("Failed to process the request.");
        }
    });
}