var submitcount=0;
var maxwords = 3000;
var minwords = 100;

function validBMInfo(email,contactname,type,first,location,country,content) {
  var errorflag = false;
  var errmsg = "The entry could not be submitted because: \n";
  
  if (!validEmail(email)) {
    errmsg += "The email address is invalid" + "\n";
    errorflag = true;
  }
  if (isblank(contactname)) {
    errmsg += "The contact name is a required field" + "\n";
    errorflag = true;
  }
  
  if (type=="" || type=="0") {
    errmsg += "The type (Bar or Bat Mitzvah) is a required field" + "\n";
    errorflag = true;
  }

  if (isblank(first)) {
    errmsg += "The first name is a required field" + "\n";
    errorflag = true;
  }

  if (isblank(location)) {
    errmsg += "The location is a required field" + "\n";
    errorflag = true;
  }

  if (country=="" || country=="0") {
    errmsg += "Country is a required field" + "\n";
    errorflag = true;
  }

  if (contentmsg=validContent(content)) {
    errmsg += contentmsg + "\n";
    errorflag = true;
  }

  if (!errorflag) {return null;}
  else {return errmsg;}
}


function validContent(content) {
  wc = returnWordCount(content);
  if (wc<minwords) {
    return "Content must be at least " + minwords + " words.";
  }
  else if (wc>maxwords) {return "Content cannot be more than " + minwords + " words.";}
  else {return null;}
}

function returnWordCount(field) {
  wordcounter=0;
  for (x=0;x<field.value.length;x++) {
    if (field.value.charAt(x) == " " && field.value.charAt(x-1) != " ")  {wordcounter++} //Count the spaces
    if (wordcounter > maxwords) {return wordcounter;} //past limit, no need to count more
  }
  return wordcounter;
}

function wordCounter(field, countfield, maxlimit) {
  wordcounter=0;
  for (x=0;x<field.value.length;x++) {
    if (field.value.charAt(x) == " " && field.value.charAt(x-1) != " ")  {wordcounter++} //Count the spaces
    if (wordcounter > maxlimit) {field.value = field.value.substring(0, x);}
    //else {countfield.value = wordcounter;}
  }
  if (field.value.charAt(x-1)!=" " & x!=0) {wordcounter++}; // check the last character, if not space add increment count
  countfield.value = wordcounter;
}

function textCounter(field, countfield, maxlimit) {
  if (field.value.length > maxlimit) {
    field.value = field.value.substring(0, maxlimit);
  }
  else {
    countfield.value = maxlimit - field.value.length;
  }
}

function countWords(field,countfield,maxlimit) {
 //document.all["nowcountbtn"].disabled=false;
 wordCounter(field,countfield,maxlimit);
 //document.all["nowcountbtn"].disabled=true;
}

//-----------------------

// A utility function that returns true if a string 
// contains only whitespace characters.

function isblank(s)
{
  for(var i = 0; i < s.length; i++) {
     var c = s.charAt(i);
     if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
  }
  return true;
}

function validEmail(emAddr) {
  // Now check for fields that are supposed to be emails.
  // Not exactly as described in RFC 2822, but a rough attempt
  // of the form "local-bit@domain-bit"
  if (!isblank(emAddr)) {
    var seenAt = false;
    var append = "";
    var errflag = false;
    for(var j = 0; j < emAddr.length; j++) {
      var c = emAddr.charAt(j);
      if ((c == ' ') || (c == '\n') || (c == '\t')) errflag = true;
      if ((c == '@') && (seenAt == true)) errflag = true;
      if (c == '@') seenAt = true;
    }
    if (seenAt == false) errflag = true;
  } else {errflag = true;}
  
  return !errflag;
}
