function  checkout (mydoc) {
   var elem;
   var xname;
   var vval= new Array(); 
   var vlen = new Array();
   var nchkd = 0;
   var nform = document.forms.length;
   for ( var x = 0;x < nform; x++) {
      for (var y = 0; y< document.forms[x].elements.length; y++) {
         var xtype = document.forms[x].elements[y].type;
         if (document.forms[x].elements[y].type  == 'text') {
            var elname = document.forms[x].elements[y].name; 
            var xval = document.forms[x].elements[y].value; 
            vval[elname] = xval; 
            vlen[elname] = xval.length; 

//          alert (elname  + " equals " + vval[elname]); 
//          alert (elname + " length " + vlen[elname]); 
//          alert ("X is " + x + "and y is " + y); 
         }
         if (document.forms[x].elements[y].type  == 'checkbox') { 
            if (document.forms[x].elements[y].checked  == true) {
               nchkd++;
            }   
         }
      }
   }
//   alert ("showing array");
//   for (var i in vval) {
//      alert (i + " equals " + vval[i] + " length " + vlen[i]); 
//   }

   var doyou;
   var phstat;  
   var emstat;  
//   alert ("this is checkout"); 
   if (vlen['SIGNEE'] == 0) {
      alert ("Please enter your name.");
      return false;
   }
   if (vlen['PHONE'] == 0 && vlen['EMAIL'] == 0) {
      alert ("Please enter either a phone number or an email address. ");
      return false;
   }

   if (nchkd < 1) { 
      alert ("Please select the Sunday for which you are volunteering");  
      return false;
   }
   if (nchkd > 1) {
      alert ("Please select just one Sunday");  
   for ( var x = 0;x < nform; x++) {
      for (var y = 0; y< document.forms[x].elements.length; y++) {
         var xtype = document[x].elements[y].type;
         if (xtype == 'checkbox') {
         document[x].elements[y].checked = false;
         }
      }
      return false;
   }
   }
//   alert ("entering phone test with "); 
   phstat = phone_test(vval['PHONE']);  
//   alert ("back from phone test"); 
   if (phstat == 0)  {                // Phone stat is bad
      doyou = confirm("This looks like a bad phone--use it anyway? (OK=Yes Cancel=Stop and correct)");
      if (doyou )  {
         //                  alert ("You approved it");
      } else  {
         alert ("Please correct the phone number.");
         return false; 
      }
   }
   emstat = email_test(vval['EMAIL']);  
   if (emstat == 0)  {                // If email stat is bad
      doyou = confirm("This looks like a bad email--use it anyway? (OK=Yes Cancel=Stop and correctNo)");
      if (doyou  )  {
         //         alert ("You approved it");
      } else  {
         alert ("Please correct the email address.");
         return false; 
      }             
   }
//   alert ("All passed -- Submitting document");
   document.form1.submit();
}


function phone_test (pn) {
   var i;              // for variable
   var nm = 0;         // number of numbers
   var pnc = 0;        // number of punctuation characters;
   var valpnc = '().-+';
   var valid  = 1;
   var pnl = pn.length;
   if (pnl == 0) { return 1 };     // Nothing is valid
//        alert ("into phone_test with " + pn + " -- pn length is " + pn.length);

   for (i=0;i<pn.length;i++) {
      var c = pn.charAt(i);
      //            alert ("c is " + c);
      if ((c >= 0) && (c <=9)) {
         nm++; 
      } else {
         if (valpnc.indexOf(c) != -1) {
            pnc++;
         } else {
            valid = 0;
         }
      }
   }
   //     alert ("digits = " + nm + " pnc = " + pnc );
   if ((nm !=7) && (nm != 10)) valid = 0;    // wrong number of digits;
   if (pnc > 4)  valid = 0;                //  Too many punctuation characters
//            alert ("phone validity is " + valid);
   return valid;  
}
function email_test (eml) {
   var elen = eml.length;
   var i;
   var val = 1;
   var ancnt = 0;
   var atcnt = 0;
   var dotcnt = 0;
   var valpnc = '-_.\@';
   var tchr;
   var x;
   if (elen == 0) return 1;   // No email -- that's okay
   //   alert ("length of email is " + elen);
   for (i=0;i<elen;i++) {
      tchr  = eml.charAt(i);
      x = alNumCk (tchr);
      if ( x ) ancnt++;     // if it's alphanumeric 
      //      if ( x ) alert (x + " is alpha num");     // if it's alphanumeric 
      if (tchr == '\@') atcnt++  // Need exactly one of these  -- should be @
      //      if (tchr == '\@') alert ("it is an at");  // Need at least one of these
      if (tchr == '.') dotcnt++;   //At least one of these  -- should be period
      //      if (tchr == '.') alert ("it is a dot");  // Need exactly one of these  -- should be @
      //      if ((!x) && (valpnc.IndexOf(tchr) == -1)) {  
         //         val = 0; 
      //      } else {
         //         alert ("Passed special character test");
      //      }
   }
   //   alert ("ancnt is " + ancnt + ", atcnt is " + atcnt + " and dotcnt is " + dotcnt);
   if (atcnt != 1) val = 0;
   if (dotcnt < 1) val = 0;
   if (ancnt < 10) val = 0;
   //   alert ("email val--returning " + val);
   return val;
}


function alNumCk(theChar) {
   //   alert ("into alnumck");
   if ((theChar < 48) || (theChar > 122) || 
      ((theChar > 57) && (theChar < 65)) || 
      ((theChar > 90) && (theChar < 97))   ) {
      return false;
   } else {
      return true;
   }
}
function badcup () {
   alert ("Not this cup! When you're done, click on the RED cup!");
}
function badflower () {
   alert ("Not the bouquet! When you're done, click on the yellow vase!");
}
