// JavaScript Document

var registration_type = "Please Select";
var total_cost = 0.0;

function submitBox()
{
	input_box = confirm("Are you sure you want to submit this form?");
		
	document.reg.subject.value = "Emulsions and Foams: " + document.reg.Firstname.value + " " + document.reg.Surname.value
	
	return input_box;
}

function resetBox()
{
	input_box = confirm("Are you sure you want to reset all the fields in this form?");
	
	return input_box;
}

function validateForm()
{
	var required_fields = document.reg.required.value.split(',');
	var box_shown = false;
	var all_filled = true;
	
	for(var i = 0; i<required_fields.length; i++)
	{
		var current_required = required_fields[i];
		
		with(document.reg)
		{			
			switch(current_required)
			{
				case "Title":
					if(Title.value == "" && !box_shown)
					{
						alert("You have not filled in your title");
						box_shown = true;
						all_filled = false;
					}
				break
				case "Firstname":
					if(Firstname.value == "" && !box_shown)
					{
						alert("You have not filled in your first name");
						box_shown = true;
						all_filled = false;
					}
				break
				case "Surname":
					if(Surname.value == "" && !box_shown)
					{
						alert("You have not filled in your surname");
						box_shown = true;
						all_filled = false;
					}
				break
				case "Department":
					if(Department.value == "" && !box_shown)
					{
						alert("You have not filled in your department");
						box_shown = true;
						all_filled = false;
					}
				break
				case "Organisation":
					if(Organisation.value == "" && !box_shown)
					{
						alert("You have not filled in your organisation");
						box_shown = true;
						all_filled = false;
					}
				break
				case "DelegateAddress":
					if(DelegateAddress.value == "" && !box_shown)
					{
						alert("You have not filled in your address");
						box_shown = true;
						all_filled = false;
					}
				break
				case "Postcode":
					if(Postcode.value == "" && !box_shown)
					{
						alert("You have not filled in your postcode");
						box_shown = true;
						all_filled = false;
					}
				break
				case "Country":
					if(Country.value == "" && !box_shown)
					{
						alert("You have not filled in your country");
						box_shown = true;
						all_filled = false;
					}
				break
				case "Telephone":
					if(Telephone.value == "" && !box_shown)
					{
						alert("You have not filled in your telephone number");
						box_shown = true;
						all_filled = false;
					}
				break
				case "email":
					if(email.value == "" && !box_shown)
					{
						alert("You have not filled in your email address");
						box_shown = true;
						all_filled = false;
					}
				break
				case "registration":
					if(registration[0].selected == true && !box_shown)
					{
						alert("You have not selected a registration fee option (section c)");
						box_shown = true;
						all_filled = false;
					}
				break
				case "payment_method":
					if(PaymentMethod[0].checked == false &&
						PaymentMethod[1].checked == false &&
						PaymentMethod[2].checked == false &&
						PaymentMethod[3].checked == false &&
						!box_shown)
					{
						alert("You have not selected a payment option");
						box_shown = true;
						all_filled = false;
					}
				break
				case "credit_card":
					if(creditCardDetailsNeeded() && !box_shown)
					{
						if(notAllCreditCardDetailsFilled() && !PaymentMethod[0].checked && !box_shown)
						{
							alert("You have not filled in all your credit card details");
							box_shown = true;
							all_filled = false;
						}
					}
				break
			}
		}
	}
	
	return all_filled;
}

function creditCardDetailsNeeded()
{
	with(document.reg)
	{
		if(PaymentMethod[1].checked == false ||
			PaymentMethod[2].checked == false ||
			PaymentMethod[3].checked == false)
		{
			return true;
		}
		else
		{
			return false;
		}
	}	
}

function notAllCreditCardDetailsFilled()
{
	with(document.reg)
	{
		if(cardNumber.value == "" || CreditCardExpiry.value == "" || CreditCardHolderNameAndAddress.value == "")
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}

function calculateCost()
{
	var registration_type_index
	
	registration_type_index = document.reg.registration.selectedIndex
	registration_type = document.reg.registration[registration_type_index].value
	
	switch(registration_type)
	{
		case "Please Select":
			total_cost = 0.0;
		break
		case "RSC/SCI members £270":
			total_cost = 270.0;
		break
		case "Non-members £330":
			total_cost = 330.0;
		break
		case "RSC/SCI student £190":
			total_cost = 190.0;
		break
		case "Non-member student or retired members £210":
			total_cost = 210.0;
		break		
	}
	
	if(document.reg.eleventh.checked)
	{
		total_cost += 73.95;
	}
	
	if(document.reg.twelfth.checked)
	{
		total_cost += 73.95;
	}
	
	if(document.reg.thirteenth.checked)
	{
		total_cost += 73.95;
	}
	
	total_cost = total_cost.toFixed(2);
	
	document.reg.GrandTotalCost.value = "£"+total_cost;
}