/*=====================================================
    AVIA CUSTOMER CHECK-IN
======================================================*/

let currentLanguage = "en";

function changeLanguage(lang){

    currentLanguage = lang;

    if(lang==="hr"){

        document.documentElement.lang="hr";

        document.getElementById("title").innerHTML=
        "Obrazac za podatke o korisniku";

        document.getElementById("subtitle").innerHTML=
        "Molimo ispunite obrazac prije potpisivanja ugovora o najmu.";

        document.getElementById("lReservation").innerHTML=
        "Broj rezervacije / Vouchera";

       document.getElementById("lFullName").innerHTML="Full Name";
        document.getElementById("lAddress").innerHTML=
        "Adresa";

        document.getElementById("lCity").innerHTML=
        "Grad";

        document.getElementById("lZip").innerHTML=
        "Poštanski broj";

        document.getElementById("lCountry").innerHTML=
        "Država";

        document.getElementById("lPhone").innerHTML=
        "Broj mobitela";

        document.getElementById("sendButton").innerHTML=
        "POŠALJI";

        document.getElementById("gdprText").innerHTML=
        "Potvrđujem da sam pročitao/la Pravila privatnosti i dajem suglasnost tvrtki AVIA Rent a Car za obradu mojih osobnih podataka u svrhu pripreme i sklapanja ugovora o najmu vozila.";

        document.getElementById("successTitle").innerHTML=
        "Hvala!";

        document.getElementById("successText").innerHTML=
        "Vaši podaci su uspješno poslani.";

    }

    else{

        document.documentElement.lang="en";

        document.getElementById("title").innerHTML=
        "Customer Information Form";

        document.getElementById("subtitle").innerHTML=
        "Please complete the form before signing the rental agreement.";

        document.getElementById("lReservation").innerHTML=
        "Reservation / Voucher Number";

       document.getElementById("lFullName").innerHTML="Ime i prezime";

        document.getElementById("lAddress").innerHTML=
        "Address";

        document.getElementById("lCity").innerHTML=
        "City";

        document.getElementById("lZip").innerHTML=
        "ZIP Code";

        document.getElementById("lCountry").innerHTML=
        "Country";

        document.getElementById("lPhone").innerHTML=
        "Mobile Phone";

        document.getElementById("sendButton").innerHTML=
        "SEND";

        document.getElementById("gdprText").innerHTML=
        "I confirm that I have read the Privacy Policy and I agree that AVIA Rent a Car may process my personal data for the purpose of preparing and concluding the vehicle rental agreement.";

        document.getElementById("successTitle").innerHTML=
        "Thank you!";

        document.getElementById("successText").innerHTML=
        "Your information has been successfully submitted.";

    }

}

document.addEventListener("DOMContentLoaded",function(){

    changeLanguage("en");

});


/*===========================================
    Only letters
===========================================*/

function onlyLetters(e){

    let key=e.key;

    let regex=/^[a-zA-ZÀ-ž\s'-]+$/;

    if(key.length===1 && !regex.test(key)){

        e.preventDefault();

    }

}


/*===========================================
    Capitalize words
===========================================*/

function capitalizeWords(input){

    let words=input.value.toLowerCase().split(" ");

    for(let i=0;i<words.length;i++){

        if(words[i].length>0){

            words[i]=words[i][0].toUpperCase()+words[i].substring(1);

        }

    }

    input.value=words.join(" ");

}


/*===========================================
    Phone formatting
===========================================*/

document.addEventListener("DOMContentLoaded",function(){

    let phone=document.getElementById("phone");

    if(phone){

        phone.addEventListener("input",function(){

            this.value=this.value.replace(/[^0-9+\-\s]/g,"");

        });

    }

});


/*===========================================
    Auto capitalize
===========================================*/

document.addEventListener("DOMContentLoaded",function(){

    let fields=[

        "firstname",
        "lastname",
        "city",
        "country"

    ];

    fields.forEach(function(id){

        let el=document.getElementById(id);

        if(el){

            el.addEventListener("blur",function(){

                capitalizeWords(this);

            });

            el.addEventListener("keypress",onlyLetters);

        }

    });

});


/*===========================================
    Success message
===========================================*/

function showSuccess(){

    document.getElementById("customerForm").style.display="none";

    document.getElementById("successMessage").style.display="block";

}


/*===========================================
    Scroll top
===========================================*/

function scrollTopForm(){

    window.scrollTo({

        top:0,

        behavior:"smooth"

    });

}