/* javascript functions for gestbook page, to verify the mail fields
 */

var spam="";

function form_setup() {
   document.form._1name.value=""
	 document.form._2email.value=""
	 document.form._3comment.value=""
	 document.form.v_code.value=""
	 document.form._1name.focus();
}

function check_Name() {
   if ((document.form._1name.value.length) > 4) {
      return (true)
   }
   alert("the name must be aleast 5 charters")
   document.form._1name.value=""
   document.form._1name.focus()
   return (false)
}

function check_EmailAddress() {
   if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.form._2email.value)){
      return (true)
   }
   alert("Enter in a valid E-mail address.")
   document.form._2email.value=""
   document.form._2email.focus()
   return (false)
}

function check_CommentField(pattern,max) {
   var Char_length = document.form._3comment.value.length;
   var first_char = pattern.charAt(0);
   var pattern_length = pattern.length;
   var test_string;
   var match=0;

   for (indx=1; indx < Char_length; indx +=1)  {
      if (document.form._3comment.value.charAt(indx-1) == first_char) {
         test_string = document.form._3comment.value.substr(indx-1,pattern_length);
         if (test_string == pattern) {
            indx = indx + pattern_length-1;
            match++;
         }

      }
   }
   if (match > max) {
       spam = "yes";
       document.form.subject.value = "PM form content?";
       return
   }
}

function active_user() {
   if ((document.form.v_code.value) == 3897) {
      document.form.subject.value = "PM form mail";
      return (true)
   }
   alert("invalid verification code")
   document.form.v_code.value=""
   document.form.v_code.focus()
   return (false)
}

function FormValidate() {

   if (check_Name() == false)  return (false);
   if (check_EmailAddress() == false)  return (false);
   if (spam == "")  check_CommentField("href=",1);
	 if (spam == "")  check_CommentField("http=",1);
   if (spam == "")  check_CommentField("url",1);
	 if (active_user() == false)  return (false);
   return (true)
}

