function isValidEmailAddress(elementValue) {  
    var emailPattern = /^[a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
    return emailPattern.test(elementValue);
}

$(document).ready(function(){
	$("input.input-text").each(function (type) {
		$(this).focus(function () {
			$(this).prev("label.inlined").addClass("focus");
		});

		$(this).keypress(function () {
			$(this).prev("label.inlined").addClass("has-text").removeClass("focus");
		});

		$(this).blur(function () {
			if($(this).val() == "") {
				$(this).prev("label.inlined").removeClass("has-text").removeClass("focus");
			}
		});
	});
	
	
	$("button.submit").attr("disabled", "true");
	
	function checkEmail(){
        var email = $("#email").val();
		if(email != 0)
		{
			if(isValidEmailAddress(email))
			{
				$("#validEmail").css({ "background-position": "left top" });
				$('input.input-text').css({"color": "#75bc72"});
				$("button.submit").removeAttr("disabled").addClass('enabled');
			} else {
				$("#validEmail").css({ "background-position": "left -17px" });
				$('input.input-text').css({"color": "#777"});
				$("button.submit").attr("disabled", "true").removeClass('enabled');
			}; 
		} else {
			$("#validEmail").css({ "background-position": "left bottom" });
			$('input.input-text').css({"color": "#777"});
			$("button.submit").attr("disabled", "true").removeClass('enabled');
		};
	};
	
	$("#email").keyup(checkEmail);
	$("#email").blur(checkEmail);
	checkEmail();
	

	var done = function(res, status) {
        if (status == "success") {
            $('.signup_form').fadeOut(1000);
            $('#email').val("");
            $('.thanks').show(1000);
        }else{
	        // display an explanation of failure
        };
	};
	
	var register_email = function() {
        var email = $("#email").val();
        if (email != ""){
            var data = { email:email};
            var args = { type:"POST", url:"/register_interest/", data:data, complete:done };
            $.ajax(args);
        }else{
            // display an explanation of failure
        };
        return false;
	};
	
	$('button.submit').click(register_email);
	$('input.input-text').bind('keypress', function(e){
	    var code = (e.keyCode ? e.keyCode : e.which);
         if(code == 13) {
             register_email();
             return false;
         }; 
	});
    
	
});
