// Felder der Registrierungsmaske
var regFields                  = new Array();    		 // Hash zum speichern der Eingabefelder der Registrierung;
var userExist									 = false;
var userWrongLogin						 = false;
var regModus                   = 0;

// Gewählte Datenbank
var datenbankId                = 1;              		 // Default = 1 Stotax Steuern, 2 = Stotax Personal;

// Benutzer eingelogged
var logged_out                 = false;              // Flag, ob Benutzer angemeldet ist, oder nicht (für Button 'Anmelden' bzw. 'Abmelden')

// Buffer für Detailsuchmaske
var detailsuche;

// Buffer für Trefferzeitraum
var trefferzeitraum            = "";
var tzr_buf                    = "";

// Buffer für SortSpec
var sortSpec                   = "";

// Benutzerinformationen anzeigen
var showBenutzerinfos          = false;

// Pushdienstinfor anzeigen
var showPushdienstempfehlung   = false;

// Buffer für Anzeige Rechtsgebiete
var rgb1                       = "";
var rgb2                       = "";
var rgb3                       = "";

// Hash für Historie
var historie                   = new Array();        // hrefs der History (zuletzt angezeigte Dokumente und Trefferlisten)
var historieTitel              = new Array();        // zugehörige Titel zu den History Einträgen
var historieTopParams          = new Array();        // zugehörige Titel zu den History Einträgen
var historieTe1                = new Array();        // zugehörige Titel zu den History Einträgen
var historie_pos               = -1;                 // aktuelle Position in der History
var historie_walk              = false;              // aktuelle Position in der History
var humanQuery                 = "";                 // Titel für Trefferliste eines History Eintrags
var docTitel                   = "";                 // Titel eines Dokuments für Historyeintrag
var xDocHref                   = "";                 // Href von XDocument mit Link auf die Daten
var zaehler                    = 0;                  // Timer zum automatischen schließen der History
var aktiv;

// Hash als Buffer für Suchparameter
var parameters                 = new Array();        // Alle möglichen und unmöglichen Parameter (speziel für die Suche)
var linked                     = false;              // Suche nach Klick auf einen Link (z.B. ABC-Register)
var location_inhalt            = "";                 // Letzter href von Inhalt vor der Suche aus einem Link
var location_document          = "";                 // Letzter href von Main vor der Suche aus einem Link

// Array für Trefferumfeld
var umfeld                     = new Array();
parameters['surroundings']     = "";
parameters['rubrikId']         = "0000";
parameters['werkId']           = "0000";

// Variablen für Dokumentanzeige
var bAuthorStandOn             = false;
var bFootnoteListOn            = true;

// Buffer für Trefferlisten
var hiddenFields               = "";
var hitListBuf                 = "";
var hitList                    = "";
var pi_rubriken                = "";
var pi_stichworte              = "";
var pi_selector                = "";
var lastWait                   = "";
var docsFound                  = 0;
var rgbName                    = "";

/************************** Funktionen für die History-Verwaltung ******************************/
// Eintrag in History hinzufuegen
function historie_add(sUri) {

	//if ( top.humanQuery.indexOf("$$") == -1 && top.historie_walk == false) {
	if (top.historie_walk == false) {
		top.historie_pos++;
		top.historie.splice(top.historie_pos,top.historie.length-top.historie_pos,sUri);
		//top.historieTopParams.splice(top.historie_pos,top.historie.length-top.historie_pos,top.parameters);
		var tempArray = new Array();
		for (var parameter in top.parameters) {
			tempArray[parameter] = top.parameters[parameter];
		}
		top.historieTopParams.splice(top.historie_pos,top.historie.length-top.historie_pos,tempArray);

		top.historieTe1.splice(top.historie_pos,top.historie.length-top.historie_pos,top.nav_oben.document.getElementsByName("te1")[0].value);
		if ( sUri.indexOf("page=XSearch") > -1 ) {
			if (top.humanQuery == "Suche in $$HumanRubrik nach $$HumanVolltext. $$HumanIndex") {
				top.historieTitel.splice(top.historie_pos,top.historieTitel.length-top.historie_pos,"Suche in 'Gesamtdatenbestand' nach ''.");
			}else{
				top.historieTitel.splice(top.historie_pos,top.historieTitel.length-top.historie_pos,top.humanQuery);
			}
		} else {
			top.historieTitel.splice(top.historie_pos,top.historieTitel.length-top.historie_pos,top.docTitel);
		}
		top.setHistoryButtons();
	}
	top.humanQuery = "";
	top.historie_walk = false;
}

// letztes Dokument aus der Historie aufrufen
function historie_back() {
	top.historie_walk = true;
	if (top.historie_pos > 0) {
		if (top.work && top.work.main) {
			try {	//Internet Explorer
				if (top.work.main.document) {
					if (top.work.main.document.title != "Keine Dokumente gefunden")
						top.historie_pos--;
				}
				else {
					// Adobe Reader PlugIn aktiv
				}
			}
			catch(e) {
				// Adobe Reader PlugIn aktiv
			}
		}
		else
			top.historie_pos--;
		top.showHistorieDokument();
	}
}

function historie_next() {
	top.historie_walk = true;
	if ( top.historie_pos < historie.length-1 ) {
		top.historie_pos++;
		top.showHistorieDokument();
	}
}

function showHistorieDokument() {
	top.setHistoryButtons();
	top.nav_oben.document.getElementsByName("te1")[0].value = top.historieTe1[top.historie_pos];

	//for (x=0; x<top.historieTopParams.length; x++) {
	//	top.parameters = new Array();
	//	top.parameters = top.historieTopParams[x];
	//	for (paramname in top.parameters)
	//		alert(x+": "+paramname+": "+top.parameters[paramname]);
	//}
	top.parameters = new Array();
	var tempArray = top.historieTopParams[top.historie_pos];
	for (var parameter in tempArray) {
		top.parameters[parameter] = tempArray[parameter];
	}
	lastDoc = top.historie[top.historie_pos];
	var sAnchor = (lastDoc.indexOf("#") != -1 ? lastDoc.substr(lastDoc.indexOf("#")) : "");
	lastDoc = lastDoc.replace(/#.*/, "");
	if (lastDoc.indexOf("quelle=") > -1)
		lastDoc = lastDoc.replace(/quelle=\w+/i, "quelle=Historie");
	else
		lastDoc = lastDoc + "&quelle=Historie";

	//if        (( lastDoc.indexOf("page=XSearch") > -1 )&&( lastDoc.indexOf("&template=") == -1 )) {
	//if        (( lastDoc.indexOf("page=XSearch") > -1 )&&( lastDoc.indexOf("&template=") > -1 )) {
	if        ( lastDoc.indexOf("page=XSearch") > -1 ) {
		top.work.location.href = "/recherche/index_historie.html?page=XSearch" + lastDoc.substr(lastDoc.indexOf("&")) + sAnchor;
	//} else if ( lastDoc.indexOf("page=XSearch") > -1 ) {
	//	top.work.location.href = lastDoc + sAnchor;
	} else if ( lastDoc.indexOf("page=XDocument") > -1 ) {
		//top.work.location.href = "/recherche/index_historie.html?page=XDocument&newLoad=1" + lastDoc.substr(lastDoc.indexOf("&")) + sAnchor;
		top.work.location.href = "/docs/index_historieX.html?page=XDocument"   + lastDoc.substr(lastDoc.indexOf("&")) + sAnchor;
	} else if ( lastDoc.indexOf("page=start") > -1 ) {
		top.work.location.href = lastDoc + sAnchor;
	} else if ( lastDoc.indexOf("page=matrix") > -1 ) {
		top.work.location.href = lastDoc + sAnchor;
	} else if ( lastDoc.indexOf("page=pushkonto") > -1 ) {
		top.work.location.href = lastDoc + sAnchor;
	} else if ( lastDoc.indexOf("page=editPushauftrag") > -1 ) {
		top.work.location.href = lastDoc + sAnchor;
	} else if ( lastDoc.indexOf("page=showMeldung") > -1 ) {
		top.work.location.href = lastDoc + sAnchor;
	} else if ( lastDoc.indexOf("&link=") > -1) {
		top.work.location.href = "/docs/index_historie.html?page=Document"     + lastDoc.substr(lastDoc.indexOf("&link=")) + sAnchor;
	} else if ( lastDoc.indexOf("page=listeDossiers") > -1 ) {
		top.work.location.href = lastDoc + sAnchor;
	} else if ( lastDoc.indexOf("page=matrixArbeitshilfen") > -1 ) {
		top.work.location.href = lastDoc + sAnchor;
	} else if ( lastDoc.indexOf("page=listeArbeitshilfen") > -1 ) {
		top.work.location.href = lastDoc + sAnchor;
	} else if ( lastDoc.indexOf("page=buchstabeArbeitshilfen") > -1 ) {
		top.work.location.href = lastDoc + sAnchor;
	} else if ( lastDoc.indexOf("page=hitlistSieheAuchVerweise") > -1 ) {
		top.work.location.href = lastDoc + sAnchor;
	} else {
		top.work.main.location.href = lastDoc + sAnchor;
	}
}

function historie_all() {
	if (top.work.document.getElementById("historie") || top.work.inhalt.document.getElementById("historie")) {
		var htmlOut = "";
		for (h=historie.length-1; h>historie.length-11; h--) {
			if ( h > -1 ) {
				var titel = historieTitel[h];
				titel = titel.replace(/<b>/ig, "");
				titel = titel.replace(/<\/b>/ig, "");
				aTitle = titel;
				if ( titel.length > 100 )
					titel = titel.substr(0,100) + "...";
				htmlOut += "<a class=\"his\" href=\"javaScript:top.showHistorie("+h+");\" title=\""+aTitle+"\">"+titel+"</a><br>";
				htmlOut += "<hr>";
			}
		}
		if ( top.work.inhalt ) {
			top.work.inhalt.document.getElementById("historie").innerHTML=htmlOut;
			if (top.work.inhalt.document.getElementById("historie").style.display == "inline") {
				top.work.inhalt.document.getElementById("historie").style.display = "none";
				top.nav_oben.document.getElementById("verlauf").className = "register_button";
			} else {
				aktiv = window.setTimeout("Zaehlen()", 1000);
				top.work.inhalt.document.getElementById("historie").style.display = "inline";
				top.nav_oben.document.getElementById("verlauf").className = "register_selected";
			}
		} else {
			top.work.document.getElementById("historie").innerHTML=htmlOut;
			if (top.work.document.getElementById("historie").style.display == "inline") {
				top.work.document.getElementById("historie").style.display = "none";
				top.nav_oben.document.getElementById("verlauf").className = "register_button";
			} else {
				aktiv = window.setTimeout("Zaehlen()", 1000);
				top.work.document.getElementById("historie").style.display = "inline";
				top.nav_oben.document.getElementById("verlauf").className = "register_selected";
			}
		}
	}
}

function Zaehlen () {
	if (zaehler < 2) {
		aktiv = window.setTimeout("Zaehlen()", 1000);
	} else {
		window.clearTimeout(aktiv);
		if ((top.work.inhalt)&&(top.work.inhalt.document.getElementById("historie"))) {
			top.work.inhalt.document.getElementById("historie").style.display = "none";
			top.nav_oben.document.getElementById("verlauf").className = "register_button";
		}else{
			if (top.work.document.getElementById("historie")) {
				top.work.document.getElementById("historie").style.display = "none";
				top.nav_oben.document.getElementById("verlauf").className = "register_button";
			}
		}
		zaehler = -1;
	}
	zaehler = zaehler + 1;
}

function showHistorie(pos) {
	top.historie_pos = pos+1;
	historie_back();
}

function setHistoryButtons() {
	if ( top.historie_pos > 0 ) {
		top.nav_oben.document.his_back.src = "/images/historie_back_active.gif";
		top.nav_oben.document.getElementById("historie_back").title = "Vorheriges Dokument";
	}else{
		top.nav_oben.document.his_back.src = "/images/historie_back_inactive.gif";
		top.nav_oben.document.getElementById("historie_back").title = "";
	}
	if ( top.historie_pos < top.historie.length-1 ) {
		top.nav_oben.document.his_next.src = "/images/historie_next_active.gif";
		top.nav_oben.document.getElementById("historie_next").title = "Nächstes Dokument";
	}else{
		top.nav_oben.document.his_next.src = "/images/historie_next_inactive.gif";
		top.nav_oben.document.getElementById("historie_next").title = "";
	}
}

// Aufruf zum setzen der Trefferanzahl in der Datenbank (Tabelle Werke)
// Die Anzahl der Treffer wird aus dem PI_Directory ermittelt
function setHitCount(modell) {
	parameters['setHitCount'] = "on";
	parameters['modellId'] = modell;
	top.work.location.href = "/recherche/index.html";
}

function clearCache() {
	top.nav_oben.openPopup('page=clearCache');
}

function clearStartpage() {
	top.nav_oben.openPopup('page=clearStartpage');
}

function setButtonActive(button) {
	top.nav_oben.document.getElementById("hitlist").className = "register_button";
	//top.nav_oben.document.getElementById("document").className = "register_button";
	top.nav_oben.document.getElementById("start").className = "register_button";
	if (top.nav_oben.document.getElementById(button)) {
		top.nav_oben.document.getElementById(button).className = "register_selected";
	}
}

function getAnker(anker) {
	uri = top.work.main.location.href;
	uri = uri.substr(0,uri.indexOf("#")+7) + anker;
	top.work.main.location.href=uri;
}

function loadAktuelleMeldungen(syncid) {
	syncidsplit = syncid.split(/\$/);
	syncnewid = "";
	inhaltfile = syncidsplit[0]+"/"+syncidsplit[1]+".htm";
	if (top.work.inhalt) {
		if (top.work.inhalt.document.location.href.indexOf(inhaltfile) == -1) {
			top.work.inhalt.document.location.href="/inhalt/"+inhaltfile;
		}
		for(j=0;j<syncidsplit.length;j++) {
			if (syncnewid != "") {
				syncnewid += "$";
			}
			syncnewid += syncidsplit[j];
			top.work.inhalt.toSyncUrl.push(syncnewid);
		}
		top.work.inhalt.nextToSync();
		if (top.work.inhalt.document.getElementById('a_'+syncid) != null) {
			top.work.main.location.href=top.work.inhalt.document.getElementById('a_'+syncid).href;
		}else{
			setTimeout("top.loadAktuelleMeldungen('"+syncid+"')",1000);
		}
	}else{
		top.work.location.href="/docs/loadInhalt.html?link="+syncid;
	}
}

function showDocument(link) {
	top.parameters["quelle"] = "";
	if ( link.indexOf('/daten') > 1 ) {
		link = link.substr(link.indexOf('/daten'));
	}
	if ( link.indexOf('/extern') > 1 ) {
		link = link.substr(link.indexOf('/extern'));
	}
	if (top.work.location.href.indexOf("page=start") > -1) top.parameters["quelle"]="Startseite";
	if (link.match(/^pi_directory=/)) {
		link = link.replace(/^pi_directory=/,"");
		top.parameters["rubrikId"]=link.substr(0,4);
		top.parameters["werkId"]=link.substr(link.indexOf("[#]")+3,4);
		top.parameters["logged_out"]=false;
		top.parameters["sortSpec"]="score desc";
		top.parameters["showDirect"]="off";
		top.parameters["offset"]=0;
		top.parameters["lPage"]=1;
		top.parameters["lRank"]=-1;
		top.parameters["newLoad"]="on";
		top.parameters["target"]="dir_"+link.substr(0,6)+"_"+link.substr(link.indexOf("[#]")+3,6);
		top.parameters["pi_directory"]=link;
		top.work.location.href = "/recherche/index.html";
		return;
	}
	if ((link.match(/^\/daten\.pdf\//))) {
		top.parameters["link"] = link;
		top.work.location.href="/docs/index.html";
		return;
	}

	if ((link.match(/^\/daten\//))||
	    (link.match(/^\/extern\/titelblaetter\//))) {
		top.parameters["link"] = link;
		top.work.location.href="/docs/index.html";
		return;
	}
	if (link.match(/^V-/)) {
		top.parameters["showDirect"] = "on";
		top.parameters["linked"] = "1";
		if (top.work &&
		    top.work.main)
			top.location_document = top.work.main.location.href;

		if (link.match(/^V-10-G-/) &&
			link.match(/-X-\d+$/)) {
			link = link.replace(/-X-\d+$/, "");
		}
		if (   link.match(/^V-30-Z-BStBl-/) &&
			! (link.match(/-N-\d+$/))) {
			link = link + "-N-*";
		}

		// hier soll eine exakte Suche durchgeführt werden:
		top.parameters["abcregister"] = "stvid<CONTAINS>\""+link+"|V\"<OR>" +
		                                "stvid<ENDS>\""+link+"\"";      // die eigentliche Suche
		top.parameters["newLoad"]     = "on";                           // damit getPage() aufgerufen wird
		//top.historie_walk             = true;                           // diese Suche nicht in History eintragen

		if (!top.work.hidden)
			top.work.location.href = "/recherche/index.html";
		else
			top.work.hidden.location.href = "/XSearch";
		return;
	}
	if (link.match(/^\[[0-9]*_[0-9]*\]/)) {
		//top.parameters["aktenzeichen"    ] = "";
		//top.parameters["datumbis"        ] = "";
		//top.parameters["datumvon"        ] = "";
		//top.parameters["gericht"         ] = "";
		//top.parameters["gruende"         ] = "";
		//top.parameters["pi_directory"    ] = "";
		//top.parameters["pi_rechtsgebiete"] = "";
		//top.parameters["pi_stichworte"   ] = "";
		//top.parameters["saveSearch"      ] = "";
		//top.parameters["surroundings"    ] = "";
		//top.parameters["thema"           ] = "";
		//top.parameters["ueberschrift"    ] = "";
		//top.parameters["werkId"          ] = "";
		top.parameters["te1"             ] = "";
		top.parameters["lPage"           ] = 1;
		top.parameters["lRank"           ] = -1;
		top.parameters["lType"           ] = 2;
		top.parameters["logged_out"      ] = false;
		top.parameters["newLoad"         ] = "on";
		top.parameters["rubrikId"        ] = link.replace(/^\[([0-9]*)_[0-9]*\]/,"$1");
		top.parameters["suchbereiche"    ] = link;
		top.parameters["template"        ] = "stotax.xtp";

		if (top.work &&
		    top.work.main)
			top.location_document = top.work.main.location.href;

		if (!top.work.hidden)
			top.work.location.href = "/recherche/index.html";
		else
			top.work.hidden.location.href = "/XSearch";
		return;
	}
}

function getStatistik() {
	top.work.location.href="/servlet/de.wmc.stv.xsearchips.stotax.Servlet?page=konto&modus=5&sort=zeitpunkt desc";
}

