

function changeRedirect(theAnswer) {
document.getElementById("Answer-Total").value = theAnswer;
	document.getElementById("redirect").value = "thankYou.php?theAnswer="+theAnswer;
}

function checkEstimates() {

	for(var i=0;i<document.forms.length;i++) {
		var thisForm = document.forms[i];
		
		thisForm.onsubmit = function() {
			if(!checkSurvey(this)) {
				alert("All questions, name & email are required.");
				return false;
			}
			else {changeRedirect(checkSurvey(this));return true}
		}
		}
	
}

function checkSurvey(theForm) {
	var theAnswer=0
	surveyOption = -1;
	errorTxt=0;
for (i=0;i<theForm.ques1.length; i++){

if (theForm.ques1[i].checked){ surveyOption = 1; theAnswer+=(theForm.ques1[i].value)*1; break; }else { surveyOption = -1; }

}
if (surveyOption == -1){errorTxt=1}

for (i=0;i<theForm.ques2.length; i++){

if (theForm.ques2[i].checked){ surveyOption = 1; theAnswer+=(theForm.ques2[i].value)*1; break; }else { surveyOption = -1; }

}
if (surveyOption == -1){errorTxt+=1}

for (i=0;i<theForm.ques3.length; i++){

if (theForm.ques3[i].checked){ surveyOption = 1;  theAnswer+=(theForm.ques3[i].value)*1;break; }else { surveyOption = -1; }

}
if (surveyOption == -1){errorTxt+=1}


for (i=0;i<theForm.ques4.length; i++){

if (theForm.ques4[i].checked){ surveyOption = 1; theAnswer+=(theForm.ques4[i].value)*1; break; }else { surveyOption = -1; }

}if (surveyOption == -1){errorTxt+=1}


for (i=0;i<theForm.ques5.length; i++){

if (theForm.ques5[i].checked){ surveyOption = 1; theAnswer+=(theForm.ques5[i].value)*1; break; }else { surveyOption = -1; }

}if (surveyOption == -1){errorTxt+=1}


for (i=0;i<theForm.ques6.length; i++){

if (theForm.ques6[i].checked){ surveyOption = 1; theAnswer+=(theForm.ques6[i].value)*1; break; }else { surveyOption = -1; }

}if (surveyOption == -1){errorTxt+=1}


for (i=0;i<theForm.ques7.length; i++){

if (theForm.ques7[i].checked){ surveyOption = 1; theAnswer+=(theForm.ques7[i].value)*1; break; }else { surveyOption = -1; }

}if (surveyOption == -1){errorTxt+=1}


for (i=0;i<theForm.ques8.length; i++){

if (theForm.ques8[i].checked){ surveyOption = 1; theAnswer+=(theForm.ques8[i].value)*1; break; }else { surveyOption = -1; }

} if (surveyOption == -1){errorTxt+=1}



for (i=0;i<theForm.ques9.length; i++){

if (theForm.ques9[i].checked){ surveyOption = 1; theAnswer+=(theForm.ques9[i].value)*1; break; }else { surveyOption = -1; }

}if (surveyOption == -1){errorTxt+=1}


if (theForm.name.value==""){ surveyOption = -1;errorTxt+=1; }

emailAlert = checkEmail(theForm.email.value);
if(emailAlert){
errorTxt+=1
}

	
	if (errorTxt > 0){
		return false;
		
	}
	
	return theAnswer;
	
}
function checkEmail (strng) {
var error="";
if (strng == "") {
   error = "\nYou didn't enter an email address.\n";
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "\nPlease enter a valid email address.\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "\nThe email address contains illegal characters.\n";
       }
    }
return error;    
}

window.onload = function() {
    checkEstimates();
}

