/* FSuggest.js */
if( typeof gFSUGGEST == 'undefined' ){
  var gFSUGGEST = 'defined';
  var gFSuggestInstanceCounter = 0;
  var gFSuggestInstances = new Array;

  bodySelect   = true;
  function FSuggest(suggestParameters){
    gFSuggestInstances[gFSuggestInstanceCounter]=this;
    this.instance=gFSuggestInstanceCounter++;
    this.currentInput = "";
    this.timer        = null;
    // Initialisieren der flags und des Inputs
    this.clickSel     = 0;
    this.scrollSel    = 0;
    this.userInput    = "";
    this.params       = null;
    this.field        = null;
    this.selectType   = false;

    this.changeOnSubmitOfMyFormular=function(){
      // wenn nach waehlen eines Eintrags der Liste (Pfeiltasten) Enter gedrueckt wird,
      // wird nur der Container geloescht, nicht das Formular abgeschickt
      var currentField=this.field;
      while(currentField!=null){
        if(currentField.nodeName=="FORM"){
          submitFunction = currentField.onsubmit;
          currentField.onsubmit = new Function ("ereignis","return gFSuggestInstances["+this.instance+"].cbonsubmit(ereignis);");
//          currentField.onsubmit = this.cbonsubmit.bind(this);
          return;
        }
        currentField=currentField.parentNode;
      }
    };
    this.cbonsubmit=function(){
      if(SLs.container){
        if(typeof(hideWaitIcon) == "function")
          hideWaitIcon();
        SLs.inputField.focus();
        return false;
      }
      else {
        this.resetHafasSuggest();
        if(typeof(submitFunction) == "function")
          submitFunction();
        return true;
      }
    };
    this.cbonkeyup = function(ereignis){
      test = this.field;
      currentKC=(ereignis)?ereignis.keyCode:window.event.keyCode;
      if(this.timer) window.clearTimeout(this.timer);
      if(test.value.length < this.params.minChar){
        // give suggestion only when at least 4 (or other set value) characters are typed in
        if(currentKC!=40&&currentKC!=38&&currentKC!=9&&currentKC!=16&&currentKC!=13){
          this.setTripleId(""); //for widget: delete tripleID
          var count = this.instance;
          fieldIndex = count;
          SLs.inputField = this.field;
          SLs.delete_SLs();
          SLs.inputField = this.field;
          if (!SLs.container){
            //document.getElementsByTagName("body")[0].appendChild(SLs.erzeugen(this.instance));
          }
          SLs.fillWithCookieValues(this);
        }else{
          this.suggestion(currentKC);
        }
      }else if(currentKC!=40&&currentKC!=38&&currentKC!=9&&currentKC!=16&&currentKC!=13){
        this.setTripleId(""); //for widget: delete tripleID
        this.timer = window.setTimeout('gFSuggestInstances['+this.instance+'].suggestion(test, currentKC)', this.params.stopDelay);
      }else{
        this.suggestion(currentKC);
      }
    };
    this.cbonfocus = function(ereignis){
      test = this.field;
      if(this.timer) window.clearTimeout(this.timer);
      if(test.value.length < this.params.minChar)
        {
        if(SLs.container)
          SLs.delete_SLs();
        var count = this.instance;
        fieldIndex = count;
        SLs.inputField = this.field;
        SLs.fillWithCookieValues(this);
        }
    };
    this.cbonclick = function(ereignis){
       if(SLs.container)
          SLs.delete_SLs();
       currentField = (ereignis)?ereignis.target:window.event.target;
       if(currentField.value.length<this.params.minChar)
         SLs.fillWithCookieValues(this);
    };
    this.suggestion= function(kc) {
      // find index
      var count = this.instance;
      fieldIndex = count;
      SLs.inputField = this.field;

      if((kc == 40) && (SLs.container)){
        // Bei Druecken der Pfeiltaste 'runter' wird in den Eintraegen navigiert
        if(SLs.container.getElementsByTagName("div")[SLs.countList+1]){
          if(SLs.countList != -1) SLs.container.getElementsByTagName("div")[SLs.countList].className = "";
          else SLs.container.scrollTop = 0;
          SLs.countList++;
          // setzen der bisherigen Scrollhoehe, falls diese ueber die Scrollbar veraendert wurde
          SLs.container.scrollTop = SLs.currentTop * 15;
          if(SLs.countList > SLs.currentBottom){
            SLs.container.scrollTop = SLs.container.scrollTop + 15;
            SLs.currentBottom++;
            SLs.currentTop++;
          }
          SLs.container.getElementsByTagName("div")[SLs.countList].className = "selected";
          SLs.container.getElementsByTagName("div")[SLs.countList].style.width = SLs.container.clientWidth + "px";
        }
      }else if((kc == 38) && (SLs.container)){
        // Bei Druecken der Pfeiltaste 'rauf' wird in den Eintraegen navigiert
        if(SLs.countList >= 0){
          SLs.container.getElementsByTagName("div")[SLs.countList].className = "";
          SLs.countList--;
          if(SLs.countList == -1){
            this.setSLSInput(SLs,this.userInput,SLs.countList);
          }else{
            // setzen der bisherigen Scrollhoehe, falls diese ueber die Scrollbar veraendert wurde
            SLs.container.scrollTop = SLs.currentTop * 15;
            if(SLs.countList < SLs.currentTop){
              SLs.container.scrollTop = SLs.container.scrollTop - 15;
              SLs.currentTop--;
              SLs.currentBottom--;
            }
            SLs.container.getElementsByTagName("div")[SLs.countList].className = "selected";
            SLs.container.getElementsByTagName("div")[SLs.countList].style.width = SLs.container.clientWidth + "px";
          }
        }
      }else if(kc != 9 && kc != 16 && kc != 13){
        // bei Druecken der Tab-, Shift- oder der Entertaste passiert nichts...
        // erzeugt den suggestion-Container und holt die Inhalte
        // user-Eingabe merken, wird bei verlassen der Auswahlliste ueber die Pfeiltasten wieder im Feld eingetragen
        this.userInput = this.field.value;
        if (SLs.container){
          SLs.delete_SLs();
          // Markierung des ausgewaehlten Eintrags zuruecksetzen.
          SLs.countList = -1;
        }
        if((this.field.value != "") && (this.params.useSuggest)){
          document.getElementsByTagName("body")[0].appendChild(SLs.erzeugen(fieldIndex));
          if(this.params.requestType == "ajax")
            SLs.holen(this.field.value+"?", fieldIndex);
          else
            SLs.getJS(this.field.value+"?", fieldIndex);
        }
      } else if(kc == 13){
        if(SLs.container){
          if(SLs.countList > -1){
            this.setSLSInput(SLs,SLs.container.getElementsByTagName('div')[SLs.countList].lastChild.firstChild.nodeValue,SLs.countList);
          }
          SLs.inputField.focus();
          SLs.delete_SLs();
        }
      }
      // naechsten Tastendruck werten
    };
    this.leaveInputField= function (){
      if (SLs.container){
        // wenn per mouse selektiert wurde bleibt der Focus im Eingabefeld
        if(this.scrollSel && this.scrollSel == 1 && !this.clickSel){
          SLs.inputField.focus();
          this.scrollSel = 0;
        }else{
          if(this.clickSel == 1){
            SLs.inputField.focus();
            this.clickSel = 0;
          }else if(SLs.countList > -1){
            this.setSLSInput(SLs,SLs.container.getElementsByTagName('div')[SLs.countList].lastChild.firstChild.nodeValue,SLs.countList);
          }
          // im IE öffnet sich gleich wieder der Container, deshalb timeout
          window.setTimeout("SLs.delete_SLs();",200);
        }
      }
    };
    this.resetHafasSuggest= function() {
//      for(var i=0;i<gFSuggestInstanceCounter;i++){
//        if(gFSuggestInstances[i] && gFSuggestInstances[i].field){
//          gFSuggestInstances[i].field.removeAttribute('autocomplete');
//        }
//      }
    };
    this.setInputFieldValue= function(s,v){
      if(typeof s.inputField!='undefined'){
        s.inputField.value=v;
      }
    };
    this.setSLSInput= function(s,name,i){
      this.setInputFieldValue(s,name);
      var currentType = '';
      var currentEvaId = '';
      if(typeof s.sls.suggestions[i]!='undefined'){
        if(typeof s.sls.suggestions[i].id!='undefined'){
          this.setTripleId(s.sls.suggestions[i].id);
          s.sls.suggestions[i].id.match(/L=(\d+)/);
          currentEvaId = RegExp.$1;
        }
        if(typeof s.sls.suggestions[i].type!='undefined'){
          this.setLocationType(s.sls.suggestions[i].type);
          currentType = s.sls.suggestions[i].type;
        }
        if(typeof this.params.callback!='undefined'){
          this.params.callback(name,
                               s.sls.suggestions[i].xcoord,
                               s.sls.suggestions[i].ycoord,
                               currentType,
                               currentEvaId);
        }
      }
    };
    this.setTripleId= function(id){
      if(typeof this.params.type!= 'undefined'){
        if(this.field.name.charAt(this.field.name.length-1) == "G")
          var fieldname = this.field.name.replace(/G$/,"ID");
        else
          var fieldname='REQ0JourneyStops'+this.params.type+'ID';

        var field=null;
        if(document.getElementsByName(fieldname)){
          field=document.getElementsByName(fieldname)[0];
        }
        if(field==null||typeof field=='undefined'){
          field=document.createElement('input');
          field.type='hidden';
          field.name=fieldname;
          this.field.parentNode.insertBefore(field,this.field.nextSibling);
        }
        field.value=id;
      }
    };
    this.setLocationType= function(type){
      if((typeof this.params.type!= 'undefined') && ((this.field.name.match(/G$/) != null) || (this.field.name=='S')|| (this.field.name=='Z'))){
        if(this.field.name == "S")
          typeName = "REQ0JourneyStopsS0A";
        else if(this.field.name == "Z")
          typeName = "REQ0JourneyStopsZ0A";
        else
          var typeName = this.field.name.replace(/G$/,"A");
        var typeSelectbox = document.getElementsByName(typeName)[0];
        if((typeSelectbox != null) && ((typeSelectbox.nodeName == "SELECT") || ((typeSelectbox.value != 7)&&(typeSelectbox.value != 255))))
        {
          typeSelectbox.value = type;
        }
        else if(typeSelectbox != null)
          {
          var field = document.getElementById("ignoreTypeCheck");
          if((field == null) || (typeof(field) == "undefined"))
            {
            field=document.createElement('input');
            field.type='hidden';
            field.name="ignoreTypeCheck";
            field.id="ignoreTypeCheck";
            field.value="yes"
            this.field.parentNode.insertBefore(field,this.field);
            }
          }
        else
          {
          var field=document.createElement('input');
          field.type='hidden';
          field.name=typeName;
          field.value=type;
          this.field.parentNode.insertBefore(field,this.field);

          }
      }
    };
//    initialize: function(suggestParameters){
      if(this.instance==0){
        document.body.onmousedown = function(){
          if(document.getElementById("suggestion") && bodySelect){
            SLs.delete_SLs();
          }
          bodySelect = true;
        };
      }
      this.params = { loc         : suggestParameters.loc,
                      type        : suggestParameters.type,
                      locType     : suggestParameters.locType,
                      minChar     : suggestParameters.minChar,
                      requestURL  : suggestParameters.requestURL,
                      cookiename  : suggestParameters.cookiename,
                      stopDelay   : suggestParameters.stopDelay,
                      callback    : suggestParameters.callback,
                      requestType : suggestParameters.requestType,
                      useSuggest  : suggestParameters.useSuggest,
                      historyType : suggestParameters.historyType,
                      filter      : suggestParameters.filter,
                      height      : suggestParameters.height,
                      width       : suggestParameters.width,
                      encoding    : suggestParameters.encoding}
      if(typeof(this.params.requestType) == "undefined")
        this.params.requestType = "js";
      if(typeof(this.params.useSuggest) == "undefined")
        this.params.useSuggest = true;
      if((typeof(this.params.encoding) != "undefined") && (this.params.encoding == "utf-8"))
        this.params.requestURL = this.params.requestURL.replace(/&/,"&encoding=utf-8&");
      if(typeof(this.params.height) == "undefined")
        this.params.height = "auto";
      if(typeof(this.params.width) == "undefined")
         this.params.width = "310px";
      if(typeof this.params.loc!="undefined" &&
         typeof document.getElementById(this.params.loc)!="undefined"){
        this.field = document.getElementById(this.params.loc);
        //if(this.instance==0)this.changeOnSubmitOfMyFormular();
        var addSubmitEventHandler = true;
        for(var i = 0; i < this.instance; i++)
          {
          if(gFSuggestInstances[i].field.form == this.field.form)
            addSubmitEventHandler = false;
          }
        if(addSubmitEventHandler)
          this.changeOnSubmitOfMyFormular();
        this.field.setAttribute("autocomplete","off");
        this.field.onfocus = function(){
          currentInput = this.value;
        }
//        this.field.onkeyup = this.cbonkeyup.bind(this);
        this.field.onkeyup = new Function ("ereignis","gFSuggestInstances["+this.instance+"].cbonkeyup(ereignis);");
        this.field.onfocus = new Function ("ereignis","gFSuggestInstances["+this.instance+"].cbonfocus(ereignis);");
        // timeout wird benoetigt, da onblur sonst die korrekte durchfuehrung eines evtl. onclick auf einen der Eintraege behindert
        this.field.onblur = new Function ("ereignis","window.clearTimeout(gFSuggestInstances["+this.instance+"].timer);gFSuggestInstances["+this.instance+"].leaveInputField();");
        //this.field.onclick = new Function("ereignis", "if(SLs.container)SLs.delete_SLs();");
        //Event.observe(this.field, 'focus', this.cbonclick.bind(this), false);
        if(this.params.setFocus == "yes" && (this.instance==0)){

          // only focus an element which is not hidden or in a hidden container
          var parent = this.field;
          var bFocus = true;
          // while parent node exists and isn't body or html tag
          while (parent) {
            if ((parent.tagName == "BODY") || (parent.tagName == "HTML")) {
              break;
            }
            // The parent node is hidden this so this element is no good.
            if ((parent.style.display == "none") || (parent.style.visibility == "hidden")) {
              bFocus = false;
              break;
            }
            // climb up DOM tree
            parent = parent.parentNode;
          }
          if (bFocus) {
            this.field.focus();
          }
        }
      }
//    },
  }
}

// suggestion-Layer-Objekt 'SuggestionLocations'
var SLs = {
  container: null,    // DIV-Container
  ajax: false,      // XMLHttpRequest-Objekt
  div_breite: "auto",   // Breite und ...
  div_hoehe: 170,     // Hoehe des suggestion-Containers
  countList: -1,
  currentTop: 0,
  currentBottom: 9,
  inputField: null,
  pos_x: 0,
  pos_y: 0,
  sls: null,
  erzeugen: function(fieldIndex) {
    // erzeugt Div-Container
    SLs.container = document.createElement("div");
    SLs.container.id = "suggestion";
    // Opera braucht zwingend eine feste Breite, um 'overflow' korrekt zu interpretieren
    if(navigator.userAgent.toLowerCase().indexOf("opera")>-1) SLs.container.style.minWidth = "148px";
    SLs.container.style.width = gFSuggestInstances[fieldIndex].params.width;
    SLs.container.style.height = gFSuggestInstances[fieldIndex].params.height;
    // Positionieren direkt unterhalb des Eingabefelds (L&F einer LB)
    SLs.pos_x = SLs.getPosX(SLs.inputField)+0;
    SLs.pos_y = SLs.getPosY(SLs.inputField)+16;
    SLs.container.style.left = SLs.pos_x + "px";
    SLs.container.style.top = SLs.pos_y + "px";
    // MSIE/Opera und Safari(Windows) setzt bei scrollen in der Auswahllist den Focus auf den Container um,
    // das Eingabefeld wird also verlassen und der Container loescht sich quasi selbst...
    // Das hier gesetzte Flag faengt diesen Fall in leaveInputField() ab.
    SLs.container.onmousedown = new Function ("ereignis", "bodySelect=false;if((navigator.userAgent.toLowerCase().indexOf('msie')>-1)||(navigator.userAgent.toLowerCase().indexOf('safari')>-1))gFSuggestInstances[fieldIndex].scrollSel=1;")
    // zurueckgeben
    return SLs.container;
  },
  holen: function(input, fieldIndex) {

     // Hack um ajax request nur für Bhf abzusetzen
     if(gFSuggestInstances[fieldIndex].params.locType)
       {
       var currentLocType = gFSuggestInstances[fieldIndex].params.locType;
       }
     else
       {
       var typeSelectName = gFSuggestInstances[fieldIndex].field.name.replace(/G$/,"A")
       var typeSelectbox = document.getElementsByName(typeSelectName)[0];
       if(typeSelectbox)
         {
         var currentLocType = typeSelectbox.value;
         }
       }
     if((currentLocType != null) && (currentLocType != 1))
       {
       SLs.delete_SLs();
       return false;
       }

    // Ajax-Verbindung herstellen
    try {     // W3C-Standard
      SLs.ajax = new XMLHttpRequest();
    } catch(w3c) {
      try {     // Internet Explorer
        SLs.ajax = new ActiveXObject("Msxml2.XMLHTTP");
      } catch(msie) {
        try {   // Internet Explorer alt
          SLs.ajax = new ActiveXObject("Microsoft.XMLHTTP");
        } catch(msie_alt) {
          SLs.delete_SLs();
          for(var i=0;i<gFSuggestInstances.length;i++){
            var f=document.getElementById(gFSuggestInstances[i].params.loc);
            if(f){
              f.onfocus = null;
              f.onkeyup = null;
              f.onblur = null;
              f.onclick = null;
            }
          }
          return false;
        }
      }
    }
    // Datei anfordern (asynchron)
    var tempURL = gFSuggestInstances[fieldIndex].params.requestURL+input+"&";
    // check if there is a location type selectbox
    if((typeSelectbox != null) && (typeSelectbox.nodeName == "SELECT"))
      {
      var replaceTypeRegExp = /REQ0JourneyStopsS0A=(\d+)&/;
      tempURL = tempURL.replace(replaceTypeRegExp,"REQ0JourneyStopsS0A="+typeSelectbox.value+"&");
      gFSuggestInstances[fieldIndex].selectType = true;
      }
    /*
    if(gFSuggestInstances[fieldIndex].params.locType && document.getElementById(gFSuggestInstances[fieldIndex].params.locType+'_hidden')){
      var currentLocType = document.getElementById(gFSuggestInstances[fieldIndex].params.locType+'_hidden').value;
      tempURL = tempURL+gFSuggestInstances[fieldIndex].params.requestURL2+currentLocType+"&";
    }
    */
    if((gFSuggestInstances[fieldIndex].params.filter == "germany") && (currentLocType) && (currentLocType == 1))
      tempURL = tempURL.replace(/&/,"&REQ0JourneyStopsS0F=selectStationNumberFilter;008000000;008099999&");
    SLs.ajax.open('GET', tempURL, true);
    SLs.ajax.setRequestHeader('Content-Type', 'text/xml');
    // umgeht Internet Explorers Caching von GET-Anfragen
    SLs.ajax.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
    SLs.ajax.send(null);
    SLs.ajax.onreadystatechange = function() {
      // wenn Ergebnis komplett empfangen ist ...
      if (SLs.ajax.readyState == 4) {
        // Fehler bei der Uebertragung (Server nicht gefunden...)
        if (SLs.ajax.status != 200) {
          SLs.delete_SLs();
          return false;
        }
        // schreibe alle Vorschlaege in sls
        if(SLs.ajax.responseText){
          eval(SLs.ajax.responseText);
          return SLs.showSuggestion();
        }
        var nicht_gefunden = document.createTextNode("No suggestions for your input '" + input + "'.");
        SLs.container.appendChild(nicht_gefunden);
        return false;
      }
    }
  },
  showSuggestion: function(){
     gFSuggestInstances[fieldIndex].params.requestURL.match(/REQ0JourneyStopsS0A=(\d)/);
     var locTypeRequest = RegExp.$1;
     if(navigator.userAgent.indexOf("Safari")>-1)
       var typeSpanStyle = "right: 18px;";
     else if (navigator.userAgent.indexOf("MSIE")>-1)
       var typeSpanStyle = "right: 18px;";
     else
       var typeSpanStyle = "right: 0px;";
     var divElements = new Array();
     for(var i = 0; i < SLs.sls.suggestions.length; i++) {
       // Werden Eingaben schnell hintereinander geloescht und das Eigabefeld ist leer,
       // wird der Container geloescht waehrend die Funktion noch laeuft, daher Abbruch.
       if(SLs.container){
         // create and append new text node and new div
         divElements[i] = SLs.container.appendChild(document.createElement("div"));
         divElements[i].id = i;
         if(((locTypeRequest == 7)||(locTypeRequest == 255)) && (typeof(SLs.sls.suggestions[i].typeStr) != "undefined") && (!gFSuggestInstances[fieldIndex].selectType))
           var currentLocType = SLs.sls.suggestions[i].typeStr;
         else
           var currentLocType = "";
         divElements[i].innerHTML = "<span style='display: block;position: absolute; background-color: #fff;"+typeSpanStyle+"'>"+ currentLocType +"</span><span>"+ SLs.sls.suggestions[i].value +"</span>";
         // das Eingabefeld wird verlassen und der Container löscht sich quasi selbst...
         // Das hier gesetzte Flag fängt diesen Fall in leaveInputField() ab.
         divElements[i].onmousedown = function() {
           gFSuggestInstances[fieldIndex].clickSel = 1;
           gFSuggestInstances[fieldIndex].setSLSInput(SLs,this.lastChild.firstChild.nodeValue,parseInt(this.id));
         }
       }
       else return false;
     }
     // Border wird erst hier gesetzt damit leeres div nicht zu sehen ist...
     SLs.container.style.borderWidth = "1px";
     SLs.container.style.zIndex = "10002";
     if(SLs.sls.length < 10)
       {
       SLs.container.style.height = "auto";
       if((gFSuggestInstances[fieldIndex].params.height != "auto") && (SLs.container.offsetHeight > gFSuggestInstances[fieldIndex].params.height.match(/\d./)[0]))
        SLs.container.style.height = gFSuggestInstances[fieldIndex].params.height;
       }
     //else SLs.container.style.height = SLs.div_hoehe + "px";
     else SLs.container.style.height = gFSuggestInstances[fieldIndex].params.height;
     SLs.container.style.maxHeight = SLs.div_hoehe + "px";
     if(navigator.userAgent.toLowerCase().indexOf("msie")>-1) SLs.container.style.width = SLs.container.clientWidth+20;
     if(navigator.userAgent.toLowerCase().indexOf('msie')>-1 && navigator.userAgent.indexOf('7.0')<0){
       SLs.hideSelect();
     }
     if(SLs.sls.length < 1) SLs.delete_SLs();
     return true;
  },
  getJS: function(input,fieldIndex) {
     // Datei anfordern (asynchron)
     var tempURL = gFSuggestInstances[fieldIndex].params.requestURL+input+"&";
     // check if there is a location type selectbox, or if the loc type is set in FSuggest params
     if(gFSuggestInstances[fieldIndex].params.locType)
       {
       var currentLocType = gFSuggestInstances[fieldIndex].params.locType;
       }
     else
       {
       var typeSelectName = gFSuggestInstances[fieldIndex].field.name.replace(/G$/,"A")
       if(typeSelectName.indexOf("A") >= 0)
         {
         var typeSelectbox = document.getElementsByName(typeSelectName)[0];
         var currentLocType = typeSelectbox.value;
         }
       }
     // get suggestions only if type ==1
     //if((typeSelectbox != null) && (typeSelectbox.value != 1))
     if((typeof(currentLocType) != "undefined") && !(currentLocType & gFSuggestInstances[fieldIndex].params.useSuggest))
       {
       SLs.delete_SLs();
       return;
       }
     if((typeSelectbox != null) && (typeSelectbox.nodeName == "SELECT"))
       {
       var replaceTypeRegExp = /REQ0JourneyStopsS0A=(\d+)&/;
       tempURL = tempURL.replace(replaceTypeRegExp,"REQ0JourneyStopsS0A="+typeSelectbox.value+"&");
       gFSuggestInstances[fieldIndex].selectType = true;
       }
     if((gFSuggestInstances[fieldIndex].params.filter == "germany") && (currentLocType) && (currentLocType == 1))
       tempURL = tempURL.replace(/&/,"&REQ0JourneyStopsS0F=selectStationNumberFilter;008000000;008099999&");
     /*
     if(gFSuggestInstances[fieldIndex].params.locType && document.getElementById(gFSuggestInstances[fieldIndex].params.locType+'_hidden')){
       var currentLocType = document.getElementById(gFSuggestInstances[fieldIndex].params.locType+'_hidden').value;
       tempURL = tempURL+gFSuggestInstances[fieldIndex].params.requestURL2+currentLocType+"&";
     }
     */
     if(document.getElementById("hafasCallJS") != null)
       {
       // remove old script
       document.getElementsByTagName("head")[0].removeChild(document.getElementById("hafasCallJS"));
       }
     var newScript = document.createElement("script");
     newScript.type = "text/javascript";
     newScript.src = tempURL+"js=true&";
     newScript.id = "hafasCallJS";
     document.getElementsByTagName('head')[0].appendChild(newScript);
  },
  hafasurl2utf8:function(text){
     text=text.replace(/\+/g,"%20");
     text=text.replace(/%C([\d,A-F])/g,"%C3%8$1");//À,Á,Â,Ã,Ä,Å,Æ,Ç,È,É,Ê,Ë,Ì,Í,Î,Ï
     text=text.replace(/%D([\d,A-F])/g,"%C3%9$1");//Ð,Ñ,Ò,Ó,Ô,Õ,Ö,÷,Ø,Ù,Ú,Û,Ü,Ý,Þ,ß
     text=text.replace(/%E([\d,A-F])/g,"%C3%A$1");//à,á,â,ã,ä,å,æ,ç,è,é,ê,ë,ì,í,î,ï
     text=text.replace(/%F([\d,A-F])/g,"%C3%B$1");//ð,ñ,ò,ó,ô,õ,ö,÷,ø,ù,ú,û,ü,ý,þ,ÿ
     text=text.replace(/%A7/g,"%C2%A7");//§

    return text;
  },
  getCookieValue:function(cookietext,name,sep){
    if(typeof cookietext=='undefined' || cookietext=="")return "";
    var pos=cookietext.search(name);
    if(pos<0)return "";
    var tmptext=cookietext.substr(pos+name.length);
    pos=tmptext.search(sep);
    if(pos>0)tmptext=tmptext.substr(0,pos);
    return decodeURIComponent(tmptext);
  },
  fillWithCookieValues: function(sug){
    if(typeof sug.params.cookiename=='undefined')return;
    var text=document.cookie;
    var cookiename=sug.params.cookiename;
    text=this.getCookieValue(text,cookiename+'=',";");
    if(text=="")return;
    text=this.getCookieValue(text,'history=',";");
    if(text=="")return;
    text=this.hafasurl2utf8(text);
    if (!SLs.container){
      document.getElementsByTagName("body")[0].appendChild(SLs.erzeugen(sug.instance));
      }
    var stops=new Array;
    var stoparrayidx=0;
    for(var stopnr=1;stopnr<=6;stopnr++){
      var stoptext=this.getCookieValue(text,"Stop"+stopnr,"&");
      if(typeof(sug.params.historyType) == "number")
        {
        var tmpType = this.getCookieValue(stoptext,"A=","@");
        if(!(tmpType & sug.params.historyType))
          continue;
        }
      if(stoptext!=""){
        stops[stoparrayidx]=new Object;
        stops[stoparrayidx].text=stoptext;
        stoparrayidx++;
      }
    }
    if(stoparrayidx == 0)
      return;
    SLs.sls=new Object;
    SLs.sls.suggestions=new Array;
    for(stopnr=0;stopnr<stops.length;stopnr++){
      SLs.sls.suggestions[stopnr]=new Object;
      SLs.sls.suggestions[stopnr].id=this.getCookieValue(stops[stopnr].text,"§L=","§")
      SLs.sls.suggestions[stopnr].value=this.getCookieValue(stops[stopnr].text,"O=","@")
      SLs.sls.suggestions[stopnr].type=this.getCookieValue(stops[stopnr].text,"A=","@")
    }
    var divElements = new Array();
    var typeSpanStyle = "right: 0px;";
    for(var i = 0; i < SLs.sls.suggestions.length; i++) {
      if(SLs.container){
        divElements[i] = SLs.container.appendChild(document.createElement("div"));
        divElements[i].id = i;
        if((typeof(historyCookieTypeString) != "undefined") && (typeof(historyCookieTypeString[SLs.sls.suggestions[i].type]) != "undefined"))
          var currentTypeString = "<span style='display: block;position: absolute; background-color: #fff;"+typeSpanStyle+"'>"+ historyCookieTypeString[SLs.sls.suggestions[i].type] +"</span>";
        else
          var currentTypeString = ""
        divElements[i].innerHTML = currentTypeString + "<span>"+SLs.sls.suggestions[i].value+"</span>";
        divElements[i].onmousedown = function() {
          gFSuggestInstances[fieldIndex].clickSel = 1;
          gFSuggestInstances[fieldIndex].setSLSInput(SLs,this.lastChild.firstChild.nodeValue,parseInt(this.id));
        }
      }
      else return false;
    }
    // Border wird erst hier gesetzt damit leeres div nicht zu sehen ist...
    SLs.container.style.borderWidth = "1px";
    SLs.container.style.zIndex = "10002";
    SLs.container.style.height = "auto";
    if((gFSuggestInstances[fieldIndex].params.height != "auto") && (SLs.container.offsetHeight > gFSuggestInstances[fieldIndex].params.height.match(/\d./)[0]))
     SLs.container.style.height = gFSuggestInstances[fieldIndex].params.height;
    if(navigator.userAgent.toLowerCase().indexOf("msie")>-1) SLs.container.style.width = SLs.container.clientWidth+20;
    if(navigator.userAgent.toLowerCase().indexOf('msie')>-1 && navigator.userAgent.indexOf('7.0')<0){
      SLs.hideSelect();
    }
    if(SLs.sls.length < 1) SLs.delete_SLs();
  },
  delete_SLs: function() {

    // loescht den suggestion-Container
    if((SLs.container) && (SLs.container.parentNode)){
      document.getElementsByTagName("body")[0].removeChild(SLs.container);
      if(navigator.userAgent.toLowerCase().indexOf('msie')>-1 && navigator.userAgent.indexOf('7.0')<0){
        SLs.showSelect();
      }
      SLs.countList = -1;
      SLs.container = null;
      SLs.currentTop = 0;
      SLs.currentBottom = 9;
    }
  },
  // Get x-positon of element
  getPosX: function(element){
    var curleft = 0;
    if (element.offsetParent){
       while (element.offsetParent){
          curleft += element.offsetLeft;
          element = element.offsetParent;
       }
    }
    else if (element.x) curleft += element.x;
    return curleft;
  },
  // Get y-position of element
  getPosY: function(element){
    var curtop = 0;
    if (element.offsetParent) {
       while (element.offsetParent){
          curtop += element.offsetTop;
          element = element.offsetParent;
       }
    }
    else if (element.y) curtop += element.y;
    return curtop;
  },
  // hide select boxes in MSIE 6
  hideSelect: function(){
    var currentSelects = document.getElementsByTagName("select");
    for(var i=0; i<currentSelects.length; i++){
      var hideCurrentSelectX = false;
      var hideCurrentSelectY = false;
      var currentX = SLs.getPosX(currentSelects[i]);
      var currentY = SLs.getPosY(currentSelects[i]);
      if(SLs.pos_x <= currentX && currentX < SLs.pos_x+SLs.container.clientWidth)
        hideCurrentSelectX = true;
      else if(SLs.pos_x < currentX+currentSelects[i].clientWidth && currentX+currentSelects[i].clientWidth <= SLs.pos_x+SLs.container.clientWidth)
        hideCurrentSelectX = true;
      else if(SLs.pos_x > currentX && currentX+currentSelects[i].clientWidth > SLs.pos_x)
        hideCurrentSelectX = true;
      if(SLs.pos_y < currentY && currentY < SLs.pos_y+SLs.container.clientHeight)
        hideCurrentSelectY = true;
      else if(SLs.pos_y < currentY+currentSelects[i].clientHeight && currentY+currentSelects[i].clientHeight < SLs.pos_y+SLs.container.clientHeight)
        hideCurrentSelectY = true;
      if((hideCurrentSelectX) && (hideCurrentSelectY))
        currentSelects[i].style.visibility = "hidden";
    }
  },
  // show select boxes in MSIE 6
  showSelect: function(){
     var currentSelects = document.getElementsByTagName("select");
     for(var i=0; i<currentSelects.length; i++){
       if(currentSelects[i].style.visibility == "hidden")
         currentSelects[i].style.visibility = "visible";
     }
  }
};


