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

var imageNames = ""; // list of images that have become 'arrows'

var digitsInZIPCode = 5;
var digitsInAcctNum = 8;
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 07451).";

var iAcctNum = "An Account number needs to have 8 digits with no dashes - (like 12345678).";
var iEmail = "E-MAIL must be a valid e-mail address (like info@ridgewoodsymphony.org).";
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,theField.name + 'Img');
  }
}

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

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

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

function checkAcctNum (theField, theFieldName)
{
s = theField;  // original function used 's'
  var zc = stripWhitespace(s.value);
  if (checkAcctNum.arguments.length == 1)
  {
    if (!(isInteger(zc)) || !(zc.length == digitsInAcctNum))
    {
      addError(iAcctNum, theField.name + 'Img');
    }
  }
  else
  {
    if (!(isInteger(zc, checkAcctNum.arguments[1])) || (!(zc.length == digitsInAcctNum) && !(zc.length == "0")))
    {
      addError(iAcctNum, theField.name + 'Img');
    }
  }
}

// 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, theField.name + 'Img');
      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, theField.name + 'Img');
      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.", theField.name + 'Img');
    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, theField.name + 'Img');
    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, theField.name + 'Img');
    return;
  }
}

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

function checkPhoneNumber (areaCode, phoneNumberThree, phoneNumberFour, extension, 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)) || (!(isEmpty(extension.value)) && !(isInteger(extension.value))))
  {
    addError(type + " must be numeric and of the format 555-123-4567 [optional] ext.5555", theField.name + 'Img');
  }
}

// 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.", theField.name + 'Img');
  }
}


// Check that yearField.value, monthField.value, and dayField.value 
// 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 (monthField, dayField, yearField, labelString, theField)
{
  if (!isMonth(monthField.value))
  {
    addError(iMonthPrefix + labelString + iMonthSuffix, theField.name + 'Img');
  }
  if (!isDay(dayField.value))
  { 
    addError(iDayPrefix + labelString + iDaySuffix, theField.name + 'Img');
  }
  if (!isYear(yearField.value)) 
  {
    addError(iYearPrefix + labelString + iYearSuffix, theField.name + 'Img');
  }
  if (!isDate (monthField.value, dayField.value, yearField.value))
  {
    addError(iDatePrefix + labelString + iDateSuffix, theField.name + ' Img');
  }
}

// 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.", theField.name + 'Img');
      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, imgName)
{
  if (!imgName == '') { imageNames+=imgName + ":"; }
  
  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.value);
  compareToDate.setMonth(month.value - 1);
  compareToDate.setYear(year.value);

  if (today.getTime() < compareToDate.getTime())
  {
    addError(theFieldName + " cannot be greater than today.", theField.name + 'Img');
  }
}
