// validation for confirmation form
function validateForm(form) {
	var alertMsg = "";

	// paying by credit card so need other fields
	if (form.paymentMethod.selectedIndex == 0) {
		if (form.cardType.selectedIndex == 0) {alertMsg += " - Card Type\n"; }
		if (!/\S/.test(form.cardNumber.value)) { alertMsg += " - Card Number\n"; }
		if (!/\S/.test(form.expiryMonth.value)) { alertMsg += " - Expiry Month\n"; }
		if (!/\S/.test(form.expiryYear.value)) { alertMsg += " - Expiry Year\n"; }
		if (!/\S/.test(form.cardName.value)) { alertMsg += " - Cardholder's Name\n"; }
		if (!/\S/.test(form.custName.value)) { alertMsg += " - Customer's Name\n"; }
		if (!/\S/.test(form.custEmail.value)) { alertMsg += " - Email Address\n"; }
		if (!/\S/.test(form.custAddress.value)) { alertMsg += " - Customer's Address\n"; }
		if (!/\S/.test(form.postcode.value)) { alertMsg += " - Postcode\n"; }
	}
	else {
		if (!/\S/.test(form.custName.value)) { alertMsg += " - Customer's Name\n"; }
		if (!/\S/.test(form.custEmail.value)) { alertMsg += ' - Email address\n'; }
	}

	if (alertMsg != "") {
		alert('ERROR on submission\n\nSome required fields are missing:\n\n' + alertMsg);
		return false;
	}
	return true;
}

$(document).ready(function() {

	// validation for checkout form
	$("#frmPubsCheckout").submit(function (event) {
		var frm = event.target;
		var msg = "";
		var levypayer = "";
		var declaration = "";
		var deliveryMethod = "";

		var $levypayer = $("#frmPubsCheckout input[name=levypayer]:checked");
		if ($levypayer.length == 0)
			msg += "# Please choose Levy Payer or Student/Researcher\n";
		else
			levypayer = $levypayer.val();

		if (frm["deliveryMethod"].selectedIndex <= 0)
			msg += "# Please choose a Delivery Method.\n";
		else
			deliveryMethod = frm["deliveryMethod"].options[frm["deliveryMethod"].selectedIndex].value;

		if (frm["copyrightDeclaration"].selectedIndex <= 0)
			msg += "# Please complete the Copyright Declaration question.\n";
		else
			declaration = frm["copyrightDeclaration"].options[frm["copyrightDeclaration"].selectedIndex].value;

		// researchers and students are required to make a copyright declaration;
		// levy payers don't unless they are receiving PDF via email
		if (levypayer == "researcher-student" && declaration == "no")
			msg += "# Students and Researchers must make a copyright declaration.\n";
		else if (levypayer != "researcher-student" && deliveryMethod == "email" && declaration == "no")
			msg += "# A copyright declaration must be make when receiving PDF copies via email.\n";

		if (msg != "") {
			event.preventDefault();
			alert("ERROR on submission\n\n" + msg);
		}
	});
});

