﻿function focus_field(id) {
  if (window.location.href.search(/#/)==-1) {
    var suchbegriff = document.getElementById(id);
    if (suchbegriff != null) {
      suchbegriff.focus();
    }
  }
}
function input_loeschen(id) {
  var field = document.getElementById(id);
  if (field != null) {
      field.value = "";
  }
}
function switch_tab(id) {
  var button1 = document.getElementById("area1_btn");
  var button2 = document.getElementById("area2_btn");
  // Area 1
  var divLandingpageTop = document.getElementById("divLandingpageTop");
  var divPlaintextMain = document.getElementById("divPlaintextMain");
  // Area 2
  var divPlaintextTop = document.getElementById("divPlaintextTop");
  var divListenFilter = document.getElementById("divListenFilter");
  var divArtikelliste = document.getElementById("divArtikelliste");
  //
  if (button1 != null) {
    if (id == 1) {
      divLandingpageTop.style.display = "block";
      divPlaintextMain.style.display = "block";
      divPlaintextTop.style.display = "none";
      divListenFilter.style.display = "none";
      divArtikelliste.style.display = "none";
      button1.className = "active";
      button2.className = "inactiv";
    }
    else {
      divLandingpageTop.style.display = "none";
      divPlaintextMain.style.display = "none";
      divPlaintextTop.style.display = "block";
      divListenFilter.style.display = "block";
      divArtikelliste.style.display = "block";
      button1.className = "inactive";
      button2.className = "active";
    }
  }
  return false;
}
function oeffneFenster(url) {
	var fenster = window.open(url, 'neuesFenster', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=100,height=100,screenX=100,screenY=100,top=100,left=100');
	fenster.focus();
}
function change_graphic(id, bild_src) {
  var img = document.getElementById(id);
  img.src = bild_src;
}
function open_print(url) {
	var fenster = window.open(url, 'Printview', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=680,height=780');
	fenster.focus();
}
function bild_zentrieren() {
  if (document.images[0]) {
    var bildschirmbreite = screen.width;
    var bildschirmhoehe = screen.height;
    var imgWidth = document.images[0].width+50;
    var imgHeight = document.images[0].height+150;
    var leftpos = bildschirmbreite / 2 - imgWidth / 2;
    var toppos = bildschirmhoehe / 2 - imgHeight / 2;
    window.moveTo(leftpos, toppos);
    window.resizeTo(imgWidth, imgHeight);
  }
  self.focus();
}
function noSpam() {
  var a = document.getElementsByTagName("a");
  for (var i = a.length-1; i >= 0; i--) {
    if (a[i].className.search(/\bemail\b/) != -1) {
      var email = a[i].firstChild.data + "@" + a[i].lastChild.data;
      a[i].innerHTML = email;
      a[i].href = "mailto:" + email;
    }
  }
}
function posNavi2Top() {
  // var divNaviRedOben = document.getElementById("divNaviRedOben");
  // var divNaviRedUnten = document.getElementById("divNaviRedUnten");
  // divNaviRedOben.innerHTML = divNaviRedUnten.innerHTML;
  var divSprachauswahlOben = document.getElementById("divSprachauswahlOben");
  var divSprachauswahlUnten = document.getElementById("divSprachauswahlUnten");
  if (divSprachauswahlOben || divSprachauswahlUnten) {
    divSprachauswahlOben.innerHTML = divSprachauswahlUnten.innerHTML;
    divSprachauswahlUnten.style.display = 'none';
  }
}
function startBlaetterkatalog(url) {
	var blaetterKatalogFenster = window.open(url, "blaetterKatalogFenster", "width=980,height=710,left=0,top=0,toolbar=no,status=no,menubar=no,resizable=yes,location=no");
	blaetterKatalogFenster.focus();
}
function getUrlWithoutSession() {
  var url = "http://" + window.location.hostname;
  if (window.location.protocol == "http:") {
    url = window.location.href;
    var pfad = window.location.pathname;
    if (pfad.length > 30) {
      if (pfad.substring(0,4) == "/(s(") {
        url = "http://" + window.location.hostname + pfad.substring(30, pfad.length) + window.location.search;
      }
    }
  }
  return url;
}
function bookmarkOver(dienst) {
  var id_bookmark_dienst = document.getElementById("id_bookmark_dienst");
  if (id_bookmark_dienst != null) {
    id_bookmark_dienst.innerHTML = dienst;
  }
}
function bookmarkOut() {
  var id_bookmark_dienst = document.getElementById("id_bookmark_dienst");
  if (id_bookmark_dienst != null) {
    id_bookmark_dienst.innerHTML = "&nbsp;";
  }
}
function changeCountry() {
	var divCC = document.getElementById("divChangeCountry");
	if (divCC != null) {
	  if (divCC.style.visibility == "visible" ) divCC.style.visibility = "hidden"
	  else divCC.style.visibility = "visible"
	}
	
}
function getBookmarks() {
  var url = Url.encode(getUrlWithoutSession());
  var titel = Url.encode(document.title);
  var bm_html = "";
  img_google = new Image();
	img_google.src = "/grafik/bookmarks/google.gif";
	bm_html += "<a href=\"http://www.google.com/bookmarks/mark?op=add&amp;hl=de&amp;bkmk=" + url + "&amp;title=" + titel + "\" title=\"Google\" target=\"_blank\" onmouseover=\"bookmarkOver(this.title)\" onmouseout=\"bookmarkOut()\">"
  bm_html += "<img src='/grafik/bookmarks/google.gif' width='18' height='18' alt='Google' style='border:none;' />"
  bm_html += "</a> "
	img_delicous = new Image();
	img_delicous.src	= "/grafik/bookmarks/delicious.gif";
	bm_html += "<a href=\"https://secure.del.icio.us/login?url=" + url + "&amp;title=" + titel + "\" title=\"Delicous\" target=\"_blank\" onmouseover=\"bookmarkOver(this.title)\" onmouseout=\"bookmarkOut()\">"
  bm_html += "<img src='/grafik/bookmarks/delicious.gif' width='18' height='18' alt='Delicous' style='border:none;' />"
  bm_html += "</a> "
	img_linkarena = new Image();
	img_linkarena.src = "/grafik/bookmarks/linkarena.gif";
	bm_html += "<a href=\"http://linkarena.com/bookmarks/addlink/?url=" + url + "&amp;title=" + titel + "\" title=\"Linkarena\" target=\"_blank\" onmouseover=\"bookmarkOver(this.title)\" onmouseout=\"bookmarkOut()\">"
  bm_html += "<img src='/grafik/bookmarks/linkarena.gif' width='18' height='18' alt='Linkarena' style='border:none;' />"
  bm_html += "</a> "
	img_mrwong = new Image();
	img_mrwong.src = "/grafik/bookmarks/mrwong.gif";
	bm_html += "<a href=\"http://www.mister-wong.de/index.php?action=addurl&amp;bm_url=" + url + "&amp;bm_description=" + titel + "\" title=\"Mr.Wong\" target=\"_blank\" onmouseover=\"bookmarkOver(this.title)\" onmouseout=\"bookmarkOut()\">"
  bm_html += "<img src='/grafik/bookmarks/mrwong.gif' width='18' height='18' alt='Mr.Wong' style='border:none;' />"
  bm_html += "</a> "
	img_webnews = new Image();
	img_webnews.src = "/grafik/bookmarks/webnews.gif";
  bm_html += "<a href=\"http://www.webnews.de/einstellen?url=" + url + "&amp;title=" + titel + "\" title=\"Webnews\" target=\"_blank\" onmouseover=\"bookmarkOver(this.title)\" onmouseout=\"bookmarkOut()\">"
  bm_html += "<img src='/grafik/bookmarks/webnews.gif' width='18' height='18' alt='Webnews' style='border:none;' />"
  bm_html += "</a> "
	img_yahoo = new Image();
	img_yahoo.src	= "/grafik/bookmarks/yahoo.gif";
  bm_html += "<a href=\"http://myweb2.search.yahoo.com/myresults/bookmarklet?u=" + url + "&amp;t=" + titel + "\" title=\"Yahoo myWeb\" target=\"_blank\" onmouseover=\"bookmarkOver(this.title)\" onmouseout=\"bookmarkOut()\">"
  bm_html += "<img src='/grafik/bookmarks/yahoo.gif' width='18' height='18' alt='Yahoo myWeb' style='border:none;' />"
  bm_html += "</a> "
	img_yigg = new Image();
	img_yigg.src = "/grafik/bookmarks/yigg.gif";
  bm_html += "<a href=\"http://yigg.de/neu?exturl=" + url + "&amp;exttitle=" + titel + "\" title=\"YiGG\" target=\"_blank\" onmouseover=\"bookmarkOver(this.title)\" onmouseout=\"bookmarkOut()\">"
  bm_html += "<img src='/grafik/bookmarks/yigg.gif' width='18' height='18' alt='YiGG' style='border:none;' />"
  bm_html += "</a> "
  var bm = document.getElementById("id_bookmarks");
  if (bm != null) {
    bm.innerHTML = bm_html;
  }
}
var Url = {
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";
        for (var n = 0; n < string.length; n++) {
            var c = string.charCodeAt(n);
            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
        }
        return utftext;
    },
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        while ( i < utftext.length ) {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    }
}

function FFSuggest() {
	var pRequest;
	var pLayer;
	var pDebug = false;
	var pInstanceName	= "suggest";
	var pSearchURL = "/Handler/FFSuggestHandler.ashx";
	var pQueryParamName	= "suchbegriff";
	var pSuchFeld = "id_schnellsuche";
	var pFormName	= "id_formschnellsuche";
	var pLayerName = "suggestLayer";
	var pQueryInput;
	var pSuggest;
	var pLastQuery;
	var pCurrentSelection	= 0;
	var pCellSpacing = 2;
	var pHighlightBgColor = "#CC3300";
	var pHighlightTextColor = "#FFFFFF";
	var pStandardBgColor = "#FCFCFC";
	var pStandardTextColor = "#333333";
	this.init = function() {
		pQueryInput = document.getElementById("id_schnellsuche");
		pQueryInput.onkeyup	= handleKeyPress;
		pQueryInput.onfocus	= showLayer;
		pQueryInput.onblur = hideLayer;
		frmSchnellsuche = getFFSuchForm();
		frmSchnellsuche.onsubmit = handleSubmit;
	}
	this.setHighlightColors = function(highlighBackgroundColor, highlighTextColor) {
		pHighlightBgColor	= highlighBackgroundColor;
		pHighlightTextColor	= highlighTextColor;
	}
	this.setStandardColors = function(standardBackgroundColor, standardTextColor) {
		pStandardBgColor = standardBackgroundColor;
		pStandardTextColor = standardTextColor;
	}
	this.setCellspacing = function(cellspacing) {
		pCellSpacing = cellspacing;
	}
	function handleSubmit() {
	  if (pSuggest) {
		  if (pSuggest[pCurrentSelection] != undefined) {
			  if (pSuggest[pCurrentSelection].split('###')[1] == "Kategorie") {
			    var ffSearchForm = getFFSuchForm();
				  ffSearchForm["filterkategorie"].value = "__" + pSuggest[pCurrentSelection].split('###')[0] + "__";
				  var ffSuchFeld = getFFSuchFeld();
				  ffSuchFeld.value = "";
			  }
			  else {
			    var ffSuchFeld = getFFSuchFeld();
				  ffSuchFeld.value = pSuggest[pCurrentSelection].split('###')[0];
			  }
		  }
		}
	}
	this.handleClick = function() {
		if (pSuggest[pCurrentSelection] != undefined) {
			if (pSuggest[pCurrentSelection].split('###')[1] == "Kategorie") {
			  var ffSearchForm = getFFSuchForm();
				ffSearchForm["filterkategorie"].value = "__" + pSuggest[pCurrentSelection].split('###')[0] + "__";
				ffSearchForm[pQueryParamName].value = "";
				ffSearchForm[pFormname].submit();
			}
			else {
			  var ffSuchFeld = getFFSuchFeld();
				ffSuchFeld.value = pSuggest[pCurrentSelection].split('###')[0];
				var ffSearchForm = getFFSuchForm();
				ffSearchForm.submit();
			}
		}
	}
	this.handleMouseOver = function(pos) {
		var tblCell = getTableCell(pos);
		unmarkAll();
		if (tblCell != null) {
			highlightSuggest(tblCell);
			pCurrentSelection = pos;
		}
	}
	this.handleMouseOut = function(pos) {
		var tblCell = getTableCell(pos);
		if (tblCell != null) {
			unmarkSuggest(tblCell);
			pCurrentSelection = -1;
		}
	}
	function handleKeyPress(evt) {
		evt = (evt) ? evt : ((event) ? event : null);
		var keyCode = evt.keyCode;
		if (keyCode == 38) {
			moveSelection("up");
		} else if (keyCode == 40) {
			moveSelection("down");
		} else {
			if (pQueryInput.value == "") {
				hideLayer();
				if (pLayer != null) pLayer.innerHTML = "";
				return null;
			}
			if (pLastQuery != pQueryInput.value) startAjax();
			pLastQuery = pQueryInput.value;
		}
	}
	function moveSelection(direction) {
		var pos = pCurrentSelection;
		if (direction == "up")	pos--;
		else pos += 1;
		if (pos < 0) {
			unmarkAll();
			pQueryInput.focus();
			pCurrentSelection	= -1;
		} else {
			var tblCell = getTableCell(pos);
			if (tblCell != null) {
				unmarkAll();
				highlightSuggest(tblCell);
				pCurrentSelection = pos;
			}
		}
		var query = pQueryInput.value;
		pQueryInput.value = "";
		pQueryInput.focus();
		pQueryInput.value = query;
	}
	function startAjax() {
		var query = pQueryInput.value;
		var requestURL = pSearchURL + "?" + pQueryParamName + "=" + query + "&ajax=1";
		var lang = document.getElementById("ff_lang");
		if (lang != null) {
		  requestURL = requestURL + "&lang=" + lang.firstChild.data;
		}
		try {
			if( window.XMLHttpRequest ) {
				pRequest = new XMLHttpRequest();
			} else if( window.ActiveXObject ) {
				pRequest = new ActiveXObject( "Microsoft.XMLHTTP" );
			} else {
				if (pDebug) alert( "" );
			}
			pLayer = document.getElementById(pLayerName);
			if (pLayer != null) {
				if (query != "") {
					pRequest.open( "GET", requestURL, true );
					pRequest.onreadystatechange = callbackAjax;
					pRequest.send( null );
				} else {
					hideLayer();
				}
			} else {
				if (pDebug) alert( "no layer for output found" );
			}
		} catch( ex ) {
			hideLayer();
			if (ex == undefined) {
				if (pDebug) alert( "Error: " + ex.getmessage );
			} else {
				if (pDebug) alert( "Error: " + ex );
			}
		}
	}
	function hideLayer() {
		if (pLayer != null) {
			pLayer.style.display = "none";
		}
	}
	this.hideLayerOutsideCall = function() {
		if (pLayer != null) {
			pLayer.style.display = "none";
		}
	}
	function showLayer() {
		if (pLayer != null && pSuggest != null && pSuggest.length >= 1) {
			pLayer.style.display	= "block";
		}
	}
	function callbackAjax() {
		if (pRequest.readyState == 4) {
			if (pRequest.status != 200) {
				hideLayer();
				if (pDebug) alert( "Error (" + pRequest.status + "): " + pRequest.statusText );
			} else {
				handleResponse(pRequest.responseText);
			}
		}
  }
	function handleResponse(text) {
		pCurrentSelection = -1;
		pSuggest = new Array();
		pSuggest = text.split("\n");
		var spst_vorschlaege = "";
		var ff_vorschlaege = document.getElementById("ff_vorschlaege");
		if (ff_vorschlaege != null) {
		  spst_vorschlaege = ff_vorschlaege.firstChild.data
		}
		var spst_item = "";
		var ff_item = document.getElementById("ff_item");
		if (ff_item != null) {
		  spst_item = ff_item.firstChild.data
		}
		var spst_items = "";
		var ff_items = document.getElementById("ff_items")
		if (ff_item != null) {
		  spst_items = ff_items.firstChild.data
		}
		var outputText = '<table cellpadding="0" cellspacing="0" class="' + pLayerName + '" width="100%" border="0" onMouseDown="' + pInstanceName + '.handleClick();">';
		outputText += '<tr><th colspan="2" class="suggestHeader">' + spst_vorschlaege + '<th></tr>';
		var pNewSuggest = new Array();
		for (var i = 0; i < pSuggest.length; i++) {
		  if (pSuggest[i].length > 0) {
			  var firstChar = pSuggest[i].charCodeAt(0);
			  if (firstChar != 13 && firstChar != 10 && pSuggest[i].length >= 1) {
				  pNewSuggest.push(pSuggest[i]);
			  }
			}
		}
		pSuggest = pNewSuggest;
	  for (var i = 0; i < pSuggest.length; i++) {
			pSuggestParts = new Array();
			pSuggestParts = pSuggest[i].split("###");
			treffer = pSuggestParts[1];
			if (treffer == 1) {
			  treffer = treffer + ' ' + spst_item;
			}
      else if (treffer > 1) {
        treffer = treffer + ' ' + spst_items;
      }
			outputText += '<tr id="' + pLayerName + '_' + i + '" style="background-color: ' + pStandardBgColor + '; padding: 2 2 2 0px;" onMouseOver="' + pInstanceName + '.handleMouseOver(' + i + ');" onMouseOut="' + pInstanceName + '.handleMouseOut(' + i + ');">'
			            + '<td nowrap="nowrap" align="left"  class="suggestArtikel">' + pSuggestParts[0] + '</td>'
                  + '<td nowrap="nowrap" align="right" class="suggestTreffer">' + treffer + '</td>'
                  +'</tr>';
		}
		outputText += '</table>';
		if (pSuggest.length >= 1) {
			showLayer();
			pLayer.innerHTML = outputText;
		} else {
			hideLayer();
			pLayer.innerHTML = "";
		}
	}
	function highlightSuggest(tblCell) {
		tblCell.style.backgroundColor	= pHighlightBgColor;
		tblCell.style.color = pHighlightTextColor;
	}
	function unmarkSuggest(tblCell) {
		tblCell.style.backgroundColor	= pStandardBgColor;
		tblCell.style.color = pStandardTextColor;
	}
	function unmarkAll() {
		var tblCell;
		for (var i in pSuggest) {
			tblCell = getTableCell(i);
			if (tblCell != null) {
				unmarkSuggest(tblCell);
			}
		}
	}
	function getTableCell(pos) {
		var tblCell;
		tblCell = document.getElementById(pLayerName + "_" + pos);
		return tblCell;
	}	
	function getFFSuchForm() {
		var frmSearch;
		frmSearch = document.getElementById(pFormName);
		return frmSearch;
	}
	function getFFSuchFeld() {
		var inputFeld;
		inputFeld = document.getElementById(pSuchFeld);
		return inputFeld;
	}
}