
$(document).ready(function(){
	
	
	var delay = (function(){
		var timer = 0;
		return function(callback, ms){
			clearTimeout (timer);
			timer = setTimeout(callback, ms);
		};
	})();
	
	$('#email').keyup(function() { delay(email_check, 800 ); });	
	
	$("form#emailSubmit").submit(function() { 
	var email = $("#email").attr('value');  
	var state = $("#state").attr('value');  
	var dataString = 'email='+ email + '&state=' + state;
		$.ajax(
			{
				type: "POST",
				url: "/global/emailProcess.php",
				dataType: 'html',
				data: dataString,
				success: function(){
					$('form#emailSubmit').hide(function(){
						$('div.success').fadeIn();
						$('#fbBox').fadeIn();

					});  
				}  
			});  
		return false;  
		});  		
});
	
function email_check(){	

var emailCheck = $('#email').val();

	if((emailCheck == "") || (emailCheck.length < 5) || (emailCheck == "Join Our Email List!")){
		
		$('#tick').hide();
		$('#button').hide();
		$('#cross').hide();
		$('#redundant').hide();
		$('#fbBox').fadeIn();
		
	}else{
		jQuery.ajax({
		   type: "POST",
		   url: "/global/check.php",
		   data: 'email='+ emailCheck,
		   cache: false,
		   success: function(response){
				if(response == 2){
					$('#tick').hide();
					$('#cross').fadeIn();
					$('#redundant').hide();
					$('#button').hide();
				}
				else if(response == 1){
					$('#tick').hide();
					$('#cross').hide();
					$('#redundant').fadeIn();
					$('#button').hide();
					
				}else{
					$('#cross').hide();
					$('#tick').fadeIn();
					$('#redundant').hide();
					$('#button').fadeIn();
					$('#fbBox').hide();
					
				}			
			}
		});
	}
};
