// JavaScript Document

function checkCustomerSurvey(){
	
	var formObj = document.getElementById("customerSurveyForm");
	var submitForm = true;
	var errorMessage = "Please correct the following errors:\r\n";
	var firstError = "";
	
	//check the name details
	if(formObj.customerName.value==""){
		formObj.customerName.className="missingInfo";
		submitForm = false;
		errorMessage += "Missing Customer Name\r\n";
		if(firstError==""){
			firstError = "#yourDetails";
		}
	}else{
		formObj.customerName.className="";
	}
	
	//check email address
	if(formObj.emailAddress.value=="" || checkEmailAddress(formObj.emailAddress.value)==false){
		formObj.emailAddress.className="missingInfo";
		submitForm = false;
		errorMessage += "Missing Email Address\r\n";
		if(firstError==""){
			firstError = "#yourDetails";
		}
	}else{
		formObj.emailAddress.className="";
	}
	
	// now we need to check the questions
	if(getCheckedValue(formObj.question1) == ""){
		errorMessage += "Question 1 is unanswered\r\n";
		document.getElementById("question1Border").className="missingInfo";
		submitForm = false;
		if(firstError==""){
			firstError = "#question1";
		}
	}else{
		document.getElementById("question1Border").className="";
	}
	
	// now we need to check the questions
	if(getCheckedValue(formObj.question2) == ""){
		errorMessage += "Question 2 is unanswered\r\n";
		document.getElementById("question2Border").className="missingInfo";
		submitForm = false;
		if(firstError==""){
			firstError = "#question2";
		}
	}else{
		document.getElementById("question2Border").className="";
	}
	
	// now we need to check the questions
	if(getCheckedValue(formObj.question3) == ""){
		errorMessage += "Question 3 is unanswered\r\n";
		document.getElementById("question3Border").className="missingInfo";
		submitForm = false;
		if(firstError==""){
			firstError = "#question3";
		}
	}else{
		document.getElementById("question3Border").className="";
	}
	
		// now we need to check the questions
	if(getCheckedValue(formObj.question4) == ""){
		errorMessage += "Question 4 is unanswered\r\n";
		document.getElementById("question4Border").className="missingInfo";
		submitForm = false;
		if(firstError==""){
			firstError = "#question4";
		}
	}else{
		document.getElementById("question4Border").className="";
	}
	
		// now we need to check the questions
	if(getCheckedValue(formObj.question5) == ""){
		errorMessage += "Question 5 is unanswered\r\n";
		document.getElementById("question5Border").className="missingInfo";
		submitForm = false;
		if(firstError==""){
			firstError = "#question5";
		}
	}else{
		document.getElementById("question5Border").className="";
	}
	
		// now we need to check the questions
	if(getCheckedValue(formObj.question6) == ""){
		errorMessage += "Question 6 is unanswered\r\n";
		document.getElementById("question6Border").className="missingInfo";
		submitForm = false;
		if(firstError==""){
			firstError = "#question6";
		}
	}else{
		document.getElementById("question6Border").className="";
	}
	
		// now we need to check the questions
	if(getCheckedValue(formObj.question7) == ""){
		errorMessage += "Question 7 is unanswered\r\n";
		document.getElementById("question7Border").className="missingInfo";
		submitForm = false;
		if(firstError==""){
			firstError = "#question7";
		}
	}else{
		document.getElementById("question7Border").className="";
	}
	
		// now we need to check the questions
	if(getCheckedValue(formObj.question8) == ""){
		errorMessage += "Question 8 is unanswered\r\n";
		document.getElementById("question8Border").className="missingInfo";
		submitForm = false;
		if(firstError==""){
			firstError = "#question8";
		}
	}else{
		document.getElementById("question8Border").className="";
	}
	
		// now we need to check the questions
	if(getCheckedValue(formObj.question9) == ""){
		errorMessage += "Question 9 is unanswered\r\n";
		document.getElementById("question9Border").className="missingInfo";
		submitForm = false;
		if(firstError==""){
			firstError = "#question9";
		}
	}else{
		document.getElementById("question9Border").className="";
	}
	
		// now we need to check the questions
	if(getCheckedValue(formObj.question10) == ""){
		errorMessage += "Question 10 is unanswered\r\n";
		document.getElementById("question10Border").className="missingInfo";
		submitForm = false;
		if(firstError==""){
			firstError = "#question10";
		}
	}else{
		document.getElementById("question10Border").className="";
	}
	
	if(submitForm){
		formObj.submit();
	}else{
		alert(errorMessage);
		//alert("FirstError: " + firstError);
		window.location.href = firstError;
	}
		
}

function checkEmailAddress(emailAddress)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(emailAddress)){
		return true;
	}
	else{
		return false;
	}

}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}