// Validation Functions

// Check form to ensure all questions have been answered on submit, if so return true
// if not return false  with an alert.
function validateQuiz(form, num) 
{
	var numOfQuestions = num;
	// Loop through questions
	for (var i=0; i<numOfQuestions; i++) 
	{	
		var isChecked = false;
		var option = document.getElementsByName('quiz' + i);
	 
		// If there is an option selected for each question, then return true
		for (var x = 0; x < option.length; x++) 
		{			
			if (option[x].checked) isChecked = true;
		}
		
		// If there is a question with no option selected, alert specifying which question and return false.
		if (!isChecked) 
		{
			alert("Please select an answer for Question " + (i + 1));	
			return false;
		}
	}
		
	return true;
}


function validateQC(form) 
{
	var myindexvalue = form.foundsite.value;
	var myindex = form.foundsite.selectedIndex;
	if (myindexvalue == 0) {
		alert("Please tell us how you heard about us.");
		form.foundsite.focus();
		return false;
	}
	
	var myindexvalue = form.topic.value;
	var myindex = form.topic.selectedIndex;
	if (myindexvalue == 0) {
		alert("Please select a topic.");
		form.topic.focus();
		return false;
	}
	
	if (form.contact_name.value == '') {
		alert("Please fill in your first name.");
		form.contact_name.focus();
		form.contact_name.select();
		return false;
	}
	
	if (!validEmail(form.contact_email.value)) {
		alert("A valid E-mail Address is required.");
		form.contact_email.focus();
		form.contact_email.select();
		return false;
	}
	return true;
}


function validatePQ(form) 
{	
	if (form.contact_name.value == '') {
		alert("Please fill in your first name.");
		form.contact_name.focus();
		form.contact_name.select();
		return false;
	}
	
	if (!validEmail(form.contact_email.value)) {
		alert("A valid E-mail Address is required.");
		form.contact_email.focus();
		form.contact_email.select();
		return false;
	}

	var myindexvalue = form.foundsite.value;
	var myindex = form.foundsite.selectedIndex;
	if (myindex == 0) {
		alert("Please tell us how you heard about us.");
		form.foundsite.focus();
		return false;
	}
	
	var myindexvalue = form.status.value;
	var myindex = form.status.selectedIndex;
	if (myindex == 0) {
		alert("Please select a status.");
		form.status.focus();
		return false;
	}
	
	var myindexvalue = form.contact_marital.value;
	var myindex = form.contact_marital.selectedIndex;
	if (myindex == 0) {
		alert("Please select a marital status.");
		form.contact_marital.focus();
		return false;
	}
		
	var myindexvalue = form.contact_ministry.value;
	var myindex = form.contact_ministry.selectedIndex;
	if (myindex == 0) {
		alert("Please select a ministry interest.");
		form.contact_ministry.focus();
		return false;
	}
	
	var myindexvalue = form.contact_send_services.value;
	var myindex = form.contact_send_services.selectedIndex;
	if (myindexvalue == 0) {
		alert("Please tell us what we can do for you.");
		form.contact_send_services.focus();
		return false;
	}
	
	var myindexvalue = form.contact_searching.value;
	var myindex = form.contact_searching.selectedIndex;
	if (myindex == 0) {
		alert("Please select an option.");
		form.contact_searching.focus();
		return false;
	}
	
	
}

function ValidateSubmit(form) 
{			
	if (form.contactmessage.value == '') {
		alert("Please fill in a comment.");
		form.contactmessage.focus();
		form.contactmessage.select();
		return false;
	}
	
	if (!validEmail(form.contactemail.value)) {
		alert("A valid E-mail Address is required.");
		form.contactemail.focus();
		form.contactemail.select();
		return false;
	}
	
	if (form.contactfrom.value == '') {
		alert("Please fill in your first name.");
		form.contactfrom.focus();
		form.contactfrom.select();
		return false;
	}

	//if we made it to here, everything's valid, so return true
	return true	
}

function validEmail(email) {
	invalidChars = ' !#$%^&*(){}[]+=~`?/:;,"'

	
	if (email == "") {
		return false;
	}
	for (i=0; i<invalidChars.length; i++) { //does it contain any invalid characters?
		badChar = invalidChars.charAt(i);
		if (email.indexOf(badChar,0) > -1) {
			return false;
		}
	}
	atPos = email.indexOf("@",1)  		//there must be one "@" symbol
		if (atPos == -1) {
		return false;
	}
	if (email.indexOf("@",atPos+1) != -1) { //and only one "@"
		return false;
	}
	periodPos = email.indexOf(".",atPos+1)  //and at least one "." after the "@"
		if (periodPos == -1) {
		return false;
	}
	if (email.charAt(atPos+1) == ".") {	//is there a "." right after the "@"
		return false;
	}
	if (periodPos+3 > email.length) {  	//must be at least 2 characters after the "."
		return false;
	}
	return true;
}

