var emailRegEx = new RegExp("^[\'_a-z0-9-]+(\\.[\'_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.(([a-z]{2,3})|(aero|coop|info|museum|name|jobs|travel))$");

$(document).ready(function() {
	// Reset the error color.
	$("form#frmSebform").find("input,select").focus(function() {
		$(this).css("background-color","");
	});

	$("form#frmSebform").find("input,select").change(function() {
		$(this).css("background-color","");
	});

	$("form#frmSebForm").find("#FirstName").focus();

	// Validate the form.
	$("form#frmSebform").submit(function() {
		var errors = [];
		var errColor = "red";

		object = $(this).find("#FirstName");
		if (object.val().length == 0) {
			errors.push("- The First Name field is required.");
			object.css("background-color",errColor)
		}
				
		object = $(this).find("#LastName");
		if (object.val().length == 0) {
			errors.push("- The Last Name field is required.");
			object.css("background-color",errColor)
		}

		object = $(this).find("#AgencyName");
		if (object.val().length == 0) {
			errors.push("- The Agency Name field is required.");
			object.css("background-color",errColor)
		}

		object = $(this).find("#Email");
		if (!emailRegEx.test(object.val())) {
			errors.push("- The Email field is required.");
			object.css("background-color",errColor)
		}

		object = $(this).find("#ConfirmEmail");
		if (object.val().length == 0) {
			errors.push("- The Confirm Email field is required.");
			object.css("background-color",errColor)
		}

		object = $(this).find("#PhoneNumber");
		if (object.val().length == 0) {
			errors.push("- The Phone Number field is required.");
			object.css("background-color",errColor)
		}

		object = $(this).find("#Address1");
		if (object.val().length == 0) {
			errors.push("- The Address field is required.");
			object.css("background-color",errColor)
		}

		object = $(this).find("#City");
		if (object.val().length == 0) {
			errors.push("- The City field is required.");
			object.css("background-color",errColor)
		}

		object = $(this).find("#State");
		if (object.val().length == 0) {
			errors.push("- The State field is required.");
			object.css("background-color",errColor)
		}

		object = $(this).find("#ZipCode");
		if (object.val().length == 0) {
			errors.push("- The Zip Code field is required.");
			object.css("background-color",errColor)
		}

		object = $(this).find("#Password");
		if (object.val().length == 0) {
			errors.push("- The Password field is required.");
			object.css("background-color",errColor)
		} else {
			confirm = $(this).find("#ConfirmPassword");
			if (confirm.val() != object.val()) {
				errors.push("- Please confirm your password.");
				confirm.css("background-color",errColor)
			}
		}

		if (errors.length > 0) {
			var errorMsg = "The following error(s) occurred:";
			for (var counter = 0; counter < errors.length; counter++) {
				errorMsg += "\n" + errors[counter]; 
			}
			alert(errorMsg);
			return false;
		}

		return true;
	});
});