
function validate (myForm) {
	var msg, show;
	
	msg = "<p style=\"font-family: Arial, Helvetica, sans-serif;font-size: 75%;font-weight: bold;\">The following required fields have not been completed:<br /><br />\n";
	show = false;
	
	// Test that each mandatory field has been filled in
	
	if (myForm.FullName.value == "") {
		msg = msg + "Personal Details<br /><br />Full Name<br />\n";
		show = true;
	}
	
	if (myForm.Postal_Address.value == "") {
		msg = msg + "Postal Address<br />\n";
		show = true;
	}

	if (myForm.Daytime_Telephone.value == "") {
		msg = msg + "Daytime Telephone Number<br />\n";
		show = true;
	}

   	// Calls emailcheck function to ensure a valid email address has been entered
	
	if (emailcheck(myForm.email.value) == false) {
		msg = msg + "Email Address<br /><br />\n";
		show = true;
	}
	
   	if (myForm.DeclinedPolicy[0].checked == false && myForm.DeclinedPolicy[1].checked == false) {
		msg = msg + "Membership Details<br /><br />Has any insurer or underwriter declined to issue or renew a policy or imposed special terms?<br /><br />\n";
		show = true;
	}
		
	if (myForm.Turnover.value == "") {
		msg = msg + "Cover Requirements<br /><br />Turnover?<br />\n";
		show = true;
	}
	
	if (myForm.NGB[0].checked == false && myForm.NGB[1].checked == false) {
		msg = msg + "Do all instructors and leaders hold the correct National Governing Bodies (NGB) qualification?<br />\n";
		show = true;
	}
			
	if (myForm.WorkSolelyWithChildren[0].checked == false && myForm.WorkSolelyWithChildren[1].checked == false) {
		msg = msg + "Do you work solely with children?<br />\n";
		show = true;
	}
	
	if (myForm.WorkOverseas[0].checked == false && myForm.WorkOverseas[1].checked == false) {
		msg = msg + "Do you undertake overseas work?<br />\n";
		show = true;
	}
	
	if (myForm.PreviousLosses.value == "") {
		msg = msg + "Previous losses (including incidents that may give rise to a claim) during the last five years?<br /><br />\n";
		show = true;
	}

    if (myForm.CriminalOffence[0].checked == false && myForm.CriminalOffence[1].checked == false) {
		msg = msg + "Insurance History<br /><br />a) convicted of or charged (but not yet tried) with a criminal offence other than motoring?<br />\n";
		show = true;
	}
	
	if (myForm.Bankrupt[0].checked == false && myForm.Bankrupt[1].checked == false) {
		msg = msg + "b) declared bankrupt or the subject of bankruptcy proceedings?<br />\n";
		show = true;
	}
	
	if (myForm.Insolvent[0].checked == false && myForm.Insolvent[1].checked == false) {
		msg = msg + "c) a director or partner in any business 6 months prior to or at the time of and/or after the appointment or a Receiver or Liquidator or dissolution through insolvency?<br />\n";
		show = true;
	}
		
	msg = msg + '</p>';
	
	/* 	if a field has been left blank then display a message to the user otherwise
		submit the form to formmail
	*/
	
	if (show) {
		msgbox(msg);
		return false;
	}
	else {
		return true;
	}
}


function msgbox(msg) {
	// Calculate screen size so the message box is centered
	var ah = (screen.availHeight - 30 - 300) / 2;
    var aw = (screen.availWidth - 10 - 350) / 2;
	
	msgwindow=window.open("","","width=500,height=550,left=" + aw + ",top=" + ah);
	msgwindow.document.write('<title>Missing Details</title>');
	msgwindow.document.write(msg);
	msgwindow.document.write('<br><center><form><input type="button" value="Return to Form" onClick="window.close()"></form></center>');
	msgwindow.document.close();
	msgwindow.document.bgColor="white";
	msgwindow.document.body.font="Arial";
	return true;
}


function emailcheck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		
		if ((str==null)||(str=="")){
			return false
		}
	
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		 
		 if (str.indexOf(dot)+1==lstr){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}