var SAMPLE_PERCENT = 10;
var COOKIE_NAME = 'agsample';

function inTrackingSample() {
    var host = window.location.host;
    // do we need to do fancy sampling magic?
    if (host.indexOf('yahoo') != -1 || host.indexOf('bmamess') != -1 || host.indexOf('mypersonalexpression') != -1) {
        var allcookies = document.cookie;
        var pos = allcookies.indexOf(COOKIE_NAME);
        // if we find the cookie then use it
        if (pos != -1) {
            var start = pos + COOKIE_NAME.length + 1;
            var end = allcookies.indexOf(';', start);
            if (end == -1) end = allcookies.length;
            var num = allcookies.substring(start, end);
            //alert('found it; ' + num + '; ' + num % SAMPLE_PERCENT);
        }
        else {
            // set the sample group number in the cookie to be used later
            var num = Math.round(Math.random() * 100);
            //alert('not found; ' + num +'; ' + num % SAMPLE_PERCENT);
            var nextyear = new Date();
            nextyear.setFullYear(nextyear.getFullYear() + 1);
            
            var domain = host.substr(host.indexOf('.'), host.length);
            if(domain.indexOf('bmamess') != -1) {
                domain = '.bmamessenger.com';
            } else if(domain.indexOf('mypersonalexpression') != -1) {
                domain = '.mypersonalexpression.com';
            }
            document.cookie = COOKIE_NAME + '=' + num + '; expires='
                    + nextyear.toGMTString() + '; domain=' + domain
                    + '; path=/';
        }
        if (num < SAMPLE_PERCENT) {
            return true;
        } else {
            return false;
        }
    }
    else {
        return true;
    }
}
