// let queryParms = window.location.search;
var isArticle = location.pathname.includes("/story/") ? true : false;
var isReview = location.pathname.includes("/review/") ? true : false;
var deviceDetails = navigator.appVersion;
var customerData = "";
var emazLink = "";

function artrevstringGen(taxVal, title, art_url, type) {
    if ($.cookie('artrevstring') == undefined) {
        var randomString = makeid();
        // call api to get the cookie months
        $.ajax({
            url: APIBaseUrl + "getarticlereviewfreecount",
            type: 'GET',
            dataType: 'json',
            contentType: "application/json",
            success: function (data, textStatus) {
                var jsondata = JSON.parse(data);
                var now = new Date();
                var cookieExpireMonth = new Date(now.getFullYear(), now.getMonth() + parseInt(jsondata.cookie_expire_months), 1);
                var meterData = {
                    "value": randomString,
                    "expire": cookieExpireMonth
                };
                $.cookie('artrevstring', JSON.stringify(meterData), { path: '/', expires: cookieExpireMonth });
                $.cookie('mtrfreecount', 0, { path: '/', expires: cookieExpireMonth });
                checkAccess(taxVal, title, art_url, type);
            },
            error: function (request, textStatus, errorThrown) {
                if (request.status == 401) {
                    return false;
                }
            }
        });
    }
}

//Subscribe link tracking start
let unique_id = "";
let utmcontent = "Desktop_Meter_";
if (window.innerWidth < 768)
    utmcontent = "Mobile_Meter_";
let sublink = "/page/subscribe/indv?utm_source=NativeBrandSite&utm_medium=banner&utm_campaign=Subscribe_202007&utm_term=AdSet1&utm_content=";
let ctrllink = "/page/login?utm_source=NativeBrandSite&utm_medium=banner&utm_campaign=Control_Aug2020&utm_term=AdSet1&utm_content=";
let reglink = "/page/register?utm_source=NativeBrandSite&utm_medium=banner&utm_campaign=Control_Aug2020&utm_term=AdSet1&utm_content=";
var homelink = "/page/home?utm_source=NativeBrandSite&utm_medium=banner&utm_campaign=Control_Aug2020&utm_term=AdSet1&utm_content=";//updated

//Subscribe link tracking end

function addMasking() {
    if (isReview) {
        $('#story_para_review').addClass('maskingHeight');
        $('#story_para_review_mask').addClass('maskingContent');
    } else if (isArticle) {
        $('#story_para_article').addClass('maskingHeight');
        $('#story_para_article_mask').addClass('maskingContent');
        $('#articleExcerptContent').removeClass('hide');
    }
}
function removeMasking() {
    if (isReview) {
        $('#story_para_review').removeClass('maskingHeight');
        $('#story_para_review_mask').removeClass('maskingContent');
    } else if (isArticle) {
        $('#story_para_article').removeClass('maskingHeight');
        $('#story_para_article_mask').removeClass('maskingContent');
        $('#articleExcerptContent').addClass('hide');
    }
}

function checkAccess(taxVal, title, art_url, type) {

    var env = 'prod';
    var ipApiURL = 'https://pro.ip-api.com/json/?key=w1SYDcurnJ5Srpt';
    if (env == 'dev') {
        ipApiURL = 'http://ip-api.com/json'; //development server
    }

    if (!localStorage.getItem("userLocation")) {
        $.ajax({
            url: ipApiURL,
            type: 'GET',
            success: function (data, textStatus) {
                // console.log("IP_Details", data);
                currentIp = {
                    ip: data.query,
                    city: data.city,
                    country: data.country
                };

                if (localStorage.getItem("isDigitalUser")) {
                    checkIpAccess(taxVal, title, art_url, type, localStorage.getItem("isDigitalUser"));
                } else {
                    $.ajax({
                        url: APIBaseUrl + "getdetailbyip",
                        type: 'POST',
                        data: JSON.stringify({
                            "ip": currentIp.ip,
                            "url": window.location.href,
                            "randomString": localStorage.getItem("randomString"),
                            "email": customerData.email,
                            "userDeviceInfo": deviceDetails,
                            "location": currentIp.city + "," + currentIp.country
                        }),
                        contentType: "application/json",
                        success: function (res, textStatus) {
                            let data = JSON.parse(res);
                            if (data && data.success) {
                                checkIpAccess(taxVal, title, art_url, type, data.isDigitalUser);
                            }
                        },
                        error: function (request, textStatus, errorThrown) {
                        }
                    });
                }
            },
            error: function (request, textStatus, errorThrown) { }
        });
    } else if (localStorage.getItem("userLocation")) {
        currentIp = {
            ip: JSON.parse(localStorage.getItem("userLocation")).query,
            city: JSON.parse(localStorage.getItem("userLocation")).city,
            country: JSON.parse(localStorage.getItem("userLocation")).country
        };

        if (localStorage.getItem("isDigitalUser")) {
            checkAccessIp(taxVal, title, art_url, type, localStorage.getItem("isDigitalUser"));
            // console.log("isDigitalUser====>", taxVal, title, art_url, type, localStorage.getItem("isDigitalUser"));

        } else {
            $.ajax({
                url: APIBaseUrl + "getdetailbyip",
                type: 'POST',
                data: JSON.stringify({
                    "ip": currentIp.ip,
                    "url": window.location.href,
                    "randomString": localStorage.getItem("randomString"),
                    "email": customerData.email,
                    "userDeviceInfo": deviceDetails,
                    "location": currentIp.city + "," + currentIp.country
                }),
                contentType: "application/json",
                success: function (res, textStatus) {
                    let data = JSON.parse(res);
                    if (data && data.success) {
                        checkAccessIp(taxVal, title, art_url, type, data.isDigitalUser);
                        // console.log("isDigitalUser====>", taxVal, title, art_url, type, localStorage.getItem("isDigitalUser"));
                    }
                },
                error: function (request, textStatus, errorThrown) {
                }
            });
        }
    }

    // let subcriberStatus = $.cookie('customerIsSubscriber')
    //     ? JSON.parse($.cookie('customerIsSubscriber'))
    //     : false;
    // taxVal = taxVal ? taxVal : "dummy";


    //User is not a subscriber
    // if (!subcriberStatus) {
    //     $('#story_para_article').addClass('maskingHeight');
    //     $('#story_para_article_mask').addClass('maskingContent');
    //     $('#articleExcerptContent').removeClass('hide');

    //     isPrivateMode().then(function (isPrivate) {
    //         //Check if incognito or private mode and user is not logged in
    //         if ($.cookie('customerData') == undefined && isPrivate) {
    //             addMasking();
    //             utmcontent = "Desktop_Incognito";
    //             if (window.innerWidth < 768)
    //                 utmcontent = "Mobile_Incognito";
    //             let sublink_abtest = sublink + utmcontent;
    //             // AB Testing 
    //             if (parseInt(res.count) == 5) {
    //                 sublink_abtest = sublink + utmcontent + "_A";
    //             } else {
    //                 sublink_abtest = sublink + utmcontent + "_B";
    //             }
    //             $(".gtm_private_ID").attr("id", utmcontent);
    //             $(".full-meter-href").attr("href", sublink_abtest);
    //             $(".meter-login-utm").attr("href", ctrllink + utmcontent + "_login");
    //             $(".meter-backHome").attr("href", homelink + utmcontent + "_Back_to_Homepage");
    //             $(".meter-register").attr("href", ctrllink + utmcontent + "_Create_an_Account");

    //             $("#privateMode").modal('show');

    //         } else {
    //             let url = APIBaseUrl + 'gettaxonomyaccess';
    //             let metercookie = JSON.parse($.cookie('artrevstring'));
    //             let taxData = { "cookie_value": metercookie.value, "taxonomy": taxVal, "article_type": type };
    //             if ($.cookie('customerToken') != undefined) {
    //                 let customerEmail = JSON.parse($.cookie('customerData'));
    //                 taxData = { "cookie_value": metercookie.value, "taxonomy": taxVal, "article_type": type, "email": customerEmail.email };
    //             }
    //             //Check if the content is free or hard paywall
    //             $.ajax({
    //                 url: url,
    //                 type: 'POST',
    //                 contentType: 'application/json',
    //                 data: JSON.stringify(taxData),
    //                 success: function (data) {
    //                     let res = $.parseJSON(data);
    //                     //res.access_type 1 is for free content and 2 is for hardpaywall and 3 is metering
    //                     console.log(res)
    //                     if (res.access_type == 1) {
    //                         removeMasking();
    //                         let metercookie = JSON.parse($.cookie('artrevstring'));

    //                         var cookieExpireMonth = new Date(metercookie.expire);
    //                         let oldmeterfreecount = $.cookie('mtrfreecount') == null ? 1 : Number($.cookie('mtrfreecount')) + 1;
    //                         utmcontent = "Desktop_Free_Banner";
    //                         if (window.innerWidth < 768)
    //                             utmcontent = "Mobile_Free_Banner";

    //                         //AB Testing code
    //                         let sublink_abtest = sublink + utmcontent;
    //                         if (res && res.count) {
    //                             if (parseInt(res.count) == 5) {
    //                                 sublink_abtest = sublink + utmcontent + "_A";
    //                             } else {
    //                                 sublink_abtest = sublink + utmcontent + "_B";
    //                             }
    //                         } else {
    //                             if (parseInt(res.free_articles_count) == 5) {
    //                                 sublink_abtest = sublink + utmcontent + "_A";
    //                             } else {
    //                                 sublink_abtest = sublink + utmcontent + "_B";
    //                             }
    //                         }

    //                         let utmId = utmcontent;

    //                         if (oldmeterfreecount <= 2) {
    //                             $.cookie('mtrfreecount', oldmeterfreecount, { path: '/', expires: cookieExpireMonth });
    //                             $note = $("#freemeternote");
    //                             $note.fadeIn();
    //                             utmcontent = utmcontent + "Full-" + oldmeterfreecount;
    //                             utmId = utmcontent;
    //                             if (parseInt(res.count) == 5) {
    //                                 utmId += "_A";
    //                             } else {
    //                                 utmId += "_B";
    //                             }
    //                             $("#freemeternote").addClass(utmId);
    //                             $(".gtm_freehalf_ID").attr("id", utmId);
    //                         } else {
    //                             $(".meter-small-num").html(res.free_content_banner_text);
    //                             $note = $("#smallmeternote");
    //                             $("#smallmeternote").addClass("Free_Banner");
    //                             utmId = utmcontent;

    //                             if (parseInt(res.count) == 5) {
    //                                 utmId += "_A";
    //                             } else {
    //                                 utmId += "_B";
    //                             }
    //                             $(".gtm_small_ID").attr("id", utmId);

    //                             $note.fadeIn();
    //                         }

    //                         $(".meter-href").attr("href", sublink_abtest);
    //                         $(".meter-login-utm").attr("href", ctrllink + utmcontent + "login");
    //                         $(".meter-reg-href").attr("href", reglink + utmcontent + "register");

    //                         $('.meterNoteclose').on('click', function () {
    //                             $note.fadeOut("slow");
    //                             $note = $("#smallmeternote");
    //                             $note.fadeIn();
    //                             $(".meterNoteopen").removeClass("display-none");
    //                             $(".meter-small-num").html(res.free_content_banner_text);
    //                         });
    //                         $('.meterNoteopen').on('click', function () {
    //                             $note.fadeOut("slow");
    //                             $note = $("#freemeternote");
    //                             $note.fadeIn();
    //                             $(".meterNoteopen").addClass("display-none");
    //                         });

    //                     } else if (res.access_type == 2) {
    //                         addMasking();
    //                         utmcontent = "Desktop_Hard_";
    //                         if (window.innerWidth < 768)
    //                             utmcontent = "Mobile_Hard_";

    //                         let utmId = utmcontent;


    //                         if (type == "review") {
    //                             $("#hardPaywallReviewMode").modal('show');
    //                             utmcontent += "ReviewBarrier"
    //                             utmId += "ReviewBarrier"
    //                             // AB Testing
    //                             if (parseInt(res.count) == 5) {
    //                                 utmId += "_A";
    //                             } else {
    //                                 utmId += "_B";
    //                             }
    //                             $("#hardPaywallReviewMode").addClass(utmId);
    //                             $(".gtm_reviewHard_ID").attr("id", utmId);
    //                         } else {
    //                             $(".paywall-text").html(res.hard_paywall_modal_text);
    //                             $("#hardPaywallMode").modal('show');
    //                             utmcontent += "Barrier"
    //                             utmId += "Barrier"
    //                             // AB Testing
    //                             if (parseInt(res.count) == 5) {
    //                                 utmId += "_A";
    //                             } else {
    //                                 utmId += "_B";
    //                             }
    //                             $("#hardPaywallMode").addClass(utmId);
    //                             $(".gtm_hard_ID").attr("id", utmId);
    //                         }

    //                         // AB Testing
    //                         let sublink_abtest = sublink + utmcontent;

    //                         if (res && res.count) {
    //                             if (parseInt(res.count) == 5) {
    //                                 sublink_abtest = sublink + utmcontent + "_A";
    //                             } else {
    //                                 sublink_abtest = sublink + utmcontent + "_B";
    //                             }
    //                         } else {
    //                             if (parseInt(res.free_articles_count) == 5) {
    //                                 sublink_abtest = sublink + utmcontent + "_A";
    //                             } else {
    //                                 sublink_abtest = sublink + utmcontent + "_B";
    //                             }
    //                         }

    //                         $(".full-meter-href").attr("href", sublink_abtest);
    //                         $(".meter-login-utm").attr("href", ctrllink + utmcontent + "_login");
    //                         $(".meter-backHome").attr("href", homelink + utmcontent + "_Back_to_Homepage");

    //                     } else {
    //                         //Call metering function if the user is not logged in and if the article is not hard paywalled or free.
    //                         metering(title, art_url, type);
    //                     }

    //                 },
    //                 error: function (request, textStatus, errorThrown) { }
    //             });
    //         }
    //     });

    // } else removeMasking();

}

function checkAccessIp(taxVal, title, art_url, type, isDigitalUser) {
    console.log("isDigitalUser====>", isDigitalUser);
    let subcriberStatus = $.cookie('customerIsSubscriber')
        ? JSON.parse($.cookie('customerIsSubscriber'))
        : false;
    taxVal = taxVal ? taxVal : "dummy";
    if (isDigitalUser == 1) {
        removeMasking();
    } else if (isDigitalUser == 0) {
        //User is not a subscriber
        if (!subcriberStatus) {
            $('#story_para_article').addClass('maskingHeight');
            $('#story_para_article_mask').addClass('maskingContent');
            $('#articleExcerptContent').removeClass('hide');

            isPrivateMode().then(function (isPrivate) {
                //Check if incognito or private mode and user is not logged in
                if ($.cookie('customerData') == undefined && isPrivate) {
                    addMasking();
                    utmcontent = "Desktop_Incognito";
                    if (window.innerWidth < 768)
                        utmcontent = "Mobile_Incognito";
                    let sublink_abtest = sublink + utmcontent;
                    // AB Testing 
                    if (parseInt(res.count) == 5) {
                        sublink_abtest = sublink + utmcontent + "_A";
                        unique_id = utmcontent + "_A";
                    } else {
                        sublink_abtest = sublink + utmcontent + "_B";
                        unique_id = utmcontent + "_B";
                    }
                    $(".gtm_private_ID").attr("id", utmcontent);
                    $(".full-meter-href").attr("href", sublink_abtest);
                    $(".full-meter-href").attr("id", unique_id);
                    $(".meter-login-utm").attr("href", ctrllink + utmcontent + "_login");
                    $(".meter-backHome").attr("href", homelink + utmcontent + "_Back_to_Homepage");
                    $(".meter-register").attr("href", ctrllink + utmcontent + "_Create_an_Account");

                    $("#privateMode").modal('show');

                } else {
                    let url = APIBaseUrl + 'gettaxonomyaccess';
                    let metercookie = JSON.parse($.cookie('artrevstring'));
                    let taxData = { "cookie_value": metercookie.value, "taxonomy": taxVal, "article_type": type };
                    if ($.cookie('customerToken') != undefined) {
                        let customerEmail = JSON.parse($.cookie('customerData'));
                        taxData = { "cookie_value": metercookie.value, "taxonomy": taxVal, "article_type": type, "email": customerEmail.email };
                    }
                    //Check if the content is free or hard paywall
                    $.ajax({
                        url: url,
                        type: 'POST',
                        contentType: 'application/json',
                        data: JSON.stringify(taxData),
                        success: function (data) {
                            let res = $.parseJSON(data);
                            //res.access_type 1 is for free content and 2 is for hardpaywall and 3 is metering
                            console.log(res)
                            if (res.access_type == 1) {
                                removeMasking();
                                let metercookie = JSON.parse($.cookie('artrevstring'));

                                var cookieExpireMonth = new Date(metercookie.expire);
                                let oldmeterfreecount = $.cookie('mtrfreecount') == null ? 1 : Number($.cookie('mtrfreecount')) + 1;
                                utmcontent = "Desktop_Free_Banner";
                                if (window.innerWidth < 768)
                                    utmcontent = "Mobile_Free_Banner";

                                //AB Testing code
                                let sublink_abtest = sublink + utmcontent;
                                if (res && res.count) {
                                    if (parseInt(res.count) == 5) {
                                        sublink_abtest = sublink + utmcontent + "_A";
                                        unique_id = utmcontent + "_A";
                                    } else {
                                        sublink_abtest = sublink + utmcontent + "_B";
                                        unique_id = utmcontent + "_B";
                                    }
                                } else {
                                    if (parseInt(res.free_articles_count) == 5) {
                                        sublink_abtest = sublink + utmcontent + "_A";
                                        unique_id = utmcontent + "_A";
                                    } else {
                                        sublink_abtest = sublink + utmcontent + "_B";
                                        unique_id = utmcontent + "_B";
                                    }
                                }

                                let utmId = utmcontent;

                                if (oldmeterfreecount <= 2) {
                                    $.cookie('mtrfreecount', oldmeterfreecount, { path: '/', expires: cookieExpireMonth });
                                    $note = $("#freemeternote");
                                    $note.fadeIn();
                                    utmcontent = utmcontent + "Full-" + oldmeterfreecount;
                                    utmId = utmcontent;
                                    if (parseInt(res.count) == 5) {
                                        utmId += "_A";
                                        unique_id = utmcontent + "_A";
                                    } else {
                                        utmId += "_B";
                                        unique_id = utmcontent + "_B";
                                    }
                                    $("#freemeternote").addClass(utmId);
                                    $(".gtm_freehalf_ID").attr("id", utmId);
                                } else {
                                    $(".meter-small-num").html(res.free_content_banner_text);
                                    $note = $("#smallmeternote");
                                    $("#smallmeternote").addClass("Free_Banner");
                                    utmId = utmcontent;

                                    if (parseInt(res.count) == 5) {
                                        utmId += "_A";
                                        unique_id = utmcontent + "_A";
                                    } else {
                                        utmId += "_B";
                                        unique_id = utmcontent + "_B";
                                    }
                                    $(".gtm_small_ID").attr("id", utmId);

                                    $note.fadeIn();
                                }

                                $(".meter-href").attr("href", sublink_abtest);
                                $(".meter-href").attr("id", unique_id);
                                $(".meter-login-utm").attr("href", ctrllink + utmcontent + "login");
                                $(".meter-reg-href").attr("href", reglink + utmcontent + "register");

                                $('.meterNoteclose').on('click', function () {
                                    $note.fadeOut("slow");
                                    $note = $("#smallmeternote");
                                    $note.fadeIn();
                                    $(".meterNoteopen").removeClass("display-none");
                                    $(".meter-small-num").html(res.free_content_banner_text);
                                });
                                $('.meterNoteopen').on('click', function () {
                                    $note.fadeOut("slow");
                                    $note = $("#freemeternote");
                                    $note.fadeIn();
                                    $(".meterNoteopen").addClass("display-none");
                                });

                            } else if (res.access_type == 2) {
                                addMasking();
                                utmcontent = "Desktop_Hard_";
                                if (window.innerWidth < 768)
                                    utmcontent = "Mobile_Hard_";

                                let utmId = utmcontent;


                                if (type == "review") {
                                    $("#hardPaywallReviewMode").modal('show');
                                    utmcontent += "ReviewBarrier"
                                    utmId += "ReviewBarrier"
                                    // AB Testing
                                    if (parseInt(res.count) == 5) {
                                        utmId += "_A";
                                        unique_id = utmcontent + "_A";
                                    } else {
                                        utmId += "_B";
                                        unique_id = utmcontent + "_B";
                                    }
                                    $("#hardPaywallReviewMode").addClass(utmId);
                                    $(".gtm_reviewHard_ID").attr("id", utmId);
                                } else {
                                    $(".paywall-text").html(res.hard_paywall_modal_text);
                                    $("#hardPaywallMode").modal('show');
                                    utmcontent += "Barrier"
                                    utmId += "Barrier"
                                    // AB Testing
                                    if (parseInt(res.count) == 5) {
                                        utmId += "_A";
                                        unique_id = utmcontent + "_A";
                                    } else {
                                        utmId += "_B";
                                        unique_id = utmcontent + "_B";
                                    }
                                    $("#hardPaywallMode").addClass(utmId);
                                    $(".gtm_hard_ID").attr("id", utmId);
                                }

                                // AB Testing
                                let sublink_abtest = sublink + utmcontent;

                                if (res && res.count) {
                                    if (parseInt(res.count) == 5) {
                                        sublink_abtest = sublink + utmcontent + "_A";
                                        unique_id = utmcontent + "_A";
                                    } else {
                                        sublink_abtest = sublink + utmcontent + "_B";
                                        unique_id = utmcontent + "_B";
                                    }
                                } else {
                                    if (parseInt(res.free_articles_count) == 5) {
                                        sublink_abtest = sublink + utmcontent + "_A";
                                        unique_id = utmcontent + "_A";
                                    } else {
                                        sublink_abtest = sublink + utmcontent + "_B";
                                        unique_id = utmcontent + "_B";
                                    }
                                }

                                $(".full-meter-href").attr("href", sublink_abtest);
                                $(".full-meter-href").attr("id", unique_id);
                                $(".meter-login-utm").attr("href", ctrllink + utmcontent + "_login");
                                $(".meter-backHome").attr("href", homelink + utmcontent + "_Back_to_Homepage");

                            } else {
                                //Call metering function if the user is not logged in and if the article is not hard paywalled or free.
                                metering(title, art_url, type);
                            }

                        },
                        error: function (request, textStatus, errorThrown) { }
                    });
                }
            });

        } else removeMasking();
    }
}

//Get the article/review limit, user count and check if user can view article
function metering(title, art_url, type) {
    let metercookie = JSON.parse($.cookie('artrevstring'));

    let data = { "cookie_value": metercookie.value, "title": title, "article_url": art_url, "type": type, "cookie_expiry": metercookie.expire };
    if ($.cookie('customerToken') != undefined) {
        let customerEmail = JSON.parse($.cookie('customerData'));
        data = { "cookie_value": metercookie.value, "title": title, "article_url": art_url, "type": type, "cookie_expiry": metercookie.expire, "email": customerEmail.email };
    }

    // console.log(data);
    let url = APIBaseUrl + 'getmeteredcontentdetails';
    $.ajax({
        url: url,
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify(data),
        success: function (data) {
            let res = $.parseJSON(data);
            let arCount = res.count - res.viewed_count;
            console.log("Metering======>", res);
            //If allow article to meter.
            // if (res.free_articles_reviews_limit_id == 5) {
            if (parseInt(res.count) == 5) {
                if (res.allow_article == 1) {
                    removeMasking();
                    let collapseText = res.collapsed_banner_text;
                    if (arCount != 1)
                        type = type + "s";

                    // if (parseInt(res.count) == 5) {
                    //     if (arCount == 1)
                    //         collapseText = "You have only <strong>1 free article</strong> remaining for this month. Don't miss out on our award-winning coverage.";
                    //     else if (arCount == 0)
                    //         collapseText = "You are reading your <strong>last free article</strong> for this month. Subscribe now to unlock unlimited access.";
                    //     else
                    //         collapseText = collapseText.replace("numvar", arCount).replace("artType", type);
                    // }


                    if (arCount == Math.floor(res.count / 2) && res.half_free_content_exhausted_modal_enabled == 1) {
                        utmcontent = "Desktop_Half_Barrier";
                        if (window.innerWidth < 768)
                            utmcontent = "Mobile_Half_Barrier";

                        $note = $("#meternote");
                        $note.fadeIn();
                        $(".meter-text").html(res.half_free_content_exhausted_modal_text);
                        // $("#smallmeternote").removeAttr('class');
                        $(".gtm_half_ID").attr("id", utmcontent + "_A");
                        $("#meternote").addClass(utmcontent + "_A");
                    } else {
                        $note = $("#smallmeternote");
                        $note.fadeIn();
                        utmcontent = utmcontent + res.viewed_count;

                        $(".meter-small-num").html(collapseText);
                        $("#smallmeternote").removeAttr('class');
                        $(".gtm_small_ID").attr("id", utmcontent + "_A");
                        $("#smallmeternote").addClass(utmcontent + "_A");
                    }

                    $('.meterNoteclose').on('click', function () {
                        $note.fadeOut("slow");
                        $note = $("#smallmeternote");
                        $note.fadeIn();
                        $(".meterNoteopen").removeClass("display-none");
                        $(".meter-small-num").html(collapseText);
                    });
                    $('.meterNoteopen').on('click', function () {
                        $note.fadeOut("slow");
                        $note = $("#meternote");
                        $note.fadeIn();
                        $(".meterNoteopen").addClass("display-none");
                        $(".meter-text").html(res.half_free_content_exhausted_modal_text);

                        $("#meternote").addClass(utmcontent + "_A");
                    });

                    let sublink_abtest = sublink + utmcontent + "_A";
                    // AB Testing
                    if (parseInt(res.count) == 5) {
                        sublink_abtest = sublink + utmcontent + "_A";
                        unique_id = utmcontent + "_A";
                    } else {
                        sublink_abtest = sublink + utmcontent + "_B";
                        unique_id = utmcontent + "_B";
                    }
                    $(".meter-href").attr("href", sublink_abtest);
                    $(".meter-href").attr("id", unique_id);
                    $(".meter-login-utm").attr("href", ctrllink + utmcontent + "_login" + "_A");
                    $(".meter-backHome").attr("href", homelink + utmcontent + "_Back_to_Homepage" + "_A");

                } else if (res.allow_article == 0) {
                    //Free articles have been exhausted
                    addMasking();
                    utmcontent = "Desktop_Full_Barrier";
                    if (window.innerWidth < 768)
                        utmcontent = "Mobile_Full_Barrier";

                    let sublink_abtest = sublink + utmcontent + "_A";
                    // AB Testing
                    if (parseInt(res.count) == 5) {
                        sublink_abtest = sublink + utmcontent + "_A";
                        unique_id = utmcontent + "_A";
                    } else {
                        sublink_abtest = sublink + utmcontent + "_B";
                        unique_id = utmcontent + "_B";
                    }
                    $(".full-meter-href").attr("href", sublink_abtest);
                    $(".full-meter-href").attr("id", unique_id);
                    $(".meter-login-utm").attr("href", ctrllink + utmcontent + "_login" + "_A");
                    $(".meter-backHome").attr("href", homelink + utmcontent + "_Back_to_Homepage" + "_A");
                    $(".paywall-text").html(res.free_content_exhausted_modal_text);
                    $("#meterExhaustMode").modal('show');
                    $("#meterExhaustMode").addClass(utmcontent + "_A");
                    $(".gtm_exhaust_ID").attr("id", utmcontent + "_A");
                }
            } else {

                if (res.allow_article == 1) {
                    removeMasking();
                    let collapseText = res.collapsed_banner_text;

                    // if (res.apply_to_all_countdown_meters == 1 ||
                    //     (res.apply_to_custom_countdown_meters == 1 &&
                    //         (res.viewed_count == 1 || res.viewed_count == 3))) {

                    if (res.apply_to_all_countdown_meters == 1 ||
                        (res.apply_to_custom_countdown_meters == 1 &&
                            (res.viewed_count == 3))) {

                        if (res.viewed_count == 3) {
                            utmcontent = "Desktop_Half_Barrier";
                            if (window.innerWidth < 768)
                                utmcontent = "Mobile_Half_Barrier";
                        } else {
                            utmcontent = utmcontent + res.viewed_count;
                        }

                        $note = $("#meternote");
                        $note.fadeIn();
                        // $(".meter-text").html(res.expanded_banner_text);
                        $(".meter-text").html(res.half_free_content_exhausted_modal_text);
                        // $("#smallmeternote").removeAttr('class');
                        $(".gtm_half_ID").attr("id", utmcontent + "_B");
                        $("#meternote").addClass(utmcontent + "_B");
                    } else {
                        $note = $("#smallmeternote");
                        $note.fadeIn();
                        utmcontent = utmcontent + res.viewed_count;

                        //$(".meterNoteopen").removeClass("display-none");
                        $(".meter-small-num").html(collapseText);
                        $("#smallmeternote").removeAttr('class');
                        $(".gtm_small_ID").attr("id", utmcontent + "_B");
                        $("#smallmeternote").addClass(utmcontent + "_B");
                    }

                    $('.meterNoteclose').on('click', function () {
                        $note.fadeOut("slow");
                        $note = $("#smallmeternote");
                        $note.fadeIn();
                        $(".meterNoteopen").removeClass("display-none");
                        $(".meter-small-num").html(collapseText);
                        $("#smallmeternote").addClass(utmcontent + "_B");
                    });
                    $('.meterNoteopen').on('click', function () {
                        $note.fadeOut("slow");
                        $note = $("#meternote");
                        $note.fadeIn();
                        $(".meterNoteopen").addClass("display-none");
                        $(".meter-text").html(res.half_free_content_exhausted_modal_text);

                        $("#meternote").addClass(utmcontent + "_B");
                    });

                    let sublink_abtest = sublink + utmcontent + "_B";
                    // AB Testing
                    if (parseInt(res.count) == 5) {
                        sublink_abtest = sublink + utmcontent + "_A";
                        unique_id = utmcontent + "_A";
                    } else {
                        sublink_abtest = sublink + utmcontent + "_B";
                        unique_id = utmcontent + "_B";
                    }
                    $(".meter-href").attr("href", sublink_abtest);
                    $(".meter-href").attr("id", unique_id);
                    $(".meter-login-utm").attr("href", ctrllink + utmcontent + "_login" + "_B");
                    $(".meter-backHome").attr("href", homelink + utmcontent + "_Back_to_Homepage" + "_B");
                } else {

                    //Free articles have been exhausted
                    addMasking();
                    utmcontent = "Desktop_Full_Barrier";
                    if (window.innerWidth < 768)
                        utmcontent = "Mobile_Full_Barrier";

                    let sublink_abtest = sublink + utmcontent + "_B";
                    // AB Testing
                    if (parseInt(res.count) == 5) {
                        sublink_abtest = sublink + utmcontent + "_A";
                        unique_id = utmcontent + "_A";
                    } else {
                        sublink_abtest = sublink + utmcontent + "_B";
                        unique_id = utmcontent + "_B";
                    }
                    $(".full-meter-href").attr("href", sublink_abtest);
                    $(".full-meter-href").attr("id", unique_id);
                    $(".meter-login-utm").attr("href", ctrllink + utmcontent + "_login" + "_B");
                    $(".meter-backHome").attr("href", homelink + utmcontent + "_Back_to_Homepage" + "_B");
                    $(".paywall-text").html(res.free_content_exhausted_modal_text);
                    $("#meterExhaustMode").modal('show');
                    $("#meterExhaustMode").addClass(utmcontent + "_B");
                    $(".gtm_exhaust_ID").attr("id", utmcontent + "_B");

                }
            }

        },
        error: function (request, textStatus, errorThrown) { }
    });
}

function accessTax(taxVal, title, art_url, type) {
    if ($.cookie('artrevstring') == undefined) {
        artrevstringGen(taxVal, title, art_url, type);
    } else {
        checkAccess(taxVal, title, art_url, type);
    }
}