function doCheckComments () {
    var comments = document.getElementById && document.getElementById ('COMMENTS');
	
	tmpary = comments.value.split("*DOT$");
	if (tmpary.length > 1)
		doCalcTotals();
}

function doValidateSendCC() {
    var first_name = document.getElementById && document.getElementById ('first_name');
    var last_name = document.getElementById && document.getElementById ('last_name');
    var email = document.getElementById && document.getElementById ('email');
    var telephone = document.getElementById && document.getElementById ('telephone');
    var address = document.getElementById && document.getElementById ('address');
    var city = document.getElementById && document.getElementById ('city');
    var state = document.getElementById && document.getElementById ('state');
    var zip = document.getElementById && document.getElementById ('zip');
    var emergency_contact = document.getElementById && document.getElementById ('emergency_contact');
    var emergency_telephone = document.getElementById && document.getElementById ('emergency_telephone');
    var how_hear = document.getElementById && document.getElementById ('how_hear');
	var ccardpaytype = document.getElementById && document.getElementById ('ccard_payment_type');
	var checkpaytype = document.getElementById && document.getElementById ('check_payment_type');
    var TOTAL_AMOUNT = document.getElementById && document.getElementById ('TOTAL_AMOUNT');
    var cardholder = document.getElementById && document.getElementById ('cardholder');
    var cardnumber = document.getElementById && document.getElementById ('cardnumber');
    var cardcode = document.getElementById && document.getElementById ('cardcode');
    var acknowledge = document.getElementById && document.getElementById ('acknowledge');
	var formOK = true;

	if (isBlank(first_name.value)) {
		alert("Please provide your first name.");
		first_name.focus();
		formOK = false;
		return formOK};

	if (isBlank(last_name.value)) {
		alert("Please provide your last name.");
		last_name.focus();
		formOK = false;
		return formOK};

	if (!(isEmail(email.value))) {
		alert("Please enter a valid email address.");
		email.focus();
		formOK = false;
		return formOK};

	if (isBlank(telephone.value)) {
		alert("Please provide your telephone number.");
		telephone.focus();
		formOK = false;
		return formOK};

	if (isBlank(address.value)) {
		alert("Please provide your street address.");
		address.focus();
		formOK = false;
		return formOK};

	if (isBlank(city.value)) {
		alert("Please provide your city.");
		city.focus();
		formOK = false;
		return formOK};

	if (state.selectedIndex==0) {
		alert("Please select a state.");
		state.focus();
		formOK = false;
		return formOK};

	if (isBlank(zip.value)) {
		alert("Please provide your zip.");
		zip.focus();
		formOK = false;
		return formOK};

	if (isBlank(emergency_contact.value)) {
		alert("Please provide an emergency contact name.");
		emergency_contact.focus();
		formOK = false;
		return formOK};

	if (isBlank(emergency_telephone.value)) {
		alert("Please provide an emergency\ncontact telephone number.");
		emergency_telephone.focus();
		formOK = false;
		return formOK};

	if (how_hear.selectedIndex==0) {
		alert("Please tell us how you heard\nabout our Summer Program.");
		how_hear.focus();
		formOK = false;
		return formOK};

	if (TOTAL_AMOUNT.value < 1) {
		alert("Please select at least one workshop.");
		formOK = false;
		return formOK};
	
	if (ccardpaytype.checked) {

		if (isBlank(cardholder.value)) {
			alert("Please provide cardholder's name.");
			cardholder.focus();
			formOK = false;
			return formOK};

		if (!(validCC(cardnumber.value))) {
			alert("Please provide a valid credit card number.");
			cardnumber.focus();
			formOK = false;
			return formOK};

		if (isBlank(cardcode.value)) {
			alert("Please provide your card verification number.");
			cardcode.focus();
			formOK = false;
			return formOK};
		}
	else {
		if (!(checkpaytype.checked)) {
			alert("Please choose a payment type.");
			ccardpaytype.focus();
			formOK = false;
			return formOK};
		}

	if (!(acknowledge.checked)) {
		alert("You must acknowledge that you\nhave read and understood\nthe cancellation policy.");
		acknowledge.focus();
		formOK = false;
		return formOK};

return formOK;

}

function doCalcTotals (e) {
	var totdiscounts = 0;
	var totnumclass = 0;
	var totnumhousing = 0;
	var totnumweekclass = 0;
	var totnumweekendclass = 0;
	var totamtclass = 0;
	var totfeeclass = 0;
	var classamt = 0;
	var classfee = 0;
    var comments = document.getElementById && document.getElementById ('COMMENTS');
	var totalamount = document.getElementById && document.getElementById ('TOTAL_AMOUNT');	
	var depositamount = document.getElementById && document.getElementById ('DEPOSIT_AMOUNT');
    var housingtotal = document.getElementById && document.getElementById ('HOUSING_TOTAL');
    var discounts = document.getElementById && document.getElementById ('DISCOUNTS');
    var numclass = document.getElementById && document.getElementById ('NUMBER_OF_CLASSES_SELECTED');
    var numhousing = document.getElementById && document.getElementById ('NUMBER_OF_WEEKS_HOUSING_REQUIRED');
    var classtotal = document.getElementById && document.getElementById ('CLASS_TOTAL');
    var classfeetotal = document.getElementById && document.getElementById ('CLASS_FEE_TOTAL');
	var payinfull = document.getElementById && document.getElementById ('PAY_IN_FULL');
    var weekend1 = document.getElementById && document.getElementById ('Weekend_1');
    var week1 = document.getElementById && document.getElementById ('Week_1');
    var week2 = document.getElementById && document.getElementById ('Week_2');
    var week3 = document.getElementById && document.getElementById ('Week_3');
    var week4 = document.getElementById && document.getElementById ('Week_4');
    var week5 = document.getElementById && document.getElementById ('Week_5');
    var week6 = document.getElementById && document.getElementById ('Week_6');
    var week7 = document.getElementById && document.getElementById ('Week_7');
    var week8 = document.getElementById && document.getElementById ('Week_8');
    var week9 = document.getElementById && document.getElementById ('Week_9');
    var week10 = document.getElementById && document.getElementById ('Week_10');
    var housingweek1 = document.getElementById && document.getElementById ('Need_Housing_Week_1');
    var housingweek2 = document.getElementById && document.getElementById ('Need_Housing_Week_2');
    var housingweek3 = document.getElementById && document.getElementById ('Need_Housing_Week_3');
    var housingweek4 = document.getElementById && document.getElementById ('Need_Housing_Week_4');
    var housingweek5 = document.getElementById && document.getElementById ('Need_Housing_Week_5');
    var housingweek6 = document.getElementById && document.getElementById ('Need_Housing_Week_6');
    var housingweek7 = document.getElementById && document.getElementById ('Need_Housing_Week_7');
    var housingweek8 = document.getElementById && document.getElementById ('Need_Housing_Week_8');
    var housingweek9 = document.getElementById && document.getElementById ('Need_Housing_Week_9');
    var housingweek10 = document.getElementById && document.getElementById ('Need_Housing_Week_10');
	
	if (!((weekend1.value) == "none selected")) {

			tmpary1 = weekend1.value.split(" + $");
			classfee = 0;
			if (tmpary1.length > 1) {
				tmpary3 = tmpary1[tmpary1.length-1].split(" fees");
				classfee = tmpary3[0];
				};
			tmpary2 = tmpary1[0].split(" - $");
			classamt = tmpary2[tmpary2.length-1];
			totnumweekendclass = totnumweekendclass + 1;
			totnumclass = totnumclass + 1;
			totamtclass = totamtclass + parseInt(classamt);
			totfeeclass = totfeeclass + parseInt(classfee);
			};
	if (!((week1.value) == "none selected")) {

			tmpary1 = week1.value.split(" + $");
			classfee = 0;
			if (tmpary1.length > 1) {
				tmpary3 = tmpary1[tmpary1.length-1].split(" fees");
				classfee = tmpary3[0];
				};
			tmpary2 = tmpary1[0].split(" - $");
			classamt = tmpary2[tmpary2.length-1];
			totnumweekclass = totnumweekclass + 1;
			totnumclass = totnumclass + 1;
			if (housingweek1.checked)
				totnumhousing = totnumhousing + 1;
			totamtclass = totamtclass + parseInt(classamt);
			totfeeclass = totfeeclass + parseInt(classfee);
			if (weekend1.selectedIndex == 6 && week1.selectedIndex == 4)
				totamtclass = totamtclass - 80;
			}
		else
			housingweek1.checked = false;
	if (!((week2.value) == "none selected")) {

			tmpary1 = week2.value.split(" + $");
			classfee = 0;
			if (tmpary1.length > 1) {
				tmpary3 = tmpary1[tmpary1.length-1].split(" fees");
				classfee = tmpary3[0];
				};
			tmpary2 = tmpary1[0].split(" - $");
			classamt = tmpary2[tmpary2.length-1];
			totnumweekclass = totnumweekclass + 1;
			totnumclass = totnumclass + 1;
			if (housingweek2.checked)
				totnumhousing = totnumhousing + 1;
			totamtclass = totamtclass + parseInt(classamt);
			totfeeclass = totfeeclass + parseInt(classfee);
			}
		else
			housingweek2.checked = false;
	if (!((week3.value) == "none selected")) {

			tmpary1 = week3.value.split(" + $");
			classfee = 0;
			if (tmpary1.length > 1) {
				tmpary3 = tmpary1[tmpary1.length-1].split(" fees");
				classfee = tmpary3[0];
				};
			tmpary2 = tmpary1[0].split(" - $");
			classamt = tmpary2[tmpary2.length-1];
			totnumweekclass = totnumweekclass + 1;
			totnumclass = totnumclass + 1;
			if (housingweek3.checked)
				totnumhousing = totnumhousing + 1;
			totamtclass = totamtclass + parseInt(classamt);
			totfeeclass = totfeeclass + parseInt(classfee);
			}
		else
			housingweek3.checked = false;
	if (!((week4.value) == "none selected")) {

			tmpary1 = week4.value.split(" + $");
			classfee = 0;
			if (tmpary1.length > 1) {
				tmpary3 = tmpary1[tmpary1.length-1].split(" fees");
				classfee = tmpary3[0];
				};
			tmpary2 = tmpary1[0].split(" - $");
			classamt = tmpary2[tmpary2.length-1];
			totnumweekclass = totnumweekclass + 1;
			totnumclass = totnumclass + 1;
			if (housingweek4.checked)
				totnumhousing = totnumhousing + 1;
			totamtclass = totamtclass + parseInt(classamt);
			totfeeclass = totfeeclass + parseInt(classfee);
			}
		else
			housingweek4.checked = false;
	if (!((week5.value) == "none selected")) {

			tmpary1 = week5.value.split(" + $");
			classfee = 0;
			if (tmpary1.length > 1) {
				tmpary3 = tmpary1[tmpary1.length-1].split(" fees");
				classfee = tmpary3[0];
				};
			tmpary2 = tmpary1[0].split(" - $");
			classamt = tmpary2[tmpary2.length-1];
			totnumweekclass = totnumweekclass + 1;
			totnumclass = totnumclass + 1;
			if (housingweek5.checked)
				totnumhousing = totnumhousing + 1;
			totamtclass = totamtclass + parseInt(classamt);
			totfeeclass = totfeeclass + parseInt(classfee);
			}
		else
			housingweek5.checked = false;
	if (!((week6.value) == "none selected")) {

			tmpary1 = week6.value.split(" + $");
			classfee = 0;
			if (tmpary1.length > 1) {
				tmpary3 = tmpary1[tmpary1.length-1].split(" fees");
				classfee = tmpary3[0];
				};
			tmpary2 = tmpary1[0].split(" - $");
			classamt = tmpary2[tmpary2.length-1];
			totnumweekclass = totnumweekclass + 1;
			totnumclass = totnumclass + 1;
			if (housingweek6.checked)
				totnumhousing = totnumhousing + 1;
			totamtclass = totamtclass + parseInt(classamt);
			totfeeclass = totfeeclass + parseInt(classfee);
			}
		else
			housingweek6.checked = false;
	if (!((week7.value) == "none selected")) {

			tmpary1 = week7.value.split(" + $");
			classfee = 0;
			if (tmpary1.length > 1) {
				tmpary3 = tmpary1[tmpary1.length-1].split(" fees");
				classfee = tmpary3[0];
				};
			tmpary2 = tmpary1[0].split(" - $");
			classamt = tmpary2[tmpary2.length-1];
			totnumweekclass = totnumweekclass + 1;
			totnumclass = totnumclass + 1;
			if (housingweek7.checked)
				totnumhousing = totnumhousing + 1;
			totamtclass = totamtclass + parseInt(classamt);
			totfeeclass = totfeeclass + parseInt(classfee);
			}
		else
			housingweek7.checked = false;
	if (!((week8.value) == "none selected")) {

			tmpary1 = week8.value.split(" + $");
			classfee = 0;
			if (tmpary1.length > 1) {
				tmpary3 = tmpary1[tmpary1.length-1].split(" fees");
				classfee = tmpary3[0];
				};
			tmpary2 = tmpary1[0].split(" - $");
			classamt = tmpary2[tmpary2.length-1];
			totnumweekclass = totnumweekclass + 1;
			totnumclass = totnumclass + 1;
			if (housingweek8.checked)
				totnumhousing = totnumhousing + 1;
			totamtclass = totamtclass + parseInt(classamt);
			totfeeclass = totfeeclass + parseInt(classfee);
			}
		else
			housingweek8.checked = false;
	if (!((week9.value) == "none selected")) {

			tmpary1 = week9.value.split(" + $");
			classfee = 0;
			if (tmpary1.length > 1) {
				tmpary3 = tmpary1[tmpary1.length-1].split(" fees");
				classfee = tmpary3[0];
				};
			tmpary2 = tmpary1[0].split(" - $");
			classamt = tmpary2[tmpary2.length-1];
			totnumweekclass = totnumweekclass + 1;
			totnumclass = totnumclass + 1;
			if (housingweek9.checked)
				totnumhousing = totnumhousing + 1;
			totamtclass = totamtclass + parseInt(classamt);
			totfeeclass = totfeeclass + parseInt(classfee);
			}
		else
			housingweek9.checked = false;
	if (!((week10.value) == "none selected")) {

			tmpary1 = week10.value.split(" + $");
			classfee = 0;
			if (tmpary1.length > 1) {
				tmpary3 = tmpary1[tmpary1.length-1].split(" fees");
				classfee = tmpary3[0];
				};
			tmpary2 = tmpary1[0].split(" - $");
			classamt = tmpary2[tmpary2.length-1];
			totnumweekclass = totnumweekclass + 1;
			totnumclass = totnumclass + 1;
			if (housingweek10.checked)
				totnumhousing = totnumhousing + 1;
			totamtclass = totamtclass + parseInt(classamt);
			totfeeclass = totfeeclass + parseInt(classfee);
			}
		else
			housingweek10.checked = false;

	if (totnumweekclass > 0)
		totdiscounts = (totnumweekclass - 1) * 50;
	if (payinfull.checked && (totalamount.value > 25))
		totdiscounts = totdiscounts + 25;

    if (housingtotal)
        housingtotal.value = 675 * totnumhousing;
    if (numhousing)
        numhousing.value = totnumhousing;
    if (numclass)
        numclass.value = totnumclass;
    if (classtotal)
        classtotal.value = totamtclass;
    if (classfeetotal)
        classfeetotal.value = totfeeclass;
    if (discounts)
        discounts.value = totdiscounts;
	if (totalamount)
		totalamount.value = totamtclass + totfeeclass + (675 * totnumhousing) - totdiscounts;
	if (depositamount)
		depositamount.value = 150 * (totnumclass + totnumhousing);

	tmpary = comments.value.split("*DOT$");
	if (tmpary.length > 1) {
		for (var i = 1; i < tmpary.length; i++) {
			if (!(isNaN(tmpary[i]))) {
				totalamount.value = parseInt(totalamount.value) - parseInt(tmpary[i]);
				discounts.value = parseInt(discounts.value) + parseInt(tmpary[i]);
			};
		};
	}

	doClearPaymentType();
    return true;
}

function doClearPaymentType() {
	var ccardpaytype = document.getElementById && document.getElementById ('ccard_payment_type');
	var checkpaytype = document.getElementById && document.getElementById ('check_payment_type');
	var chargeamount = document.getElementById && document.getElementById ('CHARGE_AMOUNT');	
	var chargebalance = document.getElementById && document.getElementById ('CHARGE_BALANCE');
	var checkamount = document.getElementById && document.getElementById ('CHECK_AMOUNT');	
	var checkbalance = document.getElementById && document.getElementById ('CHECK_BALANCE');

	chargeamount.value = 0;
	chargebalance.value = 0;
	checkamount.value = 0;
	checkbalance.value = 0;
	ccardpaytype.checked = false;
	checkpaytype.checked = false;
	return true;
}

function doFillCCard() {
	var chargeamount = document.getElementById && document.getElementById ('CHARGE_AMOUNT');	
	var chargebalance = document.getElementById && document.getElementById ('CHARGE_BALANCE');
	var checkamount = document.getElementById && document.getElementById ('CHECK_AMOUNT');	
	var checkbalance = document.getElementById && document.getElementById ('CHECK_BALANCE');
    var discounts = document.getElementById && document.getElementById ('DISCOUNTS');
	var totalamount = document.getElementById && document.getElementById ('TOTAL_AMOUNT');	
	var depositamount = document.getElementById && document.getElementById ('DEPOSIT_AMOUNT');
	var payinfull = document.getElementById && document.getElementById ('PAY_IN_FULL');

	if (payinfull.checked) {
		chargeamount.value = totalamount.value;
		chargebalance.value = 0;
	} else {
		chargeamount.value = depositamount.value;
		chargebalance.value = parseInt(totalamount.value) - parseInt(depositamount.value);
	};
	checkamount.value = 0;
	checkbalance.value = 0;
	return true;
}

function doFillCheck() {
	var checkamount = document.getElementById && document.getElementById ('CHECK_AMOUNT');	
	var checkbalance = document.getElementById && document.getElementById ('CHECK_BALANCE');
	var chargeamount = document.getElementById && document.getElementById ('CHARGE_AMOUNT');	
	var chargebalance = document.getElementById && document.getElementById ('CHARGE_BALANCE');
	var totalamount = document.getElementById && document.getElementById ('TOTAL_AMOUNT');	
	var depositamount = document.getElementById && document.getElementById ('DEPOSIT_AMOUNT');
	var payinfull = document.getElementById && document.getElementById ('PAY_IN_FULL');

	if (payinfull.checked) {
		checkamount.value = totalamount.value;
		checkbalance.value = 0;
	} else {
		checkamount.value = depositamount.value;
		checkbalance.value = parseInt(totalamount.value) - parseInt(depositamount.value);
	};
	chargeamount.value = 0;
	chargebalance.value = 0;
	return true;
}

function isBlank(s) {
	if ((s == null) || (s == "")) {return true};
	for (var i=0;i<s.length;i++) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	};
	return true;
}

function isEmail(strEmail) {

  var emailOK = true;
  var intPosAt = strEmail.indexOf("@");
  var intLPosAt = strEmail.lastIndexOf("@");
  var intPosDot = strEmail.indexOf(".");
  var intLPosDot = strEmail.lastIndexOf(".");
  
  if ((intPosAt != intLPosAt) ||
      (intPosAt < 1) ||
      (intLPosDot < intPosAt) ||
      (intLPosDot >= (strEmail.length - 2))) {
    emailOK = false;
  };
  return emailOK;
}

function validCC(strCC) {

  var ccOK = true;
  var strTmpCC = strCC + "";
  var intCheckSum = new Number(0);

  if (strCC.length == 0) {
    ccOK = false;
  } else {
    for (var intLoop = (strTmpCC.length - 1); intLoop >= 0; (intLoop = intLoop - 1)) {
      var intTmp = 0;
      if (((strTmpCC.length - 1) - intLoop) % 2) {
        intTmp = strTmpCC.substr(intLoop, 1) * 2;
        if (intTmp >= 10) {
          strTmp = intTmp + "";
          intTmp = parseInt(strTmp.substr(0, 1)) + parseInt(strTmp.substr(1, 1));
        }
      }
      else {
        intTmp = strTmpCC.substr(intLoop, 1);
      }
      intCheckSum += parseInt(intTmp);
    }
    if (intCheckSum % 10) {
      ccOK = false;
    }
  }
  return ccOK;

}
