var errors;
var popup;

function submitForm(){

	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;
		}
	}
	function radioCheck(fieldName,nrValues){
		var RadioChecked = 0;
		for (i=0; i<nrValues; i++){
			if (eval("document.Form1." + fieldName + "[" + i + "].checked")){
				RadioChecked++;
			}
		}
		if (RadioChecked == 0) {
			document.getElementById("r_" + fieldName).style.visibility = 'visible';
			errors++;
		}else{
			document.getElementById("r_" + fieldName).style.visibility = 'hidden';
		}
	}
	
	errors = 0;
	popup = "Your form entries are incomplete:\n";
	
	var txtFields=new Array("title","first_name","last_name","job_title","organisation","address1","city","postcode","tel_no","fax_no");
	for (var i=0; i<txtFields.length; i++){
		if (eval("document.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 (emailCatch(document.getElementById('email').value)){
		eval("document.getElementById('r_email').style.visibility='visible'");
		errors++;
		popup+="\nPlease enter a valid email address\n";			
	}else{
		eval("document.getElementById('r_email').style.visibility='hidden'");
	}

	// check radio buttons
	radioCheck("delegate_package",2);
	radioCheck("tuesday_stream1",2);
	radioCheck("tuesday_stream2",2);
	radioCheck("tuesday_stream3",2);
	radioCheck("wednesday_stream1",2);
	radioCheck("wednesday_stream2",2);
	radioCheck("wednesday_stream3",2);
		
	// CHECK DROPDOWNS	
	var dropdownFields = new Array("how_hear");
	for (var i=0; i<dropdownFields.length; i++){
		if (eval("document.Form1." + dropdownFields[i] + ".options[document.Form1." + dropdownFields[i] + ".selectedIndex].value") == "") {
			eval("document.getElementById('r_" + dropdownFields[i] + "').style.visibility='visible'");
			errors++;
		}else{
			eval("document.getElementById('r_" + dropdownFields[i] + "').style.visibility='hidden'");
		}
	}
		
	if (document.forms.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'");	
	}
	
	var sum = 0;
	function calculateFee(){
		// has the Pre-Conference Workshop option been selected?
		if (radioCheckboxDetect("pre_conference_workshop")){
			sum = (document.getElementById("amount").value*1) + 300;
		}else{
			sum = (document.getElementById("amount").value*1);
		}
		
		return "\u20AC" + formatAsMoney(sum);
	}	
	
	if (errors > 0){
		popup+="\nPlease complete the fields marked\nwith a red * and then re-submit.\n";
		alert(popup);
	}else{
		var payable_amount = calculateFee();
		var msg = "The payable amount is " + payable_amount + ". Continue?";
		if (confirm(msg)){
			document.getElementById("amount").value = sum;
			document.forms.Form1.submit();
		}
	}
}