index.js

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.0 kB
2
Indexable
Never
$(document).ready(function(){
    $.ajax({
        type: "GET",
        url: "/api/cities/",
        contentType: "application/json",
        success: function (cities) {
            let list = "";
            $.each(cities, function() {
                list += "<option value='" + this.id + "'>" + this.name + "</option>";
            });
            $("select.form-control").append(list);
        },
        error: function (e) {
            alert("Възникна неочаквана грешка. Моля, опитайте по-късно.");
        }
    });

    jSuites.calendar(document.getElementById('calendar'), {
        format: 'DD.MM.YYYY'
    });
});

function validateIndex() {
    let from = document.forms["indexform"]["from"].value;
    let to = document.forms["indexform"]["to"].value;
    let date = jSuites.calendar(document.getElementById('calendar')).getValue();

    if (from.length == 0 || to.length == 0) {
        alert("Моля, изберете градове!");
        return false;
    }

    const chosenDate = new Date(date);
    const today = new Date();
    const tomorrow = new Date(today);
    tomorrow.setDate(tomorrow.getDate() + 1);
    tomorrow.setHours(0,0,0,0);

    if (chosenDate < tomorrow) {
        alert("Можете да изберете най-ранна дата: " + tomorrow.toLocaleDateString("bg-BG"));
        return false;
    }

    let fromField = document.querySelector("select#from");
    let fromName = fromField.options[fromField.selectedIndex].text;
    let toField = document.querySelector("select#to");
    let toName = toField.options[toField.selectedIndex].text;

    sessionStorage.setItem("fromId", from);
    sessionStorage.setItem("toId", to);
    sessionStorage.setItem("from", fromName);
    sessionStorage.setItem("to", toName);
    sessionStorage.setItem("date", chosenDate.toLocaleDateString("bg-BG").slice(0, 10));
    window.location.href = "/routes";
    return false;
}