
function rulemailerSubscribe() {

	enableButtons(false);

	email = $('rulemailerEmailInput').value;

	//Validate email
	if(validateEmailAddress(email)) {

		//Send ajax request
		var url = 'include/rulemailer.php?action=subscribe&email=' + encodeURIComponent(email);

		//alert(url);

		new Ajax.Request(url, {
			method: 'get',
			onSuccess: function(transport) {
				//alert(transport.responseText);
				var rulemailerInfoText = $('rulemailerInfoText');
				
				if(transport.responseText.match('Success')) {
					rulemailerInfoText.update(subscribeSuccessText);
				} else {
					rulemailerInfoText.update(subscribeErrorText);
				}
				enableButtons(true);
	  		}
		});
	} else {
		wrongEmailAddressEntered();
	}
}

function rulemailerUnsubscribe() {

	enableButtons(false);

	email = $('rulemailerEmailInput').value;

	//Validate email
	if(validateEmailAddress(email)) {

		//Send ajax request
		var url = 'include/rulemailer.php?action=unsubscribe&email=' + encodeURIComponent(email);
	
		//alert(url);

		new Ajax.Request(url, {
			method: 'get',
			onSuccess: function(transport) {
				//alert(transport.responseText);
				var rulemailerInfoText = $('rulemailerInfoText');
				if(transport.responseText.match('Not in list')) {
					rulemailerInfoText.update(unsubscribeSuccessTextNotInList);
				} else if(transport.responseText.match('Success')) {
					rulemailerInfoText.update(unsubscribeSuccessText);
				} else {
					rulemailerInfoText.update(unsubscribeErrorText);
				}
				enableButtons(true);
  			}
		});
	} else {
		wrongEmailAddressEntered();
	}
}

function rulemailerCheckEmail() {

	enableButtons(false);

	email = $('rulemailerEmailInput').value;

	//Validate email
	if(validateEmailAddress(email)) {

		//Send ajax request
		var url = 'include/rulemailer.php?action=checkEmail&email=' + encodeURIComponent(email);
	
		//alert(url);

		new Ajax.Request(url, {
			method: 'get',
			onSuccess: function(transport) {
				var rulemailerInfoText = $('rulemailerInfoText');
				//alert(transport.responseText);
				if(transport.responseText.match('Not in list')) {
					rulemailerInfoText.update(checkEmailSuccessTextNotInList);
				} else if(transport.responseText.match('Unsubscribed')) {
					rulemailerInfoText.update(checkEmailSuccessTextUnsubscribed);
				} else if(transport.responseText.match('Subscribed')) {
					rulemailerInfoText.update(checkEmailSuccessTextSubscribed);
				} else {
					rulemailerInfoText.update(checkMailErrorText);		
				}
				enableButtons(true);		
	  		}
		});
	} else {
		wrongEmailAddressEntered();
	}
}

function validateEmailAddress(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
		return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}

	if (str.indexOf(at,(lat+1))!=-1){
		return false
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	}
	
	if (str.indexOf(dot,(lat+2))==-1){
		return false
	}
		
	if (str.indexOf(" ")!=-1){
		return false
	}

	 return true					
}

function enableButtons(enable) {
	if(enable) {
		if($('rulemailerSubscribeButton')!=null) {
			$('rulemailerSubscribeButton').enable();
		}
		if($('rulemailerUnsubscribeButton')!=null) {
			$('rulemailerUnsubscribeButton').enable();
		}
		if($('rulemailerCheckEmailButton')!=null) {
			$('rulemailerCheckEmailButton').enable();
		}
	} else {
		if($('rulemailerSubscribeButton')!=null) {
			$('rulemailerSubscribeButton').disable();
		}
		if($('rulemailerUnsubscribeButton')!=null) {
			$('rulemailerUnsubscribeButton').disable();
		}
		if($('rulemailerCheckEmailButton')!=null) {
			$('rulemailerCheckEmailButton').disable();
		}
	}
}

function wrongEmailAddressEntered() {
		//Email address in wrong format
		var rulemailerInfoText = $('rulemailerInfoText');		
		rulemailerInfoText.update(validateEmailErrorText);
		enableButtons(true);
}


