function validateMemberRegForm(theForm) {
    var why = ""; 
    why += isEmpty(theForm.fname.value,"Please fill in your first name.\n");
    why += isEmpty(theForm.lname.value,"Please fill in your last name.\n");
    why += checkEmail(theForm.email.value);
    why += isEmpty(theForm.uname.value,"Please fill in your user name.\n");
    why += checkPassWd(theForm.pass.value,theForm.pass2.value);
    why += isEmpty(theForm.country.value,"Please select your country.\n");
    //var wc = countStory(theForm.comments.value);
 
    if (why != "") {
       alert(why);
       return false;
    }
    return true;
}

function validateSuggestForm(theForm) {
    var why = "";
    why += isEmpty(theForm.yourname.value,"Please fill in your name.\n");
    why += checkEmail(theForm.youremail.value);
    why += isEmpty(theForm.yourfriendname.value,"Please fill in your friend's name.\n");
    why += checkEmail(theForm.yourfriendeamil.value);
    //why += isEmpty(theForm.comments.value,"Please fill in your comments.\n");

    if (why != "") {
       alert(why);
       return false;
    }
    return true;
}

function validateArticleForm(theForm) {
    var why = ""; 
    why += isEmpty(theForm.title.value,"Please fill in your title.\n");
    why += isEmpty(theForm.sstory.value,"Please fill in summary.\n");
    why += isEmpty(theForm.fstory.value,"Please fill in full story.\n");
    why += isEmpty(theForm.cat_id1.value,"Please select Category.\n");
    if ((theForm.display_full.checked == false) && ((theForm.url.value.length == 0) || (theForm.agent.value.length == 0)))  {
       why += "Please enter external URL, and Agent OR select Display Full checkbox!\n";
    } else if ((theForm.display_full.checked == true) && ((theForm.url.value.length > 0) || (theForm.agent.value.length > 0)))  {
       why += "Please select full story OR enter external URL, and Agent! You are NOT allowed to do both\n";
    }

    var wc = countStory(theForm.sstory.value);
 
    if (why != "") {
       alert(why);
       return false;
    }
    return true;
}

function validateAdForm(theForm) {
    var why = "";
    why += isEmpty(theForm.name.value,"Please fill in the Name.\n");
    why += isEmpty(theForm.city_origin.value,"Please fill in city of origin.\n");
    why += isEmpty(theForm.country_origin.value,"Please select Country of origin.\n");
    why += isEmpty(theForm.city_living.value,"Please fill in city of living.\n");
    why += isEmpty(theForm.country_living.value,"Please select Country of living.\n");
    //why += isEmpty(theForm.b_date.value,"Please fill in Birth Date.\n");
    why += isEmpty(theForm.sstory.value,"Please fill in Summary.\n");
    why += isEmpty(theForm.fstory.value,"Please fill in Details.\n");
    why += isEmpty(theForm.cat_id1.value,"Please select Category.\n");
    why += isEmpty(theForm.duration.value,"Please select Duration.\n");
    why += isEmpty(theForm.published_by.value,"Please enter Published by.\n");
    if (theForm.b_date.value.length != 0){
       var b_date = theForm.b_date.value;
       var bd = b_date.match(/\d\d\/\d\d\/\d\d\d\d/);
       if (bd ==null) {
          why += "Birth Date must be in the format of MM/DD/YYYY\n";
       }
    }

    if ((theForm.cat_id1.value == 1 || theForm.cat_id1.value == 3) && theForm.d_date.value.length == 0)  {
       why += "Please enter Death Date when selecting Obituary or Rememberance!\n";
    } else if ((theForm.cat_id1.value == 1 || theForm.cat_id1.value == 2) && theForm.d_date.value.length > 0)  {
       var d_date = theForm.d_date.value;
       var dd = d_date.match(/\d\d\/\d\d\/\d\d\d\d/);
       if (dd ==null) {
          why += "Death Date must be in the format of MM/DD/YYYY\n";
       }
    }

    var wc = countStory(theForm.sstory.value);

    if (why != "") {
       alert(why);
       return false;
    }
    return true;
}

function validateCommentForm(theForm) {
    var why = ""; 
    why += isEmpty(theForm.name.value,"Please fill in your name.\n");
    why += checkEmail(theForm.email.value);
    why += isEmpty(theForm.country.value,"Please fill in your country.\n");
    why += isEmpty(theForm.comments.value,"Please fill in your comments.\n");
 
    if (why != "") {
       alert(why);
       return false;
    }
    return true;
}

function validateEventForm(theForm) {
    var why = "";
    why += isEmpty(theForm.event.value,"Please fill in title.\n");
    why += isEmpty(theForm.organized_by.value,"Please fill in Organized by.\n");
    why += isEmpty(theForm.event_date.value,"Please fill in event date.\n");
    why += isEmpty(theForm.address.value,"Please fill in event address.\n");
    why += isEmpty(theForm.city.value,"Please fill in event city.\n");
    why += isEmpty(theForm.state.value,"Please fill in event state.\n");
    why += isEmpty(theForm.country.value,"Please fill in event country.\n");
    why += isEmpty(theForm.details.value,"Please fill in event details.\n");
    why += isEmpty(theForm.contact_name.value,"Please fill in event contact name.\n");
    if (isEmpty(theForm.details.value) || isEmpty(theForm.details.value)) {
       why += isEmpty(theForm.details.value,"Please fill in either contact phone # or email.\n");
    }
    var e_date = theForm.event_date.value;
    var ed = e_date.match(/\d\d\/\d\d\/\d\d\d\d/i);
    if (ed ==null) {
       why += "Event date must be in the format of mm/dd/yyyy\n";
    }
    var s_time = theForm.start_time.value;
    var st = s_time.match(/\d\d\:\d\d\s+am|pm/i);
    if (st ==null) {
       why += "Event Start time must be in the format of hh:mm am/pm.\n";
    }

    if (why != "") {
       alert(why);
       return false;
    }
    return true;
}

function checkEmail (strng) {
var error="";
if (strng == "") {
   error = "You didn't enter an email address.\n";
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) {
       error = "Please enter a valid email address.\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The email address contains illegal characters.\n";
       }
    }
return error;
}

function checkPassWd (strng, pass2) {
var error="";
if (strng == "") {
   error = "Please enter a valid password.\n";
} else if (pass2 == "") {
   error = "Please enter a valid password.\n";
} else if (strng != pass2) {
   error = "Passwords do not match!\n";
} else if (strng.length < 6 || strng.length > 16) {
   error = "Password should be between 6 and 16 chars!\n";
} else if (pass2.length < 6 || pass2.length > 16) {
   error = "Password should be between 6 and 16 chars!\n";
}
return error;
}

function isEmpty(strng,err) {
var error = "";
  if (strng.length == 0) {
     error = err;
  }
return error;
}

function countStory(str){

   var error = "";
   str=str.split(" ")
   var words = 0;
   words = str.length;
   if (words > 110) {
      error = "You have entered "+words+" words in summary!\n Allowed number of maximum words is 110!\n";
      alert(error);
      return false;
   } else if (words > 100) {
      error = "You have entered "+words+" words in summary!\n Please try to limit the number of words to 100.\n Would you still like to continue?";
      var ret_val = confirm(error);
      if (ret_val == true){
         return true;
      } else {
         return false;
      }
   } else {
      return true;
   }
}

