// The website owner asserts copyright and all other possible rights on this code.

function verfcard(cnum)
{var subtotal, remaining, digit, w = 1, total = 0, report="";
 remaining = cnum;
 if   (cnum.length % 2 == 0)
      {w = 2};
 while (remaining.length != 0)
       { digit     = remaining.substr(0,1);
         remaining = remaining.substr(1, remaining.length-1);
         subtotal = digit * w;
         if    (subtotal > 9)
	    {total = total + 1;
             subtotal = subtotal -10;
            };
         total = total + subtotal;
         if (w==1) {w=2} else {w=1};
       };
 if   (total % 10 != 0)
      {return false}
 else {return true};
};

function Verify()
{var errcount = 0, errors = ""
   if   (document.reg.FirstName.value == "")
        errors += ++errcount + ". Please enter your first name\n"
   else if (document.reg.FirstName.value.indexOf(' ') != -1)
            errors += ++errcount + ". Put only your first name in the box. No spaces.\n"

   if   (document.reg.LastName.value == "")
        errors += ++errcount + ". Please enter your last name\n"
   else if (document.reg.LastName.value.indexOf(' ') != -1)
            errors += ++errcount + ". Put only your last name in the box. No spaces.\n"

   if   (document.reg.Email.value =="")
        errors += ++errcount + ". Please enter your email address\n"
   else {if (	(document.reg.Email.value.length < 7) ||
           	(document.reg.Email.value.indexOf('@') == -1) ||
           	(document.reg.Email.value.indexOf('.') == -1) ||
           	(document.reg.Email.value.indexOf('@') == 0) ||
           	(document.reg.Email.value.indexOf('@') == document.reg.Email.value.length-1)
             ) 
            errors += ++errcount + ". Your email address is wrong\n"
        };

  if (document.reg.Street.value.length < 5)
      errors += ++errcount + ". The contents of the Street address are too short\n";
  if (document.reg.Town.value == "")
      errors += ++errcount + ". Please enter a value for City or Town\n";
  if (document.reg.Zip.value == "")
      errors += ++errcount + ". Please enter a value for Post Code\n";
  if (document.reg.Cardname.value == "")
      errors += ++errcount + ". Please enter a value for Nams As On Card\n";

  if (document.reg.Cardtype[document.reg.Cardtype.selectedIndex].text == "Select from the list")
      errors += ++errcount + ". Please select your card type\n";

  if ( (  document.reg.Cardtype[document.reg.Cardtype.selectedIndex].text
           == "OTHER (write card type in the Notes box)")
  && (document.reg.Notes.value.length < 4)
     ) errors += ++errcount + ". Please specify your OTHER card type in the notes box\n";

  if (   (document.reg.Cardnumber.value.length < 9)
      || isNaN(document.reg.Cardnumber.value.length)
      || (document.reg.Cardnumber.value.indexOf(' ') != -1)
     )
      errors += ++errcount + ". Your card number is missing, too short, or includes spaces\n"
 
  var year   = document.reg.ExpiryYear[document.reg.ExpiryYear.selectedIndex].text;
  var month  = document.reg.ExpiryMonth[document.reg.ExpiryMonth.selectedIndex].text;
  var year2  = document.reg.ExpiryYear2[document.reg.ExpiryYear2.selectedIndex].text;
  var month2 = document.reg.ExpiryMonth2[document.reg.ExpiryMonth2.selectedIndex].text;
  
  if (    (year != year2)
      ||  (month != month2)
	 ) errors += ++errcount + ". The card expiry dates you entered should be the same but are not\n"; 
  
  if (   ( year == "--")
      || ( month == "--")
     ) errors += ++errcount + ". The Card expiry date is not completely entered\n"
  else
     {var now = new Date();
      if (   (year < now.getFullYear())
          || (   (year == now.getFullYear())
              && (month < now.getMonth()+1)
             )
         ) errors += ++errcount + ". Your card has expired according to your entered dates\n";
     };
	 
  if (   (document.reg.Cardtype[document.reg.Cardtype.selectedIndex].text == "SWITCH")
      && (document.reg.IssueNumber[document.reg.IssueNumber.selectedIndex].text == "None")
     )
    { errors += ++errcount + ". We need an Issue number for your SWITCH card\n";
    };

  if (!verfcard(document.reg.Cardnumber.value))
    { errors += ++errcount + ". Your card number: " + document.reg.Cardnumber.value + "- is not valid. Did you enter it correctly?\n";
    };
   
  if (errcount > 0)
       {record(errors);
        errors = "Your form contains the following problems:\n\n"
               + errors
               + "\n\nPlease correct & re-submit.\n"
               + "To report a problem with the form, click the link at the top of the page.";
        alert(errors);
       };
  return errcount;
};