// Finds all forms with class="signup"
// Expects a form element named "address" for the email
// Uses the original form action for POSTing
// Appreciates when there's an element with class="subscribe-message" in the
//   form for spewing feedback
// Will hide anything inside the form with class="hideonsuccess" and display
//   anything with class="showonsuccess" when subscription succeeds
// Will fade out & disable anything inside elements with
//   class="disablewhileprocessing"
// Requires at least jQuery 1.4

jQuery(function() {
	jQuery('form.signup').submit(subscribe).each(function() {
		jQuery(this).data('oldaction', this.action);
	}).removeAttr('action').removeAttr('method');
});

function subscribe() {
	var form = jQuery(this);
	var messageDiv = jQuery('.subscribe-message, #subscribe-message', form);
	var disableWhileProcessing = jQuery('.disablewhileprocessing', form);
	var address = jQuery('input[name=address]', form);
	var cookieName = jQuery('input[name=cookie]', form).val();

	if (address.val() == '') {
		if (messageDiv.length)
			messageDiv.html(
				'You left the email address field blank.'
			).removeClass('success').addClass('error').show();
		else
			alert('You left the email address field blank.');
		return false;
	}
	else if (address.val().split('@')[1].toLowerCase() == 'example.com') {
		address.val('');
		if (messageDiv.length)
			messageDiv.html(
				'Please enter your email address.'
			).removeClass('success').addClass('error').show();
		else
			alert('Please enter your email address.');
		address.focus();
		return false;
	}

	jQuery.ajax({
		url: form.data('oldaction'),
		type: 'POST',
		data: {address: address.val(), ajax: 'true'},
		dataType: 'text',
		beforeSend: function(req) {
			jQuery('input', disableWhileProcessing).attr('disabled', 'disabled');
			disableWhileProcessing.fadeTo('fast', 0.4);
			messageDiv.removeClass('error').html('<div style="text-align:center">'
				+ '<img src="/global/images/loading-32x32.gif" alt="Loading..." /></div>');
		},
		success: function(data, textStatus, req) {
			jQuery('input', disableWhileProcessing).removeAttr('disabled');
			disableWhileProcessing.filter(':animated').stop(true);
			disableWhileProcessing.fadeTo('fast', 1);
			// Subscribed
			if (req.status == 201) {
				if (messageDiv.length)
					messageDiv.html(data).removeClass('error').addClass('success').show();
				else
					alert(data);

				var eventID = req.getResponseHeader('X-SubscriberEventID');
				var trackingBug = jQuery('<img src="https://secure.augusthome.com/'
					+ 'global/email/signed-up/' + eventID + '/?ajax=true">');
				jQuery(document.body).append(trackingBug);

				jQuery('.hideonsuccess', form).hide();
				jQuery('.showonsuccess', form).show();

				if (cookieName) {
					var date = new Date();
					date.setTime(date.getTime() + (5 * 365 * 24 * 60 * 60 * 1000));
					var expires = "; expires="+date.toGMTString();
					document.cookie = cookieName + "=" + address.val() + expires + "; path=/";
				}
			}
			// Errors
			else if (req.status == 200) {
				if (messageDiv.length)
					messageDiv.html(data).removeClass('success').addClass('error').show();
				else
					alert(data);
			}
		}
	});

	return false;
}
 

