var validhostname = top.location.protocol + "//" + top.location.hostname;

function framelink(urlStr) {
	var params = '' + parent.location;
	if(params.indexOf('?') > 0) {
		params = params.substring((params.indexOf('?')) + 1);

		if(urlStr.indexOf('?') > 0) {
			urlStr = urlStr + '&' + params;
		}
		else {
			urlStr = urlStr + "?" + params;
		}
	}
	var gbrPos = urlStr.indexOf('gbr');
	if (gbrPos == -1) {
		// Parameter gbr nicht uebergeben
		if(urlStr.indexOf('?') > 0) {
			urlStr = urlStr + '&gbr=VR';
		}
		else {
			urlStr = urlStr + '?gbr=VR';
		}
	}
	else {
		if( parent.gbr )
			urlStr = urlStr.replace(/gbr=../, 'gbr=' + parent.gbr);
		else {
			gbrStr = urlStr.substr(gbrPos + 4, 2);
			urlStr = urlStr.substring(0, gbrPos) + "gbr=" + gbrStr.toUpperCase() + urlStr.substring(gbrPos + 6);
		}
	}
	return urlStr;
}

function framefehleranzeigen() {
	top.location.href = framelink("/Framefehler.html");
}

function isvalid(currenthostname) {
	if (currenthostname.indexOf(validhostname) == 0)
		return true
	else return false;
}

function checkframeset() {
	var framesetvalid = true;
	for (var i=0; i<window.frames.length; i++)
		try {
			if (!isvalid(window.frames[i].location.href))
				framesetvalid = false;
		} catch (e) {
			framesetvalid = false;
		}
	if (!framesetvalid) framefehleranzeigen();
}

function checkBrowser() {
	var agent  = navigator.userAgent.toLowerCase();
	this.major = parseInt(navigator.appVersion);
	this.minor = parseFloat(navigator.appVersion);
	this.ns    = ((agent.indexOf('mozilla')!=-1)
			&& (agent.indexOf('spoofer')==-1)
			&& (agent.indexOf('compatible') == -1));
	this.ns5   = (this.ns && (this.minor >= 5.0));
	this.ie    = (agent.indexOf("msie") != -1);
	this.ie5   = (this.ie && (this.major >= 4));
	if (this.ns5 == true || this.ie5 == true) {
		this.location = framelink('http://' + window.location.host + '/index_nav.html')
	}
	else {
		this.location = framelink('http://' + window.location.host + '/index1.html')
	}
}

function checkBrowserBrokerage() {
	isBrokerage = (location.href.indexOf("boerse") < 0);
	gadt_par = (location.href.indexOf("gadt=") >= 0);

	var agent  = navigator.userAgent.toLowerCase();
	this.major = parseInt(navigator.appVersion);
	this.minor = parseFloat(navigator.appVersion);
	this.ns    = ((agent.indexOf('mozilla')!=-1)
			&& (agent.indexOf('spoofer')==-1)
			&& (agent.indexOf('compatible') == -1));
	this.ns5   = (this.ns && (this.minor >= 5.0));
	this.ie    = (agent.indexOf("msie") != -1);
	this.ie5   = (this.ie && (this.major >= 4));
	if( isBrokerage ) {
		if( gadt_par ) {
			top.location = framelink('http://' + window.location.host + '/frames/frame.html');
		}
		else {
			if (this.ns5 == true || this.ie5 == true) {
				top.location = framelink('https://' + document.location.host + '/modul6/fr_6.html');
			}
			else {
				top.location = framelink('http://' + window.location.host + '/modul6/falseBrowser.html');
			}
		}
	}
	else {
		if (this.ns5 == true || this.ie5 == true) {
			top.location = framelink('http://' + window.location.host + '/index_nav.html');
		}
		else {
			top.location = framelink('http://' + window.location.host + '/index1.html');
		}
	}
}

// Replace each '_' in data string with a space.
function replaceUnderscore(strReplace) {
	if( ! strReplace ) return "";
	while (strReplace.indexOf('_') > -1) {
		strReplace = strReplace.substring(0,strReplace.indexOf('_')) + " "
			+ strReplace.substring(strReplace.indexOf('_') + 1);
	}
	return strReplace ;
}

function replaceExtras(urlStr) {
	if( ! urlStr ) return "";

	if(urlStr.indexOf("%E4")>-1)     {urlStr=urlStr.replace(/%E4/g, "228");}
	if(urlStr.indexOf("%e4")>-1)     {urlStr=urlStr.replace(/%e4/g, "228");}
	if(urlStr.indexOf("%F6")>-1)     {urlStr=urlStr.replace(/%F6/g, "246");}
	if(urlStr.indexOf("%f6")>-1)     {urlStr=urlStr.replace(/%f6/g, "246");}
	if(urlStr.indexOf("%FC")>-1)     {urlStr=urlStr.replace(/%FC/g, "252");}
	if(urlStr.indexOf("%fc")>-1)     {urlStr=urlStr.replace(/%fc/g, "252");}
	if(urlStr.indexOf("%DF")>-1)     {urlStr=urlStr.replace(/%DF/g, "223");}
	if(urlStr.indexOf("%df")>-1)     {urlStr=urlStr.replace(/%df/g, "223");}

	if(urlStr.indexOf("ä")>-1)     {urlStr=urlStr.replace(/ä/g, "&#228");}
	if(urlStr.indexOf("ö")>-1)     {urlStr=urlStr.replace(/ö/g, "&#246");}
	if(urlStr.indexOf("ü")>-1)     {urlStr=urlStr.replace(/ü/g, "&#252");}
	if(urlStr.indexOf("Ä")>-1)     {urlStr=urlStr.replace(/Ä/g, "&#196");}
	if(urlStr.indexOf("Ö")>-1)     {urlStr=urlStr.replace(/Ö/g, "&#214");}
	if(urlStr.indexOf("Ü")>-1)     {urlStr=urlStr.replace(/Ü/g, "&#220");}
	if(urlStr.indexOf("ß")>-1)     {urlStr=urlStr.replace(/ß/g, "&#223");}
	if(urlStr.indexOf("\xC3\xA4")>-1) {urlStr=urlStr.replace(/\xC3\xA4/g, "&#228");}
	if(urlStr.indexOf("\xC3\xBC")>-1) {urlStr=urlStr.replace(/\xC3\xBC/g, "&#252");}
	if(urlStr.indexOf("\xC3\xB6")>-1) {urlStr=urlStr.replace(/\xC3\xB6/g, "&#246");}
	if(urlStr.indexOf("\xC3\x1E")>-1) {urlStr=urlStr.replace(/\xC3\x1E/g, "&#196");}
	if(urlStr.indexOf("\xC3\x13")>-1) {urlStr=urlStr.replace(/\xC3\x13/g, "&#214");}
	if(urlStr.indexOf("\xC3S")>-1)    {urlStr=urlStr.replace(/\xC3S/g, "&#220");}
	if(urlStr.indexOf("\xC3x")>-1)    {urlStr=urlStr.replace(/\xC3x/g, "&#223");}

	// gefaehrliche Zeichen fuer Cross Site Scripting entfernen
	if(urlStr.indexOf("<")>-1)    {urlStr=urlStr.replace(/\</g, " ");}
	if(urlStr.indexOf("\x3C")>-1) {urlStr=urlStr.replace(/\x3C/g, " ");}
	if(urlStr.indexOf(">")>-1)    {urlStr=urlStr.replace(/\>/g, " ");}
	if(urlStr.indexOf("\x3E")>-1) {urlStr=urlStr.replace(/\x3E/g, " ");}
	if(urlStr.indexOf("(")>-1)    {urlStr=urlStr.replace(/\(/g, " ");}
	if(urlStr.indexOf("\x28")>-1) {urlStr=urlStr.replace(/\x28/g, " ");}
	if(urlStr.indexOf(")")>-1)    {urlStr=urlStr.replace(/\)/g, " ");}
	if(urlStr.indexOf("\x29")>-1) {urlStr=urlStr.replace(/\x29/g, " ");}

	return urlStr;
}

function openBrokerage(windowURL) {
	if( (getProtocol()).indexOf('https') < 0 ) {
		top.location = windowURL;
	}
	else {
		newWindow = window.open
		(
			windowURL,
			'Brokerage'
//			'Börseninfos'
		);
		newWindow.focus();
	}
}

function openDepot() {
	var windowURL = "/index.html" + parent.BrkParams + "gbr=" + parent.gbr;
	openBrokerage(windowURL);
}

function getProtocol() {
	var prot;
	try {
		prot = parent.location.protocol;
		return prot;
	} catch(ex) {
		// Lesen verboten, dann wird's wohl https sein.
		return "https:";
	}
}

function getSessionID() {
	var id;
	try {
		id = parent.frames["main"].document.session;
		return id;
	} catch(ex) {
		// Lesen der SessionID ist vermutlich verboten
		// oder die SessionID existiert nicht.
	}
}

function SessionAbmelden() {
	var link = "https://" + window.location.host
		+ "/VRM/servlet/gad.VRM.bas.servlet.VRM/logout";
	var id = getSessionID();
	if (id) {
		link = link + "?ID=" + id;
	}
	parent.frames["main"].location = framelink(link);
}

function openKontoService() {
	if (parent.bankid == 0) return;
	else {
		var link = "https://internetbanking.gad.de/banking/portal?bankid=" + parent.bankid;
		if ( parent.ind != "0" && parent.ind != "i" ) {
			link = link + parent.ind;
		}
		f1 = window.open( link, "InternetBanking" );
	}
}

function getTitle() {
	var title;
	var par = '' + parent.frames["topR"].location;
	par = par.substring((par.indexOf('?')) + 1);

	if (par.length < 1) { }
	else {
		var keypairs = new Object();
		var numKP = 1;
		while (par.indexOf('&') > -1) {
			keypairs[numKP] = par.substring(0,par.indexOf('&'));
			par = par.substring((par.indexOf('&')) + 1);
			numKP++;
		}
		keypairs[numKP] = par;
		for (i in keypairs) {
			keyName  = keypairs[i].substring(0,keypairs[i].indexOf('='));
			keyValue = keypairs[i].substring((keypairs[i].indexOf('=')) + 1);
			keyValue = unescape(keyValue);
			if ((keyName == 'title') && (keyValue != "")) {
				title= replaceUnderscore(keyValue);
			}
		}
	}
	return title;
}

function setTitle( titel ) {
	try {
//		var titleURL;
//		if( parent.brk ) {
//			titleURL = "/nav_title.html?title=" + titel;
//		}
//		else {
//			titleURL = "/nav_title_other.html?title=" + titel;
//		}
//		parent.topR.location = titleURL;
		parent.topR.location = "/nav_title.html?title=" + titel;
	} catch(ex) {
//		alert("Exception :" + ex);
	}
}

function checkNavUrl( urlStr, uid ) {
	if( urlStr.indexOf("GAD") >= 0 ) {
		if( (getProtocol()).indexOf('https') < 0 )
			urlStr = top.host_info + urlStr.substr(urlStr.indexOf("GAD") + 3);
		else
			urlStr = top.host_info_s + urlStr.substr(urlStr.indexOf("GAD") + 3);
	}
	else {
		if( (urlStr.indexOf("http") < 0)
		&&  (urlStr.indexOf("mailto") < 0)
		&&  (urlStr.indexOf("javascript") < 0) ) {
			if( (getProtocol()).indexOf('https') < 0 )
				urlStr = top.host_teledata + urlStr + "&LayoutIP=1&pe=1";
			else
				urlStr = top.host_teledata_s + urlStr + "&LayoutIP=1&pe=1";
			if( uid && uid != '' ) urlStr += "&FAGSession=" + uid;
		}
	}
	if( urlStr.indexOf("http") >= 0 ) {
		ii = -1;
		for( i in top.allowedHosts ) {
			ii = urlStr.indexOf(top.allowedHosts[i]);
			if( ii >= 0 ) break;
		}
		if( ii != 0 ) urlStr = "";
	}
	return urlStr;
}

function nav(id0, id1, id2, titel, urlStr, newWindow) {
	urlStr = urlStr.replace(/^ +/, "").replace(/ +$/, "");
//alert("id0 = '" + id0 + "'\nid1 = '" + id1 + "'\nid2 = '" + id2 + "'\nTitel = '" + titel + "'\nurl = '" + urlStr + "'\nnewWindow = '" + newWindow + "'");

	if( urlStr.length > 0 ) {
		urlStr = checkNavUrl( urlStr, parent.FAGSession );
	}

	if( (newWindow == "true") && (urlStr.length > 0) ) {
		f1 = window.open(urlStr, "_blank");
		f1.focus();
	}
	else {
		var navURL = "/Info/Info/Navigation?id0=" + id0
			+ "&id1=" + id1 + "&id2=" + id2
			+ "&brk=" + parent.brk;
//		parent.frames["menu"].location = framelink(navURL);
		parent.frames[1].location = framelink(navURL);
		if( (urlStr.length > 0) ) {
			parent.frames["main"].location = urlStr;
			setTitle(titel);
		}
	}
}

function nav_brk(id0, titel, urlStr, newWindow) {
// alert("id0 = '" + id0 + "'\nTitel = '" + titel + "'\nurl = '" + urlStr + "'\nnewWindow = '" + newWindow + "'");

	if( urlStr.length <= 0 )
		return;

	var uid = getSessionID();
	urlStr = checkNavUrl( urlStr, uid );
	if( urlStr != "" ) {
		if( newWindow == "true" ) {
			f1 = window.open(urlStr, "_blank");
			f1.focus();
		}
		else {
//			newUrl = top.host_info_s + "/frames/frame.html?gadt=brk&infUrl="
			newUrl = top.host_info_s + "/frames/frame.html" + parent.BrkParams
			+ "gbr=" + parent.gbr + "&gadt=brk&infUrl="
				+ encodeURIComponent(urlStr)
				+ "&infTitel=" + titel + "&nav_id=" + id0;
			parent.typ=null;
			if( uid && uid != "" )
				newUrl = newUrl + "&FAGSession=" + uid;
//			f2 = window.open( framelink(newUrl), "Boerseninfos" );
			f2 = window.open( newUrl, "Boerseninfos" );
			f2.focus();
		}
	}
}

// Funktion berechnet u.a. den Pfad, aus dem die Headergrafik zu holen ist.
function getHeader() {
	var bild       = "privatkunden.jpg";
	var bildString = "/l_images/default/" + bild;

	if (parent.ind != "0" && parent.bankid != 0) {
		bildString = "/l_images/" + parent.bankid + parent.ind + "/" + bild;
	} else {
		document.write( '<div id="banklogo">');
		document.write( '<img src="/l_images/default/1.gif"></img>&nbsp;');
		document.write( replaceExtras(replaceUnderscore(parent.firma)) );
		document.write( '</div>');
	}
	document.write( '<div class="header" style="background-image: url(' + bildString + ');"/>');
}

function getFavicon() {
	var icon = "/favicon.ico";
	if( parent.ind != "0" && parent.bankid != 0) {
		icon = "/l_images/" + parent.bankid + parent.ind + "/favicon.ico";
	}
	return icon;
}

// Funktion oeffnet die Sitemap-Seite.
function openSitemap() {
	setTitle("Sitemap");
	try {
		parent.main.location = framelink(parent.smap_url + "?brk=" + parent.brk);
	} catch(ex) {
//		alert("Exception :" + ex);
	}
}

// Funktion oeffnet die Service-Seite
function openService() {
	var link = "service.html";
	var id;
	try {
		id = parent.frames["main"].document.session;
	} catch(ex) {
		// Lesen der SessionID ist vermutlich verboten
	}

	var gbr = parent.gbr;
	if( id ) {
		if( gbr == 'XG' ) {
			link = "serviceGADKunde.html";
		}
		else if( gbr == 'XF' ) {
			link = "serviceGRZKunde.html";
		}
	}
	// bei Individualisierung ist bankid + ind 1. Namensbestandteil
	if (parent.ind != "0" && parent.bankid != 0) {
		if (id) {
			link = parent.bankid + parent.ind + "serviceKunde.html";
		}
		else {
			link = parent.bankid + parent.ind + "service.html";
		}
	}
	newWindow = window.open
	(
		framelink('/service/' + link),
		'Service',
		'width=665,height=600,toolbar=0,location=0,'
		+ 'directories=0,status=0,menuBar=1,scrollBars=yes,'
		+ 'resizable=yes'
	);
	newWindow.focus();
	return;
}

// Funktion oeffnet die Homepage
function openHome() {
	newWindow = window.open("http://" + parent.home, "_blank");
	newWindow.focus();
	return;
}

function getWKN() {
	var wknStr = window.location.search;
	if( wknStr.indexOf("wkn=") > 0 ) {
		wknStr = wknStr.replace(/.+wkn=/, "");
		wknStr = wknStr.replace(/\&.+/, "");
	} else {
		wknStr = "";
	}
	if( wknStr == "" ) wknStr = "Kurssuche";
	return wknStr;
}

function openInfo(urlStr) {
	if( urlStr.indexOf("http") < 0 )
		urlStr = top.host_info_s + urlStr;
	newWindow = window.open
	(
		framelink(urlStr), "Boerseninfos"
	);
	newWindow.focus();
	return;
}

function openHilfeInfo() {
	hilfeUrl = parent.host_teledata + parent.hilfe_url
		+ "&RZKZ=" + parent.gbr + "&RZBK=" + parent.bankid;
//		+ "&kunde=" + parent.gbr + parent.bankid
	newWindow = window.open
	(
		hilfeUrl,
		'Hilfe',
		'width=658,height=420,toolbar=0,location=0,'
		+ 'directories=0,status=0,menuBar=1,scrollBars=yes,'
		+ 'resizable=yes'
	);
	newWindow.focus();
	return;
}

function openHilfeBrokerage() {
	var link = "hilfe_ne";
	if (parent.gbr == "XF")
	{
		link = "hilfe_xf";
	}
	if (parent.gbr == "XG")
	{
		link = "hilfe_xg";
	}

	newWindow = window.open
	(
		parent.host_info + "/hilfe/" + link + "/fr_60000.htm",
		'Hilfe',
		'width=800,height=600,toolbar=0,location=0,'
		+ 'directories=0,status=0,menuBar=1,scrollBars=yes,'
		+ 'resizable=yes'
	);
	newWindow.focus();
	return;
}

function openPopup(urlStr, titel) {
	newWindow = window.open
	(
		framelink(urlStr),
		titel,
		'width=665,height=600,toolbar=0,location=0,'
		+ 'directories=0,status=0,menuBar=1,scrollBars=yes,'
		+ 'resizable=yes'
	);
	newWindow.focus();
	return;
}

function openURL(URL,target) {
	switch (target) {
		case '_top':
			top.document.location = URL + document.location.search;
			break;
		case '_blank':
			window.open(URL + document.location.search);
			break;
		default:
			document.location = URL + document.location.search;
			break;
	}
}

function kurssucheBrk() {
	var wkn = top.frames[1].document.forms[0].wkn.value;
	if( wkn.length > 0 && wkn != 'Kurssuche' )
	{
		urlStr = parent.eki_url + wkn
			+ "&RZKZ=" + parent.gbr + "&RZBK=" + parent.bankid;
		nav_brk("", "Einzelkursabfrage", urlStr, "");
	}
}

function openChart( wkn ) {
	urlStr = parent.char_url + wkn
		+ "&RZKZ=" + parent.gbr + "&RZBK=" + parent.bankid;
	nav_brk( "", "Chartanalyse", urlStr, "" );
}
function openNews(wkn) {
	urlStr = parent.wnew_url + wkn
		+ "&RZKZ=" + parent.gbr + "&RZBK=" + parent.bankid;
	nav_brk( "", "Nachrichten&uuml;bersicht", urlStr, "" );
}
function openEKI(wkn) {
	urlStr = parent.ekur_url + wkn
		+ "&RZKZ=" + parent.gbr + "&RZBK=" + parent.bankid;
	nav_brk( "", "Einzelkursabfrage", urlStr, "" );
}

