var news_obj = {};
news_obj.lists = [
{
id:"a4db5f2336",
name:"Carl M. Cannon's Morning Note",
site:"politics"
},{
id:"d6a76c9ec3",
name: "RCP Daily Poll Wrap-Up",
site:"politics"
},{
id:"51f930c5ce",
name: "RCP Today",
site:"politics"
},{
id:"8c65b915a9",
name: "RCP Morning Reel",
site:"politics"
},{
id:"9da46952f0",
name: "RCP Events",
site:"politics"
},{
id:"694f73a8dc",
name: "Defense Morning Recon",
site:"defense"
},{
id:"267b89dc8a",
name: "Science News",
site:"science"
},{
id:"462961a8ae",
name: "RealClearEnergy Morning Volt",
site:"energy"
},{
id:"d519acabbf",
name: "World News",
site:"world"
},{
id:"8a051b373b",
name: "Education Today",
site:"education"
},{
id:"b4baf6b587",
name: "RealClearHealth Morning Scan",
site:"health"
},{
id:"113ee1bc69",
name: "Weekly Policy Picks",
site:"policy"
},{
id:"113ee1bc69",
name: "RealClearPolicy",
site:"policy"
},{
id:"7f0a4512a1",
name: "RealClearReligion Today",
site:"religion"
},{
id:"ea772b1135",
name: "RealClearBooks Today",
site:"books"
},{
id:"fbced764bd",
name: "RealClearHistory Today",
site:"history"
},{
id:"1f71a1eaa2",
name: "RealClearMarkets Today",
site:"markets"
},{
id:"d042379c8d",
name: "RealClearInvestigations Today",
site:"investigations"
},{
id:"7d83b1601b",
name: "RealClear Originals",
site:"politics"
}
];
news_obj.inline_found = false;
news_obj.submitted = false;
loadFormHTML();
setFormTriggers();
function loadFormHTML(){
//BUILD FORM HTML FOR PAGE. CSS IN master.css
var html_data = '
';
//html_data += '- RealClear Originals✓
';
html_data += '- Carl M. Cannon\'s Morning Note✓
';
html_data += '- RCP Daily Poll Wrap-Up✓
';
html_data += '- RCP Today ✓
';
html_data += '- RCP Morning Reel✓
';
html_data += '- RCP Events✓
';
html_data += '- RCD Morning Recon ✓
';
html_data += '- Science News✓
';
html_data += '- RealClearEnergy Morning Volt✓
';
html_data += '- World News✓
';
html_data += '- Education Today✓
';
html_data += '- RealClearHealth Morning Scan✓
';
//html_data += '- Europe Memo✓
';
//html_data += '- Mideast Memo✓
';
html_data += '- Weekly Policy Picks✓
';
html_data += '- RealClearPolicy Today✓
';
html_data += '- RealClearReligion Today✓
';
html_data += '- RealClearBooks Today✓
';
html_data += '- RealClearHistory Today✓
';
html_data += '- RealClearMarkets Today✓
';
html_data += '- RealClearInvestigations Today✓
';
html_data += '
';
html_data += '';
html_data += ''
+'';
html_data += '';
html_data += '
';
html_data += '
';
html_data += '
';
html_data += '
';
html_data += '
';
html_data += '
'; //end of right options div
html_data += '
';
//APPENDS HTML FORM INTO PARENT ELEMENT
//$('.jsTag').last().parent().append(html_data);
$('.newsletter-signup-container2').html(html_data);
$('.newsletter-signup-container2').each(function() {
//GETS DATA-AUTO-CHECKS ATTRIBUTE FROM JS EMBED
var att = $(this).attr('data-auto-checks');
var auto_checks = [];
if(typeof att !== 'undefined') {
auto_checks = att.split("|"); //SPLITS INTO ARRAY
}
//LOOP THROUGH ALL NEWSLETTER CHECKBOXES
if(auto_checks.length > 0){
for (var i=0; i 1023 && !$('body').hasClass('article')) || ($('body').hasClass('article') && $(window).width() > 1180) ){
var inline_sign_up_box_height = parseInt(items_height)+70;
}else{
var inline_sign_up_box_height = parseInt(items_height)+265; //190
}
$(this).parents('.inline_sign_up_box').css('height', inline_sign_up_box_height+'px').addClass('sign_up_open');
news_obj.inline_found = true;
if( (news_obj.submitted == true && $(window).width() < 1024)
|| (news_obj.submitted == true && $('body').hasClass('article') && $(window).width() <= 1180) ){
news_obj.submit_added
var current_top = parseInt($('.inline_sign_up_box ul.newsletters').css('top'));
var new_top = current_top - news_obj.submit_added;
$('.inline_sign_up_box ul.newsletters').css('top', new_top+'px');
news_obj.submitted = false;
}
}
});
});
showNewsWidget();
}
function setFormTriggers(){
//$("#list_email").val('Enter your email address');
//BUTTON CLICK
$('.newsletter-signup-container2').find("#btn_subscribe").click(function() {
validateForm_v2($(this).parent().parent());
});
//ENTER KEY
$('.newsletter-signup-container2').find('#list_email').bind('keypress', function(e) {
if(e.keyCode==13){
validateForm_v2($(this).parent().parent());
}
});
//EMAIL FOCUS SHOW OPTIONS
$('.newsletter-signup-container2').find('#list_email').focus(function(){
$(this).parent().parent().find( "#opt-in" ).slideDown();
});
}
function showNewsWidget(){
$(".jQ-news-widget").unbind('click');
$(".jQ-news-widget").click(function(){
//console.log("fired...");
var elem = $(this).parent().next(".newsletter-signup-container2");
//elem.toggle();
//console.log(elem.is(':visible'));
if( elem.is(':visible') ) {
elem.hide();
} else {
elem.show();
}
});
}
function validateForm_v2(target_parent){
var e = target_parent.find('input[name="list_email"]').val();
//console.log('here1');
//console.log(e);
if($('#opt_in_3').prop('checked') == false){
target_parent.find('#think_email').html('Please Agree to the Terms of Service.').show();
if($('#i-agree').prop('checked')){
$('#opt_in_3').focus();
}else{
$('#i-agree').focus();
}
target_parent.find('#think_subscribe').html('');
}
else if(validEmail(e)){
//console.log('here2');
target_parent.find('#think_email').html('').hide();
sendSubscribe_v2(target_parent);
}else{
//console.log('here3');
target_parent.find('#think_email').html('Please enter a valid email address.').show();
target_parent.find('#think_subscribe').html('');
}
}
function validEmail(email){
var emailReg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
return emailReg.test(email);
}
//SUBMIT FORM AND SUBSCRIBE TO EACH SELECTED NEWSLETTER
function sendSubscribe_v2(target_parent){
target_parent.find('#loader').show();
if(news_obj.inline_found == true){
target_parent.find('#btn_subscribe').hide();
}
var opt_in_1 = target_parent.find("#opt-in input#opt_in_1").is(':checked');
var opt_in_2 = target_parent.find("#opt-in input#opt_in_2").is(':checked');
var opt_in_3 = target_parent.find('#opt-in input#opt_in_3').is(':checked');
var api_lists = [];
target_parent.find("li.newsletter-select.selected").each(function() {
api_lists.push($(this).attr("id"));
});
if(api_lists.length>0){
$.ajax({
url: window.location.protocol+'//util.realclearpolitics.com/subscriptions/mailchimp_do_subscribe_v2.php?callback=?',
data: 'zelda='+target_parent.find("#zelda").val()+'&list_email='+target_parent.find("#list_email").val()+'&lists='+api_lists+'&optin1='+opt_in_1+'&optin2='+opt_in_2+'&optin3='+opt_in_3+'&loc='+target_parent.find('.jsTag').attr('data-loc')+'',
dataType: 'jsonp',
jsonpCallback: 'myData',
cache: false,
context: {
target_parent_ref: target_parent
},
success: function(y) {
var target_parent = this.target_parent_ref;
var subscribe_output = '';
api_lists_subscribed = new Array();
api_lists_already_subscribed = new Array();
for(var key in y.data.content){ //STEPS THROUGH EACH CHECKED LIST
var vars = y.data.content[key].split("|");
var status = vars[0];
var email = vars[1];
var list_id = vars[2];
var code = vars[3];
var message = vars[4];
var list_name = $('#list_'+list_id).attr('rel');
if(status!=1){
if(code==214){ //EMAIL ALREADY SUBSCRIBED TO LIST
//subscribe_output += 'Already subscribed to '+list_name+'
';
api_lists_already_subscribed.push(list_name); //APPEND TO ALREADY SUBSCRIBED LIST
}else{
//console.log(message);
subscribe_output += message;
}
}else{ //SUCCESS
api_lists_subscribed.push(list_name); //APPEND TO SUCCESS LISTS ARR
}
}
//alert(api_lists_subscribed.length);
if(api_lists_already_subscribed.length>0){
subscribe_output += 'Already subscribed to the following:
';
for ( var i = 0; i < api_lists_already_subscribed.length; i = i + 1 ) {
subscribe_output += '- • '+api_lists_already_subscribed[i]+'
';
}
subscribe_output += '
';
}
if(api_lists_subscribed.length>0){
subscribe_output += 'Confirmation email shall arrive shortly for:
';
for ( var i = 0; i < api_lists_subscribed.length; i = i + 1 ) {
subscribe_output += '- • '+api_lists_subscribed[i]+'
';
}
subscribe_output += '
';
}
target_parent.find('#list_email').val('');
target_parent.find('#think_email').html(''+subscribe_output+'
').show();
//case: if is on mobile make the window higher
if($(window).width() <= 415){
//var hight_response_ul = $('.newsletter-signup-container2').height();
//var warning_wrapper = $('.warning-wrapper').height();
//console.log(hight_response_ul);
$('.warning-wrapper ul.newsletters').css({display: 'none'});
$('.warning-wrapper #opt-in').hide();
}
target_parent.find('#loader').hide();
if(news_obj.inline_found == true){
news_obj.submitted = true;
target_parent.find('#btn_subscribe').show();
if(($(window).width() < 1024 && !$('body').hasClass('body')) || ($('body').hasClass('article') && $(window).width() <= 1180)){
var start_height = $('.inline_sign_up_box').height();
var display_height = target_parent.find('.think_email').height();
var new_height = parseInt(start_height)+parseInt(display_height)+30;
$('.inline_sign_up_box').css('height', new_height+'px');
var current_top = parseInt($('.inline_sign_up_box ul.newsletters').css('top'));
var new_top = current_top+display_height+30;
news_obj.submit_added = display_height+30;
$('.inline_sign_up_box ul.newsletters').css('top', new_top+'px');
}
}
} //END SUCCESS FUNC
}); //END AJAX
} //END IF LISTS CHECK
}