$().ready(function() {
    $('#booklovers_reader').submit(function() {
	var n = $("input:checked[name='reader_option[]']").length;
	//console.log(n);
	if (n > 3) {
	    alert('请选择3名以内作家');
	    return false;
	}
	if (!is_email($('#reader_email').val())) {
	    alert('请填入正确的E-mail地址');
	    return false;
	}
	var email = $('#reader_email').val();

	if ($('#reader_nl').attr('checked')) {
	    var nl = $('#reader_nl').val();
	} else {
	    var nl = '';
	}

	var options = new Array();
	$("input:checked[name='reader_option[]']").each(function() {
	    //alert($(this).val());
	    options.push($(this).val());
	});

	options = options.join(',');	
	//console.log(options);

	var author_name = $('#author_name').val();
	var words = $('#words').val();

	if (nl) {
	    post_to_simple_nl(email);
	}

	reader_post(email, options, nl, author_name, words);
	return false;
    });

});


function is_email(value) {
    return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
}

function reader_post(email, options, nl, author_name, words) {
    $.post('/event/booklovers/reader.php', 
           {reader_email: email, reader_option: options, reader_nl: nl, author_name: author_name, words: words},
	   function() {
	       alert('感谢提交您的信息');
	       window.location = "eventnews1.html";
	   });
    return false;
}

function post_to_simple_nl(email) {
    extra = 'booklovers+greenauthor';
    $.post('/event/membership/simple_newsletter.php?transfer=n', {tmp_email: email, comment: extra},
	   function() {
	       //alert('提交到nl');
	   });
    return false;
}

