/* PHP and mySQL forms validation */

/* format today's date for mySQL */
function getCurrentDate(today) {
   var theYear = today.getYear()
   var theMonth = today.getMonth() + 1
   var theDay = today.getDate()
   var theHour = today.getHours()
   var theMinute = today.getMinutes()
   var theSecond = today.getSeconds()
   return theYear + "-" + theMonth + "-" + theDay + " " + theHour + ":" + theMinute + ":" + theSecond
}

/* validate fields in the schools registration form */
function verifySchoolForm() {
   var themessage = "Missing or incorrect required fields:";
   var good;

   if (document.addSchool.FIRST_NAME.value=="") {
      themessage = themessage + "\n\n - Please enter the first name";
   }

   if (document.addSchool.LAST_NAME.value=="") {
      themessage = themessage + "\n\n - Please enter the last name";
   }

   if (document.addSchool.SCHOOL_DISTRICT.value=="None") {
      themessage = themessage + "\n\n - Please choose a school district";
   }

   if (document.addSchool.GRADE.value=="") {
      themessage = themessage + "\n\n - Please specify the grade level";
   }

   var goodNum
   if ( isNaN (document.addSchool.NUMBER_OF_STUDENTS.value) || document.addSchool.NUMBER_OF_STUDENTS.value < 1 || document.addSchool.NUMBER_OF_STUDENTS.value > 200 ) {
      themessage = themessage + "\n\n - Please enter the maximum number of students to be visited (a number between 1 and 200)";
      }

   var goodPhone = document.addSchool.PHONE.value.match(/[2-9][0-9][0-9][1-9][0-9][0-9][0-9][0-9][0-9][0-9]/);
   if (goodPhone) {
      good = true
   } else {
      themessage = themessage + "\n\n - Please enter a phone number (10 digits, no dashes, etc.)";
   }

   var goodEmail = document.addSchool.E_MAIL.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.biz)|(\.org)|(\..{2,2}))$)\b/gi);
   if (goodEmail){
      good = true
   } else {
      themessage = themessage + "\n\n - Please enter a valid e-mail address";
   }

   if (document.addSchool.SCHOOL.value=="") {
      themessage = themessage + "\n\n - Please enter the name of the school";
   }
 
   var goodFax = (document.addSchool.FAX.value.match(/[2-9][0-9][0-9][1-9][0-9][0-9][0-9][0-9][0-9][0-9]/) | document.addSchool.FAX.value=="" );
   if (goodFax){
      good = true
   } else {
      themessage = themessage + "\n\n - Enter a valid fax number (10 digits)";
   }

   if (themessage == "Missing or incorrect required fields:") {
      document.addSchool.DATE_CREATED.value = getCurrentDate(new Date())
      document.addSchool.submit()
      return true;
   } else {
      alert(themessage + "\n\nPlease correct and enter again");
      return false;
   }
/* end verifySchoolForm() */
}

/* validate fields in the Volunteers registration form */
function verifyVolunteerForm() {
   var themessage = "Missing or incorrect required fields:";
   var good;

   if (document.addVolunteer.FIRST_NAME.value=="") {
      themessage = themessage + "\n\n - Please enter the first name";
   }

   if (document.addVolunteer.LAST_NAME.value=="") {
      themessage = themessage + "\n\n - Please enter the last name";
   }

   var goodPhone = document.addVolunteer.PHONE.value.match(/[2-9][0-9][0-9][1-9][0-9][0-9][0-9][0-9][0-9][0-9]/);
   if (goodPhone) {
      good = true
   } else {
      themessage = themessage + "\n\n - Please enter a phone number (10 digits, no dashes, etc.)";
   }

   var goodEmail = document.addVolunteer.E_MAIL.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.biz)|(\.org)|(\..{2,2}))$)\b/gi);
   if (goodEmail){
      good = true
   } else {
      themessage = themessage + "\n\n - Please enter a valid e-mail address";
   }

   var goodFax = (document.addVolunteer.FAX.value.match(/[2-9][0-9][0-9][1-9][0-9][0-9][0-9][0-9][0-9][0-9]/) | document.addVolunteer.FAX.value=="" );
   if (goodFax){
      good = true
   } else {
      themessage = themessage + "\n\n - Enter a valid fax number (10 digits)";
   }

   if (themessage == "Missing or incorrect required fields:") {
      document.addVolunteer.DATE_CREATED.value = getCurrentDate(new Date());
      document.addVolunteer.submit();
      return true;
   } else {
      alert(themessage + "\n\nPlease correct and enter again");
      return false;
   }
/* end verifyVolunteerForm() */
}

/* validate fields in the Evaluations form */
function verifyEvaluation() {
   var themessage = "Missing or incorrect required fields:";
   var good;

   if (document.evaluation.FIRST_NAME.value=="") {
      themessage = themessage + "\n\n - Please enter the first name";
   }

   if (document.evaluation.LAST_NAME.value=="") {
      themessage = themessage + "\n\n - Please enter the last name";
   }

   var goodEmail = document.evaluation.E_MAIL.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.biz)|(\.org)|(\..{2,2}))$)\b/gi);
   if (goodEmail){
      good = true
   } else {
      themessage = themessage + "\n\n - Please enter a valid e-mail address";
   }

   if (document.evaluation.SCHOOL_DISTRICT.value=="None") {
      themessage = themessage + "\n\n - Please choose a school district";
   }

   if (document.evaluation.SCHOOL.value=="") {
      themessage = themessage + "\n\n - Please enter the name of the school";
   }

   var goodNum
   if ( isNaN (document.evaluation.NUMBER_OF_STUDENTS.value) || document.evaluation.NUMBER_OF_STUDENTS.value < 1 || document.evaluation.NUMBER_OF_STUDENTS.value > 999 ) {
      themessage = themessage + "\n\n - Please enter the number of students you visited";
      }

   if ( document.evaluation.PROGRAM_DATE.value != "yyyy-mm-dd" ) {
     var goodProgramDate = document.evaluation.PROGRAM_DATE.value.match(/2006-[0-1][0-9]-[0-3][0-9]/);
     if (goodProgramDate) {
        good = true
     } else {
        themessage = themessage + "\n\n - Enter the date the program was given. Example: 2006-02-13";
     }
   } else {
      document.evaluation.PROGRAM_DATE.value = "";
   }

   if (document.evaluation.VENUE.value=="Other" && document.evaluation.COMMENTS.value=="") {
      themessage = themessage + "\n\n - Enter information about the venue in the Comments field";
   }

   if (themessage == "Missing or incorrect required fields:") {
      document.evaluation.DATE_CREATED.value = getCurrentDate(new Date());
      document.evaluation.submit();
      return true;
   } else {
      alert(themessage + "\n\nPlease correct and enter again");
      return false;
   }
/* end verifyEvaluation() */
}
