// Finder - Funktionen
function Finder() {
  this.addFinder = addFinder;
  this.finderArray = new Array();
  this.nameList = new Array();

  this.createFinder = createFinder;
  this.changeEntryFirst = changeEntryFirst;
  this.changeEntrySecond = changeEntrySecond;
  this.changeEntryThird = changeEntryThird;

  this.finderForm =  document.finderForm;
  this.categorieFirst = null;
  this.categorieSecond = null;
  this.categorieThird = null;

  // AddEntry
  this.addEntry = addEntry;

  function addEntry(id, name) {
    this.nameList[id] = name;
  }

  // erzeugt das Finder-Array
  function addFinder(categorieFirst, categorieSecond, categorieThird) {
    if (!this.finderArray[categorieFirst]) {
      this.finderArray[categorieFirst] = new Array();
    }
    if (!this.finderArray[categorieFirst][categorieSecond]) {
      this.finderArray[categorieFirst][categorieSecond] = new Array();
    }
    this.finderArray[categorieFirst][categorieSecond][this.finderArray[categorieFirst][categorieSecond].length] = categorieThird;
  }

  function createFinder() {
    if (!document.finderForm) {
      return false;
    }
    this.finderForm = document.finderForm;
    if (document.finderForm["keyCategory1"]) {
      this.categorieFirst = document.finderForm["keyCategory1"];
    }

    if (document.finderForm["keyCategory2"]) {
      this.categorieSecond = document.finderForm["keyCategory2"];
    }

    if (document.finderForm["keyCategory3"]) {
      this.categorieThird = document.finderForm["keyCategory3"];
    }
    //this.categorieSecond.style.display = "none";
    this.categorieSecond.disabled = true;
    //this.categorieThird.style.display = "none";
    this.categorieThird.disabled = true;
	this.categorieFirst.options[0].selected = "selected";
  }


  // fuellt das erste Feld
  function changeEntryFirst(catFirst, list1, list2) {
    // Ausschalten der 2ten und 3ten Kategorie bei nicht gewAEhlter Kategorie in 1ter Selektbox
    if (this.categorieFirst.selectedIndex < 2) {
      //this.categorieSecond.style.display = "none";
      this.categorieSecond.disabled = true;
      //this.categorieThird.style.display = "none";
      this.categorieSecond.options[0].selected = "selected";
      this.categorieThird.disabled = true;
    }
    
    if (catFirst == "") return;
    if (!this.categorieSecond)  return;
    this.categorieSecond.length = 1;

    if (list1 != null) {
      for (var i =0;  i <  this.categorieFirst.length;i++) {
        if ( this.categorieFirst[i].value == list1) {
          this.categorieFirst[i].selected=true;break;
        }
      }
    }

    // Trennlinie in 2ter Selektbox
    this.categorieSecond[this.categorieSecond.length] =  new Option("--------------------------------","",false,false);

    for (var i in this.finderArray[catFirst]) {
      if (!isNaN(i)) {
        if (i == list2) {
          this.categorieSecond[this.categorieSecond.length] =  new Option(this.nameList[i],i,true,true);
          this.categorieSecond[this.categorieSecond.length-1].selected=true;
        } else {
          this.categorieSecond[this.categorieSecond.length] =  new Option(this.nameList[i],i,false,false);
        }
      }
    }

    if (this.categorieSecond[2].text == "#") {
      //this.categorieSecond.style.display = "none";
      this.categorieSecond.disabled = true;
      //this.categorieThird.style.display = "inline";
      this.categorieThird.disabled = false;
      // uEberspringen der 2ten Kategorie - wenn diese im strWord mit '#' markiert wurde
      nav.changeEntrySecond(this.categorieSecond[2].value);
    } else {
      //this.categorieSecond.style.display = "inline";
      this.categorieSecond.disabled = false;
      //this.categorieThird.style.display = "none";
      this.categorieThird.disabled = true;
    }
  }


  // fuellt das zweite Feld
  function changeEntrySecond(catSecond,list3) {

    // Ein- bzw. Ausblenden der 3ten Kategorie
    if(this.categorieFirst.selectedIndex < 2) {
      //this.categorieSecond.style.display = "none";
      this.categorieSecond.disabled = true;
      //this.categorieThird.style.display = "none";
      this.categorieThird.disabled = true;
    } else if (this.categorieSecond[2].text == "#") {
      //this.categorieThird.style.display = "inline";
      this.categorieThird.disabled = false;
    } else {
      // Grossunternehmen... (hat 2te Kategorie)
      if (this.categorieSecond.selectedIndex < 2) {
        //this.categorieThird.style.display = "none";
        this.categorieThird.disabled = true;
      } else {
        //this.categorieThird.style.display = "inline";
        this.categorieThird.disabled = false;
      }
    }

    if (catSecond == "" ) return;
    if (!this.categorieThird) return;
    this.categorieThird.length = 1;
    catFirst = this.categorieFirst[this.categorieFirst.selectedIndex].value

    // Trennlinie in 3ter Selektbox
    this.categorieThird[this.categorieThird.length] =  new Option("--------------------------------","",false,false);              

    for (var i in this.finderArray[catFirst][catSecond]) {
      if (!isNaN(i)) {
        var opt = this.finderArray[catFirst][catSecond][i];
        if (opt == list3) {
          this.categorieThird[this.categorieThird.length] =  new Option( this.nameList[opt],opt,true,true);
          this.categorieThird[this.categorieThird.length-1].selected=true;
        } else {
          this.categorieThird[this.categorieThird.length] =  new Option( this.nameList[opt],opt,false,false);
        }
      }
    }
  }


  // schickt das Formular ab
  function changeEntryThird() {
    if(this.categorieThird.selectedIndex > 1) {
      this.finderForm.submit();
    }
  }
}


// erzeugt das Finder-Objekt
nav = new Finder();

function setFinderInit(list1, list2, list3)  {
  if (list1 != null ) {
    nav.changeEntryFirst(list1,list1,list2);
  }	
  if (list2 != null) {
    nav.changeEntrySecond(list2,list3);
  }
}