

////////////////////////////////////////
// VALIDATE COLLEGES
////////////////////////////////////////
function validate_colleges(thisform)
{
  with (thisform)
  {
    if(!run_validate_colleges(name1, students1, "error_line1"))
    {
      return false;
    }
    if(!run_validate_colleges(name2, students2, "error_line2"))
    {
      return false;
    }
    if(!run_validate_colleges(name3, students3, "error_line3"))
    {
      return false;
    }
    if(!run_validate_colleges(name4, students4, "error_line4"))
    {
      return false;
    }
    if(!run_validate_colleges(name5, students5, "error_line5"))
    {
      return false;
    }
    if(!run_validate_colleges(name6, students6, "error_line6"))
    {
      return false;
    }
    if(!run_validate_colleges(name7, students7, "error_line7"))
    {
      return false;
    }
    if(!run_validate_colleges(name8, students8, "error_line8"))
    {
      return false;
    }
    if(!run_validate_colleges(name9, students9, "error_line9"))
    {
      return false;
    }
    if(!run_validate_colleges(name10, students10, "error_line10"))
    {
      return false;
    }
  }
   return true;
}

////////////////////////////////////////
// VALIDATE FOR COLLEGES
////////////////////////////////////////
function run_validate_colleges(nameA, studentsA, error_lineA)
{
    valueName = nameA.value.replace(/\s+$/g,'');
    valueStudents = studentsA.value.replace(/\s+$/g,'');

    if (!valueName)
      return true;

    if(!validate_required(nameA, error_lineA))
    {
      nameA.focus();
      return false;
    }
    if(valueStudents && !isNumeric(studentsA, error_lineA))
    {
      studentsA.focus();
      return false;
    }
    return true;
}


////////////////////////////////////////
// VALIDATE COUNTRIES
////////////////////////////////////////
function validate_countries(thisform)
{
  with (thisform)
  {
    if(!run_validate_countries(country1, students1, "error_line1"))
    {
      return false;
    }
    if(!run_validate_countries(country2, students2, "error_line2"))
    {
      return false;
    }
    if(!run_validate_countries(country3, students3, "error_line3"))
    {
      return false;
    }
    if(!run_validate_countries(country4, students4, "error_line4"))
    {
      return false;
    }
    if(!run_validate_countries(country5, students5, "error_line5"))
    {
      return false;
    }
    if(!run_validate_countries(country6, students6, "error_line6"))
    {
      return false;
    }
    if(!run_validate_countries(country7, students7, "error_line7"))
    {
      return false;
    }
    if(!run_validate_countries(country8, students8, "error_line8"))
    {
      return false;
    }
    if(!run_validate_countries(country9, students9, "error_line9"))
    {
      return false;
    }
    if(!run_validate_countries(country10, students10, "error_line10"))
    {
      return false;
    }
  }
   return true;
}

////////////////////////////////////////
// VALIDATE FOR COUNTRIES
////////////////////////////////////////
function run_validate_countries(countryA, studentsA, error_lineA)
{
    valueCountry = countryA.value.replace(/\s+$/g,'');
    valueStudents = studentsA.value.replace(/\s+$/g,'');

    if (!valueCountry && !valueStudents)
      return true;

    if(!validate_required(countryA, error_lineA))
    {
      countryA.focus();
      return false;
    }
    if(!validate_required(studentsA, error_lineA))
    {
      studentsA.focus();
      return false;
    }
    if(!isNumeric(studentsA, error_lineA))
    {
      studentsA.focus();
      return false;
    }
    return true;
}


////////////////////////////////////////
// VALIDATE ACADEMICS
////////////////////////////////////////
function validate_academics(thisform)
{
  with (thisform)
  {
    if(!run_validate_academics(header1, text1, "error_line1"))
    {
      return false;
    }
    if(!run_validate_academics(header2, text2, "error_line2"))
    {
      return false;
    }
    if(!run_validate_academics(header3, text3, "error_line3"))
    {
      return false;
    }
    if(!run_validate_academics(header4, text4, "error_line4"))
    {
      return false;
    }
    if(!run_validate_academics(header5, text5, "error_line5"))
    {
      return false;
    }
  }
   return true;
}

////////////////////////////////////////
// VALIDATE FOR ACADEMICS
////////////////////////////////////////
function run_validate_academics(headerA, textA, error_lineA)
{
    valueHeader = headerA.value.replace(/\s+$/g,'');
    valueText = textA.value.replace(/\s+$/g,'');

    if (!valueHeader && !valueText)
      return true;

    if(!validate_required(headerA, error_lineA))
    {
      headerA.focus();
      return false;
    }
    if(!validate_required(textA, error_lineA))
    {
      textA.focus();
      return false;
    }
    return true;
}


////////////////////////////////////////
// VALIDATE COURSES
////////////////////////////////////////
function validate_courses(thisform)
{
  with (thisform)
  {
    if(courseTitle1)
    {
      if(!run_validate_courses(courseTitle1, description1, "error_line1"))
      {
        return false;
      }
    }
    if(courseTitle2)
    {
      if(!run_validate_courses(courseTitle2, description2, "error_line2"))
      {
        return false;
      }
    }
    if(courseTitle3)
    {
      if(!run_validate_courses(courseTitle3, description3, "error_line3"))
      {
        return false;
      }
    }
    if(courseTitle4)
    {
      if(!run_validate_courses(courseTitle4, description4, "error_line4"))
      {
        return false;
      }
    }
    if(courseTitle5)
    {
      if(!run_validate_courses(courseTitle5, description5, "error_line5"))
      {
        return false;
      }
    }
  }
   return true;
}

////////////////////////////////////////
// VALIDATE FOR COURSES
////////////////////////////////////////
function run_validate_courses(titleA, descA, error_lineA)
{
    valueTitle = titleA.value.replace(/\s+$/g,'');
    valueDesc = descA.value.replace(/\s+$/g,'');

    if (!valueTitle && !valueDesc)
      return true;

    if(!validate_required(titleA, error_lineA))
    {
      titleA.focus();
      return false;
    }
    if(!validate_required(descA, error_lineA))
    {
      descA.focus();
      return false;
    }
    return true;
}


////////////////////////////////////////
// VALIDATE GRADE LEVELS
////////////////////////////////////////
function validate_grade_levels(thisform)
{
  with (thisform)
  {
    if(numericDescription1)
    {
      if(!run_validate_grade_levels(numericDescription1, textDescription1, "error_line1"))
      {
        return false;
      }
    }
    if(numericDescription2)
    {
      if(!run_validate_grade_levels(numericDescription2, textDescription2, "error_line2"))
      {
        return false;
      }
    }
    if(numericDescription3)
    {
      if(!run_validate_grade_levels(numericDescription3, textDescription3, "error_line3"))
      {
        return false;
      }
    }
    if(numericDescription4)
    {
      if(!run_validate_grade_levels(numericDescription4, textDescription4, "error_line4"))
      {
        return false;
      }
    }
    if(numericDescription5)
    {
      if(!run_validate_grade_levels(numericDescription5, textDescription5, "error_line5"))
      {
        return false;
      }
    }
  }
   return true;
}

////////////////////////////////////////
// VALIDATE FOR GRADE LEVELS
////////////////////////////////////////
function run_validate_grade_levels(numericA, textA, error_lineA)
{
    valueNumeric = numericA.value.replace(/\s+$/g,'');
    valueText = textA.value.replace(/\s+$/g,'');

    if (!valueNumeric && !valueText)
      return true;

    if(!validate_required(numericA, error_lineA))
    {
      numericA.focus();
      return false;
    }
    if(!validate_required(textA, error_lineA))
    {
      textA.focus();
      return false;
    }
    return true;
}


////////////////////////////////////////
// VALIDATE FACULTY
////////////////////////////////////////
function validate_faculty(thisform)
{
  with (thisform)
  {
    if(lname1)
    {
      if(!run_validate_faculty(email1, phone1, fname1, lname1, "error_line1"))
      {
        return false;
      }
    }
    if(lname2)
    {
      if(!run_validate_faculty(email2, phone2, fname2, lname2, "error_line2"))
      {
        return false;
      }
    }
    if(lname3)
    {
      if(!run_validate_faculty(email3, phone3, fname3, lname3, "error_line3"))
      {
        return false;
      }
    }
    if(lname4)
    {
      if(!run_validate_faculty(email4, phone4, fname4, lname4, "error_line4"))
      {
        return false;
      }
    }
    if(lname5)
    {
      if(!run_validate_faculty(email5, phone5, fname5, lname5, "error_line5"))
      {
        return false;
      }
    }
  }
   return true;
}

////////////////////////////////////////
// VALIDATE FOR FACULTY
////////////////////////////////////////
function run_validate_faculty(emailA, phoneA, fnameA, lnameA, error_lineA)
{
    valueEmail = emailA.value.replace(/\s+$/g,'');
    valuePhone = phoneA.value.replace(/\s+$/g,'');
    valueFName = fnameA.value.replace(/\s+$/g,'');
    valueLName = lnameA.value.replace(/\s+$/g,'');

    if (!valueEmail && !valuePhone && !valueFName && !valueLName)
      return true;

//    if(!validate_required(emailA, error_lineA))
//    {
//      emailA.focus();
//      return false;
//    }
//    if(!validate_required(phoneA, error_lineA))
//    {
//      phoneA.focus();
//      return false;
//    }
    if(!validate_required(fnameA, error_lineA))
    {
      fnameA.focus();
      return false;
    }
    if(!validate_required(lnameA, error_lineA))
    {
      lnameA.focus();
      return false;
    }
    return true;
}


////////////////////////////////////////
// VALIDATE PASSWORDS
////////////////////////////////////////
function validate_passwords(thisform)
{
  with (thisform)
  {
    if(userId1)
    {
      if(!run_validate_passwords(userId1, password1, fname1, lname1, "error_line1"))
      {
        return false;
      }
    }
    if(userId2)
    {
      if(!run_validate_passwords(userId2, password2, fname2, lname2, "error_line2"))
      {
        return false;
      }
    }
    if(userId3)
    {
      if(!run_validate_passwords(userId3, password3, fname3, lname3, "error_line3"))
      {
        return false;
      }
    }
    if(userId4)
    {
      if(!run_validate_passwords(userId4, password4, fname4, lname4, "error_line4"))
      {
        return false;
      }
    }
    if(userId5)
    {
      if(!run_validate_passwords(userId5, password5, fname5, lname5, "error_line5"))
      {
        return false;
      }
    }
  }
   return true;
}

////////////////////////////////////////
// VALIDATE FOR PASSWORDS
////////////////////////////////////////
function run_validate_passwords(userIdA, passwordA, fnameA, lnameA, error_lineA)
{
    valueUserId = userIdA.value.replace(/\s+$/g,'');
    valuePassword = passwordA.value.replace(/\s+$/g,'');
    valueFName = fnameA.value.replace(/\s+$/g,'');
    valueLName = lnameA.value.replace(/\s+$/g,'');

    if (!valueUserId && !valuePassword && !valueFName && !valueLName)
      return true;

    if(!validate_required(userIdA, error_lineA))
    {
      userIdA.focus();
      return false;
    }
    if(!validate_required(passwordA, error_lineA))
    {
      passwordA.focus();
      return false;
    }
    if(!validate_required(fnameA, error_lineA))
    {
      fnameA.focus();
      return false;
    }
    if(!validate_required(lnameA, error_lineA))
    {
      lnameA.focus();
      return false;
    }
    return true;
}


////////////////////////////////////////
// VALIDATE SUBJECTS
////////////////////////////////////////
function validate_subjects(thisform)
{
  with (thisform)
  {
    if(header1)
    {
      if(!run_validate_subjects(header1, email1, "error_line1"))
      {
        return false;
      }
    }
    if(header2)
    {
      if(!run_validate_subjects(header2, email2, "error_line2"))
      {
        return false;
      }
    }
    if(header3)
    {
      if(!run_validate_subjects(header3, email3, "error_line3"))
      {
        return false;
      }
    }
    if(header4)
    {
      if(!run_validate_subjects(header4, email4, "error_line4"))
      {
        return false;
      }
    }
    if(header5)
    {
      if(!run_validate_subjects(header5, email5, "error_line5"))
      {
        return false;
      }
    }
    if(header6)
    {
      if(!run_validate_subjects(header6, email6, "error_line6"))
      {
        return false;
      }
    }
    if(header7)
    {
      if(!run_validate_subjects(header7, email7, "error_line7"))
      {
        return false;
      }
    }
    if(header8)
    {
      if(!run_validate_subjects(header8, email8, "error_line8"))
      {
        return false;
      }
    }
    if(header9)
    {
      if(!run_validate_subjects(header9, email9, "error_line9"))
      {
        return false;
      }
    }
    if(header10)
    {
      if(!run_validate_subjects(header10, email10, "error_line10"))
      {
        return false;
      }
    }
  }
   return true;
}

////////////////////////////////////////
// VALIDATE FOR SUBJECTS
////////////////////////////////////////
function run_validate_subjects(headerA, emailA, error_lineA)
{
    valueHeader = headerA.value.replace(/\s+$/g,'');
    valueEmail = emailA.value.replace(/\s+$/g,'');

    if (!valueHeader && !valueEmail)
      return true;

    // See if the header is there.  If so, the email has to be there.
    if (valueHeader)
    {
      if(!validate_required_w_alert(emailA,"Please include the email.", error_lineA))
      {
        emailA.focus();
        return false;
      }
    }

    // See if the email is there.  If so, the header has to be there.
    if (valueEmail)
    {
      if(!validate_email_format(emailA, error_lineA))
      {
        emailA.focus();
        return false;
      }
      if(!validate_required_w_alert(headerA,"Please include the header.", error_lineA))
      {
        headerA.focus();
        return false;
      }
    }
    return true;
}


////////////////////////////////////////
// VALIDATE EVENTS
////////////////////////////////////////
function validate_events(thisform)
{
  with (thisform)
  {
    if(month1)
    {
      if(!run_validate(month1, day1, event1, "error_line1"))
      {
        return false;
      }
    }
    if(month2)
    {
      if(!run_validate(month2, day2, event2, "error_line2"))
      {
        return false;
      }
    }
    if(month3)
    {
      if(!run_validate(month3, day3, event3, "error_line3"))
      {
        return false;
      }
    }
    if(month4)
    {
      if(!run_validate(month4, day4, event4, "error_line4"))
      {
        return false;
      }
    }
    if(month5)
    {
      if(!run_validate(month5, day5, event5, "error_line5"))
      {
        return false;
      }
    }
    if(month6)
    {
      if(!run_validate(month6, day6, event6, "error_line6"))
      {
        return false;
      }
    }
    if(month7)
    {
      if(!run_validate(month7, day7, event7, "error_line7"))
      {
        return false;
      }
    }
    if(month8)
    {
      if(!run_validate(month8, day8, event8, "error_line8"))
      {
        return false;
      }
    }
    if(month9)
    {
      if(!run_validate(month9, day9, event9, "error_line9"))
      {
        return false;
      }
    }
    if(month10)
    {
      if(!run_validate(month10, day10, event10, "error_line10"))
      {
        return false;
      }
    }
  }
   return true;
}

////////////////////////////////////////
// VALIDATE FOR EVENTS
////////////////////////////////////////
function run_validate(monthA, dayA, eventA, error_lineA)
{
    valueEvent = eventA.value.replace(/\s+$/g,'');
    dayA.value = dayA.value.replace(/[^0-9]/g,"");
    valueDay = dayA.value.replace(/\s+$/g,'');

    if (!valueEvent && !valueDay)
      return true;

    // See if the event is there.  If so, the day has to be there.
    if (valueEvent)
    {
      if(!validate_required_w_alert(dayA,"Please include the day.", error_lineA))
      {
        dayA.focus();
        return false;
      }
    }

    // Make sure the day is correct for the month.
    if(!validate_day(monthA, dayA,"The day is incorrect for this month.", error_lineA))
    {
      return false;
    }

    // See if the day is there.  If so, the event has to be there.
    if (valueDay)
    {
      if(!validate_required_w_alert(eventA,"Please include the event.", error_lineA))
      {
        eventA.focus();
        return false;
      }
    }
    return true;
}


////////////////////////////////////////
// VALIDATE ORGANIZATIONS
////////////////////////////////////////
function validate_orgs(thisform)
{
  with (thisform)
  {
    validate_required(orgName, "error_orgName");
    validate_required(place, "error_place");
    validate_required(startTime, "error_startTime");

    if(!validate_required(orgName, "error_orgName"))
    {
      orgName.focus();
      return false;
    }
    if(!validate_required(place, "error_place"))
    {
      place.focus();
      return false;
    }
    if(!validate_required(startTime, "error_startTime"))
    {
      startTime.focus();
      return false;
    }
  }
}


////////////////////////////////////////
// VALIDATE CONTACT
////////////////////////////////////////
function validate_contact(thisform)
{
  with (thisform)
  {
    validate_required(name, "error_name");
    validate_email_format(email, "error_email");
    validate_required(comments, "error_comments");

    if(!validate_required(name, "error_name"))
    {
      name.focus();
      return false;
    }
    if(!validate_email_format(email, "error_email"))
    {
      email.focus();
      return false;
    }
    if(!validate_required(comments, "error_comments"))
    {
      comments.focus();
      return false;
    }
  }
  return true;
}


////////////////////////////////////////
// VALIDATE REQUIRED - GENERIC ERROR
////////////////////////////////////////
function validate_required(field, error_field)
{
  with (field)
  {
    valueTest = value.replace(/\s+$/g,'');
    if (valueTest)
    {
      document.getElementById(error_field).innerHTML="&nbsp;";
      return true;
    }
    else
    {
      document.getElementById(error_field).innerHTML="Required field. ";
      field.focus();
      return false;
    }
  }
}


////////////////////////////////////////
// VALIDATE REQUIRED - SEND ERROR TEXT
////////////////////////////////////////
function validate_required_w_alert(field, alerttxt, error_field)
{
  with (field)
  {
    valueTest = value.replace(/\s+$/g,'');
    if (valueTest)
    {
      document.getElementById(error_field).innerHTML="&nbsp;";
      return true;
    }
    else
    {
      document.getElementById(error_field).innerHTML=alerttxt;
      field.focus();
      return false;
    }
  }
}


////////////////////////////////////////
// VALIDATE DAY FOR EVENTS
////////////////////////////////////////
function validate_day(monthFld, dayFld, alerttxt, error_field)
{
  valueMonth = monthFld.value;
  valueDay = dayFld.value;

  document.getElementById(error_field).innerHTML=alerttxt;

  if(valueDay < 1)
  {
    document.getElementById(error_field).innerHTML=alerttxt;
    dayFld.focus();
    return false;
  }

  if(valueMonth == 1 && valueDay > 31)
  {
    document.getElementById(error_field).innerHTML=alerttxt;
    dayFld.focus();
    return false;
  }

  if(valueMonth == 2 && valueDay > 29)
  {
    document.getElementById(error_field).innerHTML=alerttxt;
    dayFld.focus();
    return false;
  }

  if(valueMonth == 3 && valueDay > 31)
  {
    document.getElementById(error_field).innerHTML=alerttxt;
    dayFld.focus();
    return false;
  }

  if(valueMonth == 4 && valueDay > 30)
  {
    document.getElementById(error_field).innerHTML=alerttxt;
    dayFld.focus();
    return false;
  }

  if(valueMonth == 5 && valueDay > 31)
  {
    document.getElementById(error_field).innerHTML=alerttxt;
    dayFld.focus();
    return false;
  }

  if(valueMonth == 6 && valueDay > 30)
  {
    document.getElementById(error_field).innerHTML=alerttxt;
    dayFld.focus();
    return false;
  }

  if(valueMonth == 7 && valueDay > 31)
  {
    document.getElementById(error_field).innerHTML=alerttxt;
    dayFld.focus();
    return false;
  }

  if(valueMonth == 8 && valueDay > 31)
  {
    document.getElementById(error_field).innerHTML=alerttxt;
    dayFld.focus();
    return false;
  }

  if(valueMonth == 9 && valueDay > 30)
  {
    document.getElementById(error_field).innerHTML=alerttxt;
    dayFld.focus();
    return false;
  }

  if(valueMonth == 10 && valueDay > 31)
  {
    document.getElementById(error_field).innerHTML=alerttxt;
    dayFld.focus();
    return false;
  }

  if(valueMonth == 11 && valueDay > 30)
  {
    document.getElementById(error_field).innerHTML=alerttxt;
    dayFld.focus();
    return false;
  }

  if(valueMonth == 12 && valueDay > 31)
  {
    document.getElementById(error_field).innerHTML=alerttxt;
    dayFld.focus();
    return false;
  }
  return true;
}


////////////////////////////////////////
// VALIDATE EMAIL FORMAT
////////////////////////////////////////
function validate_email_format(field, error_field)
{
  var re = /\w+@\w+\.\w+((\.\w+)*)?/ ;
  with (field)
  {
    if(value.match(re)) {
      document.getElementById(error_field).innerHTML="&nbsp;";
      return true;
    }
    else
    {
      document.getElementById(error_field).innerHTML="Required format: someone@example.com.";
      return false;
    }
  }
}


////////////////////////////////////////
// TEXT COUNTER
////////////////////////////////////////
function textCounter(field,cntfield,maxlimit) {
  if (field.value.length > maxlimit) // if too long...trim it!
  {
    field.value = field.value.substring(0, maxlimit);
  }
  else // otherwise, update 'characters left' counter
  {
    cntfield.value = maxlimit - field.value.length;
  }
}


////////////////////////////////////////
// VALIDATE PHONE
////////////////////////////////////////
function phoneCheck(field, errorfield){
  var object=document.getElementById(errorfield);
  var phoneNumber=field.value;
  phoneNumber=phoneNumber.replace(/[^0-9]/g,"");
  switch(phoneNumber.length)
  {
    case 0:
      object.innerHTML="Required field.";
      return false;
      break;
    case 10:
      var areaCode=phoneNumber.substring(0,3);
      var exchange=phoneNumber.substring(3,6);
      var remains=phoneNumber.substring(6,10);
      phoneNumber="(" + areaCode +") " + exchange + "-" + remains;
      field.value=phoneNumber;
      object.innerHTML="&nbsp;";
      return true;
      break;
    case 7:
      object.innerHTML="Area Code required.";
      return false;
      break;
    default:
      object.innerHTML="Ten digits are required.";
      return false;
      break;
  }
}


function phoneCheckNotRequired(field, errorfield){
  var object=document.getElementById(errorfield);
  var phoneNumber=field.value;
  phoneNumber=phoneNumber.replace(/[^0-9]/g,"");
  object.innerHTML="&nbsp;";
  if(phoneNumber.length < 1)
  {
    return true;
  }
  switch(phoneNumber.length)
  {
    case 10:
      var areaCode=phoneNumber.substring(0,3);
      var exchange=phoneNumber.substring(3,6);
      var remains=phoneNumber.substring(6,10);
      phoneNumber="(" + areaCode +") " + exchange + "-" + remains;
      field.value=phoneNumber;
      object.innerHTML="&nbsp;";
      return true;
      break;
    case 7:
      object.innerHTML="Area Code required.";
      return false;
      break;
    default:
      object.innerHTML="Ten digits are required.";
      return false;
      break;
  }
}


////////////////////////////////////////
// VALIDATE PASSWORD
////////////////////////////////////////
function passwordCheck(field1, field2){
  var object=document.getElementById("error_password1");
  var pw1=field1.value;
  var pw2=field2.value;
  if(pw1 == pw2)
  {
    object.innerHTML="&nbsp;";
    return true;
  }
  else
  {
    object.innerHTML="Passwords must match";
    return false;
  }
}


////////////////////////////////////////
// VALIDATE A FIELD IS AN INTEGER
////////////////////////////////////////
function isNumeric(sText, errorfield)
{
  var text=sText.value;
  var object=document.getElementById(errorfield);
  var ValidChars = "0123456789.";
  var IsNumber=true;
  var Char;

  for (i = 0; i < text.length && IsNumber == true; i++)
  {
    Char = text.charAt(i);
    if (ValidChars.indexOf(Char) == -1)
    {
      IsNumber = false;
    }
  }
  if(!IsNumber)
  {
    object.innerHTML="This field must be numeric.";
  }
  return IsNumber;
}










