var checkTimer = null;
var curCheck = null;

function subscribe(doDelay) {
	var email = $("email").value;
	if((email==null)||(email.length == 0)) {
		$("mail_incorrect").style.display="";
		$("mail_normal").style.display="none";
		$("newsbg").style.display="";			  
		return;
	}
	
	curCheck = email;
	if(doDelay) {
		if(checkTimer!= null) {
			window.clearTimeout(checkTimer);
			checkTimer = null;
		}
		$("mail_processing").style.display="";
		$("newsbg").style.display="none";
		$("mail_incorrect").style.display="none";
		$("mail_normal").style.display="none";
		checkTimer = window.setTimeout(function() { subscribe(false); }, 250);
		return;
	}
	new Ajax.Request("/mailinglist_subscribe.html?email=" + encodeURIComponent(email), 
		{	onSuccess: function(response) {
				finishsubscribe(parseInt(response.responseText), email);
			}
		});
}

function finishsubscribe(passed, test) {
	if(test == curCheck) {
		$("mail_processing").style.display="none";
		if(passed == 1) {
			$("mail_success").style.display="";
		} else{
  		$("mail_incorrect").style.display="";
  		$("newsbg").style.display="";
		}
	}
}

