//========================================================================================
//                                  Copyright © 2005 NSCC
//
// Cemetery search client-side script functions.
//
// $Log: /Internet/Cemetery.NEW/Web/scripts/cemetery_search.js $
//
//4     7/11/05 10:49 Daviesa
//Rewrote validate
//========================================================================================

function validate() {
// Validate the search form values.
// Returns TRUE if valid; otherwise returns FALSE.
    var searchForm = document.forms['search'];

    var surnameLen   = trim(searchForm.surname.value).length;
    var forenamesLen = trim(searchForm.forenames.value).length;
    var ageFromVal   = searchForm.agefrom.value;
    var ageToVal     = searchForm.ageto.value;
    var diedFromVal  = searchForm.diedfrom.value;
    var diedToVal    = searchForm.diedto.value;

    if (surnameLen   == 0 &&
        forenamesLen == 0 &&
        ageFromVal   == '' &&
        ageToVal     == '' &&
        diedFromVal  == '' &&
        diedToVal    == '') {
        window.alert('Please enter some criteria.');
        searchForm.surname.focus();
        return false;

    } else if (surnameLen > 0 && surnameLen < 3) {
        window.alert('To narrow the search results, please enter at least three characters in the surname.');
        searchForm.surname.focus();
        return false;

    } else {
        // If one age is filled in but not the other, make both the same
        if (ageFromVal != '' && ageToVal == '') {
            searchForm.ageto.value = ageFromVal;

        } else if (ageFromVal == '' && ageToVal != '') {
            searchForm.agefrom.value = ageToVal;

        // If the ages are the wrong way around, reverse them
        } else if (ageFromVal != '' && ageToVal != '') {
            if (parseInt(ageFromVal, 10) > parseInt(ageToVal, 10)) {
                if (!window.confirm('The ages will be switched to provide a valid range.')) {
                    searchForm.agefrom.focus();
                    return false;
                }
                searchForm.agefrom.value = ageToVal;
                searchForm.ageto.value   = ageFromVal;
            }
        }

        // If one year of death is filled in but not the other, make both the same
        if (diedFromVal != '' && diedToVal == '') {
            searchForm.diedto.value = diedFromVal;

        } else if (diedFromVal == '' && diedToVal != '') {
            searchForm.diedfrom.value = diedToVal;

        // If the died years are the wrong way around, reverse them
        } else if (diedFromVal != '' && diedToVal != '') {
            if (parseInt(diedFromVal, 10) > parseInt(diedToVal, 10)) {
                if (!window.confirm('The died years will be switched to provide a valid range.')) {
                    searchForm.diedfrom.focus();
                    return false;
                }
                searchForm.diedfrom.value = diedToVal;
                searchForm.diedto.value   = diedFromVal;
            }
        }
    }

    return true;
}
