// Book:       "JavaScript: The Definitive Guide"
// Author:     David Flanagan
// Publisher:  O'Reilly
// Copyright:  (c) 2002
// ISBN:       0-596-00048-0
// Somewhat mangled by r. royar

// A utility function that returns true if a string contains only
// whitespace characters.
function isblank(s) {
  for(var i = 0; i < s.length; i++) {
	var c = s.charAt(i);
	if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
  }
  return true;
}

function displayname(s) {
  switch (s) {
  case "FullName":
  case "Name":
  case"Teacher":
	return "Your full name";
  case "FirstName":
	return "Your first name";
  case "LastName":
	return "Your last name";
  case "Home_Street":
	return "Your home address";
  case "City":
	return "Your city of residence";
  case "Zip":
	return "Your street address's ZIP code";
  case "Address":
	return "Your school's complete address";
  case "School":
	return "The name of your school";
  case "District":
	return "Your school's district (or Diocese)";
  case "email":
	return "Your email address (for your receipt)";
  case "deportment":
  case "e_mail":
  case "Email":
	return "Your email address";
  case "BillingName":
	return "The name of person or office to be charged"
  case "CardName":
	return "Your name (exactly as it appears on the card)";
  case "CardNumber":
	return "Your card's number";
  case "expmonth":
	return "Your card's expiration month";
  case "expyear":
	return "Your card's expiration year";
  case "Position":
	return "Your current position (school or other work)";
  case "Session_Title":
	return "The title for your proposed session";
  case "Brief_Description":
	return "A brief description of your proposed session";
  case "Abstract":
	return "A 50-200 word summary of your proposed session";
  case "Format1":
	return "The format for your proposed session (e.g. individual or group)";
  case "Format2":
	return "The type for your proposed session (e.g. workshop or presentation)";
  case "Emphasis":
	return "The primary emphasis of proposed your session";
  case "friday_am":
	return "Your preference for a Friday morning time slot";
  case "friday_pm":
	return "Your preference for a Friday afternoon time slot";
  case "saturday_am":
	return "Your preference for a Saturday morning time slot";
  case "officer":
	return "The name of the KCTE/LA officer to contact";
  case "cravat":
  case "name":
	return "Your name";
  case "subject":
  case "executor":
	return "The Subject of your message";
  case "enlightenment":
  case "message":
	return "Your message text";
  case "Title":
	return "The title of this submission";
  case "Filename":
	return "A file to upload";
  case "Student":
	return "The name of your student writer";
  case "Grade":
	return "The grade level of your student writer";
  case "Category":
	return "The required category for judging this submission";	
  default:
	return s;
  }
}

// This is the function that performs form verification. It is invoked
// from the onsubmit event handler. The handler should return whatever
// value this function returns.
function verify(f) {
  var msg;
  var empty_fields = "";
  var errors = "";

  // Loop through the elements of the form, looking for all
  // text and textarea elements that don't have an "optional" property
  // defined. Then, check for fields that are empty and make a list of them.
  // Also, if any of these elements have a "min" or a "max" property defined,
  // verify that they are numbers and in the right range.
  // If the element has a "numeric" property defined, verify that
  // it is a number, but don't check its range.
  // Put together error messages for fields that are wrong.
  for(var i = 0; i < f.length; i++) {
	var e = f.elements[i];
	if (e.optional || (e.name == "CVV")) {
	  continue;
	} else if (((e.type == "text") || (e.type == "textarea") || (e.type == "file")) && ((e.value == null) || (e.value == "") || isblank(e.value))) {
	  empty_fields += "\n          " + displayname(e.name);
	  continue;
	} else if ((e.type == "radio") && (e.value == null)) {
	  empty_fields += "\n          " + displayname(e.name);
	  continue;
	} else if ((e.type == "select-one") && ((e.selectedIndex == -1) || (e.value == ""))) {
	  empty_fields += "\n          " + displayname(e.name);
	  continue;	  
	}
  }
  // Now, if there were any errors, display the messages, and
  // return false to prevent the form from being submitted.
  // Otherwise return true.
  if (!empty_fields && !errors) return true;

  msg = "The form was not submitted because ";
  if (empty_fields) {
	msg += "the following required field(s) are empty:" + empty_fields + "\n";
	if (errors) msg += "\n";
  }
  msg += errors;
  msg += "Please correct these error(s) and re-submit.\n";
  alert(msg);
  return false;
}

