//<script language="JavaScript">

function SubmitSearch(lngSearchAreaID)
{
	// searchstring aufbereiten
	var queryval = UpdateSearchstring("extended");
	
	var TlngSearchArea = 0;

	if (lngSearchAreaID) {
		TlngSearchArea = lngSearchAreaID;
	} else if (document.SearchForm.elements.searcharea) {
		TlngSearchArea = document.SearchForm.elements.searcharea.value;
	}

	document.forms.SearchForm.elements.z3.value = TlngSearchArea;	
	document.forms.SearchForm.elements.z.value = queryval;
	document.forms.SearchForm.submit();
}


function UpdateSearchstring(mode) {
	var currPreview = "";
	currPreview = GetSearchstring(mode);

	if (mode == "extended") {
	
		var showValue= "";
		showValue = currPreview.replace(/AND NOT/g, "ABER NICHT");
		showValue = showValue.replace(/AND/g, "UND");
		showValue = showValue.replace(/OR/g, "ODER");
	
		document.getElementById("preview").innerText = showValue;
	}

	return currPreview;
}


function GetSearchstring(mode, searchString) {

	if (!searchString && document.forms.SearchForm) {
		if (document.forms.SearchForm.elements.SEARCH)
			searchString = document.forms.SearchForm.elements.SEARCH.value;
	}

	var newSearchstring = "";
  
  if (mode == "extended") {

		// suchstring saugen
		searchString = ""; //document.SearchForm.SEARCH.value;

		if (searchString != "") {
			var lstPhrases = new Array();
		
			// wenn phrasen etc vorhaden, alles extrahieren und plazierung merken
			var lngStartPosition = searchString.indexOf("\"");
			var lngEndPosition = 0;
			if (lngStartPosition >= 0) {
				var i = 0;
				while (lngStartPosition >= 0) {
					// nächste position ermitteln
					lngEndPosition = searchString.indexOf("\"", lngStartPosition + 1);
					
					// teil extrahieren und durch index ersetzen
					lstPhrases[i] = searchString.slice(lngStartPosition, lngEndPosition + 1);
					searchString = searchString.replace(lstPhrases[i], "§" + i + "§");
					
					// nächste Position saugen
					lngStartPosition =  searchString.indexOf("\"", lngEndPosition + 1);
					
					// zähler hochsetzen
					i++;
				}
			}

			// begriff bezüglich seiner Leerzeichen zum splitten überarbeiten
			// ??????????????????????????????????????????????????????????????
			
			// begriff splitten
			var lstParts = searchString.split(" ");
			// einzene teile beschneiden und wider zusammensetzen
			for (var j = 0; j < lstParts.length; j++) {
				var tmpValue = new String(lstParts[j]);
				
				// vorne und hinten leerzeichen abschneiden
				tmpValue = tmpValue.replace(/\s$/, "");
				tmpValue = tmpValue.replace(/^\s/, "");

//				if (tmpValue.search(/§\d.§/) == -1) {
				if ((tmpValue.indexOf("§") != 0) && (tmpValue.lastIndexOf("§") != (tmpValue.length-1))) {
					if (tmpValue.indexOf("*") > 0) {
						lstParts[j] = "\"" + tmpValue + "\"";
					}
				}
			}

			var blnConnected = false;
			for (m = 0; m < lstParts.length; m++) {
				var tmpValue = lstParts[m];
				if (tmpValue != "") {
					if ((m > 0)) {
						if (tmpValue == "OR" || tmpValue == "AND" || tmpValue == "NOT") {
							newSearchstring += " " + tmpValue + " ";
							blnConnected = true;
						} else {
							if (!blnConnected) {
								newSearchstring += " AND ";
							}
							newSearchstring += tmpValue;
							blnConnected = false;
						}
					} else {
						newSearchstring += tmpValue;
					}
				}
			}
			
			// phrasen wieder einsetzen
			for (var k = 0; k < lstPhrases.length; k++) {
				newSearchstring = newSearchstring.replace("§" + k + "§", lstPhrases[k]);
			}
		} else {
			// alle woerter
			var allwords = document.SearchForm.allwords.value;
			allwords = allwords.replace(/\s$/, "");
			allwords = allwords.replace(/^\s/, "");
			lstParts = allwords.split(" "); allwords="";

			for (j = 0; j < lstParts.length; j++) {
				var tmpValue = lstParts[j];
				
				if (tmpValue == "") {
					continue;
				}
				if (j != 0) {
					allwords += " AND ";
				}
				allwords += tmpValue;
			}

			// exakte phrase
			var exactphrase = document.SearchForm.phrase.value;
			exactphrase = exactphrase.replace(/\s$/, "");
			exactphrase = exactphrase.replace(/^\s/, "");
			if (exactphrase!="") {
				exactphrase = "\"" + exactphrase + "\"";
			}

			// einen dieser woerter
			var oneword = document.SearchForm.oneword.value;
			oneword = oneword.replace(/\s$/, ""); oneword = oneword.replace(/^\s/, "");
			lstParts = oneword.split(" "); oneword="";
			
			for (j = 0; j < lstParts.length; j++) {
				var tmpValue = lstParts[j];
			
				if (tmpValue == "") {
					continue;
				}
				if (j != 0) {
					oneword += " OR ";
				}
				oneword += tmpValue;
			}

			// diese nicht
			var excludeword = document.SearchForm.excludewords.value;
			excludeword = excludeword.replace(/\s$/, ""); excludeword = excludeword.replace(/^\s/, "");
			lstParts = excludeword.split(" "); excludeword="";
			
			for (j = 0; j < lstParts.length; j++) {
				var tmpValue = lstParts[j];
				
				if (tmpValue == "") {
					continue;
				}
				if (j != 0) {
					excludeword += " AND NOT ";
				}
				excludeword += tmpValue;
			}

			// wortbestandteile
			var wordpart = document.SearchForm.wordpart.value;
			wordpart = wordpart.replace(/\s$/, "");
			wordpart = wordpart.replace(/^\s/, "");
			lstParts = wordpart.split(" "); wordpart="";
			
			for (j = 0; j < lstParts.length; j++) {
				var tmpValue = lstParts[j];
			
				if (tmpValue == "") {
					continue;
				}
				if (j != 0) {
					wordpart += " AND ";
				}
				wordpart += "\"" + tmpValue + "*\"";
			}

			// zusammenbauen
			if (allwords != "") {
				newSearchstring += ((newSearchstring=="")?"":" AND ") + allwords;
			}
			if (exactphrase != "") {
				newSearchstring += ((newSearchstring=="")?"":" AND ") + exactphrase;
			}
			if (oneword != "") {
				newSearchstring += ((newSearchstring=="")?"":" AND ") + "(" + oneword + ")";
			}
			if (wordpart != "") {
				newSearchstring += ((newSearchstring=="")?"":" AND ") + wordpart;
			}
			if (excludeword != "") {
				newSearchstring += ((newSearchstring=="")?"":" AND NOT " + excludeword);
			}
		}        
	}

	return newSearchstring;
}

function FormatSearchStringGerman(phrase) {

	var newPhrase = "";
	
	if (phrase != "") {
		newPhrase = phrase.replace(/AND NOT/g, "ABER NICHT");
		newPhrase = newPhrase.replace(/AND/g, "UND");
		newPhrase = newPhrase.replace(/OR/g, "ODER");
	}
	
	return newPhrase;
}

function ExtractSearchPhrases(phrase) {
	
	var workphrase = phrase;
	var orValues = "";
	var notValues = "";
	var phraseValues = "";
	var partValues = "";
	var allValues = "";
	
	var singleparts = workphrase.split(' AND ');

	for (i = 0; i < singleparts.length; i++) {
		var currValue = singleparts[i];
		var indx = -1;

		// ist es ein wortausschluss?
		indx = currValue.indexOf('NOT');
		if (indx >= 0) {
			if (notValues.length > 0) {
				notValues += " ";
			}
			notValues += currValue.replace(/NOT /g, '');
			continue;
		}
		
		// ist es ein wortbestandteil?
		indx = currValue.indexOf('*');
		if (indx >= 0) {
			if (partValues.length > 0) {
				partValues += " ";
			}
			var tmpValue = currValue.substr(0, indx);
			tmpValue = tmpValue.substr(1);
			partValues += tmpValue;
			continue;
		}
		
		// ist es eine phrase?
		indx = currValue.indexOf('"');
		if (indx >= 0) {
			var tmpValue = currValue.substring(0, (currValue.length - 1));
			tmpValue = tmpValue.substr(1);
			phraseValues += tmpValue;
			continue;
		}
		
		// ist es eine oderverbindung?
		indx = currValue.indexOf('(');
		if (indx >= 0) {
			var phraseparts = currValue.split(' OR ');
			orValues = phraseparts.join(' ');
			orValues = orValues.replace(/\(/g, '');
			orValues = orValues.replace(/\)/g, '');
			continue;
		}

		// wenn alles nicht trifft, dann ist es ein normales Wort
		if (indx < 0) {
			if (allValues.length > 0) {
				allValues += " ";
			}
			allValues += currValue;
			continue;
		}
	}

	// felder befüllen
	document.SearchForm.oneword.value = orValues;
	document.SearchForm.excludewords.value = notValues;
	document.SearchForm.wordpart.value = partValues;
	document.SearchForm.phrase.value = phraseValues;
	document.SearchForm.allwords.value = allValues;

	// Vorschau aktivieren
	UpdateSearchstring('extended');
}
// Neue Suche Ende

