// gedrag login formulier
// gebruikt jQuery
$(document).ready(function() {
	if ($("#miNewsletterForm")) {
		initNewsletterForm();
		$("#nlUnsubscribe").click(function() {
			// nieuwe content laden, met submitknop
			$("#formHolder").slideUp(800,function() {
				$("#formHolder").load('../html/miNewsletterU.html','',function() {
					$("#formHolder").height('auto');
					$("#formHolder").slideDown(800);
				});
			});
		});
		if ($("#pommo_check").val()) {
			$.ajax({
				type: "POST",
				url: "/pommo/user/process.php",
				data: $("#miNewsletterForm").formSerialize(),
				dataType: "json",
				success: function(msg){
					// nieuwe content laden, met submitknop
					$("#formHolder").slideUp(800,function() {
						$("#formHolder").load(msg.eventText,'',function() {
							$("#nlEmail").html($("#email").val());
							$("#formHolder").height('auto');
							$("#formHolder").slideDown(800);
						});
					});
				}
			});
		}
	}
});

function initNewsletterForm() {
	// submit instellen
	$("#miNewsletterForm").submit(
		function() {
			$("#alert").hide();
			var thisCheck = $("#terms:checked").length;
			if ($("#terms").val() && (thisCheck == 0) && $('input[@name=action]:checked').val() != '2') {
			
				$("#alert").show();
				$("#alert").html("Please agree to our Terms of Service");
				return false;
				
			} else {
				$.ajax({
					type: "POST",
					url: "../scripts/mailinglist.php",
					data: $("#miNewsletterForm").formSerialize(),
					dataType: "json",
					success: function(msg){
						if (msg.event==1) {
							// foutmelding weergeven
							$("#alert").html(msg.eventText);
							$("#alert").height('auto');
							$("#alert").slideDown(800);
							
						} else if (msg.event==2) {
							// naar nieuwe pagina
							$("#formHolder").slideUp(800,function() {
								document.location.href = msg.eventText;
							});
						} else {
							// nieuwe content laden, met submitknop
							//$("#formHolder").slideUp(800,function() {
								$("#formHolder").load(msg.eventText,'',function() {
									//$("#formHolder").height('auto');
									//$("#formHolder").slideDown(800);
								});
							//});
						}
					}
				});
			}
			return false;
		}
	);
}