$(document).ready(function() {
	$("#maillist-form").submit(function () {
		var valid = true;
		/* Check for empty fields */
		var non_empty = ['#maillist-name', '#maillist-email'];
		for (i = 0; i < non_empty.length; i++) {
			if ($(non_empty[i]).attr('value') == "" || $(non_empty[i]).attr('value') == "Name" || $(non_empty[i]).attr('value') == "Email") {
				valid = false;
				$(non_empty[i]).css('background-color', '#ffcccc');
			}
		}
		
		/* Check for valid email address when the rest of the form is invalid */
		if (valid && !validate_email($("#maillist-email").attr('value'))) {
			valid = false;
			$("#maillist-error").html("The email address provided is not valid, please try again.");
		} else {
			$("#maillist-error").html("Please complete both the name and email fields.");
		}

		if (valid == true) {
			return true;
		} else {
			$("#maillist-error").dialog({
				height: 200,
				modal: true,
				buttons: {
					Ok: function() {
						$( this ).dialog( "close" );
					}
				}
			});
			return false;
		}
	});
});

function validate_email(email)
{
	/* Check for valid email */
	var reg=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/
	if(reg.test(email)) {
		return true;
	}
	return false;
}
