// JavaScript Document

function CheckExamFields() {
	
	var emailstring = document.ExamForm.Email.value;
	var AtSign = "@"
	var Dot = "."
	var AtSignPOS = emailstring.indexOf(AtSign)
	var emailLength = emailstring.length
	var DotPOS = emailstring.indexOf(Dot)
	
	if (emailstring.indexOf(AtSign)==-1){
	   alert("Please enter a valid email address.")
	   return false
	}
	if (emailstring.indexOf(AtSign)==-1 || emailstring.indexOf(AtSign)==0 || emailstring.indexOf(AtSign)+1==emailLength){
	   alert("Please enter a valid email address.")
	   return false
	}
	if (emailstring.indexOf(Dot)==-1 || emailstring.indexOf(Dot)==0 || emailstring.indexOf(Dot)+1==emailLength){
		alert("Please enter a valid email address.")
		return false
	}
	if (emailstring.indexOf(AtSign,(AtSignPOS+1))!=-1){
		alert("Please enter a valid email address.")
		return false
	}
	if (emailstring.substring(AtSignPOS-1,AtSignPOS)==Dot || emailstring.substring(AtSignPOS+1,AtSignPOS+2)==Dot){
		alert("Please enter a valid email address.")
		return false
	}
	if (emailstring.indexOf(Dot,(AtSignPOS+2))==-1){
		alert("Please enter a valid email address.")
		return false
	}
	if (emailstring.indexOf(" ")!=-1){
		alert("Please enter a valid email address.")
		return false
	}
	
	
	//Check the already insured question
	if(document.ExamForm.CurrentlyInsured[0].checked == false && document.ExamForm.CurrentlyInsured[1].checked == false) {
		alert("Please select if you are already insured by EDIC.");
		return false;
	}
	
	//Check that all the questions are answered
	var TotalQuestions = document.ExamForm.TotalQuestions.value;
	
	for(i=1;i<=TotalQuestions;i++) {	
		TotalAnswers = eval("document.ExamForm.TotalAnswers"+i+".value");
		AnswerSelected = 0;
		for(p=0;p<=TotalAnswers-1;p++) {			
			if(eval("document.ExamForm.Answer"+i+"["+p+"].checked") == true) {
				AnswerSelected = 1;
				break;
			}
		}
		if(AnswerSelected == 0) {
			alert("Please choose an answer for question #"+i+".");
			return false;
		}
	}
		
	//Check to make sure the captcha is correct	
	TheuKey = document.ExamForm.uKey.value;			
	ThehKey = document.ExamForm.hKey.value;
	
	if(TheuKey == '' || ThehKey == '') {
		CaptchaFound = 0;
	}	
	else {	
		
		var strURL = "Ajax/MyFunctions.cfc?Method=FindCaptchaKey&TheuKey=" + TheuKey + "&ThehKey=" + ThehKey;
		
		if(window.XMLHttpRequest) {
			var objHTTP = new XMLHttpRequest();
			objHTTP.open("GET", strURL, false);					
			objHTTP.send(null);			
			CaptchaFound = dpWDDX('deserialize',objHTTP.responseText);				
		}
		else if (window.ActiveXObject) {
			var objHTTP = new ActiveXObject("Microsoft.XMLHTTP");		
			objHTTP.open("GET",strURL,false);				
			objHTTP.send(null);		
			CaptchaFound = dpWDDX('deserialize',objHTTP.responseText);
		}
	}	
	
	if(CaptchaFound == 0) {			
		alert("The verification image key that you entered is incorrect. Please check it and try again.");
		return false;
	}	
	
	
	
	
	
}
