// on load instructions at the end of this file

//---- functions ----

function BrowserCheck() {
        ver=navigator.appVersion;
        b=navigator.appName;
        dom=document.getElementById?1:0;

        ie=(b.indexOf("Microsoft Internet Explorer")!= -1)?1:0;
        ns=(b.indexOf("Netscape")!= -1)?1:0;
        if (ie && ver.match(/MSIE\s([.\d]+);/) ){
	         ieVersion = RegExp.$1;
	         ie5 = (ieVersion >= 5);
	         ie6 = (ieVersion >= 6);
	    } else { ieVersion=ie5=ie6=0; }
        ns4=(document.layers && !dom)?1:0;
        ns5=(dom && parseInt(ver) >= 5) ?1:0;
	mac=(ver.indexOf("Mac")!=-1)?1:0;
	gecko= (ver.indexOf("Gecko")!=-1);
        macns4=(mac && ns4)?1:0;
        macns5=(mac && ns5)?1:0;
        macie5=(mac && ie5)?1:0;
}


function CSSFileChooser() {
        BrowserCheck();
        if (ie || macie5 || ns5) {
            /* use default stylesheet, except for: */
            if (ns && !gecko) {
                document.write('<style type="text/css"> noscript { display: none; } </style>');
            }
        }
        else if (ns4) document.write('<link rel="stylesheet" type="text/css" href="/ems_static/css/rb_n4.css">');
        else if (macns4) document.write('<link rel="stylesheet" type="text/css" href="/ems_static/css/rb_macn4.css">');
}

function undef(object) {
	return (typeof(object)=='undefined') || (object==null);
}

function over (name) {
        if (ns5) {
                img = document.getElementsByName(name)[0];
                if (!img) img = document.getElementById(name);
                img.setAttribute('src', img.getAttribute('src').replace('_p\.','_a.'));
        }
        else {
                var x=document.images[name].src;
                x= x.replace(/_p\./,'_a.');
                document.images[name].src=x;
        }
}

function out (name) {
        if(ns5) {
                img = document.getElementsByName(name)[0];
                if (!img) img = document.getElementById(name);
                img.setAttribute('src', img.getAttribute('src').replace('_a\.','_p.'));
        }
        else {
                var x=document.images[name].src;
                x= x.replace(/_a\./,'_p.');
                document.images[name].src=x;
        }
}

function overer (name)
{
        if (ns5) {
                img = document.getElementsByName(name)[0];
                if (!img) img = document.getElementById(name);
                img.setAttribute('src', img.getAttribute('src').replace('_normal\.','_over.'));
        }
        else {
                var x=document.images[name].src;
                x= x.replace(/_normal\./,'_over.');
                document.images[name].src=x;
        }
}

function outer (name) 
{
        if(ns5) {
                img = document.getElementsByName(name)[0];
                if (!img) img = document.getElementById(name);
                img.setAttribute('src', img.getAttribute('src').replace('_over\.','_normal.'));
        }
        else {
                var x=document.images[name].src;
                x= x.replace(/_over\./,'_normal.');
                document.images[name].src=x;
        }
}


function preload(imgObj,imgSrc) {
        eval(imgObj+' = new Image()');
        eval(imgObj+'.src = "'+imgSrc+'"');
}

// fill status line
function fillStatus(txt) {
        self.status = txt
}

// clear status line
function emptyStatus() {
        self.status = ""
}

// external links
function openWindow(url,name,complex)
{
	openWindow(url,name,complex,800,600);
}

function openWindow(url,name,complex,width,height) {
	if(complex)
	{
		win = window.open(url,"_blank","");
		win.name = name;
		win.focus();
	}
	else
	{
		openMetaWin(url,"popup_"+name,width,height);
	}
}

function openWin(url, name, width, height) {
	// limit popup size to visible screen, at most
	// manually tuned values, optimized for IE - dont reason about it
	var x = width;
	var y = height;
	if(screen.availWidth && screen.availWidth-25<x){x=screen.availWidth-25;}
	if(screen.availHeight && screen.availHeight-50<y){y=screen.availHeight-50;}
	win = window.open( url,name,"width="+x+",height="+y+",scrollbars=yes,location=yes,resizable=yes,directories=yes,status=1,menubar=yes,toolbar=yes,left=20,top=50" );
	win.focus();
}

function openMetaWin(url, name, width, height){
	var x = width;
	var y = height;
	if(screen.availWidth && screen.availWidth-25<x){x=screen.availWidth-25;}
	if(screen.availHeight && screen.availHeight-50<y){y=screen.availHeight-50;}
	metaWin = window.open(url,name,"width="+x+",height="+y+",scrollbars=yes,location=no,resizable=no,directories=no,status=0,menubar=0,toolbar=no,left=20,top=10");
	metaWin.focus();
}


function openInfographicPopup(url, name, xdim, ydim) {
	var scrollflag='no';
	var topPos=50;
	if (screen.availHeight && ydim>screen.availHeight-60) {
		ydim=screen.availHeight-60;
		xdim=xdim+20;
		scrollflag='yes';
		topPos=0;
	}
	metaWin = eval("window.open('"+url+"','"+name+"' ,'width="+xdim+", height="+ydim+", scrollbars="+scrollflag+",location=no,resizable=no,directories=no,status=0,menubar=0,toolbar=no,left=20,top="+topPos+"')");		
	metaWin.focus();
}

// UTF-8 encode a string
function encodeUtf8 (str) {
	var utftext = "";
	for(var n=0; n<str.length; n++) {
		var c=str.charCodeAt(n);
		// all chars in range 0-127 => 1byte
		if (c<128) {
			utftext += String.fromCharCode(c);
		}
		// all chars in range 128 to 2047 => 2byte
		else if(c<2048) {
			utftext += '%' + ((c>>6)|192).toString(16);
			utftext += '%' + ((c&63)|128).toString(16);
		}
		// all chars in range 2048 to 66536 => 3byte
		else {
			utftext += '%' + ((c>>12)|224).toString(16);
			utftext += '%' + (((c>>6)&63)|128).toString(16);
			utftext += '%' + ((c&63)|128).toString(16);
		}
	}
	return utftext;
}


function appendMM_params(subj){
if (subj) {
if (ns4) {subj=escape(subj);}
else{subj=encodeURIComponent(subj);}
document.write('?subject='+subj);
}
}

// Opens the search popup from the parent. Parent is chosen, because the search-SendLink method needs the
// real opener for delegating the user.
// Context and skin specify index and layout/sendLink functionality.
function openSearchWindow (context, skin) {
	if (context == null) {
		context = "default";
	}
	if (skin == null) {
		skin = "default";
	}
    var form = document.searchForm;

    if (!form) {
        form=document.forms['searchForm'];
    }

	var queryStr = encodeUtf8(form.queryStr.value);
	
	URL = "http://www.rolandberger.com/rbsearch/fs-rbsearch_results.jsp?queryStr=" + queryStr + "&quickSearch=yes&context=" + context + "&skin=" + skin;

	searchResult = window.open( URL ,"luceneSearch","width=500,height=550,scrollbars=yes,location=no,resizable=no,directories=no,status=1,menubar=0,toolbar=no,titlebar=1,left=50,top=50");
	searchResult.focus();
}

// generate blind-gif with js-statistics
function generateJSStatistics () {
        var params = 'w=' + screen.width;
        params = params + '&h=' + screen.height;
        params = params + '&d=' + screen.colorDepth;
        if (navigator.cookieEnabled == true) {
                params = params + '&c=yes';
        }
        else if (navigator.cookieEnabled == false) {
                params = params + '&c=no';
        }
        else {
                params = params + '&c=unknown';
        }
        params = params + '&bn=' + escape(navigator.appName);

        var acrobat = "none";
        var acrobatPat = /acrobat/i;
        var flash = "none";
        var flashPat = /flash/i;

        if (ie) {
                acrobat = "ie";
                flash = "ie";
        }
        else {
                var i = navigator.plugins.length;

                for (j = 0; j < i; j++) {
                        var versionPat = /(\d+(\.\d+)*)/;
                        var version = "";
                        if (versionPat.test (navigator.plugins[j].description)) {
                                versionPat.exec(navigator.plugins[j].description);
                                version = RegExp.$1;
                        }
                        else {
                                version = "none";
                        }
                        if (acrobatPat.test (navigator.plugins[j].description)) {
                                acrobat = (version == "" ? "unknown" : version);
                        }
                        if (flashPat.test (navigator.plugins[j].description)) {
                                flash = (version == "" ? "unknown" : version);
                        }
                }
        }
        params = params + '&acrobat=' + escape(acrobat);
        params = params + '&flash=' + escape(flash);

        document.write ("<img name=\"img_js_test\" height=\"1\" width=\"20\" src=\"http://www.rolandberger.com/general/graphics/gfk_with_js.gif?" + params + "\">");
}

// popup of contact servlet to HTTPS server
function contact_https(URL) {
        URL="https://www.rolandberger.com" + URL
        win = window.open( URL ,"contact","toolbar=0,location=0,directories=0,status=0,scrollbars=0,resizable=0,dependent=0,width=500,height=480");
        win.focus();
}

function openZoom() {
    var zoom = top.open("http://zoom.rolandberger.com/start.html","zoom","width=500,height=600,left=0,top=0,menubar=no,locationbar=no,toolbar=no");
    zoom.moveTo(screen.width/2-250,screen.height/2-300);
    zoom.focus();
    return;
}
    
//---- on load code goes here ----

BrowserCheck();
