﻿function ValidateForm() {

    var txtFullName = document.getElementById("txtFullName");
    var txtResidentCity = document.getElementById("txtResidentCity");
    var txtAge = document.getElementById("txtAge");
    var txtDriverLicenseType = document.getElementById("txtDriverLicenseType");
    var txtPhone = document.getElementById("txtPhone");
    var txtDrushimEmail = document.getElementById("txtDrushimEmail");
    var txtFName = document.getElementById("txtFName");

    // Validate Feilds

    if ((txtFName != null) && (txtFName.value == "")) {
        alert("נא למלא שם");
        return false;
    }

    if (txtFullName != null) {
        if (txtFullName.value == "") {
            alert("נא למלא שם");
            return false;
        }
    }

    if (txtResidentCity != null) {
        if (txtResidentCity.value == "") {
            alert("נא למלא עיר מגורים");
            return false;
        }
    }

    if (txtAge != null) {
        if (txtAge.value == "") {
            alert("נא למלא גיל");
            return false;
        }
    }

    if (txtDriverLicenseType != null) {
        if (txtDriverLicenseType.value == "") {
            alert("נא למלא סוג רשיון נהיגה");
            return false;
        }
    }

    if (txtPhone != null) {
        if (txtPhone.value == "") {
            alert("נא למלא טלפון");
            return false;
        }
    }

    if (txtDrushimEmail != null) {
        if (txtDrushimEmail.value == "") {
            alert("נא למלא כתובת דואר אלקטרוני");
            return false;
        } else {
            if (!echeck(txtDrushimEmail.value)) {
                alert("נא להקליד כתובת דואר אלקטרוני תקנית");
                return false;
            }
        }
    }


    return true;

}

function echeck(str) {

    var at = "@"
    var dot = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)
    if (str.indexOf(at) == -1) {
        return false
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        return false
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        return false
    }

    if (str.indexOf(at, (lat + 1)) != -1) {
        return false
    }

    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        return false
    }

    if (str.indexOf(dot, (lat + 2)) == -1) {
        return false
    }

    if (str.indexOf(" ") != -1) {
        return false
    }

    return true
}