function check(myProgram, myForm) {
	var myProgram = myProgram.value;	

	if (myProgram == "1-CAP-Masters") {
		document.getElementById('class_list_div').innerHTML = '<select name="class_list[]" size="1" id="class_list"><option value="ALL">ALL</option></select>';
	 	myForm.program_cost.value = "$1595";
	 	myForm.total_cost.value = "$1620";
	}
	else if (myProgram == "2-CAP-wo-Masters") {
		document.getElementById('class_list_div').innerHTML = '<select name="class_list[]" size="1" id="class_list"><option value="ALL">ALL</option></select>';
		myForm.program_cost.value = "$3495";
	 	myForm.total_cost.value = "$3520";
	}
	else if (myProgram == "3-CAC") {
		document.getElementById('class_list_div').innerHTML = '<select name="class_list[]" size="1" id="class_list"><option value="ALL">ALL</option></select>';
		myForm.program_cost.value = "$2495";
	 	myForm.total_cost.value = "$2520";
	}
	else if (myProgram == "4-CAS") {
		document.getElementById('class_list_div').innerHTML = '<select name="class_list[]" size="1" id="class_list"><option value="ALL">ALL</option></select>';
		myForm.program_cost.value = "$1495";
	 	myForm.total_cost.value = "$1520";
	}
	else if (myProgram == "5-Ind") {
		document.getElementById('class_list_div').innerHTML = '<select name="class_list[]" size="10" multiple id="class_list" onchange="check2(this, this.form);"><option value="04-03-09-Signs-symptoms">04/03/09  - Signs/symptoms</option><option value="04-10-09-Signs-symptoms ">04/10/09 - Signs/symptoms</option><option value="07-17-09-12-steps">04/17/09 - 12 step meetings</option><option value="04-24-09-pharmacology1">04/24/09 - Pharmacology I</option><option value="05-01-09-hiv">05/01/09 - HIV</option><option value="05-08-09-Dual-Diagnosis-1">05/08/09 - Dual Diagnosis I</option><option value="05-15-09-Dual-Diagnosis-2">05/15/09 - Dual Diagnosis II</option><option value="05-22-09-Sex-Addictions">05/22/09 - Sex Addictions</option><option value="06-05-09-History-of-Addiction">06/05/09 - History of Addiction</option><option value="06-12-09-Food-Add">06/12/09 - Food Addiction and Eating Disorders</option><option value="06-19-09-Etiology-Causation">06/19/09 - Etiology/Causation for Addiction</option><option value="06-26-09-Gambling">06/26/09 - Gambling and other addictions</option><option value="07-10-09-Involvement-Treatment">07/10/09 - Involvement and Treatment of the Family in Recovery I</option><option value="07-17-09-Involvement-Treatment2">07/17/09 - Involvement and Treatment of the Family in Recovery II</option><option value="07-24-09-Involvement-Treatment3">07/24/09 - Involvement and Treatment of the Family in Recovery III</option><option value="07-31-09-Pharma">07/31/09 - Pharmacology II</option><option value="08-07-09-Support-Groups-violence">08/07/09 - Use of Support Groups (domestic violence, sexual abuse)</option><option value="08-14-09-Relationship-addictions">08/14/09 - Relationship of addictions to health, crime and other social problems</option><option value="08-21-09-Types-components">08/21/09 - Types of services/modalities/treatment components</option><option value="08-28-09-Individual-Counseling-Tech">08/28/09 - Individual Counseling Techniques</option><option value="09-11-09-Models-of-Treat-I">09/11/09 - Models of Treatment I</option><option value="09-18-09-Models-of-Treat-II">09/18/09 - Models of Treatment II</option><option value="09-25-09-Family-Counseling-Tech">09/25/09 - Family Counseling Techniques</option><option value="10-02-09-Group-Counseling-Tech">10/02/09 - Group Counseling Techniques</option><option value="10-09-09-Cultural-Comp">10/09/09 - Cultural Competence</option><option value="10-16-09-Special-Pop">10/16/09 - Special Population Needs</option><option value="10-23-09-Understanding-Research">10/23/09 - Understanding Research/Translating Research to Practice</option><option value="10-30-09-Rules-regulations">10/30/09 - Rules and regulations that govern full continuum of services and referrals</option><option value="11-06-09-Wellness-Nutrition">11/06/09 - Wellness and Nutrition</option><option value="11-13-09-Laws-gov">11/13/09 - Laws governing privacy, confidentiality and HIPAA</option><option value="11-20-09-Mediation-Assisted">11/20/09 - Mediation-Assisted treatment</option><option value="12-04-09-Alternative-therapies">12/04/09 - Alternative therapies</option><option value="12-11-09-Observation-com">12/11/09 - Observation and communication  skills</option><option value="12-18-09-Care-caregiver">12/18/09 - Care for the caregiver</option><option value="01-08-10-Ethics-Dec">01/08/10 - Ethics and Ethical Decision-Making</option><option value="01-15-10-Domestic-Vio">01/15/10 - Domestic Violence</option><option value="01-22-10-Management-I">01/22/10 - Management and Leadership Skills I</option><option value="01-29-10-Management-II">01/29/10 - Management and Leadership Skills II</option><option value="02-05-10-Supervision-Tech-I">02/05/10 - Supervision Techniques I</option><option value="02-12-10-Supervision-Tech-II">02/12/10 - Supervision Techniques II</option></select>';
		myForm.program_cost.value = "$0";
	 	myForm.total_cost.value = "$0";
	}
	else {
		document.getElementById('class_list_div').innerHTML = '<select name="class_list" size="5" multiple id="class_list"><option value="select_program" selected>Select Program Above</option></select>';
		myForm.program_cost.value = "$0";
	 	myForm.total_cost.value = "$0";

	}
}
function check2(myProgram, myForm) {
	
	 selected = new Array(); 
	 
	 for (var i = 0; i < myProgram.options.length; i++) { 
	 
	 if (myProgram.options[i].selected)  {
	 selected.push(myProgram.options[i].value);
	 }
	 
	 }
	 
	 var price = selected.length;
	 
	 myForm.program_cost.value = "$" + (price * 50);
	 myForm.total_cost.value = "$" + (price * 50 + 25);
}

function check_payment(myPayment) {
		var myPayment = myPayment.value;
		if (myPayment == "credit_card") {
			var myPaymentData = document.getElementById("payment_data");
			myPaymentData.style.display = "block";
			myPaymentData.style.background = "#eeeeee";
		}
		else {
			var myPaymentData = document.getElementById("payment_data");
			myPaymentData.style.display = "none";
		}
}

// Start form validation

// Setup the Array to hold any errors that will be alerted back to the user
var errors = new Array();

function display_errors(errors){
	//loop through all the errors in the errors array
	var err_msg = '';
	for(i=0;i<errors.length;i++){
		err_msg += errors[i] + "\n";
	}

	alert("There were "+errors.length+" errors:\n\n" + err_msg);
	return false;
}

// A utility function that returns true if a string contains only
// whitespace characters.
function isblank(e)
{
  if (e.value == null || e.value == "")
    return true;

  for(var i = 0; i < e.value.length; i++)
  {
     var c = e.value.charAt(i);
     if ((c != ' ') &&
         (c != '\n') &&
         (c != '\t'))
        return false;
  }
  return true;
}

// Checks if an optional field is blank
function checkblank(e)
{
  if (isblank(e))
  {
    errors.push("The Field \"" + e.description + "\" must be filled in.");
  }
  return true;
}


// Mike's check radio button function
function checkradio(e){
	var ischecked = '';
	for(i=0;i<e.length;i++){
	    (e[i].checked==false) ? ischecked += 'f' : ischecked += '';
	}
	if(ischecked=='ff'){
		errors.push("The Field \"" + e.description + "\" must be checked.");
	}
	return true;
}

// Checks if a field is numeric.
// If the optional min property is set, it checks it is greater than
// its value
// If the optional max property is set, it checks it is less than
// its value
function checknumber(e)
{
  var v = parseFloat(e.value);

  if (isNaN(v))
  {
    errors.push("The Field \"" + e.description + "\" must be a number");
  }

  if ((e.minNumber != null) && (v < e.minNumber))
  {
	errors.push("The Field \"" + e.description + "\" must be greater than or equal to "+e.minNumber);
  }

  if (e.maxNumber != null && v > e.maxNumber)
  {
	errors.push("The Field \"" + e.description + "\" must be less than or equal to "+e.maxNumber);
  }

  return true;
}

// Checks if a field looks like a date in the yyyy-mm-dd format
function checkdate(e)
{
  var slashCount = 0;
  if (e.value.length != 10)
  {
	errors.push("The Field \"" + e.description + "\" must have the format yyyy-mm-dd and be 10 characters in length");
  }

  for(var j = 0; j < e.value.length; j++)
  {
    var c = e.value.charAt(j);

    if ((c == '-'))
       slashCount++;

    if (c != '-' && (c < '0' || c > '9'))
    {
	errors.push("The Field \"" + e.description + "\" can contain only numbers and dashes");
    }
  }

  if (slashCount != 2)
  {
	errors.push("The Field \"" + e.description + "\" must have the format yyyy-mm-dd");
  }

  return true;
}

// Checks if a field contains any whitespace
function checkwhitespace(e)
{
  var seenAt = false;

  for(var j = 0; j < e.value.length; j++)
  {
     var c = e.value.charAt(j);

     if ((c == ' ') || (c == '\n') || (c == '\t'))
     {
		errors.push("The Field \"" + e.description + "\" must not contain whitespace.");
     }
  }
  return true;
}

// Now check for fields that are supposed to be emails.
// Only checks that there's one @ symbol and no whitespace
function checkemail(e)
{
  var seenAt = false;

  for(var j = 0; j < e.value.length; j++)
  {
    var c = e.value.charAt(j);

    if ((c == ' ') || (c == '\n') || (c == '\t'))
    {
		errors.push("The Field \"" + e.description + "\" must not contain whitespace");
    }

    if ((c == '@') && (seenAt == true))
    {
		errors.push("The Field \"" + e.description + "\" must contain only one @");
    }

    if ((c == '@'))
      seenAt = true;
  }

  if (seenAt == false)
  {
	errors.push("The Field \"" + e.description + "\" must contain one @");
  }
  return true;
}


// This is the function that performs <form> validation.
// It is invoked from the onSubmit( ) event handler.
// The handler should return whatever value this function
// returns.
function verify(f)
{
  
    // Loop through the elements of the form, looking for all
  // text and textarea elements. Report errors using a post validation,
  // field-by-field approach
  for(var i = 0; i < f.length; i++)
  {
     var e = f.elements[i];

     if (((e.type == "text") || (e.type == "textarea") || (e.type == "menu")))
     {
        // first check if the field is empty and shouldn't be
        if (!e.isOptional && !checkblank(e))
          return false;

        // Now check for fields that are supposed to be numeric.
        if (!isblank(e) && e.isNumeric && !checknumber(e))
          return false;

        // Now check for fields that are supposed to be dates
        if (!isblank(e) && e.isDate && !checkdate(e))
          return false;

        // Now check for fields that are supposed to be emails
        if (!isblank(e) && e.isEmail && !checkemail(e))
          return false;

        // Now check for fields that are supposed
        // not to have whitespace
        if (!isblank(e) && e.hasNospaces && !checkwhitespace(e))
          return false;

     } // if (type is text or textarea)
  } // for each character in field


//check radio buttons



/* If there are errors inside the error array, then run the function to pop up an alert with the errors listed.
   errors errors errors.... (felt like this comment needed the word errors some more) */
if(errors.length>0){
	display_errors(errors);
	return false;
}


  // There were no errors if we got this far
  return true;
}



function check_form(){
    //Reset the errors Array
    errors.length = 0;

	var form = document.regform;
	
	form.name.isOptional = false;
	form.name.description = 'Name';
	
	form.address.isOptional = false;
	form.address.description = 'Address';
	
	form.city.isOptional = false;
	form.city.description = 'City';
	
	form.state.isOptional = false;
	form.state.description = 'State';
	
	form.zip_code.isOptional = false;
	form.zip_code.description = 'Zip';
	
	form.phone.isOptional = false;
	form.phone.description = 'Primary Phone';
	
	form.alt_phone.isOptional = true;
	
	form.email.isEmail = true;
	form.email.description = 'E-Mail';
	
	if (form.program_list.value == "O") {
		errors.push("The Field \"Program\" must be filled in.");
	}
	
	if (form.class_list.value == "O") {
		errors.push("The Field \"Name/Date of Class\" must be filled in.");
	}
	
	form.program_cost.isOptional = true;
	form.reg_fee.isOptional = true;
	form.total_cost.isOptional = true;
	
	if (form.method_of_payment.value == "O") {
		errors.push("The Field \"Method of Payment\" must be filled in.");
	}
	

	
	
	if (form.method_of_payment.value == "check") {
		form.card_type.isOptional = true;
		form.name_on_card.isOptional = true;
		form.card_number.isOptional = true;
		form.exp_month.isOptional = true;
		form.exp_year.isOptional = true;
		form.cvn.isOptional = true;
		form.cc_address.isOptional = true;
		form.cc_city.isOptional = true;
		form.cc_state.isOptional = true;
		form.cc_zip_code.isOptional = true;
	}
	else if (form.method_of_payment.value == "O" || form.method_of_payment.value == "credit_card") {
		
		if (form.card_type.value == "O") {
			errors.push("The Field \"Card Type\" must be filled in.");
		}
		
		form.name_on_card.isOptional = false;
		form.name_on_card.description = 'Name on Card';
			
		form.card_number.isOptional = false;
		form.card_number.description = 'Card Number';
		
		form.exp_month.isOptional = false;
		form.exp_month.description = 'Expiration Month';
		
		form.exp_year.isOptional = false;
		form.exp_month.description = 'Expiration Year';
		
		form.cvn.isOptional = false;
		form.cvn.description = 'Card Security Code';
		
		form.cc_address.isOptional = false;
		form.cc_address.description = 'CC Address';
		
		form.cc_city.isOptional = false;
		form.cc_city.description = 'CC City';
		
		form.cc_state.isOptional = false;
		form.cc_state.description = 'CC State';
		
		form.cc_zip_code.isOptional = false;
		form.cc_zip_code.description = 'CC Zip';
	}
	
	form.inquiry.isOptional = true;
	
	return verify(form);
}