// newsletter javascript functions
var whitespace = " \t\n\r";
// TRUE if field is empty or blanks
function isEmpty(s){
  if((s == null) || (s.length == 0)) return true;
  for(var i=0;i<s.length;i++){
   var c = s.charAt(i);
   if(whitespace.indexOf(c)<0) return false;
  }
  return true;
}
function charInString (c, s){
  for (i = 0; i < s.length; i++){
    if (s.charAt(i) == c) return true;
  }
  return false
}
// Removes all characters which DO appear in string bag
// from string s.
function stripCharsInBag(s,bag){
  var i;
  var returnString = "";
  for (i = 0; i < s.length; i++){
    var c = s.charAt(i);
    if(!charInString(c,bag)) returnString += c;
  }
  return returnString;
}
var badEmailChars=" !\"#$%&()*,;<=>?[\\]^`{|}~";
var ename=0,eat=0,e1=0,edot=0,e2=0,el=0;
// format must be minimum of c@c.c
function isEmail(s){
  ename=eat=e1=edot=e2=el=0;
  s=stripCharsInBag(s,badEmailChars); s=s.toLowerCase();
  document.forms[0].email.value=s;
  el=s.length;
  for(var i=0;i<el;i++){
    c=s.charAt(i);
    if(c=='@') eat++;
    else if(eat>0){
      if(c=='.'){ edot++; }
      else { 
        switch(edot){
        case 0: e1++; break;
        case 1: e2++; break;
        default: break;
        }
      }
    }else ename++;
  }
  if(ename>0 && eat==1 && e1>0 && edot>0 && e2>0) return true;
  return false;
} 
function addEmail(){
  var f=document.forms[0];
  var s='We need '; var e=0;
  if(isEmpty(f.fname.value)){ s+='your first name, '; e++; }
  if(isEmpty(f.lname.value)){ s+='your last name, '; e++; }
  if(isEmpty(f.email.value)){ s+='your e-mail address, '; e++; }
  if(e){ s+='to add you to the distribution list.'; alert(s); return; }
  if(!isEmail(f.email.value)){
    alert(f.email.value+' is not a valid e-mail address.');
    return;
  }
  f.mode.value='add';
  f.submit();
}
function remEmail(){
  var f=document.forms[0];
  if(isEmpty(f.email.value)){
    alert('No e-mail address to remove?'); return;
  }
  var s='Are you sure you want to remove your e-mail address\r\n';
  s+='from the PapHaven Newsletter distribution list?';
  if(!confirm(s)){ return; }
  f.mode.value='del';
  f.submit();
}
