$(document).ready(function(){

	$('.showoptin').hide();
	$('.showoptinsuccess').hide();

	$("#clickoptin").click(function(e){
		e.preventDefault();
		$('#showprofile').fadeOut('slow');
		$('#showoptin').fadeIn('slow');
		$('#emailerror').hide();
	});
	
	$(".clickoptin").each(function(){
		$(this).click(function(e){
		e.preventDefault();
		$('#header_users_contact').fadeOut('slow');
		$('#showoptin_top').fadeIn('slow');
		$('#emailerror_top').hide(); 
	});
	})

	var isPreventDefault = false;

	$('#optin').submit(function(e) {
		if (!isPreventDefault) {
			e.preventDefault();
		}
		else {
			return true;	
		}

		$.post("/process_optin.php", $(this).serialize(), function(msg) {
			msg = msg.replace(/^\s*|\s*$/g,"");

			if (msg == "OK") {
				isPreventDefault = true;
				$('#showoptin').fadeOut('slow');
		                $('#showoptinsuccess').fadeIn('slow');								
			}
			else {
				$('#emailerror').show();
			}
		});
	});
	
	$('#optin_top').submit(function(e) {
		if (!isPreventDefault) {
			e.preventDefault();
		}
		else {
			return true;	
		}

		$.post("/process_optin.php", $(this).serialize(), function(msg) {
			msg = msg.replace(/^\s*|\s*$/g,"");

			if (msg == "OK") {
				isPreventDefault = true;
				$('#showoptin_top').fadeOut('slow');
		                $('#showoptinsuccess_top').fadeIn('slow');								
			}
			else {
				$('#emailerror_top').show();
			}
		});
	});

});


