﻿// This method checks for a valid email address
//   if the email field is filled out
function checkEmail(event) {

    // jQuery to find the div that contains the email textbox
    var divQuestionDisplay = $('span[id*=lblQuestionText]:contains("E-mail")').parent().parent();
    // jQuery to find the textbox within the div
    var emailTextbox = $('input[id*=tbAnswerText]', divQuestionDisplay)[0];

    var emailAddress = emailTextbox.value;

    $('p#jQueryEmailError').remove();
    
    if (emailAddress.length > 0) {
        if (/^[a-zA-Z_0-9.]+@[\w-]+\..+$/.test(emailAddress)) {
            return true;
        }
        else {
            $(divQuestionDisplay).append('<p class="error" id="jQueryEmailError">Please enter a valid email address.</p>');
            return false;
        }
    }
    else {
        return true;
    }

}

// The CustomValidator added to the page will call this method
function CheckForm(sender, args) {

    args.IsValid = checkEmail();

}