function stripCharsInBag (s, bag) {

    var i;
    var returnString = "";

    // Search through string's characters one by one; If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++) {   
      // Check that current character isn't whitespace.
      var c = s.charAt(i);
      if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function isEmail(string) {
    if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
      return true;
    else
      return false;
}

function fixYear(date) {
  return (window.navigator.appName == "Netscape") ? eval(date.getYear()+1900) : date.getYear();
}

function AfterToday(dateString) {

  var now = new Date();
  var year = fixYear(now);
  var today = new Date(year,now.getMonth(),now.getDate());
  var date = new Date('20' + dateString.substring(6,8), dateString.substring(0,2)-1, dateString.substring(3,5));

  return ((date > today) ? 1 : 0);
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function isValidDate (myDate,sep) {
// checks if date passed is in valid dd/mm/yy format

    if (myDate.length == 8) {
        if (myDate.substring(2,3) == sep && myDate.substring(5,6) == sep) {
            var month = myDate.substring(0,2);
            var date  = myDate.substring(3,5);
            var year  = myDate.substring(6,8);
            year = '20' + year;

            var test = new Date(year,month-1,date);

            if (year == y2k(test.getYear()) && (month-1 == test.getMonth()) && (date == test.getDate())) {

                if (AfterToday(myDate)) {
                    return true;
                } else {
                    alert('The date that you have supplied is not after today!');
                    return false;
                }
            } else {
                alert('The date that you have supplied does not exist.');
                return false;
            }
        } else {
            alert('The date that you have supplied is not in the proper form.  Please supply a date in dd/mm/yy format.');
            return false;
        }
    } else {
        alert('The date that you have supplied is not the correct length.  Please supply a date in dd/mm/yy format.');
        return false;
    }

} //end isValidDate

function Validate (it) {  //validate form input

    //it == document.forms[signup]
    var paypal = document.forms["paypal"];

    if (!it.realname.value) {
      alert("Please enter your name.");
      it.realname.focus();
      return (false);
    }

    var normalizedPhone = stripCharsInBag(it.phone.value, '().- ');

    if (!normalizedPhone) {
      alert("Please enter your phone number.");
      it.phone.focus();
      return false;
    }
    else {
      if (normalizedPhone.length != 10) {
        alert("Please enter a 10-digit phone number.");
        it.phone.focus();
        return false;
      }
      else {
        var Chars = "0123456789";
        for (var i = 0; i < normalizedPhone.length; i++) {
          if (Chars.indexOf(normalizedPhone.charAt(i)) == -1) {
            alert("A phone number can contain only numbers.");
            it.phone.focus();
            return false;
          }
        }
      }
    }

    // Email address must be of form a\@b.c
    if (isEmail(it.email.value) == false) {
      alert("Please enter a valid email address.");
      it.email.focus();
      return false;
    }

    if (!it.address.value) {
      alert("Please enter your street address.");
      it.address.focus();
      return (false);
    }

    if (!it.city.value) {
      alert("Please enter your city name.");
      it.city.focus();
      return (false);
    }

    if (!it.state.value) {
      alert("Please choose your state.");
      it.state.focus();
      return (false);
    }

    if (!it.zip.value) {
      alert("Please enter your zip code.");
      it.zip.focus();
      return (false);
    }

    if (it.zip.value.length != 5) {
      alert("Please enter a 5-digit zip code.");
      it.zip.focus();
      return (false);
    }

    var Chars = "0123456789";
    for (var i = 0; i < 5; i++) {
      if (Chars.indexOf(it.zip.value.charAt(i)) == -1) {
        alert("A zip code can contain only numbers.");
        it.zip.focus();
        return (false);
      }
    }
    
    if (!isValidDate(it.DOB.value,'/')) {
      it.DOB.focus();
      return (false);
    }

  if (it.payment_method[0].checked) { //paypal payment was selected

	if (it.payment_amount[0].checked) { //full payment at this time

	  paypal.item_number.value = "Full Camp Tuition";
	  paypal.amount.value = "625.00";
	  paypal.handling.value = "12.00";
	  paypal.address1.value = it.address.value;
	  paypal.city.value = it.city.value;
	  paypal.state.value = it.state.value;
	  paypal.zip.value = it.zip.value;

	  paypal.target = "paypal";
	  paypal.action = "https://www.paypal.com/cgi-bin/webscr";
	  paypal.submit();

	  it.redirect.value += "formmail.shtml";
	  it.target = "_self";
	  it.action = "../cgi-bin/formmail/formmail.cgi";
	  it.submit();

	} else if (it.payment_amount[1].checked) { //just a deposit for now

	  paypal.item_number.value = "Camp Tuition Deposit";
	  paypal.amount.value = "100.00";
	  paypal.handling.value = "3.00";
	  paypal.address1.value = it.address.value;
	  paypal.city.value = it.city.value;
	  paypal.state.value = it.state.value;
	  paypal.zip.value = it.zip.value;

	  paypal.target = "paypal";
	  paypal.action = "https://www.paypal.com/cgi-bin/webscr";
	  paypal.submit();

	  it.redirect.value += "formmail.shtml";
	  it.target = "_self";
	  it.action = "../cgi-bin/formmail/formmail.cgi";
	  it.submit();

	} else {

          alert("Would you like to pay a $100 deposit, or pay for the complete camp tuition at this time?");
	  return (false);

	}

  } else if (it.payment_method[1].checked) { //snailmail payment was selected

	if ((!it.payment_amount[0].checked) && (!it.payment_amount[1].checked)) { //no payment choice made

          alert("Would you like to pay a $100 deposit, or pay for the complete camp tuition at this time?");
	  return (false);

	}

	it.redirect.value += "snailmail.shtml";
	it.target = "_self";
	it.action = "../cgi-bin/formmail/formmail.cgi";
	it.submit();

  } else {
        alert("You must choose a payment method.");
	return (false);
  }

} //end Validate