function checkEmail(str) {
	var at = "@", dot = ".", lat = str.indexOf(at), lstr = str.length, returnValue = true;
	if (str.indexOf(at) === -1) {
		returnValue = false;
	}
	if (str.indexOf(at) === -1 || str.indexOf(at) === 0 || str.indexOf(at) === lstr) {
		returnValue = false;
	}
	if (str.indexOf(dot) === -1 || str.indexOf(dot) === 0 || str.indexOf(dot) === lstr) {
		returnValue = false;
	}
	if (str.indexOf(at, (lat + 1)) !== -1) {
		returnValue = false;
	}
	if (str.substring(lat - 1, lat) === dot || str.substring(lat + 1, lat + 2) === dot) {
		returnValue = false;
	}
	if (str.indexOf(dot, (lat + 2)) === -1) {
		returnValue = false;
	}
	if (str.indexOf(" ") !== -1) {
		returnValue = false;
	}
	return returnValue;
}

$(function () {
	$('.emailError').hide();

	$(".emailError").click(function () {
		$(this).fadeOut(400);
	});

	$("#submit_btn").click(function () {
		var name = $("input#name").val(), email = $("input#email").val();

		$('.emailError').fadeOut(400);

		if (name === "") {
			$("#name_error").fadeIn(400);
			$("input#name").focus();
 			return false;
		}

		if (email === "") {
			$("#email_error").fadeIn(400);
			$("input#email").focus();
			return false;
		}
    
		if (checkEmail(email) === false) {
			$("#email_domain_error").fadeIn(400);
			$("input#email").focus();
			return false;
		}
    
		$.ajax({
			type: "POST",
			url: "/assets/subscribe.ashx",
			data: {
				"name": name,
				"email": email
			},
			statusCode: {
				200: function () {
					$('#signUp').fadeOut(750, function () {
						$('#signUp').html("<div id='message'></div>");
						$('#message').html("<br/><p>Thank you!</p><p>We will be in touch soon.</p>");
						$('#signUp').fadeIn(750, function () {});
					});
				},
				501: function() {
					$("#email_signed").fadeIn(400);
					$("input#email").focus();
				},
				502: function () {
					$("#email_domain_error").fadeIn(400);
					$("input#email").focus();
				}
			}
		});

		return false;
	});
});
