// JavaScript Document

function submitForm(form){
	
	var theform = form;
	
	var error_msg = document.getElementById("error_msg"); //if not we should creat one..
	
	var valid = true;
	
	//check for empty values
	var inputs = document.getElementsByTagName("input");
	
	var checks = 0; //var used to count num of radio btns checked
	
	for(var i=0; i < inputs.length; i++){
	
		if(inputs[i].className=='reqd' && inputs[i].value == ''){
			inputs[i].style.background = "rgb(255,255,180)";
			valid = false;
		}
		
		if(inputs[i].type=='radio'){
			if(inputs[i].checked){
				checks++;
			}
		}
	
	}

	//if valid is false at this point return with blank fields error message
	//prepare error message and display
	if(!valid){
		var msg = "Please fill all required fields";
		error_msg.innerHTML = msg;
		return false;
	}
	
	//before moving on to email validation make sure all radio buttons are checked.
	if(checks < 8){
		var msg = "Please answer all questions before submitting.";
		error_msg.innerHTML = msg;
		return false;
	}
 
	//move on to email validation *only run this if the email is not empty
	if(theform["email"].value != ""){
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(reg.test(theform["email"].value) == false){ 
			 valid = false; 
			 theform["email"].style.backgroundColor = "rgb(255,255,180)"; 
			 var msg = "Please enter a valid email";
			 error_msg.innerHTML = msg;
		}
	}
	//finally if still valid send ajax request and submit form data
	if(valid){
		
		return true;
	}

 return false;
}

