// Validation of form entry
function isValid() {
    console.log("Validation started....");
    /* VALIDATE RENTAL ARTICLE NUMBER */
    let rental_art_nr = document.getElementById("mietart_nr").value;
    if(rental_art_nr == ''){  // check if rental art nr is present.
        setErrorPanel("Bitte eine Mietartikel Nummer angeben.");  return false;
    }
    console.log("Testing if " + rental_art_nr[0] + " is a letter. IsLetter=" + rental_art_nr[0].match(/[a-z]/i));
    if(rental_art_nr[0].match(/[a-z]/i) == null){  // check if first character is a letter. all article numbers start with a letter.
        setErrorPanel("Bitte eine gültige Mietartikel Nummer angeben."); return false;
    }

    /* VALIDATE START DATE */
    let start_date_string = document.getElementById("von").value;
    let start_date = new Date(start_date_string);
    if(start_date_string == '' || start_date == null){ // check if start date is present and valid.
        setErrorPanel("Bitte ein valides Startdatum angeben."); return false;
    }

    /* VALIDATE END DATE */
    let end_date_string = document.getElementById("bis").value;
    let end_date = new Date(end_date_string);
    if(end_date_string == '' || end_date == null){ // check if end date is present and valid.
        setErrorPanel("Bitte ein valides Enddatum angeben."); return false;
    }
    
    /* VALIDATE START DATE IS BEFORE END DATE */
    console.log("Testing if start date " + start_date.toISOString() + " is before end date " + end_date.toISOString() + " Is=" + (end_date >= start_date));
    if(end_date < start_date){ // check if start date is before end date.
        setErrorPanel("Startdatum muss vor Enddatum liegen."); return false;
    }

    /* VALIDATE START DATE IS AFTER TODAY */
    let now = new Date();  // need to remove time from the "now" date object, therefore create new one with only date:
    let now_dateOnly = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate());
    console.log("Testing if start date " + start_date.toISOString() + " is after now " + now_dateOnly.toISOString() + ". IsTodayOrLater=" + (start_date >= now_dateOnly));
    if(start_date < now_dateOnly){ // check if start date in the past. today is ok.
        setErrorPanel("Startdatum liegt in der Vergangenheit."); return false;
    }

    /* VALIDATE CUSTOMER NUMBER */
    let customer_nr = document.getElementById("kunden_nr").value;
    if(customer_nr == ''){  // check if customer number empty
        setErrorPanel("Bitte eine Kundennummer angeben."); return false;
    }
    console.log("Testing if customer number " + customer_nr + " is a number. IsInteger=" + (isNaN(customer_nr) == false));
    if(isNaN(customer_nr) == true){  // check if customer number is a number
        setErrorPanel("Kundennummer ist ungültig."); return false;
    }

    console.log("Validation sucessful");
    return true;  // valid
}

// Unhides error panel and shows text
function setErrorPanel(errorText) {
    let error_panel = document.getElementById("error-panel");
    let error_text = document.getElementById("error-text");

    error_text.innerText = errorText;
    error_panel.hidden = false;
}