// Script elements for the on-line enrolment form.

// This sets the 'amount' according to course and concessions
// Which will be replaced by a small table when I get to do it
function setFee() {
	course=selectedRadio(document.enrolment_form.course_id);
	concession=selectedRadio(document.enrolment_form.concession);
	if (course=="P1"||course=="E1") {
	 if (concession=="Full") { document.enrolment_form.amount.value="135.00"; }
	 else if (concession=="Student") { document.enrolment_form.amount.value="45.00"; } 
	}
	else if (course=="P1;E1") { 
	 if (concession=="Full") { document.enrolment_form.amount.value="202.50"; }
	 else if (concession=="Student") { document.enrolment_form.amount.value="90.00"; } 
	}
}


// This performs primary validation of the enrolment form when the submit button is pressed
function validate(that) {
	validation = "false"
	error = ""
	// Check that terms & conditions are agreed to
	if (!that.terms_conditions.checked) { error = "In order to proceed please indicate that you\nagree to our terms and conditions." }

	// Check that a course has been selected
	else {
		if (!selectedRadio(that.course_id)) { error = "Please select which course(s) you would like to attend." }

	// Check personal details are all present and email is likely valid
		else {
			re = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/;
			missingfield = ""
			if (!that.title.value) { missingfield = "Title\n" }
			if (!that.first_name.value) { missingfield += "First name\n" }
			if (!that.last_name.value) { missingfield += "Last name\n" }
			if (!that.address_1.value) { missingfield += "First line of your address\n" }
			if (!that.town.value) { missingfield += "Nearest town or city\n" }
			if (!that.postcode.value) { missingfield += "Postcode\n" }
			if (!re.test(that.email_1.value)) { missingfield += "Contact email address\n" }
			if (missingfield) { error = "The following required information is missing or invalid:\n\n" + missingfield +"\n" }
		}	
	}

	// Check that heard about has been selected if everything else is OK
	if (!error) {
		heard=selectedRadio(that.heard_about);
		if (!heard) { error = "Please let us know how you heard about the course.\n" }
	}

	// Report error state 
	if (error) {
		alert(error)
		//heardAbout(that.heard_about_us)
		setFee()
		return false 
	}
	// Submit the form
	else {
		//heardAbout(that.heard_about_us)
		setFee()
		return true
	}
}


// Radio buttons, what a pain in the butt. This returns the value of the selected button.
function selectedRadio(thisRadio) {
	radio="";
	for (i = 0; i <thisRadio.length; i++) {
		if (thisRadio[i].checked) {
			radio = thisRadio[i].value
		}
	}
	return(radio);	
}

