 function checkReferrer ()
 {
    function init()
    {
        var cObj;
        var cValidDays = 30;
        if (document.cookie) {
            var cName  = 'billigermietwagen';
            var cStart = 0;
            var cEnd   = 0;
            var cJson   = ''
            var cSource = 0;
            var cCookie = document.cookie
            var cStart = cCookie.indexOf(cName+"=");
            if (cStart != -1) {
                cStart = cStart+cName.length + 1;
                cEnd   = cCookie.indexOf(";", cStart);
                if (cEnd == -1) {
                    cEnd = cCookie.length;
                }
                cJson = cCookie.substring(cStart, cEnd);
            }
            cJson = new String(decodeURI(decodeURI(cJson)));
            cJson = cJson.replace(/%3A/g, ':');
            cJson = cJson.replace(/%2C/g, ',');
            try {
                eval('cObj = '+ cJson);
            } catch (f) {
                cObj = new Array();
            }

            var cLocation = location.href;
            if (cLocation.indexOf('source=') !=-1) {
                cStart = cLocation.indexOf("?");
                if (cStart == -1) {
                    return;
                }
                cLocation = cLocation.substring(cStart +1, cLocation.length);
                arr = cLocation.split('&');
                var source = 0;
                for (var i=0; i<arr.length; ++i) {
                    if (arr[i].indexOf('source=') !=-1) {
                        cSource = arr[i].substring(7, arr[i].length);
                        break;
                    }
                }
            } else if (document.referrer != '') {
                if(cObj.source != "google") {
                    var ref = document.referrer
                    if (ref.search(/(http|https):\/\/\w{0,3}\.{0,1}google\..{2,4}\//) != -1) {
                        cSource = 'google_natural';
                    }
                } else {
                    return;
                }
            }
            if (cSource) {
                cObj.source = cSource;
                //console.log('save source:'+cSource);
                var preJson = '{';
                var counter = 0;
                for (var i in cObj) {
                    if (counter>0) {
                        preJson += ',';
                    }
                    ++counter;
                    preJson += '"' + i + '":';
                    if (typeof(cObj[i]) == 'string') {
                        preJson += '"' + cObj[i] + '"';
                    } else {
                        preJson += cObj[i];
                    }
                }
                preJson += '}';
                preJson = preJson.replace(/:/g,'%3A');
                preJson = preJson.replace(/,/g,'%2C');
                preJson = encodeURI(preJson);
                var validTo = new Date();
                var validFor = validTo.getTime() + (cValidDays * 24 * 60 * 60 * 1000);
                validTo.setTime(validFor);
               document.cookie = encodeURI(cName + '=' + preJson) + '; expires=' + validTo.toGMTString();
            }
        }
    }
    this.init = init
}
var ref = new checkReferrer();

