// JavaScript Document
// Form Validation functions --- Donation Form

function Form_Validator(DonationForm)
{  
  if (DonationForm.transactionamount.value == "")
  {
    alert("Please enter the value of your payment");
    DonationForm.transactionamount.focus();
    return (false);
  }

  var checkOK = "0123456789.,";
  var checkStr = DonationForm.transactionamount.value;
  var allValid = true;
  var validGroups = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    if (ch == ".")
    {
      allNum += ".";
      decPoints++;
    }
    else if (ch == "," && decPoints != 0)
    {
      validGroups = false;
      break;
    }
    else if (ch != ",")
      allNum += ch;
  }
  if (!allValid)
  {
    alert("Only numbers 0 to 9 and the period should be entered in the Amount field - e.g. 100.00");
    DonationForm.transactionamount.focus();
    return (false);
  }
  if (decPoints > 1 || !validGroups)
  {
    alert("There cannot be more than one decimal point in the payment amount");
    DonationForm.transactionamount.focus();
    return (false);
  }
  
  
  if (DonationForm.transactionamount.value < 5)
  {
    alert("Sorry, but the card processing service will not allow us to process charges of less than £5");
    DonationForm.transactionamount.focus();
    return (false);
  }
   
  if (DonationForm.email.value == "")
  {
    alert("Please enter your email address");
    DonationForm.email.focus();
    return (false);
  }

  if (DonationForm.email.value.length < 8)
  {
    alert("You do not appear to have entered a valid email address");
    DonationForm.email.focus();
    return (false);
  }

  return (true);
}