jQuery(function(){

	jQuery('#waiting, #error').hide();
	if(jQuery.cookie('has_subscribed')){
		jQuery('form, #main>p').hide();
	}
	
	$(document).keypress(function(e) {
	    if(e.keyCode == 13) {
	        add_subscriber();
	    }
	});
});



function add_subscriber() {
	
	var email; 
	var name;
	var data = "";
	
		jQuery('#waiting').fadeIn(300);
		jQuery('#CMSubform').animate({opacity: 0.4}, 300);
		email = jQuery('#cm_sub_email').val();
		name = jQuery('#cm_sub_name').val();
		
		jQuery.post('/wp-content/themes/dealfueltemp/logic.php', {
			'email':email,
			'name':name
		},
		function(data){
			console.log(data);
			if(data == "email_not_valid") {
				jQuery('#error')
					.html('Eep. You\'ve entered an invalid email address, please rectify it and try again!')
					.fadeIn(300)
					.delay(3000)
					.fadeOut(300);
				jQuery('#waiting').fadeOut(300);
				jQuery('#CMSubform').animate({opacity: 1}, 300);
			} 
			else if(data == "email_subscribed") {
				jQuery('#CMSubform, #main>p').delay(1000).slideUp(750);
				jQuery('#error')
					.html('Thanks! We\'ve got you. You\'ll now get updates with the coolest deals on the web!')
					.fadeIn(300)
					.delay(3000)
					.fadeOut(300);
				jQuery('#waiting').fadeOut(300);
				jQuery.cookie('has_subscribed', true);
			}
			else {alert(data); response = false;}
			});
		
}
