icuro.landing = icuro.landing || {};

icuro.landing.contact = (function () {
	function contactFormPost(e) {
		e.preventDefault();

		var formAction = $('#landing-form').attr('action');
		var serialize = $("#landing-form").serialize();

		$.ajax({
		    url : formAction,
		    type : 'POST',
		    data : serialize,
		    tryCount : 0,
		    retryLimit : 3,
		    success : function() {
		        $('.alert').remove();
		        $("#landing-form").prepend('<div class="alert alert-success" role="alert"><b>Success!</b> We will be in contact with you soon.</div>');
		    },
		    error : function(xhr, textStatus, errorThrown ) {
		        if (textStatus == 'timeout') {
		            this.tryCount++;
		            if (this.tryCount <= this.retryLimit) {
		                //try again
		                $.ajax(this);
		                return;
		            }            
		            return;
		        }
		        
		        $('.alert').remove();
		        $("#landing-form").prepend('<div class="alert alert-success" role="alert"><b>Error!</b> Please fill in all fields and try again.</div>');
		    }
		});
	}

	$("#landing-form").submit(function( e ) {
		e.preventDefault();
		contactFormPost(e);
	});
	
});