function validate(form) {
  var errPrefix = "We had some problems while trying "
		+ "to submit your comments:\n\n"
  var err = "";

  var name = form.fullName.value;
  var email = form.email.value;
  var comments = form.comments.value;

  // Check the Name
  if (isEmpty(name) || isWhitespace(name)) {
    err += "  - Your Name is missing; please provide your name.\n\n";
  }

  // Check the Email
  if (isEmpty(email)) {
    err += "  - Your Email is missing; please provide your email address.\n\n";
  } else if (!isEmail(email)) {
    err += "  - Your Email address is not valid;\n"
	 + "    please provide a valid email address.\n\n";
  }

  // Check the Comments
  if (isEmpty(comments) || isWhitespace(comments)) {
    err += "  - Your Comments are missing; please fill in your comments.\n\n";
  }

  if (err.length) {
    alert(errPrefix+err);
    return false;
  }

  return true;
}

function isEmail(s) {
  if (isEmpty(s)) return emptyReturn(isEmail.arguments);
  if (isWhitespace(s)) return false;
  var i = 1;
  var sLength = s.length;
  while ((i < sLength) && (s.charAt(i) != "@")) {
    i++
  }
  if ((i >= sLength) || (s.charAt(i) != "@")) return false;
  else i += 2;
  while ((i < sLength) && (s.charAt(i) != ".")) {
    i++
  }
  if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
  else return true;
}

var defaultEmptyOK = false
var whitespace = " \t\n\r";
function isEmpty(s) {
  return ((s == null) || (s.length == 0));
}
function emptyReturn() {
  var argv = emptyReturn.arguments;
  if (argv.length == 1) return defaultEmptyOK;
  else return (argv[0] == true);
}

function isWhitespace (s) {
    if (isEmpty(s)) return true;
    for (var i = 0; i < s.length; i++) {   
      var c = s.charAt(i);
      if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}
