//Einstellungen: 
var DoInsertNr = true;       //Art-Nr. einfügen?
var IsSearchPopup = true;    //Suchseite in einem Popup-Fenster?
var IsPopupAllowed = false;  //Darf Such-Fenster bei mehreren Seiten wieder angezeigt werden?
var strPathToHTMLPage = '../aaa_spezial/js/';  //Pfad zum searcharticle.htm
//var SearchIntegration = 'searcharticle_d.htm';   //Sprachabhängige Datei aufrufen

//Variabeln: 
var DoSearchWord = false;    //Default 
var DoShowPop = false;       //Default
var SearchWord = ''; 
var strParam = ''; 

if (!IsSearchPopup && IsPopupAllowed) {IsPopupAllowed = false;}   // Falsche Einstellung korrigieren. 

function schliessen()
		{window.close();}
		
function OpenNewWindow(Parameter, WinName)
    {
	 window.open(Parameter, WinName,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=960,height=720');
	}
	
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
  }

  var GlobalArtNr = '';             // Default
  var GlobalIndexNr = 'art_1.js';   // Default 
  var GlobalTeilNr = '1';           // Default
  
  var ActArt = new Array(); 
  var ActNr = new Array(); 

  function GoSeekPopup()
    {	

	  GlobalArtNr = document.getElementById("SeekValue").value; 
	  //GlobalArtNr = this.SuchWert.value;
	  

	  GlobalIndexNr = GoLookup(GlobalArtNr, GlobalIndexNr); 
      strParam = strPathToHTMLPage + SearchIntegration + "?anr=" + GlobalArtNr + "&atyp=" + GlobalTeilNr + "&spage=" + StartPage + "&pprog=" + ProgName; 
	  // Suchen wir Artikel-Nr in Index-Datei: 
	  if (GlobalArtNr != '')
	   {
 	    if (IsSearchPopup)
	     {
	      var popbox = window.open(strParam, "_SearchArticle",["toolbar=0", "location=0", "directories=0", "status=0", "menubar=0", "scrollbars=1", "resizable=1", "width=350", "height=390"]);
          popbox.opener = self;
          popbox.focus();
		 }
	   else
	     {
		  location.href = strParam;   
	     } 
	 }
	}
	
  function GoShowPopup(TArtNr)
    {	
      GlobalArtNr = TArtNr;	  
	  GlobalIndexNr = GoLookup(GlobalArtNr, GlobalIndexNr); 
      
	  strParam = strPathToHTMLPage  + SearchIntegration + "?anr=" + GlobalArtNr + "&atyp=" + GlobalTeilNr + "&spage=" + StartPage; 
	  // Suchen wir Artikel-Nr in Index-Datei:   
 	  if (IsSearchPopup)
	    {	  
	      var popbox = window.open(strParam, "_SearchArticle",["toolbar=0", "location=0", "directories=0", "status=1", "menubar=0", "scrollbars=1", "resizable=1", "width=400", "height=230"]);
          popbox.opener = self;
          popbox.focus();
		}
	  else
	    {
		 location.href = strParam;    
	    }
	}


  function GoLookup(TArtNr, TDefault)
    {
		var NewValue = TDefault; 

		if (ActIndex.length != 0)
		  {
			var iShow = 1; 
			// Test ob bereits das 1. ELement zu klein: 
			if (TArtNr <= ActIndex[0])
			  { 
				NewValue = "art_1.js"
			  }
		    else
			  {
			   var Zaehler = 0;
			   var bnLoop = true;  
			    
               while (bnLoop && Zaehler < ActIndex.length/3 ) 
			     {
				   var strVGL = " W "; 	
				   if (TArtNr >= ActIndex[Zaehler*3]) strVGL += " 1"; 
				   if ((ActIndex[(Zaehler*3)+1] >= TArtNr)) strVGL += " 2 ";    
                   if ((TArtNr >= ActIndex[Zaehler*3]) && (ActIndex[(Zaehler*3)+1] >= TArtNr)) 
				     {
					   NewValue = ActIndex[(Zaehler*3)+2]; 
					   GlobalTeilNr = Zaehler+1; 
					   bnLoop = false
					 }				  	 
                  Zaehler++;
				 }
				if (bnLoop) NewValue = ActIndex[((Zaehler-1)*3)+2]; 
			 }
			 return NewValue; 
		  }
		else 
		  {
			  alert("Leider nichts"); 
			  return TDefault; 
		  }

	}

    //Parameter vorhanden?
	var urlsecond=location.href.split("&"); 
	if(urlsecond[1])
	  {
       var urlterms2=urlsecond[1].split("=");	
	   if (urlterms2[0] == "Show")
	   {
	    DoShowPop = true; 
	   }		  
	  }
    
	
	var urlquery=location.href.split("?");
    if(urlquery[1])
      { 
       var urlterms=urlquery[1].split("=");	
	   if (urlterms[0] == "ArtNr")
	   {
	    DoSearchWord = true; 
	    SearchWord = urlterms[1]; 
		SearchWord = SearchWord.replace('&Show','');
		SearchWord = SearchWord.replace('#A1','');
	   }
	  }


/*-------------------------------------------------------------------
 * This is the function that actually highlights a text string by
 * adding HTML tags before and after all occurrences of the search
 * term. You can pass your own tags if you'd like, or if the
 * highlightStartTag or highlightEndTag parameters are omitted or
 * are empty strings then the default <font> tags will be used.
 --------------------------------------------------------------------*/
function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) 
{
  // the highlightStartTag and highlightEndTag parameters are optional
  if ((!highlightStartTag) || (!highlightEndTag)) {
    highlightStartTag = "<a name='A1'><font style='color:blue; background-color:yellow;'>";
    highlightEndTag = "</font>";
  }
  
  // find all occurences of the search term in the given text,
  // and add some "highlight" tags to them (we're not using a
  // regular expression search, because we want to filter out
  // matches that occur within HTML tags and script blocks, so
  // we have to do a little extra validation)
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();
    
  while (bodyText.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
      newText += bodyText;
      bodyText = "";
    } else {
      // skip anything inside an HTML tag
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
        // skip anything inside a <script> block
        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
          newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
          bodyText = bodyText.substr(i + searchTerm.length);
          lcBodyText = bodyText.toLowerCase();
          i = -1;
        }
      }
    }
  }
  
  return newText;
}

/*-------------------------------------------------------------------
 * This is sort of a wrapper function to the doHighlight function.
 * It takes the searchText that you pass, optionally splits it into
 * separate words, and transforms the text on the current web page.
 * Only the "searchText" parameter is required; all other parameters
 * are optional and can be omitted.
 -------------------------------------------------------------------*/
function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
{
  // if the treatAsPhrase parameter is true, then we should search for 
  // the entire phrase that was entered; otherwise, we will split the
  // search string so that each word is searched for and highlighted
  // individually
  if (treatAsPhrase) {
    searchArray = [searchText];
  } else {
    searchArray = searchText.split(" ");
  }
  
  if (!document.body || typeof(document.body.innerHTML) == "undefined") {
    if (warnOnFailure) {
      alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
    }
    return false;
  }
  
  var bodyText = document.body.innerHTML;
  for (var i = 0; i < searchArray.length; i++) {
	var string = searchArray[i];

	//remove parenthesis and commas
	string = string.replace('(','');
	string = string.replace(')','');
	string = string.replace(',','');

	//remove boolean operators
	if (string.indexOf("AND") == -1 && string.indexOf("OR") == -1 && string.indexOf("NEAR") == -1) {
		//do not search for empty characters
		 if (string != "") {
			 bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
		 }
	}
  }
  
  document.body.innerHTML = bodyText;
  return true;
}


function formattext(SearchWord)
  {
	  highlightSearchTerms(SearchWord);
  }


function zeigetext()
  {
	  var strSearch = document.SeekWord.SuchBegriff.value
	  //alert("DRIN TEXT = " + strSearch); 
	  highlightSearchTerms(strSearch); 
  }
  

function DoExecuteFirst()
  {
	 if (DoSearchWord) {
		    formattext(SearchWord); 
			if (DoInsertNr) {document.SeekWord.SuchWert.value = SearchWord;};
		  }
     if (DoShowPop) { if(IsPopupAllowed) GoShowPopup(SearchWord);}  
  }  
  
function DoTestSubmit()
  {
   if (document.SeekWord.SuchWert.value != '') GoSeekPopup();
  }
