
<div class="signup-form">
    <h3 id="mc_headline">
        Get the Essential California newsletter delivered to your inbox
    </h3>

    <div class="search">
        <div id="mc_embed_signup">
              <form id="mailchimp-subscribe">
                  <input class="letterlist" type="hidden" name="listid" value="">
                  <input class="lettername" type="hidden" name="listname" value="">
                  <input type="hidden" name="domain" value="//newsletters.tribpub.com">
                  <input class="submit-field" type="text" name="email" placeholder="Enter your email" value="">
                  <input class="submit-button" name="submit" type="submit" value="Sign up" disabled="disabled">
                  <div class="mailchimp-response"></div>
              </form>
        </div>
    </div>
    <div class="clear"></div>
</div>

<script>
//assign id # based on order
var signupNumber = $(".signup-form").length;
$("#mc_headline").attr("id", "mc_headline" + signupNumber);
$("#mc_embed_signup").attr("id", "mc_embed_signup" + signupNumber);
$("#mailchimp-subscribe").attr("id", "mailchimp-subscribe" + signupNumber);

var newsletters = {
	"Essential California": "6e35f7f85b",
	"Opinion": "06c6002be6",
	"California Inc.": "b7e005cdb1",
	"Classic Hollywood": "6b16d1d198",
	"Counter Intelligence": "6b9d0c1d9e",
	"Dodgers Dugout": "ada9589456",
	"Arts and Culture": "b157977cf5",
	"Great Reads": "4e3ae5d53e",
	"Hot Property": "a8a9b6a7e3",
	"Indie Focus": "a7e301665c",
	"In the Kitchen": "2c3fe54b6d",
	"Theme Parks": "97a620229b",
	"Today's Headlines": "b04355194f",
	"Water & Power": "865427f9e9",
	"Essential Politics": "db59b9bd47"
};


{% if data_title %}
	var letterName = "{{ data_title }}";
{% else %}
	var letterName = "Essential California";
{% endif %}

{% if data_headline %}
    var headline = "{{ data_headline }}";
{% else %}
    var headline = "Get the " + letterName + " newsletter delivered to your inbox";
{% endif %}

var letterId = newsletters[letterName];

// change HTML above
$("#mailchimp-subscribe"+signupNumber+" .letterlist").val(letterId);
$("#mailchimp-subscribe"+signupNumber+" .lettername").val(letterName);
$("#mc_headline"+signupNumber).text(headline);

//code below from https://d2ne9hs4xtfl62.cloudfront.net/v2/js/subscribe_form.js
$('form#mailchimp-subscribe'+signupNumber+' input[name="submit"]').prop("disabled", false);

$('form#mailchimp-subscribe'+signupNumber).submit( function() {
	var valid = true, msg, $form = $(this);
	email = $form.find('input[name="email"]').val().toLowerCase(),
	listid = $form.find('input[name="listid"]').val(),
	listname = $form.find('input[name="listname"]').val(),
	domain = $form.find('input[name="domain"]').val(),
	$response = $form.find('.mailchimp-response'),
	regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	domain.replace("http://", "//");

	$response.html('processing...');
	$form.removeClass('error').removeClass('success');
	$form.find('input[name="submit"]').prop("disabled", true);

	if (!regex.test(email)) {
		valid = false;
		msg = 'Please provide a valid email.';
	}
	else if (!listid) {
		valid = false;
		msg = 'Please provide a valid list id.';
	}

	if (valid) {
		url = domain + '/service/v2/list/subscribe/email/'+email+'/listid/'+listid+'.jsonp';
		$.ajax({
			url: url,
			dataType: 'json',
			success: function(data) {

				var cssClass = '';
				var msg = '';

				if(data.status === 'subscribed' && (data.action === 'add' || data.action === 'update')) {
					cssClass = 'success';
					msg = 'Thank you for subscribing to ' + listname + '.';
				}
				else if(data.status === 'subscribed' && data.action === '') {
					cssClass = 'success';
					msg = 'Thank you, you are already subscribed to ' + listname + '.';
				}
				else if(data.status === 'pending') {
					msg = 'You have already requested subscription to ' + listname + '. Please check your email to confirm your pending status.';
				}
				else if(data.status === 'cleaned') {
					msg = 'Please contact an administrator to get access to ' + listname + '.';
				}
				else {
					msg = 'Oops, an error occurred. Please try again later. code:1001';
				}
				$form.addClass(cssClass).find('input[name="email"]').val('');
				$response.html(msg);
				$form.find('input[name="submit"]').prop("disabled", false);
			},
			error: function(error) {
				$form.addClass('error');
				$response.html('Oops, an error occurred. Please try again later. code:1002');
				$form.find('input[name="submit"]').prop("disabled", false);
			}
		});
	}
	else {
		$form.addClass('error');
		$response.html(msg);
		$form.find('input[name="submit"]').prop("disabled", false);
	}
	return false;
});

</script>
