var errors;
var popup;

function submitForm(){
	
	errors = 0;
	popup="\nYour form entries are incomplete:\n";
	
	// check all mandatory regular text fields
	var textFieldNames = new Array("first_name","last_name","job_title","organisation","tel_no");
	for (var i=0; i<textFieldNames.length; i++){
		textCheck(textFieldNames[i]);
	}	
	// check email field
	if (!emailCheck("email")){
		popup+="\nPlease check that all email addresses are valid\n";
	}	
	// check mandatory dropdowns
	 var dropdownNames = new Array("title");
	 for (var i=0; i<dropdownNames.length; i++){
		dropdownCheck(dropdownNames[i]);
	}
	
	// check that a delegate_package has been chosen
	radioSetCheck("attending");

	// check that Terms and Conditions checkbox has been checked
	if (!checkboxCheck("tandc")){
		popup+="\nPlease tick to confirm that you have read and\nagree with the Terms and Conditions\n";
	}
	
	if (errors > 0){
		popup+="\nPlease complete the fields marked\nwith a red * and then re-submit.\n";
		alert(popup);
	}else{
		var msg = "The total payable amount is \u20AC" + calculateFee() + ". Continue?";
		if (confirm(msg)){
			document.forms.Form1.submit();
		}			
	}	
}

function calculateFee(){
	
	// initialise sum value
	var sum = 0;
	
	// chosen attendee options
	if (radioCheckboxDetect("attending_day1_only")){
		sum = 95.00;
	}else if (radioCheckboxDetect("attending_whole_event")){
		sum = 199.00;
	}
	// conference dinner option
//	sum = (sum*1) + (dropdownChoice("attend_dinner")*60.00);
	if (radioCheckboxDetect("attend_dinner")){
		sum = (sum*1) + 50.00;
	}
	
	// guided tour option
	if (radioCheckboxDetect("guided_tour")){
		sum = (sum*1) + 10.00;
	}
	
	sum = formatAsMoney(sum);

	// add to 'amount' field
	document.getElementById("amount").value = sum;

	return sum;	
}

function showHide(element){
	
	/*
	<p style="margin-top: 30px;">
	<label for="whereheard_text">Please provide details:</label>
	<input name="whereheard_text" id="whereheard_text" type="text" size="50" maxlength="100" />
	</p>
	*/

	var whereheard_further = document.getElementById("whereheard_further");

	function createFurtherInfoTextfield(){
		var newP = document.createElement("p");
		newP.setAttribute("style","margin-top:30px");
			var newLabel = document.createElement("label");
			newLabel.setAttribute("for","whereheard_text");
				var newText = document.createTextNode("Please provide details:");
			newLabel.appendChild(newText);	

			var newInput = document.createElement("input");
			newInput.setAttribute("id","whereheard_text");
			newInput.setAttribute("name","whereheard_text");
			newInput.setAttribute("type","text");
			newInput.setAttribute("size","50");
			newInput.setAttribute("maxlength","100");
			
		newP.appendChild(newLabel);
		newP.appendChild(newInput);
		
		whereheard_further.appendChild(newP);
	}
	
	if ((dropdownChoice('whereheard') == 'Mailing list') || (dropdownChoice('whereheard') == 'Other')){
		createFurtherInfoTextfield();
	}else{
		while(whereheard_further.hasChildNodes()){
			whereheard_further.removeChild(whereheard_further.lastChild);
		}
	}	
}
