//copyright 2017-2025 by Ron Sell at https://sparkmembership.com/

document.abundanceLocationID = 2065;

document.igniteInspired = 0;



function docLoaded(f) {
    /in/.test(document.readyState) ? setTimeout('docLoaded(' + f + ')', 9) : f();
}

window.ModalEffectsInitialized = false;
var uplevelFormID;
var upID = "";

function loadCSS(url) {

    var link = document.createElement("link");
    link.href = url;
    link.type = "text/css";
    link.rel = "stylesheet";
    link.media = "screen,print";

    document.getElementsByTagName("head")[0].appendChild(link);
}

//This will get our querystring value if a two part form
function getQueryString(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}
//set cookie
function setCookie(cookieName, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var cookieValue = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    cookieValue += "; path=/";
    document.cookie = cookieName + "=" + cookieValue;
}
//get cookie value
function getCookie(cookieName) {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == cookieName) {
            return unescape(y);
        } else {
            return "";
        }
    }
}

String.prototype.replaceAll = function (find, replace) {
    var str = this;
    return str.replace(new RegExp(find.toString().replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), replace);
};

//for webforms in an iframe
window.addEventListener('message', function (e) {
    //http://benvinegar.github.io/seamless-talk/#/33
    var $iframe = jQuery('#uplevel-form-iframe');

    var eventName = e.data[0];
    var data = e.data[1];

    switch (eventName) {
        case 'setHeight':
            $iframe.height(data);
            break;
    }
}, false);



function GetUuid() {
    var newuuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : r & 0x3 | 0x8;
        return v.toString(16);
    });

    return newuuid;
}


function bindInlineForms() {
    //binds elements on the page with uplevel inline forms
    jQuery(".uplevelInlineForm").each(function () {
        //look for inline forms
        var specificUplevelForm = jQuery(this);
        var webformID = jQuery(this).attr("data-uplevel-inlineform");
        uplevelFormID = webformID;

        var ajaxdata = 'action=getWebform&wfid=' + webformID;

        jQuery.ajax({
            type: "POST",
            async: false,
            url: "https://app.sparkmembership.com/wf/inlineWebforms.ashx",
            data: ajaxdata,
            dataType: "html",
            error: function (response) {
                console.log("uplevel bindInlineForms() Issue: " + response);
                return false;
            },
            success: function (response) {
                specificUplevelForm.html(response);

                var uplevelFormContainerWidth = specificUplevelForm.width();

                if (uplevelFormContainerWidth < 666 && uplevelFormContainerWidth > 545) {
                    specificUplevelForm.find(".ab-optin-container").removeClass("lt665");
                    specificUplevelForm.find(".ab-optin-container").removeClass("lt545");
                    specificUplevelForm.find(".ab-optin-container").addClass("lt665");
                }

                if (uplevelFormContainerWidth < 546) {
                    specificUplevelForm.find(".ab-optin-container").removeClass("lt665");
                    specificUplevelForm.find(".ab-optin-container").removeClass("lt545");
                    specificUplevelForm.find(".ab-optin-container").addClass("lt545");
                }
                try {
                    specificUplevelForm.find('form').find("#ab_emailaddress").attr("type", "text")
                } catch { }

                specificUplevelForm.find('form').submit(function () {

                    try {
                        var mn = specificUplevelForm.find('form').find("#ab_mobile").val()

                        if (mn.length < 7) {
                            var isvalidMobile = false;
                            if (!isvalidMobile) {

                                alert("Invalid Mobile Number.");
                                return false;
                            }
                        }
                    } catch {
                    }
                    try {
                        var em = specificUplevelForm.find('form').find("#ab_emailaddress").val()
                        var emailReg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
                        isValidEm = emailReg.test(em);
                        if (!isValidEm) {

                            alert("Invalid Email Address.");
                            return false;
                        }
                    } catch {
                    }

                });

            }
        });
    });
}

function bindWebForms() {
    //binds elements on the page with uplevel web forms
    jQuery(".uplevelWebForm").each(function () {
        //look for webforms
        var formID = jQuery(this).attr("data-uplevel-webform");

        jQuery(this).html('<iframe id="uplevel-form-iframe" class="uplevel-iframe-form" src="https://sparkpages.io/forms/?frm=' + formID + '&contactID=' + upID + '" height="600" frameborder="0" border="0" cellspacing="0" style="border-style: none; height: 600px; width: 100%"></iframe>');

    });
}

var popupformsCompleted = [];
var popupformsCompletedV2 = [];

function bindPopUpForms() {
    var hasWebforms = false;
    var hasWebforms_v2 = false;

    var bodyTags = document.getElementsByTagName('body');
    var bodyTag = bodyTags[0];

    jQuery(".uplevelBox-trigger").each(function () {
        //this adds all the popup webforms onto the page.
        var webformID = jQuery(this).attr("data-webformid");

        if (popupformsCompleted.indexOf(webformID) === -1) { //do not want to handle the same webform more than once.
            var customButtonColor = jQuery(this).attr("data-customButtonColor");

            var script = document.createElement('script');
            script.type = "text/javascript";
            script.src = '//app.sparkmembership.com/wf/UplevelBox.ashx?lid=' + document.abundanceLocationID + '&wfid=' + webformID + '&customButtonColor=' + customButtonColor;
            document.getElementsByTagName('body')[0].appendChild(script);

            hasWebforms = true;

            popupformsCompleted.push(webformID);
        }
    });

    jQuery(".uplevelBox2-trigger").each(function () {
        var webformID = jQuery(this).attr("data-webformid");

        if (popupformsCompletedV2.indexOf(webformID) === -1) { //do not want to handle the same webform more than once.
            var customButtonColor = jQuery(this).attr("data-customButtonColor");

            var script = document.createElement('script');
            script.type = "text/javascript";
            script.src = '//app.sparkmembership.com/wf/UplevelBox.ashx?lid=' + document.abundanceLocationID + '&wfid=' + webformID + '&customButtonColor=' + customButtonColor;
            document.getElementsByTagName('body')[0].appendChild(script);

            hasWebforms_v2 = true;

            popupformsCompletedV2.push(webformID);
        }
    });

    jQuery("a[href^='#sparkPopup2-']").each(function () {
        var webformID = jQuery(this).attr("href");
        webformID = webformID.replace("sparkPopup2-", "");

        if (popupformsCompletedV2.indexOf(webformID) === -1) { //do not want to handle the same webform more than once.


            var script = document.createElement('script');
            script.type = "text/javascript";
            script.src = '//app.sparkmembership.com/wf/UplevelBox.ashx?lid=' + document.abundanceLocationID + '&wfid=' + webformID;
            document.getElementsByTagName('body')[0].appendChild(script);

            hasWebforms_v2 = true;

            popupformsCompletedV2.push(webformID);
        }
    });

    if (hasWebforms) {
        //only want to call these once
        loadCSS('//app.sparkmembership.com/landingPageResources/uplevelButtons.min.css');

        var script = document.createElement('script');
        script.type = "text/javascript";
        script.src = '//app.sparkmembership.com/wf/UplevelBox.js.ashx?lid=' + document.abundanceLocationID;
        document.getElementsByTagName('body')[0].appendChild(script);

    }

    if (hasWebforms_v2) {
        //only want to call these once
        loadCSS('//app.sparkmembership.com/landingPageResources/uplevelButtons.min.css');
        loadCSS('//app.sparkmembership.com/plugins/uplevelbox2/uplevelbox2.css');

        var script = document.createElement('script');
        script.type = "text/javascript";
        script.src = '//app.sparkmembership.com/wf/UplevelBox.js.ashx?lid=' + document.abundanceLocationID + '&v=2';
        document.getElementsByTagName('body')[0].appendChild(script);
        //attaching the UplevelBox2 to the link is handled in UplevelBox2.js (very end)

    }

    setTimeout(function () {
        var sparkcoupon = getQueryString('sparkcoupon');
        if (sparkcoupon.length === 0) {
            sparkcoupon = getCookie('sparkcoupon');
        }
        jQuery("input[id^=ab_sparkcoupon]").val(sparkcoupon);
        var utm_source = getQueryString('utm_source');
        if (utm_source.length === 0) {
            utm_source = getCookie('utm_source');
        }
        jQuery("input[id^=ab_utm_source]").val(utm_source);
        var utm_medium = getQueryString('utm_medium');
        if (utm_medium.length === 0) {
            utm_medium = getCookie('utm_medium');
        }
        jQuery("input[id^=ab_utm_medium]").val(utm_medium);
        var utm_campaign = getQueryString('utm_campaign');
        if (utm_campaign.length === 0) {
            utm_campaign = getCookie('utm_campaign');
        }
        jQuery("input[id^=ab_utm_campaign]").val(utm_campaign);
        var utm_content = getQueryString('utm_content');
        if (utm_content.length === 0) {
            utm_content = getCookie('utm_content');
        }
        jQuery("input[id^=ab_utm_content]").val(utm_content);
        var utm_term = getQueryString('utm_term');
        if (utm_term.length === 0) {
            utm_term = getCookie('utm_term');
        }
        jQuery("input[id^=ab_utm_term]").val(utm_term);

        setCookie('sparkcoupon', sparkcoupon, 960);
        setCookie('utm_source', utm_source, 960);
        setCookie('utm_medium', utm_medium, 960);
        setCookie('utm_campaign', utm_campaign, 960);
        setCookie('utm_content', utm_content, 960);
        setCookie('utm_term', utm_term, 960);

        var sparkCampaignID = getQueryString('sparkCampaignID');
        jQuery("input[id^=ab_sparkCampaignID]").val(sparkCampaignID);
    }, 2000);
}



function deferInlineForms() {
    if (typeof jQuery == 'undefined') {
        setTimeout(function () { deferInlineForms(); }, 50);
    } else {
        bindWebForms();
        bindInlineForms();
        bindPopUpForms();
    }
}

//how many times have they visited
var howManyVisits = getCookie("howManyVisits");
var intHowMany = parseFloat(howManyVisits);
if (isNaN(intHowMany)) {
    intHowMany = 1;
}
else {
    intHowMany++;
}
setCookie('howManyVisits', intHowMany, 960);

//the rest is only done after full page has been loaded
docLoaded(function () {

    //upID = contactID
    //uuid = uinique user id assigned, mostly because we don't have a contactID (yet)

    var affiliateCode = '' + getQueryString("aff");
    if (affiliateCode.length === 0) {
        affiliateCode = '' + getCookie("aff");
    }

    if (affiliateCode.length > 0) {
        setCookie("aff", affiliateCode, 365);
        var affiliateBox = document.getElementsByName('ab_affiliateCode');
        if (affiliateBox.length > 0) {
            document.getElementsByName('ab_affiliateCode')[0].value = affiliateCode;
        }
    }

    var bodyTags = document.getElementsByTagName('body');
    var bodyTag = bodyTags[0];
    var referrer = encodeURIComponent(document.referrer);
    var thisPage = encodeURIComponent(window.location.href);
    var pageTitle = encodeURIComponent(document.title);
    var landingPageID = "";
    var salesPageID = "";

    try {
        landingPageID = document.getElementById('hLandingPageID').value;
    } catch (e) {
        landingPageID = "";
    }

    try {
        salesPageID = document.getElementById('hSalesPageID').value;
    } catch (e) {
        salesPageID = "";
    }


    var uuid = "";
    try {
        upID = getQueryString("upID");
    } catch (e) {

    }
    if (upID.length === 0) {
        //let's see if it is in the c querystring
        try {
            upID = getQueryString("c"); //this will be a shrunk number
        } catch (e) {

        }
    }
    try {
        uuid = getCookie('uuid');
    } catch (e) {

    }
    try {
        if (typeof uuid === 'undefined') {
            uuid = GetUuid();
        }
        if (uuid.length === 0) {
            uuid = GetUuid();
        }
    } catch (e) {

    }

    if (upID.length === 0) {
        //not in qs? get it from cookie
        upID = getCookie('upID');
    }
    if (typeof upID === "undefined") {
        //use a uuid function
        if (typeof uuid === 'undefined') {
            uuid = GetUuid();
        }
        if (uuid.length === 0) {
            uuid = GetUuid();
        }
        upID = uuid;
    }
    if (upID.length === 0) {
        //use a uuid function
        if (typeof uuid === 'undefined') {
            uuid = GetUuid();
        }
        if (uuid.length === 0) {
            uuid = GetUuid();
        }
        upID = uuid;
    }
    //It's either a page with a form or it's not..
    var ab_fid;
    try {
        ab_fid = document.getElementsByName("ab_fid");
    } catch (e) {
        ab_fid = '';
    }


    var sparkcoupon = getQueryString('sparkcoupon');
    var utm_source = getQueryString('utm_source');
    var utm_medium = getQueryString('utm_medium');
    var utm_campaign = getQueryString('utm_campaign');
    var utm_term = getQueryString('utm_term');
    var utm_content = getQueryString('utm_content');
    var sparkCampaignID = getQueryString('sparkCampaignID');

    try {
        if (sparkcoupon.length === 0) {
            //maybe it is in cookie
            sparkcoupon = getCookie('sparkcoupon');
        } else {
            setCookie('sparkcoupon', sparkcoupon, 365);
        }
    } catch (e) {
        sparkcoupon = '';
    }

    try {
        if (utm_source.length === 0) {
            //maybe it is in cookie
            utm_source = getCookie('utm_source');
        } else {
            setCookie('utm_source', utm_source, 365);
        }
    } catch (e) {
        utm_source = '';
    }

    try {
        if (utm_medium.length === 0) {
            //maybe it is in cookie
            utm_medium = getCookie('utm_medium');
        } else {
            setCookie('utm_medium', utm_medium, 365);
        }
    } catch (e) {
        utm_medium = '';
    }

    try {
        if (utm_campaign.length === 0) {
            //maybe it is in cookie
            utm_campaign = getCookie('utm_campaign');
        } else {
            setCookie('utm_campaign', utm_campaign, 365);
        }
    } catch (e) {
        utm_campaign = '';
    }

    try {
        if (sparkCampaignID.length === 0) {
            //maybe it is in cookie
            sparkCampaignID = getCookie('sparkCampaignID');
        } else {
            setCookie('sparkCampaignID', sparkCampaignID, 365);
        }
    } catch (e) {
        sparkCampaignID = '';
    }



    if (ab_fid.length != 0) {
        //It's a form
        //set text box with abid value from qs (two step forms)
        //var contactID = abid;
        //var abidTextbox = document.getElementsByName("abid")
        //abidTextbox.value = abid;

        var formID = ab_fid[0].value;

        bodyTag.insertAdjacentHTML("beforeend", '<img width=\"0\" height = \"0\" alt= \"\" style=\"display:block\" class=\"sparkTracker\" src=\"https://app.sparkmembership.com/wf/userTrack.ashx?upID=' + upID + '&uuid=' + uuid + '&LID=' + document.abundanceLocationID + '&referrer=' + referrer + '&thisPage=' + thisPage + '&FORM=' + formID + '&pageTitle=' + pageTitle + '&landingPageID=' + landingPageID + '&salesPageID=' + salesPageID + '&affiliateCode=' + affiliateCode + '&actionTaken=WEBFORMHIT&timeshere=' + intHowMany + '&sparkcoupon=' + sparkcoupon + '&utm_source=' + utm_source + '&utm_medium=' + utm_medium + '&utm_campaign=' + utm_campaign + '&utm_content=' + utm_content + '&utm_term=' + utm_term + '&igniteInspired=' + document.igniteInspired + '&sparkCampaignID=' + sparkCampaignID + '\" \/">');


        //set upID cookie
        setCookie('upID', upID, 960);
        setCookie('uuid', uuid, 960);


    } else {
        //it's a page
        bodyTag.insertAdjacentHTML("beforeend", '<img width=\"0\" height = \"0\" alt= \"\" style=\"display:block\" class=\"sparkTracker\" src=\"https://app.sparkmembership.com/wf/userTrack.ashx?upID=' + upID + '&uuid=' + uuid + '&LID=' + document.abundanceLocationID + '&referrer=' + referrer + '&thisPage=' + thisPage + '&pageTitle=' + pageTitle + '&landingPageID=' + landingPageID + '&salesPageID=' + salesPageID + '&affiliateCode=' + affiliateCode + '&actionTaken=PAGEHIT&timeshere=' + intHowMany + '&sparkcoupon=' + sparkcoupon + '&utm_source=' + utm_source + '&utm_medium=' + utm_medium + '&utm_campaign=' + utm_campaign + '&utm_content=' + utm_content + '&utm_term=' + utm_term + '&igniteInspired=' + document.igniteInspired + '&sparkCampaignID=' + sparkCampaignID + '\">');

        setCookie('upID', upID, 960);
        setCookie('uuid', uuid, 960);
    }

    setTimeout(function () {
        try {
            document.getElementsByName("ab_upID")[0].value = upID;
        } catch (errorX) {

        }

        try {
            document.getElementsByName("ab_uuid")[0].value = uuid;
        } catch (errorY) {

        }

        try {
            var affiliateBox2 = document.getElementsByName('ab_affiliateCode');
            console.log(affiliateBox2.length);
            if (affiliateBox2.length > 0) {
                document.getElementsByName('ab_affiliateCode')[0].value = affiliateCode;
            }
        } catch (errorZ) {

        }



    }, 3000);


    if (typeof jQuery == 'undefined') {
        //if jQuery is not installed, install it.
        console.log("uplevel: jQuery Not Installed");
        var script = document.createElement('script');
        script.type = "text/javascript";
        script.src = "//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js";
        document.getElementsByTagName('head')[0].appendChild(script);

        deferInlineForms(); //takes a little for jQuery to load, we will defer till it's loaded
    } else {
        bindInlineForms();
        bindPopUpForms();
        bindWebForms();
    }
});