function validateMemberForm(theForm, userType) {
  var reason = "";
  //alert(userType);
  
  reason += validateFirstName(theForm.fname);
  reason += validateLastName(theForm.lname);
  reason += validateAge(theForm.age);
  reason += validateDob(theForm.dob);
  reason += validateGender(theForm.elements[4], theForm.elements[5]);
  reason += validateAddress(theForm.addrs);
  reason += validatePhone(theForm.phone);
  reason += validateEmail(theForm.email);
  //if (userType == 'admin'){
  //  reason += validateUserName(theForm.uname);
  //  reason += validatePassword(theForm.pwd);
  //  reason += validateUsertype(theForm.type);
 //   reason += validateCategory(theForm.category);
 //   reason += validateStatus(theForm.status);
 // }
  
  if (reason != "") {
    alert("Correction:\n" + reason);
    return false;
  }

  return true;
}
function validateFirstName(fld) {
  var error = "";
  var illegalChars = /\W/; // allow letters, numbers, and underscores

  if (fld.value == "") {
      fld.style.background = 'Yellow';
      error = "Please enter a first name.\n";
  } else if (illegalChars.test(fld.value)) {
      fld.style.background = 'Yellow';
      error = "The first name contains illegal characters.\n";
  }else if (!(fld.value.search(/(0-9)+/))) {
      error = "The first name must not contain numbers.\n";
      fld.style.background = 'Yellow';
  } else {
      fld.style.background = 'White';
  }
  return error;
}
function validateLastName(fld) {
  var error = "";
  var illegalChars = /[\W_]/; // allow only letters and numbers

  if (fld.value == "") {
      fld.style.background = 'Yellow';
      error = "Please enter a last name.\n";
  } else if (illegalChars.test(fld.value)) {
      error = "The last name contains illegal characters.\n";
      fld.style.background = 'Yellow';
  } else {
      fld.style.background = 'White';
  }
  return error;
}
function validateAge(fld) {
  var error = "";
  var illegalChars = /[\W_@]/; // allow only letters and numbers
  var minAge = 5;
  var maxAge = 18;
    
  if (fld.value == "") {
      fld.style.background = 'Yellow';
      error = "Please enter age.\n";
  } else if ((fld.value.length > 2)) {
      error = "The age is the wrong length. \n";
      fld.style.background = 'Yellow';
  } else if (illegalChars.test(fld.value)) {
      error = "The age contains illegal characters.\n";
      fld.style.background = 'Yellow';
  }else if ((fld.value < minAge)) {
      error = "The minimum age is 5.\n";
      fld.style.background = 'Yellow';
  }else if ((fld.value > maxAge)) {
      error = "The maximum age is 18.\n";
            fld.style.background = 'Yellow';
  }else{
    var validChars = "0123456789.";
    var isNumber=true;
    var nChar;
    for (i = 0; i < fld.value.length && isNumber == true; i++){
        nChar = fld.value.charAt(i);
      if (validChars.indexOf(nChar) == -1)
        {
          isNumber = false;
        }
    }
    if (isNumber != true){
    error = "The age must contain only numbers.\n";
    fld.style.background = 'Yellow';
    }
  }

  return error;
}
function validateDob(fld){
  var error = "";

  if(fld.value == ""){
      error = "Date of birth missing. Please enter mm/dd/yyyy!\n";
      fld.style.background = 'Yellow';
  }else if (fld.value.length != 10){         //checking date length. If it not equals 10 - display warning and return false
      error = "Invalid date! Too short. The correct date format is like '01/01/2004'.\n";
      fld.style.background = 'Yellow';
  }else if ((fld.value.charAt(2) != '/') || (fld.value.charAt(5) != '/')){         //check position of slashes in date string. It should be 2 and 5 because the first character position is 0 not one
            error = "Invalid date! The '/' in the wrong place. The correct date format is '01/01/2004'.\n";
            fld.style.background = 'Yellow';
  }else{
      var valid = "0123456789/";           //declare valid variable with all valid characters: digits from 0 to 9 and backslash
      var slashcount = 0;                  //declare variable for counting number of slashes
      var temp = "";

      for (var i=0; i < fld.value.length; i++){
        temp = "" + fld.value.substring(i, i+1);

        if (temp == "/"){       //if character is backslash- count it
            slashcount++;
        }
        if (valid.indexOf(temp) == "-1"){
            error = "Invalid characters in your date. Please try again.\n";
            fld.style.background = 'Yellow';
        }else if((fld.value.charAt(2) != '/') || ( fld.value.charAt(5) != '/')){         //check position of slashes in date string. It should be 2 and 5 because the first character position is 0 not one
            error = "Invalid date! The "-" in the wrong place. The correct date format is '01/01/2004'.\n";
            fld.style.background = 'Yellow';
        }else{
            fld.style.background = 'White';
        }
     }if (slashcount != 2){           //if number of slashes not equals 2 display warning
        error = "Invalid date! Two slashes needed. The correct date format is '01/01/2004'.\n";
        fld.style.background = 'Yellow';
     }
   }

  return error;
}
function validateGender(Mfld, Ffld) {
   var error = "";

   if (!Mfld.checked && !Ffld.checked){
        error = "Please select gender.\n";
   }
   return error;
}

function validateAddress(fld) {
    var error = "";
    //alert(fld.name);
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Please enter an address.\n";
        fld.style.background = 'yellow';
    } else if ((fld.value.length > 150)) {
        error = "The address is too long. \n";
        fld.style.background = 'Yellow';
    }else {
        fld.style.background = 'White';
    }
   return error;
}
function validatePhone(fld) {
    var error = "";

    if (fld.value == "") {
        error = "Please enter a phone number.\n";
        fld.style.background = 'yellow';
    } else if ((fld.value.length > 12)) {
        error = "The phone number is too long. \n";
        fld.style.background = 'Yellow';
    }else if ((fld.value.length < 7)){
        error = "The phone number is too short. \n";
        fld.style.background = 'Yellow';
    }else {
        fld.style.background = 'White';
    }
   return error;
}
function validateEmail(fld) {
   var error = "";
   var len = fld.value.length;         // get length of the email string

   //if email field is empty - warning displays and return false
   if (fld.value == ""){
       error = "Please enter your email.\n";
       fld.style.background = 'yellow';
   }else if((fld.value.charAt(0)=='@')||(fld.value.charAt(0)=='.')){           //'@' and '.' must NOT be at the beginning
       error = "Invalid email: the @ and/or the '.' must not be at the beginning.";
       fld.style.background = 'Yellow';
   }else if((fld.value.charAt(len-1)=='@')||(fld.value.charAt(len-1)=='.')){   // @ and '.' must NOT be at the end of string.
       error = "Invalid email: the @ and/or '.' must not be at the end."
       fld.style.background = 'Yellow';
   }else{
       //count number of @ occurrences and number of dot occurrences
       var count = 0;
       var dotcount = 0;
       for (i=0; i< fld.value.length; i++)
       {
        if(fld.value.charAt(i) == '@')
               count++;
        if(fld.value.charAt(i) == '.')
               dotcount++;
       }
       //if @ or dot occurs not once display warning and return false
       //if((count !=1)||(dotcount !=1)){
       if (dotcount == 0){
           error = "Invalid email. The '.' must occur at least once, example: myemail@mysite.com";
           fld.style.background = 'Yellow';
           return error;
       }
       if (count != 1){
           //error = "Invalid email. The @ and/or '.' must occur once, example: myemail@mysite.com";
           error = "Invalid email. The '@' must occur once, example: myemail@mysite.com";
           fld.style.background = 'Yellow';
           return error;
      }
   }
   return error;
}

