function validateForm(form) {
    if (isNotEmpty(form.realname)) {
        if (isNotEmpty(form.emailfirst)) {
            if (isEmailAddr(form.emailfirst)) {
                if (isNotEmpty(form.emailsecond)) {
                    if (isEmailAddr(form.emailsecond)) {
                        if (isNotEmpty(form.comments)) {
                    return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}



function isNotEmpty(elem) {
        var str = elem.value;
        if(str == null ||str.length == 0) {
            alert("Please complete all fields.");
            return false;
        } else {
            return true;
        }

}


function isEmailAddr(elem) {
        var str = elem.value;
        str = str.toLowerCase();
        if (str.indexOf("@") > 1) {
           var addr = str.substring(0, str.indexOf("@"));
           var domain = str.substring(str.indexOf("@") +1, str.length);
           //at least one top level domain required
           if (domain.indexOf(".") == -1) {
               alert("Please verify the domain portion of the email address.");
                     return false;
           }
           // parse address portion first, character by character
           for (var i = 0; i< addr.length; i++) {
               oneChar = addr.charAt(i).charCodeAt(0);
               //dot or hyphen not allowed in first position; dot in last
               if((i == 0 && (oneChar == 45 || oneChar == 46)) ||
                  (i == addr.length -1 && oneChar ==46)) {
                   alert("Please verify user name portion of the email address.");
                   return false
               }
               //acceptable characters (- . _ 0-9 a-z)
               if (oneChar == 45 || oneChar ==46 || oneChar == 95 || (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
                   continue;
               }else {
                   alert("Please verify the user portion of the email address.");
                   return false;
               }
           }
           for (i = 0; i < domain.length; i++) {
               oneChar = domain.charAt(i).charCodeAt(0);
               if ((i == 0 && (oneChar ==45 || oneChar == 46)) || ((i == domain.length -1 || i== domain.length -2) && oneChar ==46)) {
                   alert("Please verify domain portion of the email address.");
                   return false;
               }
               if (oneChar == 45 || oneChar == 46 || oneChar ==95 || (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
                   continue;
               }else {
                   alert("Please verify the domain portion of the email address.");
                   return false;
               }
           }
           return true;
        }
        alert("The email address may not be formatted correctly. Please verify.");
        return false;
}
    
    
