function isValidEmailAddress(asEmailAddress) {
    var rxEmailAddress = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9\-_])+(\.[a-zA-Z0-9_-]+)+$/;
    return rxEmailAddress.test(asEmailAddress);
}

function isValidPhoneNumber(asPhoneNumber) {
    var rxPhoneNumber = /^(\d?[\(\-\s./]?[1-9]\d{2}[\)\-\s./]?)?\d{3}[\-\s./]?\d{4}([a-zA-Z0-9\-\s.])*$/;
    return rxPhoneNumber.test(asPhoneNumber);
}

function isEmpty(asValue) {
    if (asValue.length == 0 || asValue == null)
        return true;
    return false; 
}

function validate() {   
    var txtComment = document.CSWebForm.comment;
    var txtEmailAddress = document.CSWebForm.email;
    var txtPhoneNumber = document.CSWebForm.phone;
    var txtFirstName = document.CSWebForm.fname;
    var sCSContactInfo = "If you continue to have difficulties, please call (608) 266-2621 to speak with Customer Service Representative.";

    if (isEmpty(txtComment.value) || txtComment.value == 'Your Question Here (Required)') {
        alert("Please be sure to enter your question, comment or concern.");
        txtComment.select();
        return false;
    }

    if (!isEmpty(txtEmailAddress.value)) {
        if (!isValidEmailAddress(txtEmailAddress.value)) {
            alert("The email address you provided is not valid.\n\n" + sCSContactInfo);
            txtEmailAddress.select();
            return false;
        }
    }
        
    if (!isEmpty(txtPhoneNumber.value)) {
        if (!isValidPhoneNumber(txtPhoneNumber.value)) {
            alert("The phone number you provided is not valid.\n\n" + sCSContactInfo);
            txtPhoneNumber.select();
            return false;
        }
    }

    if (isEmpty(txtEmailAddress.value) && (isEmpty(txtPhoneNumber.value) || isEmpty(txtFirstName.value))) {
        if (confirm("You have not provided enough information for us to be able to contact you.  In order to respond " +
                    "we will need either your email address or your phone number and first name.\n\n" +
                    "  - If you would like to submit your information as is, please click OK.\n" +
                    "  - If you would like to go back and provide us with contact information, please click Cancel.")) {
            return true;
        } else {
            txtEmailAddress.select();
            return false;
        }
    }
    return true;
}
