var errorMessage = "";
var valid = true;

function checkField(id) {
	var element = document.getElementById(id);
	if ((element.value != "") && (element.value != " ")) {
		return true;
	}
	else {
		errorMessage = errorMessage + "Please fill in the following field: " + id + ". \n" ;
		valid = false;
		return false;
	}
}

function checkEmail(id) {
	var email = document.getElementById(id).value;
	var length = email.length;
	var result = true;
	var at_first_position = email.indexOf("@") + 1;
	var at_last_position = email.lastIndexOf("@") + 1;
	var dot_first_position = email.indexOf(".") + 1;
	var dot_last_position = email.lastIndexOf(".") + 1;

	if ((at_first_position == at_last_position)					/* Only one "@" */
			&& (at_first_position != 1)							/* "@" is not the first character */
			&& (dot_first_position != 1)						/* "." is not the first character  */
			&& (dot_last_position < length)						/* "." is not the last character  */
			&& ((at_first_position - dot_first_position) != 1)  /* If there is a "." before the "@", there is an other character between both*/
			&& ((at_first_position + 1) < dot_last_position)    /* There is a "." after the "@" and a charcater between both*/
			&& (length >= 7)) {									/*The email adress is 7 char. long or more*/
		return true;
	}
	else
	{
		errorMessage = errorMessage + "Please enter a valid email adress. \n" ;
		valid = false;
		return false;
	}
}

function checkEnquiry() {
	$(".required").each(function() {
		checkField(this.id);
	});

	$(".requiredEmail").each(function() {
		checkEmail(this.id);
	});

	if (!valid) {
		alert(errorMessage);
	}

	errorMessage = "";
	validReturn = valid;
	valid = true;
	return validReturn;
}
