var errorMsg1 = "Please fill in all the mandatory fields";

var mandatory = new Array('name', 'university', 'position', 'email');

function checkAndSubmit(){

	if(!(notEmpty(document.forms[1].firstname) && notEmpty(document.forms[1].lastname) && notEmpty(document.forms[1].email) && notEmpty(document.forms[1].telephone))){
		alert(errorMsg1);
		return;
	}
	else
	{
			if( radioGroupValidation( document.forms[1].gender, "Please fill in all the mandatory fields" ) == false )
			{
				return;
			}

			if ( document.forms[1].country.value == '')
			{
				alert(errorMsg1);
				return;
			}

			if( emailValidator(document.forms[1].email, "Please enter a valid email address.") == false)
			{
				return;			
			}
	}

	document.forms[1].a11_comment.value = (document.forms[1].a11_comment.value).replace(/'/g,"");
	document.forms[1].a12_comment.value = (document.forms[1].a12_comment.value).replace(/'/g,"");
	document.forms[1].a13_comment.value = (document.forms[1].a13_comment.value).replace(/'/g,"");
	document.forms[1].a14_comment.value = (document.forms[1].a14_comment.value).replace(/'/g,"");
	document.forms[1].a15_comment.value = (document.forms[1].a15_comment.value).replace(/'/g,"");
	
	document.forms[1].a21_comment.value = (document.forms[1].a21_comment.value).replace(/'/g,"");
	document.forms[1].a22_comment.value = (document.forms[1].a22_comment.value).replace(/'/g,"");
	document.forms[1].a23_comment.value = (document.forms[1].a23_comment.value).replace(/'/g,"");

	document.forms[1].a31_comment.value = (document.forms[1].a31_comment.value).replace(/'/g,"");
	document.forms[1].a32_comment.value = (document.forms[1].a32_comment.value).replace(/'/g,"");
	document.forms[1].a33_comment.value = (document.forms[1].a33_comment.value).replace(/'/g,"");
	document.forms[1].a34_comment.value = (document.forms[1].a34_comment.value).replace(/'/g,"");

	document.forms[1].a41_comment.value = (document.forms[1].a41_comment.value).replace(/'/g,"");
	document.forms[1].a42_comment.value = (document.forms[1].a42_comment.value).replace(/'/g,"");
	
  	document.forms[1].a51_comment.value = (document.forms[1].a51_comment.value).replace(/'/g,"");
	document.forms[1].a52_comment.value = (document.forms[1].a52_comment.value).replace(/'/g,"");
	document.forms[1].a53_comment.value = (document.forms[1].a53_comment.value).replace(/'/g,"");
	document.forms[1].a54_comment.value = (document.forms[1].a54_comment.value).replace(/'/g,"");
	document.forms[1].a55_comment.value = (document.forms[1].a55_comment.value).replace(/'/g,"");
	document.forms[1].a56_comment.value = (document.forms[1].a56_comment.value).replace(/'/g,"");
	document.forms[1].a57_comment.value = (document.forms[1].a57_comment.value).replace(/'/g,"");
	document.forms[1].a58_comment.value = (document.forms[1].a58_comment.value).replace(/'/g,"");

	document.forms[1].a61_comment.value = (document.forms[1].a61_comment.value).replace(/'/g,"");
	document.forms[1].a62_comment.value = (document.forms[1].a62_comment.value).replace(/'/g,"");

	document.forms[1].a71_comment.value = (document.forms[1].a71_comment.value).replace(/'/g,"");
	document.forms[1].a72_comment.value = (document.forms[1].a72_comment.value).replace(/'/g,"");
	document.forms[1].a73_comment.value = (document.forms[1].a73_comment.value).replace(/'/g,"");

	document.forms[1].b11_comment.value = (document.forms[1].b11_comment.value).replace(/'/g,"");
	document.forms[1].b12_comment.value = (document.forms[1].b12_comment.value).replace(/'/g,"");
	document.forms[1].b13_comment.value = (document.forms[1].b13_comment.value).replace(/'/g,"");
	document.forms[1].b14_comment.value = (document.forms[1].b14_comment.value).replace(/'/g,"");


	document.forms[1].b21_comment.value = (document.forms[1].b21_comment.value).replace(/'/g,"");
	document.forms[1].b22_comment.value = (document.forms[1].b22_comment.value).replace(/'/g,"");
	document.forms[1].b23_comment.value = (document.forms[1].b23_comment.value).replace(/'/g,"");
	document.forms[1].b24_comment.value = (document.forms[1].b24_comment.value).replace(/'/g,"");


	document.forms[1].b31_comment.value = (document.forms[1].b31_comment.value).replace(/'/g,"");
	document.forms[1].b32_comment.value = (document.forms[1].b32_comment.value).replace(/'/g,"");
	document.forms[1].b33_comment.value = (document.forms[1].b33_comment.value).replace(/'/g,"");

	document.forms[1].b41_comment.value = (document.forms[1].b41_comment.value).replace(/'/g,"");
	document.forms[1].b42_comment.value = (document.forms[1].b42_comment.value).replace(/'/g,"");
	document.forms[1].b43_comment.value = (document.forms[1].b43_comment.value).replace(/'/g,"");

	/*Submit the form*/
	document.forms[1].submit();


}

/* Check if the input is empty or not. If yes return false. If no return true */
function notEmpty(elem){
	if(elem.value.length == 0){
			return false;
	}
	return true;
}

/*************************************************/

/* Check if at least one radio button is selected*/
function radioGroupValidation( group, msg ){

	// require at least one radio button be selected
	var radioSelected = false;
	for (i = 0;  i < group.length;  i++)
	{
		if (group[i].checked)
		radioSelected = true;
	}
	if (!radioSelected)
	{
		alert(msg);

		return (false);
	}

}



/*************************************************/

/* Check if the user's email address is valid */
function emailValidator(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert(helperMsg);
		return false;
	}

}

