var error = "";
var valid = true;
var errorCount = 0;

var digitsInZIPCode = 5;
var digitsInPlusFour = 4;
var digitsInRoutingNumber = 9;
var maxErrors = 5;

var mPrefix = "Please enter a value into the ";
var mSuffix = " field.";

var iZIPCode = "ZIP CODE must be a 5 digit U.S. ZIP Code (like 94043).";
var iZIPPlusFour = "ZIP CODE +4 must be a 9 digit U.S. ZIP Code (like 94043-1209).";
var iEmail = "E-MAIL must be a valid e-mail address (like username@domain.com).";
var iDatePrefix = "The Day, Month, and Year for ";
var iDateSuffix = " do not form a valid date.";

function checkString (theField, theFieldName)
{
  if (isWhitespace(theField.value))
  {
    addError(mPrefix + theFieldName + mSuffix);
  }
}

// Routing Number must be numeric and have a length equal to eight

function checkRoutingNumber (theField)
{
  var routingNumber = stripWhitespace(theField.value);
  if (!(isInteger(routingNumber)) || !(routingNumber.length == digitsInRoutingNumber))
  {
    addError("The ROUTING NUMBER must be exactly nine (9) digits.");
  }
}

// ZIP Code must be numeric and have a length equal to five

function checkZIPCode (theField)
{
  s = theField;  // original function used 's'
  var zc = stripWhitespace(s.value);
  if (checkZIPCode.arguments.length == 1)
  {
    if (!(isInteger(zc)) || !(zc.length == digitsInZIPCode))
    {
      addError(iZIPCode);
    }
  }
  else
  {
    if (!(isInteger(zc, checkZIPCode.arguments[1])) || (!(zc.length == digitsInZIPCode) && !(zc.length == "0")))
    {
      addError(iZIPCode);
    }
  }
}

// ZIP Code must be numeric and have a length equal to five

function checkZIPCodePlusFour (theField, theFourField)
{
  var zc = stripWhitespace(theField.value);
  var p4 = stripWhitespace(theFourField.value);
  
  if (!(isInteger(zc)) || !(zc.length == digitsInZIPCode))
  {
    addError(iZIPPlusFour);
  }
  if (!(isWhitespace(p4)))
  {
    if (!(isInteger(p4)) || (!(p4.length == digitsInPlusFour) && !(p4.length == "0")))
    {
      addError(iZIPPlusFour);
    }
  }
}

// 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 checkEmail (theField, theFieldName)
{ 
  if (checkEmail.arguments.length == 2) { required = true; } else {required = false; }

  s = theField.value;

  if (isEmpty(s))
  {
    if (required)
    {
      addError(mPrefix + theFieldName + mSuffix);
      return;
    }
    else
    {
      // The field is not required, empty is OK, just return
      return;
    }
  }
   
  // is s whitespace?
  if (isWhitespace(s))
  {
    if (required)
    {
      addError(mPrefix + theFieldName + mSuffix);
      return;
    }
    else
    {
      // The field is not required, whitespace is OK, just return
      return;
    }
  }
  
  // Make sure s doesn't equal the value we put as default
  if (s == "username@domain.com")
  {
    addError("E-MAIL address is not valid. 'username@domain.com' is only an example format.");
    return;
  }

  // Make sure s doesn't contain spaces
  for(var i = 0; i < theField.value.length; i++)
  {
    if(theField.value.charAt(i) == " ")
    {
      addError(iEmail + " Spaces are not allowed.");
      return;
    }
  }
    
  // 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) != "@"))
  {
    addError(iEmail);
    return;
  }
  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) != "."))
  {
    addError(iEmail);
    return;
  }
}

// The phone number must be numeric and have length () 3-4.

function checkPhoneNumber (areaCode, phoneNumberThree, phoneNumberFour, type, theField)
{
  if (isEmpty(areaCode.value) || areaCode.value.length != 3 || !(isInteger(areaCode.value)) || isEmpty(phoneNumberThree.value) || phoneNumberThree.value.length != 3 || !(isInteger(phoneNumberThree.value)) || isEmpty(phoneNumberFour.value) || phoneNumberFour.value.length != 4 || !(isInteger(phoneNumberFour.value)))
  {
    addError(type + " must be numeric and of the format 555-123-4567");
  }
}

// A Credit Card number must be numeric and have length 15 or 16.

function checkCreditCardNumber (ccNumber)
{
  if (isEmpty(ccNumber.value) || (ccNumber.value.length != 15 && ccNumber.value.length != 16) || !(isInteger(ccNumber.value)))
  {
    addError("CREDIT CARD NUMBER must have fifteen or sixteen digits");
  }
}

// SSN must be numeric and have length 3-2-4

function checkSSN (ssnThree, ssnTwo, ssnFour, theField)
{
  if (!(isInteger(ssnThree.value)) || !(isInteger(ssnTwo.value)) || !(isInteger(ssnFour.value)) || !(ssnThree.value.length == 3) || !(ssnTwo.value.length == 2) || !(ssnFour.value.length == 4))
  {
    addError("SSN must be 9 digits, 0-9, and in the format 123-12-1234.");
  }
}

// Check that year, month, and day combination form a valid date.
//
// If they don't, labelString (the name of the date, like "Birth Date")
// is displayed to tell the user which date field is invalid.
//

function checkDate (month, day, year, labelString, theField)
{
  if (!isMonth(month))
  {
    addError(iMonthPrefix + labelString + iMonthSuffix);
  }
  if (!isDay(day))
  { 
    addError(iDayPrefix + labelString + iDaySuffix);
  }
  if (!isYear(year)) 
  {
    addError(iYearPrefix + labelString + iYearSuffix);
  }
  if (!isDate (month, day, year))
  {
    addError(iDatePrefix + labelString + iDateSuffix);
  }
}

// Log an error if there are any spaces in the field value

function checkSpaces(theField, theFieldName)
{
  for(var i = 0; i < theField.value.length; i++)
  {
    if(theField.value.charAt(i) == " ")
    {
      addError(theFieldName + " cannot have spaces.  Please re-enter.");
      break;
    }
  }
  return;
}

// Strip any spaces from the beginning and end of all text values on the form passed

function stripTextValues(form)
{
  for (var i = 0; i < form.elements.length; i++)
  {
    if (form.elements[i].type == "text")
    {
      stripBegEndSpaces(form.elements[i]);
    }
  }
}

function addError(errorMessage)
{ 
  if (errorCount < maxErrors)
  {
    error+= errorMessage + "\n";
    valid = false;
    errorCount++;
  }
  else if (errorCount == maxErrors)
  {
    error+= "...more.\n";
    valid = false;
    errorCount++;
  }
  else
  {
    // Do nothing, errorCount is greater than maxErrors to display
  }
}

// Log an error if the date passed is greater than today

function checkDateGreaterThanToday(month, day, year, theFieldName, theField)
{
  today = new Date();

  compareToDate = new Date();

  compareToDate.setDate(day);
  compareToDate.setMonth(month - 1);
  compareToDate.setYear(year);

  if (today.getTime() < compareToDate.getTime())
  {
    addError(theFieldName + " cannot be greater than today.");
  }
}

// Log an error if the date passed is not at least 18 years ago.

function checkBirthday(month, day, year, theFieldName, theField)
{
  eighteenBirthday = new Date();
  birthday = new Date();

  eighteenBirthday.setYear(eighteenBirthday.getYear() - 18);

  birthday.setDate(day);
  birthday.setMonth(month - 1);
  birthday.setYear(year);

  if (eighteenBirthday.getTime() < birthday.getTime())
  {
    addError("You must be 18 years of age to be the Joint Applicant for a CampusMate bank account.");
  }
}

// Log an error if the date passed is less than or equal to today

function checkDateLessThanEqualToday(month, day, year, theFieldName, theField)
{
  today = new Date();

  compareToDate = new Date();

  compareToDate.setDate(day);
  compareToDate.setMonth(month - 1);
  compareToDate.setYear(year);

  if (!(today.getTime() < compareToDate.getTime()))
  {
    addError(theFieldName + " cannot be less than or equal to today.");
  }
}

// Log an error if the date passed is less than or equal to yesterday

function checkIfDateBeforeToday(month, day, year, theFieldName)
{
  today = new Date();
  yesterday = new Date();
  
  yesterday.setDate(today.getDate() - 1);

  compareToDate = new Date();

  compareToDate.setDate(day);
  compareToDate.setMonth(month - 1);
  compareToDate.setYear(year);

  if (!(yesterday.getTime() < compareToDate.getTime()))
  {
    addError(theFieldName + " cannot be before today.");
  }
}
