if(!this.GA_delay_logging) this.GA_delay_logging = 0;
if(!this.GA_obj) this.GA_obj = undefined;

(function() {
    var deferGA = false;

    var processJSON = function(req) {
        if(req.readyState == 4) {
            if(req.status == 200) {
                var json = eval('(' + req.responseText + ')');
                if(json.cookies) {
                    for(var i=0; i<json.cookies.length; i++)
                        document.cookie = json.cookies[i];

                    if(document.cookie.indexOf("www.apache.sid=") >= 0)
                        location.reload();
                }
            }
            if(deferGA) {
                GA_delay_logging=0;
                if(GA_obj) GA_obj._runInit();
            }
        }
    }

    var getLocalization = function() {
        if(document.cookie.indexOf("www.apache.sid=") < 0) return false;

        if(!GA_delay_logging) {
            GA_delay_logging=1;
            deferGA=true;
        }
        var url = "/cgi-registry/preferences/localize/ip2station-json.cgir";
        var req;
        if (window.XMLHttpRequest) { // branch for native XMLHttpRequest object
            req = new XMLHttpRequest();
            req.onreadystatechange = function() { processJSON(req) };
            req.open("GET", url, true);
            req.send(null);
        } else if (window.ActiveXObject) { // branch for IE/Windows ActiveX version
            req = new ActiveXObject("Microsoft.XMLHTTP");
            if (req) {
                req.onreadystatechange = function() { processJSON(req) };
                req.open("GET", url, true);
                req.send();
            }
        }
        return false;
    }

    if(document.cookie.indexOf("pbsol.station=") < 0 && document.cookie.indexOf("pbsol.auto_local") < 0)
        getLocalization();
})();
