﻿var mandatory = new Array('name', 'university', 'position', 'email');
var errorMsg1 = "Παρακαλώ συμπληρώστε όλα τα υποχρεωτικά προσωπικά στοιχεία";
var errorMsg2 = "Η μορφή του e-mail δεν είναι έγκυρη";

function checkAndSubmit(){


	//Check if the user has given all the mandatory personal data
	if(!(notEmpty(document.forms[1].university) && notEmpty(document.forms[1].position) && notEmpty(document.forms[1].speciality) && notEmpty(document.forms[1].email)))
	{
		alert(errorMsg1);
		return;
	}   
        else
	{
		if( emailValidator(document.forms[1].email, "Η μορφή του e-mail δεν είναι έγκυρη") == false)
		{
			return;			
		}
	}


	if( radioGroupValidation( document.forms[1].oa_knowledge, "Παρακαλώ απαντήστε στην ερώτηση 1" ) == false )
	{
		return;
	}

        if( radioGroupValidation( document.forms[1].oa_structures_knowledge, "Παρακαλώ απαντήστε στην ερώτηση 3" ) == false )
	{
		return;
	}

        if( radioGroupValidation( document.forms[1].oa_univ_structure1, "Παρακαλώ απαντήστε στην ερώτηση 4" ) == false )
	{
		return;
	}

	if( radioGroupValidation( document.forms[1].oa_univ_repos_interest, "Παρακαλώ απαντήστε στην ερώτηση 6" ) == false )
	{
		return;
	}

        if( radioGroupValidation( document.forms[1].oa_repos_usage, "Παρακαλώ απαντήστε στην ερώτηση 7" ) == false )
	{
		return;
	}

	if( radioGroupValidation( document.forms[1].publish_mat_in_oarepos, "Παρακαλώ απαντήστε στην ερώτηση 10" ) == false )
	{
		return;
	}

        
        if( !( (document.forms[1].reason_to_not_submit_in_repos1.checked==true) || (document.forms[1].reason_to_not_submit_in_repos2.checked==true) || (document.forms[1].reason_to_not_submit_in_repos3.checked==true) || (document.forms[1].reason_to_not_submit_in_repos4.checked==true) || (document.forms[1].reason_to_not_submit_in_repos5_checked.checked==true) || notEmpty(document.forms[1].reason_to_not_submit_in_repos5_txt) ) )
	{
		alert("Παρακαλώ απαντήστε στην ερώτηση 12");
		return;

	}
        
        if( !( (document.forms[1].repos_criteria1.checked==true) || (document.forms[1].repos_criteria2.checked==true) || (document.forms[1].repos_criteria3.checked==true) || (document.forms[1].repos_criteria4.checked==true) || (document.forms[1].repos_criteria5.checked==true) || (document.forms[1].repos_criteria6.checked==true) || notEmpty(document.forms[1].repos_criteria6_txt)) )
	{
		alert("Παρακαλώ απαντήστε στην ερώτηση 13");
		return;

	}


	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;
	}

}


