﻿var ShowEmailSignUpPopUp = false;

$(document).ready(function() {
    var hitCountCookie = $.cookie('hitcount');
    var sourceCode = '';//CookieCutter($.cookie('budk'), 'sourcecode', '&', null);
    
    try {
        if(document.URL.toLowerCase().indexOf('comloginc.net') != -1) {
            sourceCode = CookieCutter($.cookie('budk'), 'sourcecode', '&', null);
        } else {
            if(document.URL.toLowerCase().indexOf('wholesale') != -1) {
                sourceCode = CookieCutter($.cookie('budkwholesale'), 'sourcecode', '&', null);
            } else if(document.URL.toLowerCase().indexOf('budk') != -1) {
                sourceCode = CookieCutter($.cookie('budkCLI'), 'sourcecode', '&', null);
            } else if(document.URL.toLowerCase().indexOf('matthews') != -1) {
                sourceCode = CookieCutter($.cookie('matthewscutlery'), 'sourcecode', '&', null);
            } else if(document.URL.toLowerCase().indexOf('kennesaw') != -1) {
                sourceCode = CookieCutter($.cookie('kennesawcutlery'), 'sourcecode', '&', null);
            }
        }    
    }
    catch(e) {}
    
    if(IsUserAuthenticated == 'False') {
        if(hitCountCookie == null) {
            $.cookie('hitcount', 'count=1&didshow=false', {path:'/'});
        } else {
            var count = CookieCutter(hitCountCookie, 'count', '&', null);            
            var didshow = CookieCutter(hitCountCookie, 'didshow', '&', null);
            count++;
            //alert(count + '\r\n' + didshow);
            if(document.URL.indexOf('cart.aspx') == -1 && document.URL.indexOf('checkout.aspx') == -1) {
                if(parseInt(count) >= parseInt(EmailSignUpPopUpDepth) && didshow == 'false' && EmailSignUpPopUp == '1') {
                    //array of source codes to trigger popup
                    var popupSourceCodes = EmailSignUpPopUpSourceCode.split('|');
                    var hasPopUpSourceCode = false;
                    //see if user has a trigger source code
                    for(var i = 0; i < popupSourceCodes.length; i++) {
                        if(sourceCode == popupSourceCodes[i]) {
                            hasPopUpSourceCode = true;
                            break;
                        }
                    }
                    if(sourceCode == '' || hasPopUpSourceCode) {
                        didshow = 'true';
                        ShowEmailSignUpPopUp = true;
                        //alert('hc');
                        var t = setTimeout(ShowEmailSignUp, EmailSignUpPopUpDelay);
                    }
                }
            }
            
            $.cookie('hitcount', 'count=' + count + '&didshow=' + didshow, {path:'/'});
        }
    }
});
