/*
    THIS JAVASCRIPT OBJECT IST COPYRIGHT PROTECTED
    OBJECT CREATED BY ELMAR EIGNER, OPEN-EYE.DE
    THE USAGE OF THIS SCRIPT IS NOT ALLOWED
    IF YOU NEED A SCRIPT LIKE THAT, CONTACT ME!
    http://www.open-eye.de
*/
var globalassistedListInstanceCount = 0;

function assistedList (onform, onsearchfield, ondependingfield, listarray) {

    this.deflist   = listarray;
    
    this.formname  = onform;
    this.inpfld    = onsearchfield;
    this.dpdfld    = ondependingfield;
    this.oldinp    = '';
    this.newinp    = '';        
    this.isconfig  = -1;
    this.timeron   = new Array(); 
    this.actgroup  = '';  
    this.initIt    = function() { 
            if ( ! document.forms[this.formname] ) {
              return false;
            }
            this.inpfoelem = document.forms[this.formname].elements[this.inpfld];
            if ( !this.inpfoelem ) {
                alert("Error in assistedList configuration!\ninput field undef.\n\nFehler in der assistedList Konfiguration!\nSucheingabe Feld undef.");
            } else {
                document.forms[this.formname].reset();
                this.oldinp    = this.inpfoelem.value;
                this.outfoelem = document.forms[this.formname].elements[this.dpdfld];
                if ( !this.outfoelem ) {
                    alert("Error in assistedList configuration!\ndependency select undef.\n\nFehler in der assistedList Konfiguration!\nAbhängigkeits Select undef.");
                } else {                        
                    this.isconfig  = 1;    
                    this.buildList();
                    this.timeron[globalassistedListInstanceCount] = window.setInterval('mylist.chghandle()', 100); 
                    globalassistedListInstanceCount++;
                }
            }
        };
    this.chghandle = function() { 
            this.newinp = this.inpfoelem.value;
            if ( this.newinp != this.oldinp ) {
                this.oldinp = this.newinp;    
                this.rewriteList();
            }
            return true;
        };                
    this.buildList = function() { 
            for ( i=0; i < this.deflist.length; i++ ) {
                anewoption = new Option(this.deflist[i][1], this.deflist[i][0], false, false);
                this.outfoelem.options[i] = anewoption;
            }
            return true;
        };                
    this.rewriteList = function() {
            if ( ! document.forms[this.formname] ) {
              return false;
            }
            for ( i=this.outfoelem.options.length-1; i >= 0; i-- ) {
                this.outfoelem.options[i] = null;
            }
            this.outfoelem.options.length = 0;
            newopcnt = 0;
            for ( i=0; i < this.deflist.length; i++ ) {
                tpatt = eval('/^' + this.inpfoelem.value + '/i');
                if ( this.actgroup != '' ) {
		  if ( tpatt.test(this.deflist[i][1]) && this.deflist[i][2] == this.actgroup ) {
                    anewoption = new Option(this.deflist[i][1], this.deflist[i][0], false, false);
                    this.outfoelem.options[newopcnt] = anewoption;
                    newopcnt++;
                  }
		}
		else {
		  if ( tpatt.test(this.deflist[i][1]) ) {
                    anewoption = new Option(this.deflist[i][1], this.deflist[i][0], false, false);
                    this.outfoelem.options[newopcnt] = anewoption;
                    newopcnt++;
                  }
		}
            }
	    if ( newopcnt == 1 ) {
		this.outfoelem.options[0].selected = true;
	    }
            return true;
        };                
    this.rewriteListByGroup = function(foobj, gval) {;
            this.actgroup  = gval;
	    for ( i=this.outfoelem.options.length-1; i >= 0; i-- ) {
                this.outfoelem.options[i] = null;
            }
            this.outfoelem.options.length = 0;
            newopcnt = 0;
            for ( i=0; i < this.deflist.length; i++ ) {
                if ( this.inpfoelem.value != '' ) {
		  tpatt = eval('/^' + this.inpfoelem.value + '/i');
		} else {
		  tpatt = eval('/.+/');
		}
                if ( tpatt.test(this.deflist[i][1]) && this.deflist[i][2] == gval) {
                    anewoption = new Option(this.deflist[i][1], this.deflist[i][0], false, false);
                    this.outfoelem.options[newopcnt] = anewoption;
                    newopcnt++;
                }
            }
	    if ( newopcnt == 1 ) {
		this.outfoelem.options[0].selected = true;
	    }
            return true;
        };              
    this.addToInput = function() { 
            tval = this.outfoelem.options[this.outfoelem.options.selectedIndex].value;
            this.oldinp = tval;
            this.inpfoelem.value = tval;
            return true;
        };            
    this.foundById = function( elemid ) { 
            //tval = this.outfoelem.options[this.outfoelem.options.selectedIndex].value;
            for ( i=0; i < this.deflist.length; i++ ) {
                if ( this.deflist[i][0] == elemid ) return this.deflist[i];
            }
        };
}    

