/**
 * validateSubmittal.js
 *
 * Validate the form input so we don't see things that
 * we don't want to.
 */
 
// Variables needed for validation
var missingPrefix = "You did not enter a value into the ";
var missingSuffix = " field. \nThis is a required field. Please enter it now.";
var whitespace = " \t\n\r"; // Any whitespace characters

// Invalid warnings
var iEmail = "The email field must be a valid email address\n(like here@iam.com). \nPlease re-enter it now.";
var iEmailShort = "The email field must be a valid email address.\nPlease re-enter it now.";
var iCreditCardPrefix = "This is not a valid ";
var iURL = "The URL (comment) field should be a fully\nqualified URL like http://www.here.com\nor ftp://ftp.here.com.\nPlease reenter.";
var iCreditCardSuffix = " credit card number. (Click the link on this form to see a list of sample numbers.) Please reenter it now.";
var iZIPCode = "This field must be a valid U.S. ZIP Code (like 94043). Please reenter it now.";

 
 
/**********************************
 * Functions
 **********************************/
 
/**
 * Checks to see if the password and the re-enter password
 * fields are the same.
 * @param field1, field 2   The 2 password fields to compare
 */
function checkPasswords(field1, field2) {
	if ( (field1.value) == (field2.value) )
		return true;
	else
		return warnInvalid(field1, iPasswd);
} // checkPasswords()



/**
 * Check to see field1 and field2 values are
 * the same. If not, alert with the errorMessage.
 */
function checkValidateFields(field1, field2,errorMsg) {
	if ( (field1.value) == (field2.value) )
		return true;
	else
		return warnInvalid(field1, errorMsg);
} // checkValidateFields()



/**
 * Check a pulldown to see if is in a certain position,
 * and returns a false or true depending on the boolean
 * passed through.
 * If the 'which' is 0, then we are testing that we
 * DON'T want that position chosen.
 *
 * If the 'which' is 1, then we are testing that the pulldown
 * MUST be that position.
 *
 * @param theField  The form field
 * @param which  boolean deciding if position is right or wrong
 * @param position Which select position is tested
 * @param Error message to display
 */
function checkPullDown(theField,which,position,errorMsg) {
	var fieldIndex = theField.selectedIndex;

	if (which == 0) {
		if (fieldIndex == position) {
			// Means we DON'T want it to be this position,
			// thus return warning.
			alert(errorMsg);
			return false;
		} else {
			return true;
		}
	} else {
		if (fieldIndex != position) {
			// Means we DON'T want it to be this position,
			// thus return warning.
			alert(errorMsg);
			return false;
		} else {
			return true;
		}
	}

} // checkPullDown()
 
 
 /**
  * Checks the field passed in and if it is empty,
  * send a warning message.
  */
 function checkString(theField, s) {
  if (isWhitespace(theField.value))
   return warnEmpty(theField, s);
  else
   return true;
 } // checkString
 
  /**
  * Check to see that the e-mail entered was a valid e-mail
  * address.
  */
 function checkEmail(theField) {
  if (!isEmail(theField.value, false))
   return warnInvalid (theField, iEmail);
  else
   return true;
 } // checkEmail()
 
 /**
  * Check to see that the e-mail entered was a valid e-mail
  * address (short version: rweeks@eng).
  */
 function checkEmailShort(theField) {
  if (!isEmailShort(theField.value, false))
   return warnInvalid (theField, iEmailShort);
  else
   return true;
 } // checkEmail()
 
 /**
  * Checks to see if this is a formed URL -
  * basically meaning that it needs the <proto>://,
  * so check for '://' for now.
  */
 function checkURL(theField) {
 	var value = theField.value;
 	if ( (value.indexOf('://') == -1) || 
 	     (value.indexOf('.') == -1) ||
 	     (value.indexOf(' ') != -1) ) {
 		return warnInvalid (theField, iURL);
 	} else
 		return true;
 
 } // checkURL()
 
 
 
 /**
  * The Warnings.....
  */
 
 /**
  * Notify user that required field theField is empty.
  * String s describes expected contents of theField.value.
  * Put focus in theField and return false.
  */
 function warnEmpty(theField, s) {
  theField.focus();
  alert(missingPrefix + s + missingSuffix);
  return false;
 } // warnEmpty

 /**
  * Notify the user if the field is not a
  * valid format.
  */
 function warnInvalid (theField, s) {
  theField.focus();
  theField.select();
  alert(s);
  return false;
 } // warnInvalid


 // Check whether string s is empty.
 function isEmpty(s) {
  return ((s == null) || (s.length == 0));
 } // isEmpty()
 
 
 /**
  * Type checking
  */
 
 /**
  * Returns true if string s is empty or
  * whitespace characters only.
  */
 function isWhitespace(s) {
  var i;

  // Is s empty?
  if (isEmpty(s)) return true;

  // Search through string's characters one by one
  // until we find a non-whitespace character.
  // When we do, return false; if we don't, return true.
  for (i = 0; i < s.length; i++) {
   // Check that current character isn't whitespace.
   var c = s.charAt(i);

   if (whitespace.indexOf(c) == -1) return false;
  }

  // All characters are whitespace.
  return true;
 } // isWhitespace(s)


 /**
  * Checks to see if the argument is a valid integer.
  */
 function isInteger(s) {
  var i;

  for (i = 0; i < s.length; i++) {
   // Check that current character is number.
   var c = s.charAt(i);
   if (!isDigit(c)) return false;
  }

  // All characters are numbers.
  return true;
 } // isInteger()
 
 /**
  * Returns true if the character is a digit.
  */
 function isDigit(c) {
  return ((c >= "0") && (c <= "9"))
 } // isDigit()

 /**
  * Check the format of the e-mail address entered.
  *
  * Email address must be of form a@b.c -- in other words:
  *    - there must be at least one character before the @
  *    - there must be at least one character before and after the .
  *    - the characters @ and . are both required
  */
 function isEmail(s) {
  // is s whitespace?
  if (isWhitespace(s)) return false;

  // there must be >= 1 character before @, so we
  // start looking at character position 1
  // (i.e. second character)
  var i = 1;
  var sLength = s.length;

  // look for @
  while ((i < sLength) && (s.charAt(i) != "@")) {
   i++;
  }

  if ((i >= sLength) || (s.charAt(i) != "@"))
   return false;
  else
   i += 2;

  // look for .
  while ((i < sLength) && (s.charAt(i) != ".")) {
   i++;
  }

  // there must be at least one character after the .
  if ((i >= sLength - 1) || (s.charAt(i) != "."))
   return false;
  else
   return true;
 } // isEmail
 
  /**
  * Check the short format of the e-mail address entered.
  *
  * Email address must be of form a@b -- in other words:
  *    - there must be at least one character before and after the @
  *    - @ is required
  *    - ex: rweeks@eng
  */
 function isEmailShort(s) {
  // is s whitespace?
  if (isWhitespace(s)) return false;

  // there must be >= 1 character before @, so we
  // start looking at character position 1
  // (i.e. second character)
  var i = 1;
  var sLength = s.length;

  // look for @
  while ((i < sLength) && (s.charAt(i) != "@")) {
   i++;
  }

  if ((i >= sLength) || (s.charAt(i) != "@"))
   return false;
  else
   return true;
 } // isEmailShort

