// JavaScript Document
$("#unsubscribe").hide();
$("#unsubscribeLink").click(function() {$("#unsubscribe").fadeIn();});
$("#subscribeButton").click(function() {register(); return false;});
$("#unsubscribeButton").click(function() {unregister(); return false;});

var readyContent = false;
var readyContentUnregister = false;
function register() {
	$.getScript(server + "siteServices/newsletter/subscribe.jsp?" + $("#subscribeForm").serialize(), function() {displayContent();});
	$("#subscribeForm").fadeOut(1000, function() {displayContent();});
}

function displayContent() {
	if (readyContent) {
		if (result.errorCode == 0)
			$("#subscribeForm").html("<div class='message'>Thank you for subscribing to our newsletter!</div>");
		else
			$("#subscribeForm").html("<div class='message'>An error occured, please try again later</div>");
		
		$("#subscribeForm").fadeIn();
	}
	else
		readyContent = true;
}


function unregister() {
	$.getScript(server + "siteServices/newsletter/unsubscribe.jsp?" + $("#unsubscribeForm").serialize(), function() {displayContentUnregister();});

	$("#unsubscribeForm").fadeOut(1000, function() {displayContentUnregister();});
}

function displayContentUnregister() {
	if (readyContentUnregister) {
		if (result.errorCode == 0)
			$("#unsubscribeForm").html("<div class='message'>Thank you for unsubscribing to our newsletter!</div>");
		else if (result.errorCode == 2)
			$("#unsubscribeForm").html("<div class='message'>You are not registered on our server</div>");
		else
			$("#unsubscribeForm").html("<div class='message'>An error occured, please try again later</div>");
		$("#unsubscribeForm").fadeIn();
	}
	else
		readyContentUnregister = true;
}