﻿function validateLastNameEmailAndApptDate(lastNameClientId, emailClientId, apptDateClientId, apptTimeClientId) {
    var isOk = false;

    if (isBlank(document.getElementById(lastNameClientId).value)) {
        alert('Please enter your last name');
        document.getElementById(lastNameClientId).focus();
    }
    else if (!validateEmail(document.getElementById(emailClientId).value, true, true))
    {
        document.getElementById(emailClientId).focus();
    }
    else if (!validateDate(document.getElementById(apptDateClientId))) {
        alert('Please select a valid appointment date');
        document.getElementById(apptDateClientId).value;
    }
    else {
        // Get the selected date
        var selectedDate = new Date(document.getElementById(apptDateClientId).value);

        // Get today's date
        var today = new Date();

        // Clear the time parts
        today.setHours(0, 0, 0, 0);

        // Make sure the selected date is in the future
        if (selectedDate <= today) {
            document.getElementById(apptDateClientId).value = '';
            alert('Please select a date greater than today');
        }
        else if (document.getElementById(apptTimeClientId).selectedIndex==0)
        {
            alert('Please select an appointment time');
            document.getElementById(apptTimeClientId).focus();
        }
        else // Date is in the future
        {
            isOk = true;
        }
    }

    return isOk;
}

function validateApptDate(textBox, filterBtnId) {
    // Get the selected date
    var selectedDate = new Date(textBox.value);

    // Get today's date
    var today = new Date();

    // Clear the time parts
    today.setHours(0, 0, 0, 0);

    // Make sure the selected date is in the future
    if (selectedDate <= today) {
        textBox.value = '';
        alert('Please select a date greater than today');
    }
    else // Date is in the future
    {
        // Invoke the filter appointment times button
        document.getElementById(filterBtnId).click();
    }
}

function validateEmail(addr, man, db) {
    if (addr == '' && man) {
        if (db) alert('email address is mandatory');
        return false;
    }
    if (addr == '') return true;
    var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
    for (i = 0; i < invalidChars.length; i++) {
        if (addr.indexOf(invalidChars.charAt(i), 0) > -1) {
            if (db) alert('email address contains invalid characters');
            return false;
        }
    }
    for (i = 0; i < addr.length; i++) {
        if (addr.charCodeAt(i) > 127) {
            if (db) alert("email address contains non ascii characters.");
            return false;
        }
    }

    var atPos = addr.indexOf('@', 0);
    if (atPos == -1) {
        if (db) alert('email address must contain an @');
        return false;
    }
    if (atPos == 0) {
        if (db) alert('email address must not start with @');
        return false;
    }
    if (addr.indexOf('@', atPos + 1) > -1) {
        if (db) alert('email address must contain only one @');
        return false;
    }
    if (addr.indexOf('.', atPos) == -1) {
        if (db) alert('email address must contain a period in the domain name');
        return false;
    }
    if (addr.indexOf('@.', 0) != -1) {
        if (db) alert('period must not immediately follow @ in email address');
        return false;
    }
    if (addr.indexOf('.@', 0) != -1) {
        if (db) alert('period must not immediately precede @ in email address');
        return false;
    }
    if (addr.indexOf('..', 0) != -1) {
        if (db) alert('two periods must not be adjacent in email address');
        return false;
    }
    var suffix = addr.substring(addr.lastIndexOf('.') + 1);
    if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
        if (db) alert('invalid primary domain in email address');
        return false;
    }
    return true;
}

function validateDate(fld) 
{
    isValid = true;
    var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
    if ((!fld.value.match(RegExPattern)) || !(fld.value!='')) 
        isValid = false;
        
    return isValid;
}

function isBlank(text)
{
    return (text.replace(/ /g,'').length==0)
}
