var errors;
var popup;

function submitForm(){

	function emailCheck(fieldName){
		var emailField = document.getElementById(fieldName);
		var emailMarker = document.getElementById("r_" + fieldName);
		var emailMarker = document.getElementById("r_" + fieldName);
		var ampers=0;
		var dots=0;
		if (emailField.value.length > 4){	
			for (i=0; i<(emailField.value.length); i++){
				if (emailField.value.substring(i,(i+1))=="@") ampers++;
				if (emailField.value.substring(i,(i+1))==".") dots++;
			}			
		}
		if ((emailField.value.length < 5)||(ampers != 1)||(dots == 0)){
			if (emailMarker) emailMarker.style.visibility = 'visible';
			errors++;
			return false;
		}else{
			if (emailMarker) emailMarker.style.visibility = 'hidden';
			return true;
		}
	}
	function dropdownCheck(fieldName){
		var dropdownField = document.getElementById(fieldName);
		var dropdownMarker = document.getElementById("r_" + fieldName);
		if (dropdownField.options[dropdownField.selectedIndex].value == ""){
			if (dropdownMarker) dropdownMarker.style.visibility = 'visible';
			errors++;
			return false;
		}else{
			if (dropdownMarker) dropdownMarker.style.visibility = 'hidden';
			return true;
		}
	}	
	function dropdownChoice(fieldName){
		var dropdownField = document.getElementById(fieldName);
		return dropdownField.options[dropdownField.selectedIndex].value;
	}
	
	errors = 0;
	popup = "Your form entries are incomplete:\n";
	
	var txtFields=new Array("title","first_name","last_name","job_title","organisation","address1","city","country","tel_no");
	for (var i=0; i<txtFields.length; i++){
		if (eval("document.getElementById('Form1')." + txtFields[i] + ".value") == "") {
			eval("document.getElementById('r_" + txtFields[i] + "').style.visibility='visible'");
			errors++;
		}else{
			eval("document.getElementById('r_" + txtFields[i] + "').style.visibility='hidden'");
		}
	}
	// check email field
	if (!emailCheck("delegate_email")){
		popup+="\nPlease enter a valid email address\n";
	}

	// 'delegate_package' field checking
	var radioName="delegate_package";
		var nrValues=2;
		var RadioChecked=0;
		for (i=0; i<nrValues; i++){
			if (eval("window.document.getElementById('Form1')." + radioName + "[" + i + "].checked")){
				RadioChecked++;
			}
		}
		if (RadioChecked==0) {
			document.getElementById("r_delegate_package").style.visibility='visible';
			errors++;
			popup+="\nPlease choose a Delegate Package\n";
		}else{
			document.getElementById("r_delegate_package").style.visibility='hidden';
		}
		
	// check Additional Dinner attendees name fields if necessary
	var additionals_choice = dropdownChoice("additional_dinner_attendees");
	if (additionals_choice > 0){
//		alert(additionals_choice);
		var dinner_errors = 0;
		for (var i=1; i<=additionals_choice; i++){
			if (document.getElementById("additional_dinner_" +i+ "_name").value == ''){
				dinner_errors++;
			}
		}
		if (dinner_errors > 0){
			errors++;
			popup+="\nPlease provide a name for each additional Dinner attendee\n";
		}
	}
	
	if ($("input[name=room_type]:checked").length == 0){
		errors++;
		popup+="\nPlease choose an accommodation option\n";
	}
	
	// check that at least 1 accommodation day has been checked if needed
	if (($("#room_type_single").attr('checked') == true)||
		($("#room_type_double").attr('checked') == true)||
		($("#room_type_shared").attr('checked') == true)){
		var days_checked = $("input[class=accommodation_days]:checked").length;
		if (days_checked == 0){
			errors++;
			popup+="\nPlease select at least 1 overnight accommodation date\n";			
		}
	}
	// check 'How did you hear about the conference' field
	dropdownCheck("how_hear");
			
	if (window.document.getElementById('Form1').tandc.checked!=true){
   		popup+="\nPlease tick to confirm that you have read and\nagree with the Terms and Conditions.\n";
		eval("document.getElementById('r_tandc').style.visibility='visible'");
		errors++;
	}else{
		eval("document.getElementById('r_tandc').style.visibility='hidden'");	
	}
	
	if (errors > 0){
		popup+="\nPlease complete the fields marked\nwith a red * and then re-submit.";
		alert(popup);
	}else{
		calculatePrices();
		var msg = "The payable amount is \u20AC" + document.getElementById("amount").value + ". Continue?";
		if (confirm(msg)){
			document.getElementById('Form1').submit();
		}
	}
}

function submitForm2(){

	function emailCatch(email){	
		if (email.length>4){	
			var ampers=0;
			var dots=0;		
			for (i=0;i<(email.length);i++){
				if (email.substring(i,(i+1))=="@"){
					ampers++;
				}
			}		
			for (i=0;i<(email.length);i++){
				if (email.substring(i,(i+1))=="."){
					dots++;
				}
			}		
		}
		if ((email.length<5)||(ampers!=1)||(dots==0)){
			return true;
		}
	}
	
	errors = 0;
	popup = "Your form entries are incomplete:\n";
	
	var txtFields=new Array("name");
	for (var i=0; i<txtFields.length; i++){
		if (eval("document.getElementById('register')." + txtFields[i] + ".value") == "") {
			errors++;
		}
	}
	// check email field
	if (emailCatch(document.getElementById('register').email.value)){
		errors++;
		popup+="\nPlease enter a valid email address\n";			
	}else{
	}
	
	if (errors > 0){
		popup+="\nPlease complete the fields marked\nwith a red * and then re-submit.";
		alert(popup);
	}else{
		document.getElementById('register').submit();
	}
}


function calculateAmount(){
	
	var amount = 0;
	
	// which delegate package option has been selected
	if (document.getElementById('Form1').delegate_package[0].checked == true){
		amount += 400.00;
	}else if (document.getElementById('Form1').delegate_package[1].checked == true){
		amount += 600.00;
	}
		
	// check if bringing a partner, and if so check the gala dinner option
	if (document.getElementById("conference_dinner").checked == true){
		amount += 55.00;
	}

	// round off amount to pounds and pence
	function formatAsMoney(mnt) {
		mnt -= 0;
		mnt = (Math.round(mnt*100))/100;
		return (mnt == Math.floor(mnt)) ? mnt + '.00' : ( (mnt*10 == Math.floor(mnt*10)) ? mnt + '0' : mnt);
	}

	document.getElementById("amount").value = formatAsMoney(amount);
//	alert(document.getElementById("amount").value);	
	return true;		
}