function isRegExpSupported() {
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (supported==1)
    return true;
  else
    return false;

}

function setColor(el, bg)
{
  if (el.style) el.style.backgroundColor = bg;
}


function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (!isRegExpSupported())
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function isValidStr(str) {
  // are regular expressions supported?
  if (!isRegExpSupported())
    return (str.length > 5);
  var r = new RegExp("[^a-zA-Z0-9]");
  return ((str.length > 5) && (!r.test(str)));
}

/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;
var maxCharactersInPhoneNumber = 12;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return ((isInteger(s) && s.length >= minDigitsInIPhoneNumber) &&
 (s.length <= maxCharactersInPhoneNumber));
}

function isValidPhone(phonestr,id){

	if ((phonestr==null)||(phonestr=="")){
		alert("Please enter your phone number")
		return false
	}
	if (checkInternationalPhone(phonestr)==false){
	   if (id == 1)
		  alert("Please enter a valid home phone number")
		if (id == 2)
		   alert("Please enter a valid cell phone number")
		return false
	}
	return true
 }

function requestPasswd(form){
  var bgBad  = "#cccc99";
  var bgGood = "white";
  var valid  = true;

  if (form.userID.value == "") {
    valid = false;
    setColor(form.userID, bgBad);
    form.userID.focus();
    alert("Please fill out the User ID.");
    return valid;
  }
  else {
    if (isEmail(form.userID.value))
    {
      setColor(form.userID,bgGood)
    }
    else {
      valid = false;
      setColor(form.userID,bgBad);
      form.userID.focus();
      alert("Please fill out a valid User ID.");
      return valid;
    }
  }
  if (form.passwdHintAnswer.value == "") {
    valid = false;
    setColor(form.passwdHintAnswer, bgBad);
    form.passwdHintAnswer.focus();
    alert("Please provide Password Hint Answer.");
    return valid;
  }
  else {
      setColor(form.passwdHintAnswer,bgGood)
    }

  if (form.myHiddenHintParameter.value == -1) {
    valid = false;
    alert("Please select a password hint question");
    return valid;
  }

    return true;
}
function checkPasswd(form)
{
  var bgBad  = "#cccc99";
  var bgGood = "white";
  var valid  = true;

  if (isValidStr(form.oldPasswd.value)) {
    setColor(form.oldPasswd, bgGood);
  }
  else {
    valid = false;
    setColor(form.oldPasswd, bgBad);
  //  form.passwd.focus();
    alert("Please fill out your old Password.");
    return valid;
  }
  if (isValidStr(form.newPasswd.value)) {
    setColor(form.newPasswd, bgGood);
  }
  else {
    valid = false;
    setColor(form.newPasswd, bgBad);
  //  form.passwd.focus();
    alert("Please enter a valid New Password.");
    return valid;
  }
  if (isValidStr(form.newPasswd1.value)) {
    setColor(form.newPasswd1, bgGood);
  }
  else {
    valid = false;
    setColor(form.newPasswd1, bgBad);
  //  form.passwd.focus();
    alert("Please comfirm your New Password.");
    return valid;
  }

  if (form.newPasswd.value == form.newPasswd1.value) {
    setColor(form.newPasswd1, bgGood);
  }
  else {
    valid = false;
    setColor(form.newPasswd1, bgBad);
    alert("The new password is not confirmed, Please reenter the same Password .");
    form.newPasswd1.focus();
    return valid;
  }
  return valid;
}


function checkIt(str)
{
/* Javascript date check by Justin Klein Keane  Copyright (C) 2004

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

	/* Note the date field is named 'date' in the form 'theForm' */
	var theDateString = str;
	var LenDateString = str.length;
	var monthEndsAt = theDateString.search('/');
	if (monthEndsAt < 2)
		{alert ("Month must be two digits");
		 return false;}
	var LenRestOfString = (LenDateString - monthEndsAt);
	var monthYearString = theDateString.substr(monthEndsAt+1, LenRestOfString);
	var YearBeginsAt = monthYearString.search('/');
	if (YearBeginsAt < 2)
		{alert ("Day must be two digits");
		 return false;}

	var endYearAt = (monthYearString.length) - YearBeginsAt + 1;
	var theYearString = monthYearString.substr(YearBeginsAt+1, endYearAt);
	if ((LenDateString < 8) || (LenDateString > 10))
		{alert ("Improperly formatted date string (mm/dd/yyyy)");
		 return false;
		}
	// check to make sure the first slash is in the right place
	else if ((theDateString.substr(1,1) != "/") && (theDateString.substr(2,1) != "/"))
		{alert ("Improperly formatted date string (mm/dd/yyyy)");
		 alert ("The slashes aren't in the right place");
		 return false;
		}
	// check to make sure the month is not zero
	else if ((theDateString.substr(2,1) == '/') && (theDateString.substr(0,2) < 1))
		{alert ("Improperly formatted date string (mm/dd/yyyy)");
		 return false;
		}
	// check to make sure the month is not greater than 12
	else if ((theDateString.substr(2,1) == '/') && (theDateString.substr(0,2) > 12))
		{alert ("Improperly formatted date string (mm/dd/yyyy)");
		 return false;
		}
	// check to make sure the second slash is in the right place
	else if ((monthYearString.substr(1,1) != "/") && (monthYearString.substr(2,1) != "/"))
		{alert ("Improperly formatted date string (mm/dd/yyyy)");
		 alert ("The slashes aren't in the right place");
		 return false;
		}
	// check to make sure the day isn't zero
	else if ((theDateString.substr(2,1) == '/') && (theDateString.substr(3,2) < 1))
		{alert ("Improperly formatted date string (mm/dd/yyyy)");
		 return false;
		}
	// check to make sure the day isn't greater than 32

	else if ((theDateString.substr(2,1) == '/') && (theDateString.substr(3,2) > 32))
		{alert ("Improperly formatted date string (mm/dd/yyyy)");
		 return false;
		}
	// check to make sure the year is four digits
	else if (theYearString.length != 4)
		{alert ("The year needs to be four digits");
		 return false;
		}
	else
		{
		return true;
		}
}

function validateZIP(field) {
var valid = "0123456789";
var hyphencount = 0;

if (field.length!=5) {
alert("Please enter your 5 digit zip code.");
return false;
}
for (var i=0; i < field.length; i++) {
temp = "" + field.substring(i, i+1);
if (valid.indexOf(temp) == "-1") {
alert("Invalid characters in your zip code.  Please try again.");
return false;
}
}
return true;
}



function checkLogin(form)
{
  var bgBad  = "#cccc99";
  var bgGood = "white";
  var valid  = true;
    if (isEmail(form.userid.value)) {
    setColor(form.userid, bgGood);
  }
  else {
    valid = false;
    setColor(form.userid, bgBad);
    form.userid.focus();
    alert("Please fill out a valid Login Name.");
    return valid;
  }

  if (isValidStr(form.passwd.value)) {
    setColor(form.passwd, bgGood);
    form.passwd.focus();
  }
  else {
    valid = false;
    setColor(form.passwd, bgBad);
    form.passwd.focus();
    alert("Please fill out a valid Password.");
    return valid;
  }
}

function checkRegister(form)
{
  var bgBad  = "#cccc99";
  var bgGood = "white";
  var valid  = true;

  if (form.firstname.value == "") {
     valid = false;
     setColor(form.firstname, bgBad);
     form.firstname.focus();
     alert("Please fill out the First Name.");
     return valid;
  }
  else {
    setColor(form.firstname, bgGood);
  }
  if (form.lastname.value == "") {
     valid = false;
     setColor(form.lastname, bgBad);
     form.lastname.focus();
     alert("Please fill out the Last Name.");
     return valid;
  }
  else {
    setColor(form.lastname, bgGood);
  }
  if (form.email.value == "") {
    valid = false;
    setColor(form.email, bgBad);
    form.email.focus();
    alert("Please fill out the email.");
    return valid;
  }
  else {
    if (isEmail(form.email.value))
    {
      setColor(form.email,bgGood);
      form.passwd.focus();
    }
    else {
      valid = false;
      setColor(form.email,bgBad);
      form.email.focus();
      alert("Please fill out a valid email address.");
      return valid;
    }
  }
  if (form.passwd.value == "") {
    valid = false;
    setColor(form.passwd, bgBad);
    form.passwd.focus();
    alert("Please fill out the password.");
    return valid;
  }
  else {
    setColor(form.passwd, bgGood);
    form.passwd1.focus();
  }
  if (form.passwd1.value == "") {
    valid = false;
    setColor(form.passwd1, bgBad);
    form.passwd1.focus();
    alert("Please fill out the confirmed Password.");
    return valid;
  }
  else {
    if (form.passwd.value == form.passwd1.value) {
      setColor(form.passwd1, bgGood);
      form.passwordHint.focus();
    }
    else {
      valid = false;
      setColor(form.passwd1, bgBad);
      alert("The password is not confirmed, Please reenter the same Password .");
      form.passwd1.focus();
      return valid;
    }
  }
  if (form.myHiddenParameter.value == -1) {
    valid = false;
    alert("Please select a password hint question");
    return valid;
  }

  if (form.hintAnswer.value == "") {
     valid = false;
     setColor(form.hintAnswer, bgBad);
     form.hintAnswer.focus();
     alert("Please fill out the hint answer");
     return valid;
  }
  else {
    setColor(form.hintAnswer, bgGood);
  }
  return valid;
}

function checkContactInfo(form)
{
  var bgBad  = "#cccc99";
  var bgGood = "white";
  var valid  = true;


 if (form.address.value == "") {
     valid = false;
     setColor(form.address, bgBad);
     form.address.focus();
     alert("Please fill out Street Address");
     return valid;
  }
  else {
    setColor(form.address, bgGood);
  }
  if (form.city.value == "") {
     valid = false;
     setColor(form.city, bgBad);
     form.city.focus();
     alert("Please fill out city name.");
     return valid;
  }
  else {
    setColor(form.city, bgGood);
  }
  if (form.zip.value == "") {
     valid = false;
     setColor(form.zip, bgBad);
     form.zip.focus();
     alert("Please fill out zip code");
     return valid;
  }
  else {
    if (validateZIP(form.zip.value))
    setColor(form.zip, bgGood);
    else
    {
       valid = false;
       return valid;
    }
  }
  if (form.homephone.value == "") {
     valid = false;
     setColor(form.homephone, bgBad);
     form.homephone.focus();
     alert("Please fill out home phone");
     return valid;
  }
  else {
    if (isValidPhone(form.homephone.value,1))
      setColor(form.homephone, bgGood);
    else {
      valid = false;
      setColor(form.homephone, bgBad);
     form.homephone.focus();
     return valid;
   }
  }
  if (form.cellphone.value != "") {
    if (isValidPhone(form.cellphone.value,2))
      setColor(form.cellphone, bgGood);
    else {
      valid = false;
      setColor(form.cellphone, bgBad);
     form.cellphone.focus();
     return valid;
   }
  }
  if (!valid) {
    alert("Please fill out the entire form.");
    return valid;
  }
  return valid;
}


function checkRegisterStudent(form)
{
  var bgBad  = "#cccc99";
  var bgGood = "white";
  var valid  = true;

  if (form.firstname.value == "") {
     valid = false;
     setColor(form.firstname, bgBad);
     form.firstname.focus();
     alert("Please fill out the First Name.");
     return valid;
  }
  else {
    setColor(form.firstname, bgGood);
  }
  if (form.lastname.value == "") {
     valid = false;
     setColor(form.lastname, bgBad);
     form.lastname.focus();
     alert("Please fill out the Last Name.");
     return valid;
  }
  else {
    setColor(form.lastname, bgGood);
  }
  if (form.birthdate.value == "") {
     valid = false;
     setColor(form.birthdate, bgBad);
     form.birthdate.focus();
     alert("Please fill out the Birth date");
     return valid;
  }
  else {
    if (checkIt(form.birthdate.value))
      setColor(form.birthdate, bgGood);
    else {
      valid = false;
      setColor(form.birthdate, bgBad);
      form.birthdate.focus();
      return valid;
    }
  }

  if (form.fathersname.value == "") {
     valid = false;
     setColor(form.fathersname, bgBad);
     form.fathersname.focus();
     alert("Please fill out Fathers Name.");
     return valid;
  }
  else {
    setColor(form.fathersname, bgGood);
  }

if (form.mothersname.value == "") {
     valid = false;
     setColor(form.mothersname, bgBad);
     form.mothersname.focus();
     alert("Please fill out Mothers Name.");
     return valid;
  }
  else {
    setColor(form.mothersname, bgGood);
  }
 if (form.address.value == "") {
     valid = false;
     setColor(form.address, bgBad);
     form.address.focus();
     alert("Please fill out Street Address");
     return valid;
  }
  else {
    setColor(form.address, bgGood);
  }
  if (form.city.value == "") {
     valid = false;
     setColor(form.city, bgBad);
     form.city.focus();
     alert("Please fill out city name.");
     return valid;
  }
  else {
    setColor(form.city, bgGood);
  }
  if (form.zip.value == "") {
     valid = false;
     setColor(form.zip, bgBad);
     form.zip.focus();
     alert("Please fill out zip code");
     return valid;
  }
  else {
    if (validateZIP(form.zip.value))
    setColor(form.zip, bgGood);
    else
    {
       setColor(form.zip, bgBad);
       form.zip.focus();
       valid = false;
       return valid;
    }
  }
  if (form.homephone.value == "") {
     valid = false;
     setColor(form.homephone, bgBad);
     form.homephone.focus();
     alert("Please fill out home phone");
     return valid;
  }
  else {
    if (isValidPhone(form.homephone.value,1))
      setColor(form.homephone, bgGood);
    else {
      valid = false;
      setColor(form.homephone, bgBad);
     form.homephone.focus();
     return valid;
   }
  }
  if (form.cellphone.value != "") {
    if (isValidPhone(form.cellphone.value,2))
      setColor(form.cellphone, bgGood);
    else {
      valid = false;
      setColor(form.cellphone, bgBad);
     form.cellphone.focus();
     return valid;
   }
  }
  if (form.myHiddenSessionID.value == -1) {
     valid = false;
     alert("Please select the religion session");
     return valid;
  }
  if (form.myHiddenCourseID.value != -1 && form.myHiddenSatCourseID.value != -1)
  {
    if (form.myHiddenSessionID.value == 1)
    {
     valid = false;
     alert("Please select a different religion session due to conflict with SAT session");
     return valid;
    }
  }

  if (!valid) {
    alert("Please fill out the entire form.");
    return valid;
  }

  return valid;
}



